xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 78b84d54b8cb73f15d70558b7632ea385e76dbdf)
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>
11d441b888SJed Brown #include <../src/mat/blocktranspose.h>
12*78b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
13*78b84d54SShri Abhyankar #include <petscthreadcomm.h>
14*78b84d54SShri Abhyankar #endif
150716a85fSBarry Smith 
160716a85fSBarry Smith #undef __FUNCT__
170716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_SeqAIJ"
180716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
190716a85fSBarry Smith {
200716a85fSBarry Smith   PetscErrorCode ierr;
210716a85fSBarry Smith   PetscInt       i,m,n;
220716a85fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
230716a85fSBarry Smith 
240716a85fSBarry Smith   PetscFunctionBegin;
250716a85fSBarry Smith   ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr);
260716a85fSBarry Smith   ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr);
270716a85fSBarry Smith   if (type == NORM_2) {
280716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
290716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
300716a85fSBarry Smith     }
310716a85fSBarry Smith   } else if (type == NORM_1) {
320716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
330716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
340716a85fSBarry Smith     }
350716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
360716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
370716a85fSBarry Smith       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
380716a85fSBarry Smith     }
390716a85fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");
400716a85fSBarry Smith 
410716a85fSBarry Smith   if (type == NORM_2) {
428f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
430716a85fSBarry Smith   }
440716a85fSBarry Smith   PetscFunctionReturn(0);
450716a85fSBarry Smith }
460716a85fSBarry Smith 
474a2ae208SSatish Balay #undef __FUNCT__
486ce1633cSBarry Smith #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ"
496ce1633cSBarry Smith PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
506ce1633cSBarry Smith {
516ce1633cSBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
526ce1633cSBarry Smith   const MatScalar   *aa = a->a;
536ce1633cSBarry Smith   PetscInt          i,m=A->rmap->n,cnt = 0;
546ce1633cSBarry Smith   const PetscInt    *jj = a->j,*diag;
556ce1633cSBarry Smith   PetscInt          *rows;
566ce1633cSBarry Smith   PetscErrorCode    ierr;
576ce1633cSBarry Smith 
586ce1633cSBarry Smith   PetscFunctionBegin;
596ce1633cSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
606ce1633cSBarry Smith   diag = a->diag;
616ce1633cSBarry Smith   for (i=0; i<m; i++) {
626ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
636ce1633cSBarry Smith       cnt++;
646ce1633cSBarry Smith     }
656ce1633cSBarry Smith   }
666ce1633cSBarry Smith   ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr);
676ce1633cSBarry Smith   cnt  = 0;
686ce1633cSBarry Smith   for (i=0; i<m; i++) {
696ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
706ce1633cSBarry Smith       rows[cnt++] = i;
716ce1633cSBarry Smith     }
726ce1633cSBarry Smith   }
736ce1633cSBarry Smith   ierr = ISCreateGeneral(((PetscObject)A)->comm,cnt,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
746ce1633cSBarry Smith   PetscFunctionReturn(0);
756ce1633cSBarry Smith }
766ce1633cSBarry Smith 
776ce1633cSBarry Smith #undef __FUNCT__
78b3a44c85SBarry Smith #define __FUNCT__ "MatFindNonzeroRows_SeqAIJ"
79b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
80b3a44c85SBarry Smith {
81b3a44c85SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
82b3a44c85SBarry Smith   const MatScalar   *aa;
83b3a44c85SBarry Smith   PetscInt          m=A->rmap->n,cnt = 0;
84b3a44c85SBarry Smith   const PetscInt    *ii;
85b3a44c85SBarry Smith   PetscInt          n,i,j,*rows;
86b3a44c85SBarry Smith   PetscErrorCode    ierr;
87b3a44c85SBarry Smith 
88b3a44c85SBarry Smith   PetscFunctionBegin;
89b3a44c85SBarry Smith   *keptrows = 0;
90b3a44c85SBarry Smith   ii        = a->i;
91b3a44c85SBarry Smith   for (i=0; i<m; i++) {
92b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
93b3a44c85SBarry Smith     if (!n) {
94b3a44c85SBarry Smith       cnt++;
95b3a44c85SBarry Smith       goto ok1;
96b3a44c85SBarry Smith     }
97b3a44c85SBarry Smith     aa  = a->a + ii[i];
98b3a44c85SBarry Smith     for (j=0; j<n; j++) {
99b3a44c85SBarry Smith       if (aa[j] != 0.0) goto ok1;
100b3a44c85SBarry Smith     }
101b3a44c85SBarry Smith     cnt++;
102b3a44c85SBarry Smith     ok1:;
103b3a44c85SBarry Smith   }
104b3a44c85SBarry Smith   if (!cnt) PetscFunctionReturn(0);
105b3a44c85SBarry Smith   ierr = PetscMalloc((A->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr);
106b3a44c85SBarry Smith   cnt  = 0;
107b3a44c85SBarry Smith   for (i=0; i<m; i++) {
108b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
109b3a44c85SBarry Smith     if (!n) continue;
110b3a44c85SBarry Smith     aa  = a->a + ii[i];
111b3a44c85SBarry Smith     for (j=0; j<n; j++) {
112b3a44c85SBarry Smith       if (aa[j] != 0.0) {
113b3a44c85SBarry Smith         rows[cnt++] = i;
114b3a44c85SBarry Smith         break;
115b3a44c85SBarry Smith       }
116b3a44c85SBarry Smith     }
117b3a44c85SBarry Smith   }
118b3a44c85SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
119b3a44c85SBarry Smith   PetscFunctionReturn(0);
120b3a44c85SBarry Smith }
121b3a44c85SBarry Smith 
122b3a44c85SBarry Smith #undef __FUNCT__
12379299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1247087cfbeSBarry Smith PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
12579299369SBarry Smith {
12679299369SBarry Smith   PetscErrorCode ierr;
12779299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
128d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
12954f21887SBarry Smith   MatScalar      *aa = aij->a;
13054f21887SBarry Smith   PetscScalar    *v;
131ace3abfcSBarry Smith   PetscBool      missing;
13279299369SBarry Smith 
13379299369SBarry Smith   PetscFunctionBegin;
13409f38230SBarry Smith   if (Y->assembled) {
13509f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
13609f38230SBarry Smith     if (!missing) {
13779299369SBarry Smith       diag = aij->diag;
13879299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
13979299369SBarry Smith       if (is == INSERT_VALUES) {
14079299369SBarry Smith 	for (i=0; i<m; i++) {
14179299369SBarry Smith 	  aa[diag[i]] = v[i];
14279299369SBarry Smith 	}
14379299369SBarry Smith       } else {
14479299369SBarry Smith 	for (i=0; i<m; i++) {
14579299369SBarry Smith 	  aa[diag[i]] += v[i];
14679299369SBarry Smith 	}
14779299369SBarry Smith       }
14879299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
14979299369SBarry Smith       PetscFunctionReturn(0);
15079299369SBarry Smith     }
15186c113feSBarry Smith     aij->idiagvalid  = PETSC_FALSE;
15286c113feSBarry Smith     aij->ibdiagvalid = PETSC_FALSE;
15309f38230SBarry Smith   }
15409f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
15509f38230SBarry Smith   PetscFunctionReturn(0);
15609f38230SBarry Smith }
15779299369SBarry Smith 
15879299369SBarry Smith #undef __FUNCT__
1594a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
160ace3abfcSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
16117ab2063SBarry Smith {
162416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
163dfbe8321SBarry Smith   PetscErrorCode ierr;
16497f1f81fSBarry Smith   PetscInt       i,ishift;
16517ab2063SBarry Smith 
1663a40ed3dSBarry Smith   PetscFunctionBegin;
167d0f46423SBarry Smith   *m     = A->rmap->n;
1683a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
169bfeeae90SHong Zhang   ishift = 0;
17053e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
171d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
172bfeeae90SHong Zhang   } else if (oshift == 1) {
173d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
1743b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
175d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
176d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
177ecc77c7aSBarry Smith     if (ja) {
17897f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
1793b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
180ecc77c7aSBarry Smith     }
1816945ee14SBarry Smith   } else {
182ecc77c7aSBarry Smith     *ia = a->i;
183ecc77c7aSBarry Smith     if (ja) *ja = a->j;
184a2ce50c7SBarry Smith   }
1853a40ed3dSBarry Smith   PetscFunctionReturn(0);
186a2744918SBarry Smith }
187a2744918SBarry Smith 
1884a2ae208SSatish Balay #undef __FUNCT__
1894a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
190ace3abfcSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
1916945ee14SBarry Smith {
192dfbe8321SBarry Smith   PetscErrorCode ierr;
1936945ee14SBarry Smith 
1943a40ed3dSBarry Smith   PetscFunctionBegin;
1953a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
196bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
197606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
198ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
199bcd2baecSBarry Smith   }
2003a40ed3dSBarry Smith   PetscFunctionReturn(0);
20117ab2063SBarry Smith }
20217ab2063SBarry Smith 
2034a2ae208SSatish Balay #undef __FUNCT__
2044a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
205ace3abfcSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
2063b2fbd54SBarry Smith {
2073b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
208dfbe8321SBarry Smith   PetscErrorCode ierr;
209d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
21097f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
2113b2fbd54SBarry Smith 
2123a40ed3dSBarry Smith   PetscFunctionBegin;
213899cda47SBarry Smith   *nn = n;
2143a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2153b2fbd54SBarry Smith   if (symmetric) {
216d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
2173b2fbd54SBarry Smith   } else {
21897f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
21997f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
22097f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
22197f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
2223b2fbd54SBarry Smith     jj = a->j;
2233b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
224bfeeae90SHong Zhang       collengths[jj[i]]++;
2253b2fbd54SBarry Smith     }
2263b2fbd54SBarry Smith     cia[0] = oshift;
2273b2fbd54SBarry Smith     for (i=0; i<n; i++) {
2283b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
2293b2fbd54SBarry Smith     }
23097f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
2313b2fbd54SBarry Smith     jj   = a->j;
232a93ec695SBarry Smith     for (row=0; row<m; row++) {
233a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
234a93ec695SBarry Smith       for (i=0; i<mr; i++) {
235bfeeae90SHong Zhang         col = *jj++;
2363b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2373b2fbd54SBarry Smith       }
2383b2fbd54SBarry Smith     }
239606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2403b2fbd54SBarry Smith     *ia = cia; *ja = cja;
2413b2fbd54SBarry Smith   }
2423a40ed3dSBarry Smith   PetscFunctionReturn(0);
2433b2fbd54SBarry Smith }
2443b2fbd54SBarry Smith 
2454a2ae208SSatish Balay #undef __FUNCT__
2464a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
247ace3abfcSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
2483b2fbd54SBarry Smith {
249dfbe8321SBarry Smith   PetscErrorCode ierr;
250606d414cSSatish Balay 
2513a40ed3dSBarry Smith   PetscFunctionBegin;
2523a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2533b2fbd54SBarry Smith 
254606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
255606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
2563b2fbd54SBarry Smith 
2573a40ed3dSBarry Smith   PetscFunctionReturn(0);
2583b2fbd54SBarry Smith }
2593b2fbd54SBarry Smith 
26087d4246cSBarry Smith #undef __FUNCT__
26187d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
26287d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
26387d4246cSBarry Smith {
26487d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
26587d4246cSBarry Smith   PetscInt       *ai = a->i;
26687d4246cSBarry Smith   PetscErrorCode ierr;
26787d4246cSBarry Smith 
26887d4246cSBarry Smith   PetscFunctionBegin;
26987d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
27087d4246cSBarry Smith   PetscFunctionReturn(0);
27187d4246cSBarry Smith }
27287d4246cSBarry Smith 
2734a2ae208SSatish Balay #undef __FUNCT__
2744a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
27597f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
27617ab2063SBarry Smith {
277416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
278e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
27997f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
2806849ba73SBarry Smith   PetscErrorCode ierr;
281e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
28254f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
283ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
284ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
28517ab2063SBarry Smith 
2863a40ed3dSBarry Smith   PetscFunctionBegin;
28771fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
28817ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
289416022c9SBarry Smith     row  = im[k];
2905ef9f2a5SBarry Smith     if (row < 0) continue;
2912515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
292e32f2f54SBarry 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);
2933b2fbd54SBarry Smith #endif
294bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
29517ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
296416022c9SBarry Smith     low  = 0;
297c71e6ed7SBarry Smith     high = nrow;
29817ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
2995ef9f2a5SBarry Smith       if (in[l] < 0) continue;
3002515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
301e32f2f54SBarry 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);
3023b2fbd54SBarry Smith #endif
303bfeeae90SHong Zhang       col = in[l];
30416371a99SBarry Smith       if (v) {
3054b0e389bSBarry Smith 	if (roworiented) {
3065ef9f2a5SBarry Smith 	  value = v[l + k*n];
307bef8e0ddSBarry Smith 	} else {
3084b0e389bSBarry Smith 	  value = v[k + l*m];
3094b0e389bSBarry Smith 	}
31016371a99SBarry Smith       } else {
31175567043SBarry Smith         value = 0.;
31216371a99SBarry Smith       }
313abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
31436db0b34SBarry Smith 
3157cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
316e2ee6c50SBarry Smith       lastcol = col;
317416022c9SBarry Smith       while (high-low > 5) {
318416022c9SBarry Smith         t = (low+high)/2;
319416022c9SBarry Smith         if (rp[t] > col) high = t;
320416022c9SBarry Smith         else             low  = t;
32117ab2063SBarry Smith       }
322416022c9SBarry Smith       for (i=low; i<high; i++) {
32317ab2063SBarry Smith         if (rp[i] > col) break;
32417ab2063SBarry Smith         if (rp[i] == col) {
325416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
32617ab2063SBarry Smith           else                  ap[i] = value;
327e44c0bd4SBarry Smith           low = i + 1;
32817ab2063SBarry Smith           goto noinsert;
32917ab2063SBarry Smith         }
33017ab2063SBarry Smith       }
331abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
332c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
333e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
334fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
335c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
336416022c9SBarry Smith       /* shift up all the later entries in this row */
337416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
33817ab2063SBarry Smith         rp[ii+1] = rp[ii];
33917ab2063SBarry Smith         ap[ii+1] = ap[ii];
34017ab2063SBarry Smith       }
34117ab2063SBarry Smith       rp[i] = col;
34217ab2063SBarry Smith       ap[i] = value;
343416022c9SBarry Smith       low   = i + 1;
344e44c0bd4SBarry Smith       noinsert:;
34517ab2063SBarry Smith     }
34617ab2063SBarry Smith     ailen[row] = nrow;
34717ab2063SBarry Smith   }
34888e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
3493a40ed3dSBarry Smith   PetscFunctionReturn(0);
35017ab2063SBarry Smith }
35117ab2063SBarry Smith 
35281824310SBarry Smith 
3534a2ae208SSatish Balay #undef __FUNCT__
3544a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
355a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
3567eb43aa7SLois Curfman McInnes {
3577eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
35897f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
35997f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
36054f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
3617eb43aa7SLois Curfman McInnes 
3623a40ed3dSBarry Smith   PetscFunctionBegin;
3637eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
3647eb43aa7SLois Curfman McInnes     row  = im[k];
365e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
366e32f2f54SBarry 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);
367bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
3687eb43aa7SLois Curfman McInnes     nrow = ailen[row];
3697eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
370e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
371e32f2f54SBarry 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);
372bfeeae90SHong Zhang       col = in[l] ;
3737eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
3747eb43aa7SLois Curfman McInnes       while (high-low > 5) {
3757eb43aa7SLois Curfman McInnes         t = (low+high)/2;
3767eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
3777eb43aa7SLois Curfman McInnes         else             low  = t;
3787eb43aa7SLois Curfman McInnes       }
3797eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
3807eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
3817eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
382b49de8d1SLois Curfman McInnes           *v++ = ap[i];
3837eb43aa7SLois Curfman McInnes           goto finished;
3847eb43aa7SLois Curfman McInnes         }
3857eb43aa7SLois Curfman McInnes       }
38697e567efSBarry Smith       *v++ = 0.0;
3877eb43aa7SLois Curfman McInnes       finished:;
3887eb43aa7SLois Curfman McInnes     }
3897eb43aa7SLois Curfman McInnes   }
3903a40ed3dSBarry Smith   PetscFunctionReturn(0);
3917eb43aa7SLois Curfman McInnes }
3927eb43aa7SLois Curfman McInnes 
39317ab2063SBarry Smith 
3944a2ae208SSatish Balay #undef __FUNCT__
3954a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
396dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
39717ab2063SBarry Smith {
398416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3996849ba73SBarry Smith   PetscErrorCode ierr;
4006f69ff64SBarry Smith   PetscInt       i,*col_lens;
4016f69ff64SBarry Smith   int            fd;
402b37d52dbSMark F. Adams   FILE           *file;
40317ab2063SBarry Smith 
4043a40ed3dSBarry Smith   PetscFunctionBegin;
405b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
406d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
4070700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
408d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
409d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
410416022c9SBarry Smith   col_lens[3] = a->nz;
411416022c9SBarry Smith 
412416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
413d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
414416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
41517ab2063SBarry Smith   }
416d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
417606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
418416022c9SBarry Smith 
419416022c9SBarry Smith   /* store column indices (zero start index) */
4206f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
421416022c9SBarry Smith 
422416022c9SBarry Smith   /* store nonzero values */
4236f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
424b37d52dbSMark F. Adams 
425b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
426b37d52dbSMark F. Adams   if (file) {
427b37d52dbSMark F. Adams     fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
428b37d52dbSMark F. Adams   }
429b37d52dbSMark F. Adams 
4303a40ed3dSBarry Smith   PetscFunctionReturn(0);
43117ab2063SBarry Smith }
432416022c9SBarry Smith 
43309573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
434cd155464SBarry Smith 
4354a2ae208SSatish Balay #undef __FUNCT__
4364a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
437dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
438416022c9SBarry Smith {
439416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
440dfbe8321SBarry Smith   PetscErrorCode    ierr;
441d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
442e060cb09SBarry Smith   const char        *name;
443f3ef73ceSBarry Smith   PetscViewerFormat format;
44417ab2063SBarry Smith 
4453a40ed3dSBarry Smith   PetscFunctionBegin;
446b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
44771c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
44897f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
449d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
450d00d2cf4SBarry Smith       nofinalvalue = 1;
451d00d2cf4SBarry Smith     }
452d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
453d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
45477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
45577431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
456b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
45717ab2063SBarry Smith 
45817ab2063SBarry Smith     for (i=0; i<m; i++) {
459416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
460aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
46177431f27SBarry 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);
46217ab2063SBarry Smith #else
46377431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
46417ab2063SBarry Smith #endif
46517ab2063SBarry Smith       }
46617ab2063SBarry Smith     }
467d00d2cf4SBarry Smith     if (nofinalvalue) {
468d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
469d00d2cf4SBarry Smith     }
470317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
471fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
472d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
47368369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
474cd155464SBarry Smith      PetscFunctionReturn(0);
475fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
476d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4777566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
47844cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
47977431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
48044cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
481aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
48236db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
483a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48436db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
485a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48636db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
487a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4886831982aSBarry Smith         }
48944cd7ae7SLois Curfman McInnes #else
490a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
49144cd7ae7SLois Curfman McInnes #endif
49244cd7ae7SLois Curfman McInnes       }
493b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
49444cd7ae7SLois Curfman McInnes     }
495d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
496fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
49797f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
498d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4997566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
50097f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
501496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
502496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
503496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
504496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
505aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
50636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
507496be53dSLois Curfman McInnes #else
508496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
509496be53dSLois Curfman McInnes #endif
510496be53dSLois Curfman McInnes         }
511496be53dSLois Curfman McInnes       }
512496be53dSLois Curfman McInnes     }
5132e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
51477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
5152e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
51677431f27SBarry 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);}
51777431f27SBarry 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);}
51877431f27SBarry 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);}
51977431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
52077431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
52177431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
522496be53dSLois Curfman McInnes     }
523b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
524606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
525496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
526496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
52777431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
528496be53dSLois Curfman McInnes       }
529b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
530496be53dSLois Curfman McInnes     }
531b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
532496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
533496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
534496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
535aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
53636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
537b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5386831982aSBarry Smith           }
539496be53dSLois Curfman McInnes #else
540b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
541496be53dSLois Curfman McInnes #endif
542496be53dSLois Curfman McInnes         }
543496be53dSLois Curfman McInnes       }
544b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
545496be53dSLois Curfman McInnes     }
546d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
547fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
54897f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
54987828ca2SBarry Smith     PetscScalar value;
55002594712SBarry Smith 
551d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5527566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
55302594712SBarry Smith     for (i=0; i<m; i++) {
55402594712SBarry Smith       jcnt = 0;
555d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
556e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
55702594712SBarry Smith           value = a->a[cnt++];
558e24b481bSBarry Smith           jcnt++;
55902594712SBarry Smith         } else {
56002594712SBarry Smith           value = 0.0;
56102594712SBarry Smith         }
562aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
563b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
56402594712SBarry Smith #else
565b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
56602594712SBarry Smith #endif
56702594712SBarry Smith       }
568b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
56902594712SBarry Smith     }
570d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5713c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
572d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5737566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
5743c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5753c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
5763c215bfdSMatthew Knepley #else
5773c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
5783c215bfdSMatthew Knepley #endif
579d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
5803c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
5813c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5823c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5833c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
5843c215bfdSMatthew 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);
5853c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
5863c215bfdSMatthew 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);
5873c215bfdSMatthew Knepley         } else {
5883c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
5893c215bfdSMatthew Knepley         }
5903c215bfdSMatthew Knepley #else
5913c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
5923c215bfdSMatthew Knepley #endif
5933c215bfdSMatthew Knepley       }
5943c215bfdSMatthew Knepley     }
595d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5963a40ed3dSBarry Smith   } else {
597d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5987566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
599d5f3da31SBarry Smith     if (A->factortype){
60016cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
60116cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
60216cd7e1dSShri Abhyankar         /* L part */
60316cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
60416cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
60516cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
60616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
60716cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
60816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
60916cd7e1dSShri Abhyankar           } else {
61016cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
61116cd7e1dSShri Abhyankar           }
61216cd7e1dSShri Abhyankar #else
61316cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
61416cd7e1dSShri Abhyankar #endif
61516cd7e1dSShri Abhyankar         }
61616cd7e1dSShri Abhyankar 	/* diagonal */
61716cd7e1dSShri Abhyankar 	j = a->diag[i];
61816cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
61916cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
6202c990fa1SHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(1.0/a->a[j]),PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr);
62116cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
6222c990fa1SHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(1.0/a->a[j]),-PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr);
62316cd7e1dSShri Abhyankar           } else {
6242c990fa1SHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
62516cd7e1dSShri Abhyankar           }
62616cd7e1dSShri Abhyankar #else
6272c990fa1SHong Zhang           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,1.0/a->a[j]);CHKERRQ(ierr);
62816cd7e1dSShri Abhyankar #endif
62916cd7e1dSShri Abhyankar 
63016cd7e1dSShri Abhyankar 	/* U part */
63116cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
63216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
63316cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
63416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
63516cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
63616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
63716cd7e1dSShri Abhyankar           } else {
63816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
63916cd7e1dSShri Abhyankar           }
64016cd7e1dSShri Abhyankar #else
64116cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
64216cd7e1dSShri Abhyankar #endif
64316cd7e1dSShri Abhyankar }
64416cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
64516cd7e1dSShri Abhyankar         }
64616cd7e1dSShri Abhyankar     } else {
64717ab2063SBarry Smith       for (i=0; i<m; i++) {
64877431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
649416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
650aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
65136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
652a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
65336db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
654a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6553a40ed3dSBarry Smith           } else {
656a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
65717ab2063SBarry Smith           }
65817ab2063SBarry Smith #else
659a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
66017ab2063SBarry Smith #endif
66117ab2063SBarry Smith         }
662b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
66317ab2063SBarry Smith       }
66416cd7e1dSShri Abhyankar     }
665d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
66617ab2063SBarry Smith   }
667b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6683a40ed3dSBarry Smith   PetscFunctionReturn(0);
669416022c9SBarry Smith }
670416022c9SBarry Smith 
6714a2ae208SSatish Balay #undef __FUNCT__
6724a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
673dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
674416022c9SBarry Smith {
675480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
676416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
677dfbe8321SBarry Smith   PetscErrorCode    ierr;
678d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
67936db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
680b0a32e0cSBarry Smith   PetscViewer       viewer;
681f3ef73ceSBarry Smith   PetscViewerFormat format;
682cddf8d76SBarry Smith 
6833a40ed3dSBarry Smith   PetscFunctionBegin;
684480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
685b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
68619bcc07fSBarry Smith 
687b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
688416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
6890513a670SBarry Smith 
690fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
6910513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
692b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
693416022c9SBarry Smith     for (i=0; i<m; i++) {
694cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
695bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
696bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
697aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
69836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
699cddf8d76SBarry Smith #else
700cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
701cddf8d76SBarry Smith #endif
702b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
703cddf8d76SBarry Smith       }
704cddf8d76SBarry Smith     }
705b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
706cddf8d76SBarry Smith     for (i=0; i<m; i++) {
707cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
708bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
709bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
710cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
711b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
712cddf8d76SBarry Smith       }
713cddf8d76SBarry Smith     }
714b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
715cddf8d76SBarry Smith     for (i=0; i<m; i++) {
716cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
717bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
718bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
719aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
72036db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
721cddf8d76SBarry Smith #else
722cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
723cddf8d76SBarry Smith #endif
724b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
725416022c9SBarry Smith       }
726416022c9SBarry Smith     }
7270513a670SBarry Smith   } else {
7280513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
7290513a670SBarry Smith     /* first determine max of all nonzero values */
73097f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
731b0a32e0cSBarry Smith     PetscDraw   popup;
73236db0b34SBarry Smith     PetscReal scale;
7330513a670SBarry Smith 
7340513a670SBarry Smith     for (i=0; i<nz; i++) {
7350513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
7360513a670SBarry Smith     }
737b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
738b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
739b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
7400513a670SBarry Smith     count = 0;
7410513a670SBarry Smith     for (i=0; i<m; i++) {
7420513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
743bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
744bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
74597f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
746b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
7470513a670SBarry Smith         count++;
7480513a670SBarry Smith       }
7490513a670SBarry Smith     }
7500513a670SBarry Smith   }
751480ef9eaSBarry Smith   PetscFunctionReturn(0);
752480ef9eaSBarry Smith }
753cddf8d76SBarry Smith 
7544a2ae208SSatish Balay #undef __FUNCT__
7554a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
756dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
757480ef9eaSBarry Smith {
758dfbe8321SBarry Smith   PetscErrorCode ierr;
759b0a32e0cSBarry Smith   PetscDraw      draw;
76036db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
761ace3abfcSBarry Smith   PetscBool      isnull;
762480ef9eaSBarry Smith 
763480ef9eaSBarry Smith   PetscFunctionBegin;
764b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
765b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
766480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
767480ef9eaSBarry Smith 
768480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
769d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
770480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
771b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
772b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
773480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
7743a40ed3dSBarry Smith   PetscFunctionReturn(0);
775416022c9SBarry Smith }
776416022c9SBarry Smith 
7774a2ae208SSatish Balay #undef __FUNCT__
7784a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
779dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
780416022c9SBarry Smith {
781dfbe8321SBarry Smith   PetscErrorCode ierr;
782ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
783416022c9SBarry Smith 
7843a40ed3dSBarry Smith   PetscFunctionBegin;
785251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
786251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
787251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
788c45a1595SBarry Smith   if (iascii) {
7893a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
7900f5bd95cSBarry Smith   } else if (isbinary) {
7913a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
7920f5bd95cSBarry Smith   } else if (isdraw) {
7933a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
794913ac41fSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
7954108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
7963a40ed3dSBarry Smith   PetscFunctionReturn(0);
79717ab2063SBarry Smith }
79819bcc07fSBarry Smith 
7994a2ae208SSatish Balay #undef __FUNCT__
8004a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
801dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
80217ab2063SBarry Smith {
803416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8046849ba73SBarry Smith   PetscErrorCode ierr;
80597f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
806d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
80754f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
8083447b6efSHong Zhang   PetscReal      ratio=0.6;
80917ab2063SBarry Smith 
8103a40ed3dSBarry Smith   PetscFunctionBegin;
8113a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
81217ab2063SBarry Smith 
81343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
81417ab2063SBarry Smith   for (i=1; i<m; i++) {
815416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
81617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
81794a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
81817ab2063SBarry Smith     if (fshift) {
819bfeeae90SHong Zhang       ip = aj + ai[i] ;
820bfeeae90SHong Zhang       ap = aa + ai[i] ;
82117ab2063SBarry Smith       N  = ailen[i];
82217ab2063SBarry Smith       for (j=0; j<N; j++) {
82317ab2063SBarry Smith         ip[j-fshift] = ip[j];
82417ab2063SBarry Smith         ap[j-fshift] = ap[j];
82517ab2063SBarry Smith       }
82617ab2063SBarry Smith     }
82717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
82817ab2063SBarry Smith   }
82917ab2063SBarry Smith   if (m) {
83017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
83117ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
83217ab2063SBarry Smith   }
83317ab2063SBarry Smith   /* reset ilen and imax for each row */
83417ab2063SBarry Smith   for (i=0; i<m; i++) {
83517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
83617ab2063SBarry Smith   }
837bfeeae90SHong Zhang   a->nz = ai[m];
83865e19b50SBarry 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);
83917ab2063SBarry Smith 
84009f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
841d0f46423SBarry 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);
842ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
843ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
8448e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
845dd5f02e7SSatish Balay   a->reallocs          = 0;
8464e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
84736db0b34SBarry Smith   a->rmax              = rmax;
8484e220ebcSLois Curfman McInnes 
849cd6b891eSBarry Smith   ierr = MatCheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
85088e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
85171c2f376SKris Buschelman 
8524108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
85371f1c65dSBarry Smith 
85471f1c65dSBarry Smith   a->idiagvalid  = PETSC_FALSE;
855bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_FALSE;
8563a40ed3dSBarry Smith   PetscFunctionReturn(0);
85717ab2063SBarry Smith }
85817ab2063SBarry Smith 
8594a2ae208SSatish Balay #undef __FUNCT__
86099cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
86199cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
86299cafbc1SBarry Smith {
86399cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
86499cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
86554f21887SBarry Smith   MatScalar      *aa = a->a;
86699cafbc1SBarry Smith 
86799cafbc1SBarry Smith   PetscFunctionBegin;
86899cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
86986c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
87086c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
87199cafbc1SBarry Smith   PetscFunctionReturn(0);
87299cafbc1SBarry Smith }
87399cafbc1SBarry Smith 
87499cafbc1SBarry Smith #undef __FUNCT__
87599cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
87699cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
87799cafbc1SBarry Smith {
87899cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
87999cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
88054f21887SBarry Smith   MatScalar      *aa = a->a;
88199cafbc1SBarry Smith 
88299cafbc1SBarry Smith   PetscFunctionBegin;
88399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
88486c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
88586c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
88699cafbc1SBarry Smith   PetscFunctionReturn(0);
88799cafbc1SBarry Smith }
88899cafbc1SBarry Smith 
889*78b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
890*78b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ_Kernel(PetscInt thread_id,Mat A)
891*78b84d54SShri Abhyankar {
892*78b84d54SShri Abhyankar   PetscErrorCode ierr;
893*78b84d54SShri Abhyankar   PetscInt       *trstarts=A->rmap->trstarts;
894*78b84d54SShri Abhyankar   PetscInt       n,start,end;
895*78b84d54SShri Abhyankar   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
896*78b84d54SShri Abhyankar 
897*78b84d54SShri Abhyankar   start = trstarts[thread_id];
898*78b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
899*78b84d54SShri Abhyankar   n     = end - start;
900*78b84d54SShri Abhyankar   ierr = PetscMemzero(a->a+start,n*sizeof(PetscScalar));CHKERRQ(ierr);
901*78b84d54SShri Abhyankar   return 0;
902*78b84d54SShri Abhyankar }
903*78b84d54SShri Abhyankar 
904*78b84d54SShri Abhyankar #undef __FUNCT__
905*78b84d54SShri Abhyankar #define __FUNCT__ "MatZeroEntries_SeqAIJ"
906*78b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
907*78b84d54SShri Abhyankar {
908*78b84d54SShri Abhyankar   PetscErrorCode ierr;
909*78b84d54SShri Abhyankar   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
910*78b84d54SShri Abhyankar 
911*78b84d54SShri Abhyankar   PetscFunctionBegin;
912*78b84d54SShri Abhyankar   ierr = PetscThreadCommRunKernel(((PetscObject)A)->comm,(PetscThreadKernel)MatZeroEntries_SeqAIJ_Kernel,1,A);CHKERRQ(ierr);
913*78b84d54SShri Abhyankar   a->idiagvalid = PETSC_FALSE;
914*78b84d54SShri Abhyankar   a->ibdiagvalid = PETSC_FALSE;
915*78b84d54SShri Abhyankar   PetscFunctionReturn(0);
916*78b84d54SShri Abhyankar }
917*78b84d54SShri Abhyankar #else
91899cafbc1SBarry Smith #undef __FUNCT__
9194a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
920dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
92117ab2063SBarry Smith {
922416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
923dfbe8321SBarry Smith   PetscErrorCode ierr;
9243a40ed3dSBarry Smith 
9253a40ed3dSBarry Smith   PetscFunctionBegin;
926d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
92786c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
92886c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
9293a40ed3dSBarry Smith   PetscFunctionReturn(0);
93017ab2063SBarry Smith }
931*78b84d54SShri Abhyankar #endif
932416022c9SBarry Smith 
9334a2ae208SSatish Balay #undef __FUNCT__
9344a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
935dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
93617ab2063SBarry Smith {
937416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
938dfbe8321SBarry Smith   PetscErrorCode ierr;
939d5d45c9bSBarry Smith 
9403a40ed3dSBarry Smith   PetscFunctionBegin;
941aa482453SBarry Smith #if defined(PETSC_USE_LOG)
942d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
94317ab2063SBarry Smith #endif
944e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
9456bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
9466bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
94705b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
948d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
94905b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
95071f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
95105b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
9526bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
95305b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
9546bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
95505b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
9566bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
957cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
9580b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
959a30b2313SHong Zhang 
9604108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
961bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
962901853e0SKris Buschelman 
963dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
964901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
965901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
966901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
967901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
968901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
9695a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr);
970901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
971901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
972a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
973901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
9743a40ed3dSBarry Smith   PetscFunctionReturn(0);
97517ab2063SBarry Smith }
97617ab2063SBarry Smith 
9774a2ae208SSatish Balay #undef __FUNCT__
9784a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
979ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool  flg)
98017ab2063SBarry Smith {
981416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9824846f1f5SKris Buschelman   PetscErrorCode ierr;
9833a40ed3dSBarry Smith 
9843a40ed3dSBarry Smith   PetscFunctionBegin;
985a65d3064SKris Buschelman   switch (op) {
986a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
9874e0d8c25SBarry Smith     a->roworiented       = flg;
988a65d3064SKris Buschelman     break;
989a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
990a9817697SBarry Smith     a->keepnonzeropattern    = flg;
991a65d3064SKris Buschelman     break;
992512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
993512a5fc5SBarry Smith     a->nonew             = (flg ? 0 : 1);
994a65d3064SKris Buschelman     break;
995a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
9964e0d8c25SBarry Smith     a->nonew             = (flg ? -1 : 0);
997a65d3064SKris Buschelman     break;
998a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
9994e0d8c25SBarry Smith     a->nonew             = (flg ? -2 : 0);
1000a65d3064SKris Buschelman     break;
100128b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
100228b2fa4aSMatthew Knepley     a->nounused          = (flg ? -1 : 0);
100328b2fa4aSMatthew Knepley     break;
1004a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
10054e0d8c25SBarry Smith     a->ignorezeroentries = flg;
10060df259c2SBarry Smith     break;
1007cd6b891eSBarry Smith   case MAT_CHECK_COMPRESSED_ROW:
1008cd6b891eSBarry Smith     a->compressedrow.check = flg;
1009d487561eSHong Zhang     break;
10103d472b54SHong Zhang   case MAT_SPD:
10113d472b54SHong Zhang     A->spd_set                         = PETSC_TRUE;
10123d472b54SHong Zhang     A->spd                             = flg;
10133d472b54SHong Zhang     if (flg) {
10143d472b54SHong Zhang       A->symmetric                     = PETSC_TRUE;
10153d472b54SHong Zhang       A->structurally_symmetric        = PETSC_TRUE;
10163d472b54SHong Zhang       A->symmetric_set                 = PETSC_TRUE;
10173d472b54SHong Zhang       A->structurally_symmetric_set    = PETSC_TRUE;
10183d472b54SHong Zhang     }
10193d472b54SHong Zhang     break;
1020b1646e73SJed Brown   case MAT_SYMMETRIC:
1021b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1022b1646e73SJed Brown   case MAT_HERMITIAN:
1023b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
10244e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1025a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1026a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1027290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1028a65d3064SKris Buschelman     break;
1029b87ac2d8SJed Brown   case MAT_USE_INODES:
1030b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1031b87ac2d8SJed Brown     break;
1032a65d3064SKris Buschelman   default:
1033e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1034a65d3064SKris Buschelman   }
10354108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
10363a40ed3dSBarry Smith   PetscFunctionReturn(0);
103717ab2063SBarry Smith }
103817ab2063SBarry Smith 
10394a2ae208SSatish Balay #undef __FUNCT__
10404a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1041dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
104217ab2063SBarry Smith {
1043416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10446849ba73SBarry Smith   PetscErrorCode ierr;
1045d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
104635e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
104717ab2063SBarry Smith 
10483a40ed3dSBarry Smith   PetscFunctionBegin;
1049d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1050e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
105135e7444dSHong Zhang 
1052d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
1053d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
105435e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
10552c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
105635e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
105735e7444dSHong Zhang     PetscFunctionReturn(0);
105835e7444dSHong Zhang   }
105935e7444dSHong Zhang 
10602dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
10611ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
106235e7444dSHong Zhang   for (i=0; i<n; i++) {
106335e7444dSHong Zhang     nz = ai[i+1] - ai[i];
10642f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
106535e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
106635e7444dSHong Zhang       if (aj[j] == i) {
106735e7444dSHong Zhang         x[i] = aa[j];
106817ab2063SBarry Smith         break;
106917ab2063SBarry Smith       }
107017ab2063SBarry Smith     }
107117ab2063SBarry Smith   }
10721ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
10733a40ed3dSBarry Smith   PetscFunctionReturn(0);
107417ab2063SBarry Smith }
107517ab2063SBarry Smith 
1076c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10774a2ae208SSatish Balay #undef __FUNCT__
10784a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1079dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
108017ab2063SBarry Smith {
1081416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
10825c897100SBarry Smith   PetscScalar       *x,*y;
1083dfbe8321SBarry Smith   PetscErrorCode    ierr;
1084d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
10855c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1086a77337e4SBarry Smith   MatScalar         *v;
1087a77337e4SBarry Smith   PetscScalar       alpha;
108804fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
10893447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
1090ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
10915c897100SBarry Smith #endif
109217ab2063SBarry Smith 
10933a40ed3dSBarry Smith   PetscFunctionBegin;
10942e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
10951ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10961ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10975c897100SBarry Smith 
10985c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1099bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
11005c897100SBarry Smith #else
11013447b6efSHong Zhang   if (usecprow){
11023447b6efSHong Zhang     m    = cprow.nrows;
11033447b6efSHong Zhang     ii   = cprow.i;
11047b2bb3b9SHong Zhang     ridx = cprow.rindex;
11053447b6efSHong Zhang   } else {
11063447b6efSHong Zhang     ii = a->i;
11073447b6efSHong Zhang   }
110817ab2063SBarry Smith   for (i=0; i<m; i++) {
11093447b6efSHong Zhang     idx   = a->j + ii[i] ;
11103447b6efSHong Zhang     v     = a->a + ii[i] ;
11113447b6efSHong Zhang     n     = ii[i+1] - ii[i];
11123447b6efSHong Zhang     if (usecprow){
11137b2bb3b9SHong Zhang       alpha = x[ridx[i]];
11143447b6efSHong Zhang     } else {
111517ab2063SBarry Smith       alpha = x[i];
11163447b6efSHong Zhang     }
111704fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
111817ab2063SBarry Smith   }
11195c897100SBarry Smith #endif
1120dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
11211ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11221ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11233a40ed3dSBarry Smith   PetscFunctionReturn(0);
112417ab2063SBarry Smith }
112517ab2063SBarry Smith 
11264a2ae208SSatish Balay #undef __FUNCT__
11275c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1128dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
11295c897100SBarry Smith {
1130dfbe8321SBarry Smith   PetscErrorCode ierr;
11315c897100SBarry Smith 
11325c897100SBarry Smith   PetscFunctionBegin;
1133170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
11345c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
11355c897100SBarry Smith   PetscFunctionReturn(0);
11365c897100SBarry Smith }
11375c897100SBarry Smith 
1138c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1139*78b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
1140*78b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ_Kernel(PetscInt thread_id,Mat A,Vec xx,Vec yy)
1141*78b84d54SShri Abhyankar {
1142*78b84d54SShri Abhyankar   PetscErrorCode    ierr;
1143*78b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1144*78b84d54SShri Abhyankar   PetscScalar       *y;
1145*78b84d54SShri Abhyankar   const PetscScalar *x;
1146*78b84d54SShri Abhyankar   const MatScalar   *aa;
1147*78b84d54SShri Abhyankar   PetscInt          *trstarts=A->rmap->trstarts;
1148*78b84d54SShri Abhyankar   PetscInt          n,start,end,i;
1149*78b84d54SShri Abhyankar   const PetscInt   *aj,*ai;
1150*78b84d54SShri Abhyankar   PetscScalar      sum;
1151*78b84d54SShri Abhyankar 
1152*78b84d54SShri Abhyankar   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1153*78b84d54SShri Abhyankar   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1154*78b84d54SShri Abhyankar   start = trstarts[thread_id];
1155*78b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
1156*78b84d54SShri Abhyankar   aj    = a->j;
1157*78b84d54SShri Abhyankar   aa    = a->a;
1158*78b84d54SShri Abhyankar   ai    = a->i;
1159*78b84d54SShri Abhyankar   for(i=start;i<end;i++) {
1160*78b84d54SShri Abhyankar     n = ai[i+1] - ai[i];
1161*78b84d54SShri Abhyankar     aj = a->j + ai[i];
1162*78b84d54SShri Abhyankar     aa = a->a + ai[i];
1163*78b84d54SShri Abhyankar     sum = 0.0;
1164*78b84d54SShri Abhyankar     PetscSparseDensePlusDot(sum,x,aa,aj,n);
1165*78b84d54SShri Abhyankar     y[i] = sum;
1166*78b84d54SShri Abhyankar   }
1167*78b84d54SShri Abhyankar   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1168*78b84d54SShri Abhyankar   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1169*78b84d54SShri Abhyankar   return 0;
1170*78b84d54SShri Abhyankar }
1171*78b84d54SShri Abhyankar 
1172*78b84d54SShri Abhyankar #undef __FUNCT__
1173*78b84d54SShri Abhyankar #define __FUNCT__ "MatMult_SeqAIJ"
1174*78b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
1175*78b84d54SShri Abhyankar {
1176*78b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1177*78b84d54SShri Abhyankar   PetscScalar       *y;
1178*78b84d54SShri Abhyankar   const PetscScalar *x;
1179*78b84d54SShri Abhyankar   const MatScalar   *aa;
1180*78b84d54SShri Abhyankar   PetscErrorCode    ierr;
1181*78b84d54SShri Abhyankar   PetscInt          m=A->rmap->n;
1182*78b84d54SShri Abhyankar   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
1183*78b84d54SShri Abhyankar   PetscInt          n,i,nonzerorow=0;
1184*78b84d54SShri Abhyankar   PetscScalar       sum;
1185*78b84d54SShri Abhyankar   PetscBool         usecprow=a->compressedrow.use;
1186*78b84d54SShri Abhyankar 
1187*78b84d54SShri Abhyankar #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1188*78b84d54SShri Abhyankar #pragma disjoint(*x,*y,*aa)
1189*78b84d54SShri Abhyankar #endif
1190*78b84d54SShri Abhyankar 
1191*78b84d54SShri Abhyankar   PetscFunctionBegin;
1192*78b84d54SShri Abhyankar   aj  = a->j;
1193*78b84d54SShri Abhyankar   aa  = a->a;
1194*78b84d54SShri Abhyankar   ii  = a->i;
1195*78b84d54SShri Abhyankar   if (usecprow){ /* use compressed row format */
1196*78b84d54SShri Abhyankar     ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1197*78b84d54SShri Abhyankar     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1198*78b84d54SShri Abhyankar     m    = a->compressedrow.nrows;
1199*78b84d54SShri Abhyankar     ii   = a->compressedrow.i;
1200*78b84d54SShri Abhyankar     ridx = a->compressedrow.rindex;
1201*78b84d54SShri Abhyankar     for (i=0; i<m; i++){
1202*78b84d54SShri Abhyankar       n   = ii[i+1] - ii[i];
1203*78b84d54SShri Abhyankar       aj  = a->j + ii[i];
1204*78b84d54SShri Abhyankar       aa  = a->a + ii[i];
1205*78b84d54SShri Abhyankar       sum = 0.0;
1206*78b84d54SShri Abhyankar       nonzerorow += (n>0);
1207*78b84d54SShri Abhyankar       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1208*78b84d54SShri Abhyankar       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1209*78b84d54SShri Abhyankar       y[*ridx++] = sum;
1210*78b84d54SShri Abhyankar     }
1211*78b84d54SShri Abhyankar     ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1212*78b84d54SShri Abhyankar     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1213*78b84d54SShri Abhyankar   } else { /* do not use compressed row format */
1214*78b84d54SShri Abhyankar #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1215*78b84d54SShri Abhyankar     fortranmultaij_(&m,x,ii,aj,aa,y);
1216*78b84d54SShri Abhyankar #else
1217*78b84d54SShri Abhyankar     ierr = PetscThreadCommRunKernel(((PetscObject)A)->comm,(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
1218*78b84d54SShri Abhyankar #endif
1219*78b84d54SShri Abhyankar   }
1220*78b84d54SShri Abhyankar   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1221*78b84d54SShri Abhyankar   PetscFunctionReturn(0);
1222*78b84d54SShri Abhyankar }
1223*78b84d54SShri Abhyankar #else
12245c897100SBarry Smith #undef __FUNCT__
12254a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1226dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
122717ab2063SBarry Smith {
1228416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1229d9fead3dSBarry Smith   PetscScalar       *y;
123054f21887SBarry Smith   const PetscScalar *x;
123154f21887SBarry Smith   const MatScalar   *aa;
1232dfbe8321SBarry Smith   PetscErrorCode    ierr;
1233003131ecSBarry Smith   PetscInt          m=A->rmap->n;
1234003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
12358aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1236362ced78SSatish Balay   PetscScalar       sum;
1237ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
123817ab2063SBarry Smith 
1239b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
124097952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1241fee21e36SBarry Smith #endif
1242fee21e36SBarry Smith 
12433a40ed3dSBarry Smith   PetscFunctionBegin;
12443649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12451ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
124697952fefSHong Zhang   aj  = a->j;
124797952fefSHong Zhang   aa  = a->a;
1248416022c9SBarry Smith   ii  = a->i;
12494eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
125097952fefSHong Zhang     m    = a->compressedrow.nrows;
125197952fefSHong Zhang     ii   = a->compressedrow.i;
125297952fefSHong Zhang     ridx = a->compressedrow.rindex;
125397952fefSHong Zhang     for (i=0; i<m; i++){
125497952fefSHong Zhang       n   = ii[i+1] - ii[i];
125597952fefSHong Zhang       aj  = a->j + ii[i];
125697952fefSHong Zhang       aa  = a->a + ii[i];
125797952fefSHong Zhang       sum = 0.0;
1258a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1259003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1260003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
126197952fefSHong Zhang       y[*ridx++] = sum;
126297952fefSHong Zhang     }
126397952fefSHong Zhang   } else { /* do not use compressed row format */
1264b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1265b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1266b05257ddSBarry Smith #else
1267*78b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
1268*78b84d54SShri Abhyankar     ierr = PetscThreadCommRunKernel(((PetscObject)A)->comm,(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
1269*78b84d54SShri Abhyankar #else
127017ab2063SBarry Smith     for (i=0; i<m; i++) {
1271003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1272003131ecSBarry Smith       aj  = a->j + ii[i];
1273003131ecSBarry Smith       aa  = a->a + ii[i];
127417ab2063SBarry Smith       sum  = 0.0;
1275a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1276003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
127717ab2063SBarry Smith       y[i] = sum;
127817ab2063SBarry Smith     }
12798d195f9aSBarry Smith #endif
1280*78b84d54SShri Abhyankar #endif
1281b05257ddSBarry Smith   }
1282dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
12833649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12841ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12853a40ed3dSBarry Smith   PetscFunctionReturn(0);
128617ab2063SBarry Smith }
1287*78b84d54SShri Abhyankar #endif
128817ab2063SBarry Smith 
1289c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
12904a2ae208SSatish Balay #undef __FUNCT__
12914a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1292dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
129317ab2063SBarry Smith {
1294416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1295f15663dcSBarry Smith   PetscScalar       *y,*z;
1296f15663dcSBarry Smith   const PetscScalar *x;
129754f21887SBarry Smith   const MatScalar   *aa;
1298dfbe8321SBarry Smith   PetscErrorCode    ierr;
1299d0f46423SBarry Smith   PetscInt          m = A->rmap->n,*aj,*ii;
1300f15663dcSBarry Smith   PetscInt          n,i,*ridx=PETSC_NULL;
1301362ced78SSatish Balay   PetscScalar       sum;
1302ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
13039ea0dfa2SSatish Balay 
13043a40ed3dSBarry Smith   PetscFunctionBegin;
1305f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
13061ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
13072e8a6d31SBarry Smith   if (zz != yy) {
13081ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
13092e8a6d31SBarry Smith   } else {
13102e8a6d31SBarry Smith     z = y;
13112e8a6d31SBarry Smith   }
1312bfeeae90SHong Zhang 
131397952fefSHong Zhang   aj  = a->j;
131497952fefSHong Zhang   aa  = a->a;
1315cddf8d76SBarry Smith   ii  = a->i;
13164eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
13174eb6d288SHong Zhang     if (zz != yy){
13184eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
13194eb6d288SHong Zhang     }
132097952fefSHong Zhang     m    = a->compressedrow.nrows;
132197952fefSHong Zhang     ii   = a->compressedrow.i;
132297952fefSHong Zhang     ridx = a->compressedrow.rindex;
132397952fefSHong Zhang     for (i=0; i<m; i++){
132497952fefSHong Zhang       n  = ii[i+1] - ii[i];
132597952fefSHong Zhang       aj  = a->j + ii[i];
132697952fefSHong Zhang       aa  = a->a + ii[i];
132797952fefSHong Zhang       sum = y[*ridx];
1328f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
132997952fefSHong Zhang       z[*ridx++] = sum;
133097952fefSHong Zhang     }
133197952fefSHong Zhang   } else { /* do not use compressed row format */
1332f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1333f15663dcSBarry Smith   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1334f15663dcSBarry Smith #else
133517ab2063SBarry Smith     for (i=0; i<m; i++) {
1336f15663dcSBarry Smith       n    = ii[i+1] - ii[i];
1337f15663dcSBarry Smith       aj  = a->j + ii[i];
1338f15663dcSBarry Smith       aa  = a->a + ii[i];
133917ab2063SBarry Smith       sum  = y[i];
1340f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
134117ab2063SBarry Smith       z[i] = sum;
134217ab2063SBarry Smith     }
134302ab625aSSatish Balay #endif
1344f15663dcSBarry Smith   }
1345dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1346f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13471ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13482e8a6d31SBarry Smith   if (zz != yy) {
13491ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
13502e8a6d31SBarry Smith   }
13518154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
13526b375ea7SVictor Minden   /*
1353918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1354918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1355918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
13566b375ea7SVictor Minden   */
1357918e98c3SVictor Minden #endif
13583a40ed3dSBarry Smith   PetscFunctionReturn(0);
135917ab2063SBarry Smith }
136017ab2063SBarry Smith 
136117ab2063SBarry Smith /*
136217ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
136317ab2063SBarry Smith */
13644a2ae208SSatish Balay #undef __FUNCT__
13654a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1366dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
136717ab2063SBarry Smith {
1368416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
13696849ba73SBarry Smith   PetscErrorCode ierr;
1370d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
137117ab2063SBarry Smith 
13723a40ed3dSBarry Smith   PetscFunctionBegin;
137309f38230SBarry Smith   if (!a->diag) {
137409f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
13759518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
137609f38230SBarry Smith   }
1377d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
137809f38230SBarry Smith     a->diag[i] = a->i[i+1];
1379bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1380bfeeae90SHong Zhang       if (a->j[j] == i) {
138109f38230SBarry Smith         a->diag[i] = j;
138217ab2063SBarry Smith         break;
138317ab2063SBarry Smith       }
138417ab2063SBarry Smith     }
138517ab2063SBarry Smith   }
13863a40ed3dSBarry Smith   PetscFunctionReturn(0);
138717ab2063SBarry Smith }
138817ab2063SBarry Smith 
1389be5855fcSBarry Smith /*
1390be5855fcSBarry Smith      Checks for missing diagonals
1391be5855fcSBarry Smith */
13924a2ae208SSatish Balay #undef __FUNCT__
13934a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1394ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1395be5855fcSBarry Smith {
1396be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
139797f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1398be5855fcSBarry Smith 
1399be5855fcSBarry Smith   PetscFunctionBegin;
140009f38230SBarry Smith   *missing = PETSC_FALSE;
1401d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
140209f38230SBarry Smith     *missing  = PETSC_TRUE;
140309f38230SBarry Smith     if (d) *d = 0;
1404358d2f5dSShri Abhyankar     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
140509f38230SBarry Smith   } else {
1406f1e2ffcdSBarry Smith     diag = a->diag;
1407d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1408bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
140909f38230SBarry Smith 	*missing = PETSC_TRUE;
141009f38230SBarry Smith 	if (d) *d = i;
141109f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
1412358d2f5dSShri Abhyankar 	break;
141309f38230SBarry Smith       }
1414be5855fcSBarry Smith     }
1415be5855fcSBarry Smith   }
1416be5855fcSBarry Smith   PetscFunctionReturn(0);
1417be5855fcSBarry Smith }
1418be5855fcSBarry Smith 
141971f1c65dSBarry Smith EXTERN_C_BEGIN
142071f1c65dSBarry Smith #undef __FUNCT__
142171f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
14227087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
142371f1c65dSBarry Smith {
142471f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
142571f1c65dSBarry Smith   PetscErrorCode ierr;
1426d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
142754f21887SBarry Smith   MatScalar      *v = a->a;
142854f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
142971f1c65dSBarry Smith 
143071f1c65dSBarry Smith   PetscFunctionBegin;
143171f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
143271f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
143371f1c65dSBarry Smith   diag = a->diag;
143471f1c65dSBarry Smith   if (!a->idiag) {
143571f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
143671f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
143771f1c65dSBarry Smith     v        = a->a;
143871f1c65dSBarry Smith   }
143971f1c65dSBarry Smith   mdiag = a->mdiag;
144071f1c65dSBarry Smith   idiag = a->idiag;
144171f1c65dSBarry Smith 
1442028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
144371f1c65dSBarry Smith     for (i=0; i<m; i++) {
144471f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1445e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
144671f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
144771f1c65dSBarry Smith     }
144871f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
144971f1c65dSBarry Smith   } else {
145071f1c65dSBarry Smith     for (i=0; i<m; i++) {
145171f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
145271f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
145371f1c65dSBarry Smith     }
1454dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
145571f1c65dSBarry Smith   }
145671f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
145771f1c65dSBarry Smith   PetscFunctionReturn(0);
145871f1c65dSBarry Smith }
14595a9745a3SMatthew Knepley EXTERN_C_END
146071f1c65dSBarry Smith 
1461c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
14624a2ae208SSatish Balay #undef __FUNCT__
146341f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
146441f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
146517ab2063SBarry Smith {
1466416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1467e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1468e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
146954f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1470dfbe8321SBarry Smith   PetscErrorCode     ierr;
1471d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
147297f1f81fSBarry Smith   const PetscInt     *idx,*diag;
147317ab2063SBarry Smith 
14743a40ed3dSBarry Smith   PetscFunctionBegin;
1475b965ef7fSBarry Smith   its = its*lits;
147691723122SBarry Smith 
147771f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
147871f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
147971f1c65dSBarry Smith   a->fshift = fshift;
148071f1c65dSBarry Smith   a->omega  = omega;
1481ed480e8bSBarry Smith 
148271f1c65dSBarry Smith   diag = a->diag;
148371f1c65dSBarry Smith   t     = a->ssor_work;
1484ed480e8bSBarry Smith   idiag = a->idiag;
148571f1c65dSBarry Smith   mdiag = a->mdiag;
1486ed480e8bSBarry Smith 
14871ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
14883649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
148971f1c65dSBarry Smith   CHKMEMQ;
1490ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
149117ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
149217ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1493ed480e8bSBarry Smith     bs = b;
149417ab2063SBarry Smith     for (i=0; i<m; i++) {
149571f1c65dSBarry Smith         d    = fshift + mdiag[i];
1496416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1497ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1498ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
149917ab2063SBarry Smith         sum  = b[i]*d/omega;
1500003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
150117ab2063SBarry Smith         x[i] = sum;
150217ab2063SBarry Smith     }
15031ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15043649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1505efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
15063a40ed3dSBarry Smith     PetscFunctionReturn(0);
150717ab2063SBarry Smith   }
1508c783ea89SBarry Smith 
150948af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1510e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
15113a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
151217ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1513887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
151417ab2063SBarry Smith 
151517ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
151617ab2063SBarry Smith 
1517887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
151817ab2063SBarry Smith     */
151917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
152017ab2063SBarry Smith 
152117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
152217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1523416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1524ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1525ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
152617ab2063SBarry Smith       sum  = b[i];
1527e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1528ed480e8bSBarry Smith       x[i] = sum*idiag[i];
152917ab2063SBarry Smith     }
153017ab2063SBarry Smith 
153117ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1532416022c9SBarry Smith     v = a->a;
1533ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
153417ab2063SBarry Smith 
153517ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1536ed480e8bSBarry Smith     ts = t;
1537416022c9SBarry Smith     diag = a->diag;
153817ab2063SBarry Smith     for (i=0; i<m; i++) {
1539416022c9SBarry Smith       n    = diag[i] - a->i[i];
1540ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1541ed480e8bSBarry Smith       v    = a->a + a->i[i];
154217ab2063SBarry Smith       sum  = t[i];
1543003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1544ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1545733d66baSBarry Smith       /*  x = x + t */
1546733d66baSBarry Smith       x[i] += t[i];
154717ab2063SBarry Smith     }
154817ab2063SBarry Smith 
1549dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
15501ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15513649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
15523a40ed3dSBarry Smith     PetscFunctionReturn(0);
155317ab2063SBarry Smith   }
155417ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
155517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
155617ab2063SBarry Smith       for (i=0; i<m; i++) {
1557416022c9SBarry Smith         n    = diag[i] - a->i[i];
1558ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1559ed480e8bSBarry Smith         v    = a->a + a->i[i];
156017ab2063SBarry Smith         sum  = b[i];
1561e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
15625c99c7daSBarry Smith         t[i] = sum;
1563ed480e8bSBarry Smith         x[i] = sum*idiag[i];
156417ab2063SBarry Smith       }
15655c99c7daSBarry Smith       xb = t;
1566efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
15673a40ed3dSBarry Smith     } else xb = b;
156817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
156917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1570416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1571ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1572ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
157317ab2063SBarry Smith         sum  = xb[i];
1574e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
15755c99c7daSBarry Smith         if (xb == b) {
1576ed480e8bSBarry Smith           x[i] = sum*idiag[i];
15775c99c7daSBarry Smith         } else {
15785c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
157917ab2063SBarry Smith         }
15805c99c7daSBarry Smith       }
1581efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
158217ab2063SBarry Smith     }
158317ab2063SBarry Smith     its--;
158417ab2063SBarry Smith   }
158517ab2063SBarry Smith   while (its--) {
158617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
158717ab2063SBarry Smith       for (i=0; i<m; i++) {
1588416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1589ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1590ed480e8bSBarry Smith         v    = a->a + a->i[i];
159117ab2063SBarry Smith         sum  = b[i];
1592e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1593ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
159417ab2063SBarry Smith       }
15959f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
159617ab2063SBarry Smith     }
159717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
159817ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1599416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1600ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1601ed480e8bSBarry Smith         v    = a->a + a->i[i];
160217ab2063SBarry Smith         sum  = b[i];
1603e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1604ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
160517ab2063SBarry Smith       }
16069f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
160717ab2063SBarry Smith     }
160817ab2063SBarry Smith   }
16091ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16103649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
161171f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
161217ab2063SBarry Smith }
161317ab2063SBarry Smith 
16142af78befSBarry Smith 
16154a2ae208SSatish Balay #undef __FUNCT__
16164a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1617dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
161817ab2063SBarry Smith {
1619416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
16204e220ebcSLois Curfman McInnes 
16213a40ed3dSBarry Smith   PetscFunctionBegin;
16224e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
16234e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
16244e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
16254e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
16264e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
16278e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
16287adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1629d5f3da31SBarry Smith   if (A->factortype) {
16304e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
16314e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
16324e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
16334e220ebcSLois Curfman McInnes   } else {
16344e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
16354e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
16364e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
16374e220ebcSLois Curfman McInnes   }
16383a40ed3dSBarry Smith   PetscFunctionReturn(0);
163917ab2063SBarry Smith }
164017ab2063SBarry Smith 
16414a2ae208SSatish Balay #undef __FUNCT__
16424a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
16432b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
164417ab2063SBarry Smith {
1645416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
16463b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
16476849ba73SBarry Smith   PetscErrorCode    ierr;
164897b48c8fSBarry Smith   const PetscScalar *xx;
164997b48c8fSBarry Smith   PetscScalar       *bb;
1650ace3abfcSBarry Smith   PetscBool         missing;
165117ab2063SBarry Smith 
16523a40ed3dSBarry Smith   PetscFunctionBegin;
165397b48c8fSBarry Smith   if (x && b) {
165497b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
165597b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
165697b48c8fSBarry Smith     for (i=0; i<N; i++) {
165797b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
165897b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
165997b48c8fSBarry Smith     }
166097b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
166197b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
166297b48c8fSBarry Smith   }
166397b48c8fSBarry Smith 
1664a9817697SBarry Smith   if (a->keepnonzeropattern) {
1665f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1666e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1667bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1668f1e2ffcdSBarry Smith     }
1669f4df32b1SMatthew Knepley     if (diag != 0.0) {
167009f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1671e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1672f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1673f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1674f1e2ffcdSBarry Smith       }
1675f1e2ffcdSBarry Smith     }
167688e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1677f1e2ffcdSBarry Smith   } else {
1678f4df32b1SMatthew Knepley     if (diag != 0.0) {
167917ab2063SBarry Smith       for (i=0; i<N; i++) {
1680e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
16817ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1682416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1683f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1684bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
16857ae801bdSBarry Smith         } else { /* in case row was completely empty */
1686f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
168717ab2063SBarry Smith         }
168817ab2063SBarry Smith       }
16893a40ed3dSBarry Smith     } else {
169017ab2063SBarry Smith       for (i=0; i<N; i++) {
1691e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1692416022c9SBarry Smith         a->ilen[rows[i]] = 0;
169317ab2063SBarry Smith       }
169417ab2063SBarry Smith     }
169588e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1696f1e2ffcdSBarry Smith   }
169743a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16983a40ed3dSBarry Smith   PetscFunctionReturn(0);
169917ab2063SBarry Smith }
170017ab2063SBarry Smith 
17014a2ae208SSatish Balay #undef __FUNCT__
17026e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
17036e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
17046e169961SBarry Smith {
17056e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
17066e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
17076e169961SBarry Smith   PetscErrorCode    ierr;
17082b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
17096e169961SBarry Smith   const PetscScalar *xx;
17106e169961SBarry Smith   PetscScalar       *bb;
17116e169961SBarry Smith 
17126e169961SBarry Smith   PetscFunctionBegin;
17136e169961SBarry Smith   if (x && b) {
17146e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
17156e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
17162b40b63fSBarry Smith     vecs = PETSC_TRUE;
17176e169961SBarry Smith   }
17186e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
17196e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
17206e169961SBarry Smith   for (i=0; i<N; i++) {
17216e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
17226e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
17236e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
17246e169961SBarry Smith   }
17256e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
17266e169961SBarry Smith     if (!zeroed[i]) {
17276e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
17286e169961SBarry Smith         if (zeroed[a->j[j]]) {
17292b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
17306e169961SBarry Smith           a->a[j] = 0.0;
17316e169961SBarry Smith         }
17326e169961SBarry Smith       }
17332b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
17346e169961SBarry Smith   }
17356e169961SBarry Smith   if (x && b) {
17366e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
17376e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
17386e169961SBarry Smith   }
17396e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
17406e169961SBarry Smith   if (diag != 0.0) {
17416e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
17426e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
17436e169961SBarry Smith     for (i=0; i<N; i++) {
17446e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
17456e169961SBarry Smith     }
17466e169961SBarry Smith   }
17476e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
17486e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17496e169961SBarry Smith   PetscFunctionReturn(0);
17506e169961SBarry Smith }
17516e169961SBarry Smith 
17526e169961SBarry Smith #undef __FUNCT__
17534a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1754a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
175517ab2063SBarry Smith {
1756416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
175797f1f81fSBarry Smith   PetscInt   *itmp;
175817ab2063SBarry Smith 
17593a40ed3dSBarry Smith   PetscFunctionBegin;
1760e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
176117ab2063SBarry Smith 
1762416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1763bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
176417ab2063SBarry Smith   if (idx) {
1765bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1766bfeeae90SHong Zhang     if (*nz) {
17674e093b46SBarry Smith       *idx = itmp;
176817ab2063SBarry Smith     }
176917ab2063SBarry Smith     else *idx = 0;
177017ab2063SBarry Smith   }
17713a40ed3dSBarry Smith   PetscFunctionReturn(0);
177217ab2063SBarry Smith }
177317ab2063SBarry Smith 
1774bfeeae90SHong Zhang /* remove this function? */
17754a2ae208SSatish Balay #undef __FUNCT__
17764a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1777a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
177817ab2063SBarry Smith {
17793a40ed3dSBarry Smith   PetscFunctionBegin;
17803a40ed3dSBarry Smith   PetscFunctionReturn(0);
178117ab2063SBarry Smith }
178217ab2063SBarry Smith 
17834a2ae208SSatish Balay #undef __FUNCT__
17844a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1785dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
178617ab2063SBarry Smith {
1787416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
178854f21887SBarry Smith   MatScalar      *v = a->a;
178936db0b34SBarry Smith   PetscReal      sum = 0.0;
17906849ba73SBarry Smith   PetscErrorCode ierr;
179197f1f81fSBarry Smith   PetscInt       i,j;
179217ab2063SBarry Smith 
17933a40ed3dSBarry Smith   PetscFunctionBegin;
179417ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1795416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1796aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
179736db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
179817ab2063SBarry Smith #else
179917ab2063SBarry Smith       sum += (*v)*(*v); v++;
180017ab2063SBarry Smith #endif
180117ab2063SBarry Smith     }
18028f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
18033a40ed3dSBarry Smith   } else if (type == NORM_1) {
180436db0b34SBarry Smith     PetscReal *tmp;
180597f1f81fSBarry Smith     PetscInt    *jj = a->j;
1806d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1807d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1808064f8208SBarry Smith     *nrm = 0.0;
1809416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1810bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
181117ab2063SBarry Smith     }
1812d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1813064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
181417ab2063SBarry Smith     }
1815606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
18163a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1817064f8208SBarry Smith     *nrm = 0.0;
1818d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1819bfeeae90SHong Zhang       v = a->a + a->i[j];
182017ab2063SBarry Smith       sum = 0.0;
1821416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1822cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
182317ab2063SBarry Smith       }
1824064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
182517ab2063SBarry Smith     }
18263a40ed3dSBarry Smith   } else {
1827e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
182817ab2063SBarry Smith   }
18293a40ed3dSBarry Smith   PetscFunctionReturn(0);
183017ab2063SBarry Smith }
183117ab2063SBarry Smith 
18324e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
18334e938277SHong Zhang #undef __FUNCT__
18344e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
18354e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
18364e938277SHong Zhang {
18374e938277SHong Zhang   PetscErrorCode ierr;
18384e938277SHong Zhang   PetscInt       i,j,anzj;
18394e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ *)A->data,*b;
18404e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
18414e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
18424e938277SHong Zhang 
18434e938277SHong Zhang   PetscFunctionBegin;
18444e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
18454e938277SHong Zhang   ierr = PetscMalloc((an+1)*sizeof(PetscInt),&ati);CHKERRQ(ierr);
18464e938277SHong Zhang   ierr = PetscMalloc(ai[am]*sizeof(PetscInt),&atj);CHKERRQ(ierr);
18474e938277SHong Zhang   ierr = PetscMalloc(an*sizeof(PetscInt),&atfill);CHKERRQ(ierr);
18484e938277SHong Zhang   ierr = PetscMemzero(ati,(an+1)*sizeof(PetscInt));CHKERRQ(ierr);
18494e938277SHong Zhang 
18504e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
18514e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
18524e938277SHong Zhang   for (i=0;i<ai[am];i++) {
18534e938277SHong Zhang     ati[aj[i]+1] += 1;
18544e938277SHong Zhang   }
18554e938277SHong Zhang   /* Form ati for csr format of A^T. */
18564e938277SHong Zhang   for (i=0;i<an;i++) {
18574e938277SHong Zhang     ati[i+1] += ati[i];
18584e938277SHong Zhang   }
18594e938277SHong Zhang 
18604e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
18614e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
18624e938277SHong Zhang 
18634e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
18644e938277SHong Zhang   for (i=0;i<am;i++) {
18654e938277SHong Zhang     anzj = ai[i+1] - ai[i];
18664e938277SHong Zhang     for (j=0;j<anzj;j++) {
18674e938277SHong Zhang       atj[atfill[*aj]] = i;
18684e938277SHong Zhang       atfill[*aj++]   += 1;
18694e938277SHong Zhang     }
18704e938277SHong Zhang   }
18714e938277SHong Zhang 
18724e938277SHong Zhang   /* Clean up temporary space and complete requests. */
18734e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
18744e938277SHong Zhang   ierr = MatCreateSeqAIJWithArrays(((PetscObject)A)->comm,an,am,ati,atj,PETSC_NULL,B);CHKERRQ(ierr);
1875a2f3521dSMark F. Adams   (*B)->rmap->bs = A->cmap->bs;
1876a2f3521dSMark F. Adams   (*B)->cmap->bs = A->rmap->bs;
1877a2f3521dSMark F. Adams 
18784e938277SHong Zhang   b = (Mat_SeqAIJ *)((*B)->data);
18794e938277SHong Zhang   b->free_a   = PETSC_FALSE;
18804e938277SHong Zhang   b->free_ij  = PETSC_TRUE;
18814e938277SHong Zhang   b->nonew    = 0;
18824e938277SHong Zhang   PetscFunctionReturn(0);
18834e938277SHong Zhang }
18844e938277SHong Zhang 
18854a2ae208SSatish Balay #undef __FUNCT__
18864a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1887fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
188817ab2063SBarry Smith {
1889416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1890416022c9SBarry Smith   Mat            C;
18916849ba73SBarry Smith   PetscErrorCode ierr;
1892d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
189354f21887SBarry Smith   MatScalar      *array = a->a;
189417ab2063SBarry Smith 
18953a40ed3dSBarry Smith   PetscFunctionBegin;
1896e32f2f54SBarry 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");
1897fc4dec0aSBarry Smith 
1898fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1899d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1900d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1901bfeeae90SHong Zhang 
1902bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
19037adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1904d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
1905a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(C,A->cmap->bs,A->rmap->bs);CHKERRQ(ierr);
19067adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1907ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1908606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1909a541d17aSBarry Smith   } else {
1910a541d17aSBarry Smith     C = *B;
1911a541d17aSBarry Smith   }
1912a541d17aSBarry Smith 
191317ab2063SBarry Smith   for (i=0; i<m; i++) {
191417ab2063SBarry Smith     len    = ai[i+1]-ai[i];
191587d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1916b9b97703SBarry Smith     array += len;
1917b9b97703SBarry Smith     aj    += len;
191817ab2063SBarry Smith   }
19196d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19206d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
192117ab2063SBarry Smith 
1922815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1923416022c9SBarry Smith     *B = C;
192417ab2063SBarry Smith   } else {
1925eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
192617ab2063SBarry Smith   }
19273a40ed3dSBarry Smith   PetscFunctionReturn(0);
192817ab2063SBarry Smith }
192917ab2063SBarry Smith 
1930cd0d46ebSvictorle EXTERN_C_BEGIN
1931cd0d46ebSvictorle #undef __FUNCT__
19325fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
19337087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1934cd0d46ebSvictorle {
1935cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
193654f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
193754f21887SBarry Smith   MatScalar      *va,*vb;
19386849ba73SBarry Smith   PetscErrorCode ierr;
193997f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1940cd0d46ebSvictorle 
1941cd0d46ebSvictorle   PetscFunctionBegin;
1942cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1943cd0d46ebSvictorle 
1944cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1945cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
19465485867bSBarry Smith   if (ma!=nb || na!=mb){
19475485867bSBarry Smith     *f = PETSC_FALSE;
19485485867bSBarry Smith     PetscFunctionReturn(0);
19495485867bSBarry Smith   }
1950cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1951cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1952cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
195397f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
195497f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1955cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1956cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1957cd0d46ebSvictorle 
1958cd0d46ebSvictorle   *f = PETSC_TRUE;
1959cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1960cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
196197f1f81fSBarry Smith       PetscInt         idc,idr;
19625485867bSBarry Smith       PetscScalar vc,vr;
1963cd0d46ebSvictorle       /* column/row index/value */
19645485867bSBarry Smith       idc = adx[aptr[i]];
19655485867bSBarry Smith       idr = bdx[bptr[idc]];
19665485867bSBarry Smith       vc  = va[aptr[i]];
19675485867bSBarry Smith       vr  = vb[bptr[idc]];
19685485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
19695485867bSBarry Smith         *f = PETSC_FALSE;
19705485867bSBarry Smith         goto done;
1971cd0d46ebSvictorle       } else {
19725485867bSBarry Smith         aptr[i]++;
19735485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1974cd0d46ebSvictorle       }
1975cd0d46ebSvictorle     }
1976cd0d46ebSvictorle   }
1977cd0d46ebSvictorle  done:
1978cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
19793aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
1980cd0d46ebSvictorle   PetscFunctionReturn(0);
1981cd0d46ebSvictorle }
1982cd0d46ebSvictorle EXTERN_C_END
1983cd0d46ebSvictorle 
19841cbb95d3SBarry Smith EXTERN_C_BEGIN
19851cbb95d3SBarry Smith #undef __FUNCT__
19861cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
19877087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
19881cbb95d3SBarry Smith {
19891cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
199054f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
199154f21887SBarry Smith   MatScalar      *va,*vb;
19921cbb95d3SBarry Smith   PetscErrorCode ierr;
19931cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
19941cbb95d3SBarry Smith 
19951cbb95d3SBarry Smith   PetscFunctionBegin;
19961cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
19971cbb95d3SBarry Smith 
19981cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
19991cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
20001cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
20011cbb95d3SBarry Smith     *f = PETSC_FALSE;
20021cbb95d3SBarry Smith     PetscFunctionReturn(0);
20031cbb95d3SBarry Smith   }
20041cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
20051cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
20061cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
20071cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
20081cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
20091cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
20101cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
20111cbb95d3SBarry Smith 
20121cbb95d3SBarry Smith   *f = PETSC_TRUE;
20131cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
20141cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
20151cbb95d3SBarry Smith       PetscInt         idc,idr;
20161cbb95d3SBarry Smith       PetscScalar vc,vr;
20171cbb95d3SBarry Smith       /* column/row index/value */
20181cbb95d3SBarry Smith       idc = adx[aptr[i]];
20191cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
20201cbb95d3SBarry Smith       vc  = va[aptr[i]];
20211cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
20221cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
20231cbb95d3SBarry Smith         *f = PETSC_FALSE;
20241cbb95d3SBarry Smith         goto done;
20251cbb95d3SBarry Smith       } else {
20261cbb95d3SBarry Smith         aptr[i]++;
20271cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
20281cbb95d3SBarry Smith       }
20291cbb95d3SBarry Smith     }
20301cbb95d3SBarry Smith   }
20311cbb95d3SBarry Smith  done:
20321cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
20331cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
20341cbb95d3SBarry Smith   PetscFunctionReturn(0);
20351cbb95d3SBarry Smith }
20361cbb95d3SBarry Smith EXTERN_C_END
20371cbb95d3SBarry Smith 
20389e29f15eSvictorle #undef __FUNCT__
20399e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2040ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20419e29f15eSvictorle {
2042dfbe8321SBarry Smith   PetscErrorCode ierr;
20439e29f15eSvictorle   PetscFunctionBegin;
20445485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20459e29f15eSvictorle   PetscFunctionReturn(0);
20469e29f15eSvictorle }
20479e29f15eSvictorle 
20484a2ae208SSatish Balay #undef __FUNCT__
20491cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2050ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20511cbb95d3SBarry Smith {
20521cbb95d3SBarry Smith   PetscErrorCode ierr;
20531cbb95d3SBarry Smith   PetscFunctionBegin;
20541cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20551cbb95d3SBarry Smith   PetscFunctionReturn(0);
20561cbb95d3SBarry Smith }
20571cbb95d3SBarry Smith 
20581cbb95d3SBarry Smith #undef __FUNCT__
20594a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2060dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
206117ab2063SBarry Smith {
2062416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
206354f21887SBarry Smith   PetscScalar    *l,*r,x;
206454f21887SBarry Smith   MatScalar      *v;
2065dfbe8321SBarry Smith   PetscErrorCode ierr;
2066d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
206717ab2063SBarry Smith 
20683a40ed3dSBarry Smith   PetscFunctionBegin;
206917ab2063SBarry Smith   if (ll) {
20703ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
20713ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2072e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2073e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
20741ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2075416022c9SBarry Smith     v = a->a;
207617ab2063SBarry Smith     for (i=0; i<m; i++) {
207717ab2063SBarry Smith       x = l[i];
2078416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
207917ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
208017ab2063SBarry Smith     }
20811ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2082efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
208317ab2063SBarry Smith   }
208417ab2063SBarry Smith   if (rr) {
2085e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2086e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
20871ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2088416022c9SBarry Smith     v = a->a; jj = a->j;
208917ab2063SBarry Smith     for (i=0; i<nz; i++) {
2090bfeeae90SHong Zhang       (*v++) *= r[*jj++];
209117ab2063SBarry Smith     }
20921ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2093efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
209417ab2063SBarry Smith   }
209586c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
209686c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
20973a40ed3dSBarry Smith   PetscFunctionReturn(0);
209817ab2063SBarry Smith }
209917ab2063SBarry Smith 
21004a2ae208SSatish Balay #undef __FUNCT__
21014a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
210297f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
210317ab2063SBarry Smith {
2104db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
21056849ba73SBarry Smith   PetscErrorCode ierr;
2106d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
210797f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
21085d0c19d7SBarry Smith   const PetscInt *irow,*icol;
21095d0c19d7SBarry Smith   PetscInt       nrows,ncols;
211097f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
211154f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2112416022c9SBarry Smith   Mat            C;
2113ace3abfcSBarry Smith   PetscBool      stride,sorted;
211417ab2063SBarry Smith 
21153a40ed3dSBarry Smith   PetscFunctionBegin;
211614ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
2117e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
211814ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
2119e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
212099141d43SSatish Balay 
212117ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2122b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2123b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
212417ab2063SBarry Smith 
2125fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2126251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2127fee21e36SBarry Smith   if (stride && step == 1) {
212802834360SBarry Smith     /* special case of contiguous rows */
21290e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
213002834360SBarry Smith     /* loop over new rows determining lens and starting points */
213102834360SBarry Smith     for (i=0; i<nrows; i++) {
2132bfeeae90SHong Zhang       kstart  = ai[irow[i]];
2133a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
213402834360SBarry Smith       for (k=kstart; k<kend; k++) {
2135bfeeae90SHong Zhang         if (aj[k] >= first) {
213602834360SBarry Smith           starts[i] = k;
213702834360SBarry Smith           break;
213802834360SBarry Smith 	}
213902834360SBarry Smith       }
2140a2744918SBarry Smith       sum = 0;
214102834360SBarry Smith       while (k < kend) {
2142bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2143a2744918SBarry Smith         sum++;
214402834360SBarry Smith       }
2145a2744918SBarry Smith       lens[i] = sum;
214602834360SBarry Smith     }
214702834360SBarry Smith     /* create submatrix */
2148cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
214997f1f81fSBarry Smith       PetscInt n_cols,n_rows;
215008480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2151e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2152d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
215308480c60SBarry Smith       C = *B;
21543a40ed3dSBarry Smith     } else {
21553bef6203SJed Brown       PetscInt rbs,cbs;
21567adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2157f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
21583bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
21593bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
21603bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
21617adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2162ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
216308480c60SBarry Smith     }
2164db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2165db02288aSLois Curfman McInnes 
216602834360SBarry Smith     /* loop over rows inserting into submatrix */
2167db02288aSLois Curfman McInnes     a_new    = c->a;
2168db02288aSLois Curfman McInnes     j_new    = c->j;
2169db02288aSLois Curfman McInnes     i_new    = c->i;
2170bfeeae90SHong Zhang 
217102834360SBarry Smith     for (i=0; i<nrows; i++) {
2172a2744918SBarry Smith       ii    = starts[i];
2173a2744918SBarry Smith       lensi = lens[i];
2174a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2175a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
217602834360SBarry Smith       }
217787828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2178a2744918SBarry Smith       a_new      += lensi;
2179a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
2180a2744918SBarry Smith       c->ilen[i]  = lensi;
218102834360SBarry Smith     }
21820e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
21833a40ed3dSBarry Smith   } else {
218402834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
21850e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
218697f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
21870e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
21884dcab191SBarry Smith     for (i=0; i<ncols; i++) {
21894dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
21904dcab191SBarry Smith       if (icol[i] >= oldcols) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Requesting column beyond largest column icol[%D] %D <= A->cmap->n %D",i,icol[i],oldcols);
21914dcab191SBarry Smith #endif
21924dcab191SBarry Smith       smap[icol[i]] = i+1;
21934dcab191SBarry Smith     }
21944dcab191SBarry Smith 
219502834360SBarry Smith     /* determine lens of each row */
219602834360SBarry Smith     for (i=0; i<nrows; i++) {
2197bfeeae90SHong Zhang       kstart  = ai[irow[i]];
219802834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
219902834360SBarry Smith       lens[i] = 0;
220002834360SBarry Smith       for (k=kstart; k<kend; k++) {
2201bfeeae90SHong Zhang         if (smap[aj[k]]) {
220202834360SBarry Smith           lens[i]++;
220302834360SBarry Smith         }
220402834360SBarry Smith       }
220502834360SBarry Smith     }
220617ab2063SBarry Smith     /* Create and fill new matrix */
2207a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2208ace3abfcSBarry Smith       PetscBool  equal;
22090f5bd95cSBarry Smith 
221099141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
2211e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2212d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
22130f5bd95cSBarry Smith       if (!equal) {
2214e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
221599141d43SSatish Balay       }
2216d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
221708480c60SBarry Smith       C = *B;
22183a40ed3dSBarry Smith     } else {
22193bef6203SJed Brown       PetscInt rbs,cbs;
22207adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2221f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
22223bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
22233bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
22243bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
22257adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2226ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
222708480c60SBarry Smith     }
222899141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
222917ab2063SBarry Smith     for (i=0; i<nrows; i++) {
223099141d43SSatish Balay       row    = irow[i];
2231bfeeae90SHong Zhang       kstart = ai[row];
223299141d43SSatish Balay       kend   = kstart + a->ilen[row];
2233bfeeae90SHong Zhang       mat_i  = c->i[i];
223499141d43SSatish Balay       mat_j  = c->j + mat_i;
223599141d43SSatish Balay       mat_a  = c->a + mat_i;
223699141d43SSatish Balay       mat_ilen = c->ilen + i;
223717ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2238bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2239ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
224099141d43SSatish Balay           *mat_a++ = a->a[k];
224199141d43SSatish Balay           (*mat_ilen)++;
224299141d43SSatish Balay 
224317ab2063SBarry Smith         }
224417ab2063SBarry Smith       }
224517ab2063SBarry Smith     }
224602834360SBarry Smith     /* Free work space */
224702834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2248606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2249606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
225002834360SBarry Smith   }
22516d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22526d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
225317ab2063SBarry Smith 
225417ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2255416022c9SBarry Smith   *B = C;
22563a40ed3dSBarry Smith   PetscFunctionReturn(0);
225717ab2063SBarry Smith }
225817ab2063SBarry Smith 
22591df811f5SHong Zhang #undef __FUNCT__
226082d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2261fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat* subMat)
226282d44351SHong Zhang {
226382d44351SHong Zhang   PetscErrorCode ierr;
226482d44351SHong Zhang   Mat            B;
226582d44351SHong Zhang 
226682d44351SHong Zhang   PetscFunctionBegin;
226782d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
226882d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
2269a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(B,mat->rmap->bs,mat->cmap->bs); CHKERRQ(ierr);
227082d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
227182d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
227282d44351SHong Zhang   *subMat = B;
227382d44351SHong Zhang   PetscFunctionReturn(0);
227482d44351SHong Zhang }
227582d44351SHong Zhang 
227682d44351SHong Zhang #undef __FUNCT__
22774a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
22780481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2279a871dcd8SBarry Smith {
228063b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2281dfbe8321SBarry Smith   PetscErrorCode ierr;
228263b91edcSBarry Smith   Mat            outA;
2283ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
228463b91edcSBarry Smith 
22853a40ed3dSBarry Smith   PetscFunctionBegin;
2286e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
22871df811f5SHong Zhang 
2288b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2289b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2290a871dcd8SBarry Smith 
229163b91edcSBarry Smith   outA              = inA;
2292d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
2293c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
22946bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
2295c3122656SLisandro Dalcin   a->row = row;
2296c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
22976bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
2298c3122656SLisandro Dalcin   a->col = col;
229963b91edcSBarry Smith 
230036db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
23016bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
23024c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
230352e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2304f0ec6fceSSatish Balay 
230594a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2306d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2307d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
230894a9d846SBarry Smith   }
230963b91edcSBarry Smith 
2310f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2311137fb511SHong Zhang   if (row_identity && col_identity) {
2312ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2313137fb511SHong Zhang   } else {
2314719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2315137fb511SHong Zhang   }
23163a40ed3dSBarry Smith   PetscFunctionReturn(0);
2317a871dcd8SBarry Smith }
2318a871dcd8SBarry Smith 
23194a2ae208SSatish Balay #undef __FUNCT__
23204a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2321f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2322f0b747eeSBarry Smith {
2323f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2324f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2325efee365bSSatish Balay   PetscErrorCode ierr;
23260805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
23273a40ed3dSBarry Smith 
23283a40ed3dSBarry Smith   PetscFunctionBegin;
2329f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
2330efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
233186c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
233286c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
23333a40ed3dSBarry Smith   PetscFunctionReturn(0);
2334f0b747eeSBarry Smith }
2335f0b747eeSBarry Smith 
23364a2ae208SSatish Balay #undef __FUNCT__
23374a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
233897f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2339cddf8d76SBarry Smith {
2340dfbe8321SBarry Smith   PetscErrorCode ierr;
234197f1f81fSBarry Smith   PetscInt       i;
2342cddf8d76SBarry Smith 
23433a40ed3dSBarry Smith   PetscFunctionBegin;
2344cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2345b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2346cddf8d76SBarry Smith   }
2347cddf8d76SBarry Smith 
2348cddf8d76SBarry Smith   for (i=0; i<n; i++) {
23496a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2350cddf8d76SBarry Smith   }
23513a40ed3dSBarry Smith   PetscFunctionReturn(0);
2352cddf8d76SBarry Smith }
2353cddf8d76SBarry Smith 
23544a2ae208SSatish Balay #undef __FUNCT__
23554a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
235697f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
23574dcbc457SBarry Smith {
2358e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
23596849ba73SBarry Smith   PetscErrorCode ierr;
23605d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
23615d0c19d7SBarry Smith   const PetscInt *idx;
236297f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2363f1af5d2fSBarry Smith   PetscBT        table;
2364bbd702dbSSatish Balay 
23653a40ed3dSBarry Smith   PetscFunctionBegin;
2366d0f46423SBarry Smith   m     = A->rmap->n;
2367e4d965acSSatish Balay   ai    = a->i;
2368bfeeae90SHong Zhang   aj    = a->j;
23698a047759SSatish Balay 
2370e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
237106763907SSatish Balay 
237297f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
237353b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
237406763907SSatish Balay 
2375e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2376b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2377e4d965acSSatish Balay     isz  = 0;
23786831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2379e4d965acSSatish Balay 
2380e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
23814dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2382b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2383e4d965acSSatish Balay 
2384dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2385e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2386f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
23874dcbc457SBarry Smith     }
238806763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
23896bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2390e4d965acSSatish Balay 
239104a348a9SBarry Smith     k = 0;
239204a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
239304a348a9SBarry Smith       n = isz;
239406763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2395e4d965acSSatish Balay         row   = nidx[k];
2396e4d965acSSatish Balay         start = ai[row];
2397e4d965acSSatish Balay         end   = ai[row+1];
239804a348a9SBarry Smith         for (l = start; l<end ; l++){
2399efb16452SHong Zhang           val = aj[l] ;
2400f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2401e4d965acSSatish Balay         }
2402e4d965acSSatish Balay       }
2403e4d965acSSatish Balay     }
240470b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2405e4d965acSSatish Balay   }
240694bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2407606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
24083a40ed3dSBarry Smith   PetscFunctionReturn(0);
24094dcbc457SBarry Smith }
241017ab2063SBarry Smith 
24110513a670SBarry Smith /* -------------------------------------------------------------- */
24124a2ae208SSatish Balay #undef __FUNCT__
24134a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2414dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
24150513a670SBarry Smith {
24160513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
24176849ba73SBarry Smith   PetscErrorCode ierr;
24183b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
24195d0c19d7SBarry Smith   const PetscInt *row,*col;
24205d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
242156cd22aeSBarry Smith   IS             icolp,irowp;
24223b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
24233b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
24240513a670SBarry Smith 
24253a40ed3dSBarry Smith   PetscFunctionBegin;
24264c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
242756cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
24284c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
242956cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
24300513a670SBarry Smith 
24310513a670SBarry Smith   /* determine lengths of permuted rows */
243297f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
24330513a670SBarry Smith   for (i=0; i<m; i++) {
24340513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
24350513a670SBarry Smith   }
24367adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2437f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
2438a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
24397adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2440ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2441606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
24420513a670SBarry Smith 
244397f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
24440513a670SBarry Smith   for (i=0; i<m; i++) {
244532ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24460513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2447cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
244832ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24490513a670SBarry Smith   }
2450606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
24513c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
24520513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24530513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
245456cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
245556cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
24566bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
24576bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
24583a40ed3dSBarry Smith   PetscFunctionReturn(0);
24590513a670SBarry Smith }
24600513a670SBarry Smith 
24614a2ae208SSatish Balay #undef __FUNCT__
24624a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2463dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2464cb5b572fSBarry Smith {
2465dfbe8321SBarry Smith   PetscErrorCode ierr;
2466cb5b572fSBarry Smith 
2467cb5b572fSBarry Smith   PetscFunctionBegin;
246833f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
246933f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2470be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2471be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2472be6bf707SBarry Smith 
2473700c5bfcSBarry 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");
2474d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2475cb5b572fSBarry Smith   } else {
2476cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2477cb5b572fSBarry Smith   }
2478cb5b572fSBarry Smith   PetscFunctionReturn(0);
2479cb5b572fSBarry Smith }
2480cb5b572fSBarry Smith 
24814a2ae208SSatish Balay #undef __FUNCT__
24824994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
24834994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2484273d9f13SBarry Smith {
2485dfbe8321SBarry Smith   PetscErrorCode ierr;
2486273d9f13SBarry Smith 
2487273d9f13SBarry Smith   PetscFunctionBegin;
2488ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2489273d9f13SBarry Smith   PetscFunctionReturn(0);
2490273d9f13SBarry Smith }
2491273d9f13SBarry Smith 
24924a2ae208SSatish Balay #undef __FUNCT__
24934a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2494a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
24956c0721eeSBarry Smith {
24966c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
24976c0721eeSBarry Smith   PetscFunctionBegin;
24986c0721eeSBarry Smith   *array = a->a;
24996c0721eeSBarry Smith   PetscFunctionReturn(0);
25006c0721eeSBarry Smith }
25016c0721eeSBarry Smith 
25024a2ae208SSatish Balay #undef __FUNCT__
25034a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2504dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
25056c0721eeSBarry Smith {
25066c0721eeSBarry Smith   PetscFunctionBegin;
25076c0721eeSBarry Smith   PetscFunctionReturn(0);
25086c0721eeSBarry Smith }
2509273d9f13SBarry Smith 
2510ee4f033dSBarry Smith #undef __FUNCT__
2511ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2512dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2513ee4f033dSBarry Smith {
25146849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
25156849ba73SBarry Smith   PetscErrorCode ierr;
25164e269d77SPeter Brune   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow;
2517efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
251887828ca2SBarry Smith   PetscScalar    *vscale_array;
2519ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2520ee4f033dSBarry Smith   Vec            w1,w2,w3;
2521ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2522ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2523ee4f033dSBarry Smith 
2524ee4f033dSBarry Smith   PetscFunctionBegin;
2525ee4f033dSBarry Smith   if (!coloring->w1) {
2526ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
252752e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2528ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
252952e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2530ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
253152e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2532ee4f033dSBarry Smith   }
2533ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2534ee4f033dSBarry Smith 
2535ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2536acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2537ee4f033dSBarry Smith   if (flg) {
2538ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2539ee4f033dSBarry Smith   } else {
2540ace3abfcSBarry Smith     PetscBool  assembled;
25410b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
25420b9b6f31SBarry Smith     if (assembled) {
2543ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2544ee4f033dSBarry Smith     }
25450b9b6f31SBarry Smith   }
2546ee4f033dSBarry Smith 
2547ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2548ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2549ee4f033dSBarry Smith 
25504e269d77SPeter Brune   if (!coloring->fset) {
255166f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2552ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
255366f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
25544e269d77SPeter Brune   } else {
25554e269d77SPeter Brune     coloring->fset = PETSC_FALSE;
2556ee4f033dSBarry Smith   }
2557ee4f033dSBarry Smith 
2558ee4f033dSBarry Smith   /*
2559ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2560ee4f033dSBarry Smith   */
25611ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
25621ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2563ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2564ee4f033dSBarry Smith     /*
2565ee4f033dSBarry Smith        Loop over each column associated with color adding the
2566ee4f033dSBarry Smith        perturbation to the vector w3.
2567ee4f033dSBarry Smith     */
2568ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2569ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2570ee4f033dSBarry Smith       dx  = xx[col];
2571ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2572ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2573ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2574ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2575ee4f033dSBarry Smith #else
2576ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2577ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2578ee4f033dSBarry Smith #endif
2579ee4f033dSBarry Smith       dx                *= epsilon;
2580ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2581ee4f033dSBarry Smith     }
2582ee4f033dSBarry Smith   }
25831ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2584ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2585ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2586ee4f033dSBarry Smith 
2587ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2588ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2589ee4f033dSBarry Smith 
2590ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2591ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2592ee4f033dSBarry Smith 
25931ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2594ee4f033dSBarry Smith   /*
2595ee4f033dSBarry Smith       Loop over each color
2596ee4f033dSBarry Smith   */
2597ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
259849b058dcSBarry Smith     coloring->currentcolor = k;
2599ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
26001ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2601ee4f033dSBarry Smith     /*
2602ee4f033dSBarry Smith        Loop over each column associated with color adding the
2603ee4f033dSBarry Smith        perturbation to the vector w3.
2604ee4f033dSBarry Smith     */
2605ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2606ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2607ee4f033dSBarry Smith       dx  = xx[col];
26085b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2609ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2610ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2611ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2612ee4f033dSBarry Smith #else
2613ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2614ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2615ee4f033dSBarry Smith #endif
2616ee4f033dSBarry Smith       dx            *= epsilon;
2617e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2618ee4f033dSBarry Smith       w3_array[col] += dx;
2619ee4f033dSBarry Smith     }
26201ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2621ee4f033dSBarry Smith 
2622ee4f033dSBarry Smith     /*
2623ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2624ee4f033dSBarry Smith     */
2625ee4f033dSBarry Smith 
262666f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2627ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
262866f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2629efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2630ee4f033dSBarry Smith 
2631ee4f033dSBarry Smith     /*
2632ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2633ee4f033dSBarry Smith     */
26341ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2635ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2636ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2637ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2638ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2639ee4f033dSBarry Smith       srow   = row + start;
2640ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2641ee4f033dSBarry Smith     }
26421ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2643ee4f033dSBarry Smith   }
264449b058dcSBarry Smith   coloring->currentcolor = k;
26451ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
26461ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2647ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2648ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2649ee4f033dSBarry Smith   PetscFunctionReturn(0);
2650ee4f033dSBarry Smith }
2651ee4f033dSBarry Smith 
26528229c054SShri Abhyankar /*
26538229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
26548229c054SShri Abhyankar    have different nonzero structure.
26558229c054SShri Abhyankar */
2656ac90fabeSBarry Smith #undef __FUNCT__
26578229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
26588229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2659ec7775f6SShri Abhyankar {
26608229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2661ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2662ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2663ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2664ec7775f6SShri Abhyankar 
2665ec7775f6SShri Abhyankar   PetscFunctionBegin;
2666ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2667ec7775f6SShri Abhyankar   for(i=0; i<m; i++) {
26688af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
26698af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
26708af7cee1SJed Brown     nnz[i] = 0;
26718af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
26728af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
26738af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
26748af7cee1SJed Brown       nnz[i]++;
26758af7cee1SJed Brown     }
26768af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2677ec7775f6SShri Abhyankar   }
2678ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2679ec7775f6SShri Abhyankar }
2680ec7775f6SShri Abhyankar 
2681ec7775f6SShri Abhyankar #undef __FUNCT__
2682ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2683f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2684ac90fabeSBarry Smith {
2685dfbe8321SBarry Smith   PetscErrorCode ierr;
268697f1f81fSBarry Smith   PetscInt       i;
2687ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
26880805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2689ac90fabeSBarry Smith 
2690ac90fabeSBarry Smith   PetscFunctionBegin;
2691ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2692f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2693f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
269486c113feSBarry Smith     y->idiagvalid  = PETSC_FALSE;
269586c113feSBarry Smith     y->ibdiagvalid = PETSC_FALSE;
2696c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2697a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2698a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
26996bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2700a30b2313SHong Zhang     }
2701a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2702d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2703a30b2313SHong Zhang       y->XtoY = X;
2704407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2705c537a176SHong Zhang     }
2706f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2707118727c9SMark F. Adams     ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %d/%d = %G\n",x->nz,y->nz,(PetscReal)(x->nz)/(y->nz+1));CHKERRQ(ierr);
2708ac90fabeSBarry Smith   } else {
27098229c054SShri Abhyankar     Mat      B;
27108229c054SShri Abhyankar     PetscInt *nnz;
271116b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2712ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2713bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
27144aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2715a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr);
2716176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
27178229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2718ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2719ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2720ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
27218229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2722ac90fabeSBarry Smith   }
2723ac90fabeSBarry Smith   PetscFunctionReturn(0);
2724ac90fabeSBarry Smith }
2725ac90fabeSBarry Smith 
2726521d7252SBarry Smith #undef __FUNCT__
2727354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
27287087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2729354c94deSBarry Smith {
2730354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2731354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2732354c94deSBarry Smith   PetscInt    i,nz;
2733354c94deSBarry Smith   PetscScalar *a;
2734354c94deSBarry Smith 
2735354c94deSBarry Smith   PetscFunctionBegin;
2736354c94deSBarry Smith   nz = aij->nz;
2737354c94deSBarry Smith   a  = aij->a;
2738354c94deSBarry Smith   for (i=0; i<nz; i++) {
2739354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2740354c94deSBarry Smith   }
2741354c94deSBarry Smith #else
2742354c94deSBarry Smith   PetscFunctionBegin;
2743354c94deSBarry Smith #endif
2744354c94deSBarry Smith   PetscFunctionReturn(0);
2745354c94deSBarry Smith }
2746354c94deSBarry Smith 
2747e34fafa9SBarry Smith #undef __FUNCT__
2748985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2749985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2750e34fafa9SBarry Smith {
2751e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2752e34fafa9SBarry Smith   PetscErrorCode ierr;
2753d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2754e34fafa9SBarry Smith   PetscReal      atmp;
2755985db425SBarry Smith   PetscScalar    *x;
2756e34fafa9SBarry Smith   MatScalar      *aa;
2757e34fafa9SBarry Smith 
2758e34fafa9SBarry Smith   PetscFunctionBegin;
2759e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2760e34fafa9SBarry Smith   aa   = a->a;
2761e34fafa9SBarry Smith   ai   = a->i;
2762e34fafa9SBarry Smith   aj   = a->j;
2763e34fafa9SBarry Smith 
2764985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2765e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2766e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2767e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2768e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2769e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
27709189402eSHong Zhang     x[i] = 0.0;
2771e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2772985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2773985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2774985db425SBarry Smith       aa++; aj++;
2775985db425SBarry Smith     }
2776985db425SBarry Smith   }
2777985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2778985db425SBarry Smith   PetscFunctionReturn(0);
2779985db425SBarry Smith }
2780985db425SBarry Smith 
2781985db425SBarry Smith #undef __FUNCT__
2782985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2783985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2784985db425SBarry Smith {
2785985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2786985db425SBarry Smith   PetscErrorCode ierr;
2787d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2788985db425SBarry Smith   PetscScalar    *x;
2789985db425SBarry Smith   MatScalar      *aa;
2790985db425SBarry Smith 
2791985db425SBarry Smith   PetscFunctionBegin;
2792e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2793985db425SBarry Smith   aa   = a->a;
2794985db425SBarry Smith   ai   = a->i;
2795985db425SBarry Smith   aj   = a->j;
2796985db425SBarry Smith 
2797985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2798985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2799985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2800e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2801985db425SBarry Smith   for (i=0; i<m; i++) {
2802985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2803d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2804985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2805985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2806985db425SBarry Smith       x[i] = 0.0;
2807985db425SBarry Smith       if (idx) {
2808985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2809985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2810985db425SBarry Smith           if (aj[j] > j) {
2811985db425SBarry Smith             idx[i] = j;
2812985db425SBarry Smith             break;
2813985db425SBarry Smith           }
2814985db425SBarry Smith         }
2815985db425SBarry Smith       }
2816985db425SBarry Smith     }
2817985db425SBarry Smith     for (j=0; j<ncols; j++){
2818985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2819985db425SBarry Smith       aa++; aj++;
2820985db425SBarry Smith     }
2821985db425SBarry Smith   }
2822985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2823985db425SBarry Smith   PetscFunctionReturn(0);
2824985db425SBarry Smith }
2825985db425SBarry Smith 
2826985db425SBarry Smith #undef __FUNCT__
2827c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2828c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2829c87e5d42SMatthew Knepley {
2830c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2831c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2832c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2833c87e5d42SMatthew Knepley   PetscReal      atmp;
2834c87e5d42SMatthew Knepley   PetscScalar    *x;
2835c87e5d42SMatthew Knepley   MatScalar      *aa;
2836c87e5d42SMatthew Knepley 
2837c87e5d42SMatthew Knepley   PetscFunctionBegin;
2838e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2839c87e5d42SMatthew Knepley   aa   = a->a;
2840c87e5d42SMatthew Knepley   ai   = a->i;
2841c87e5d42SMatthew Knepley   aj   = a->j;
2842c87e5d42SMatthew Knepley 
2843c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2844c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2845c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
28463bb78c5cSMatthew G Knepley   if (n != A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector, %d vs. %d rows", A->rmap->n, n);
2847c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2848c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2849289a08f5SMatthew Knepley     if (ncols) {
2850289a08f5SMatthew Knepley       /* Get first nonzero */
2851289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2852289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2853289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2854289a08f5SMatthew Knepley       }
285512431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2856289a08f5SMatthew Knepley     } else {
2857289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2858289a08f5SMatthew Knepley     }
2859c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2860c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2861289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2862c87e5d42SMatthew Knepley       aa++; aj++;
2863c87e5d42SMatthew Knepley     }
2864c87e5d42SMatthew Knepley   }
2865c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2866c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2867c87e5d42SMatthew Knepley }
2868c87e5d42SMatthew Knepley 
2869c87e5d42SMatthew Knepley #undef __FUNCT__
2870985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2871985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2872985db425SBarry Smith {
2873985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2874985db425SBarry Smith   PetscErrorCode ierr;
2875d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2876985db425SBarry Smith   PetscScalar    *x;
2877985db425SBarry Smith   MatScalar      *aa;
2878985db425SBarry Smith 
2879985db425SBarry Smith   PetscFunctionBegin;
2880e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2881985db425SBarry Smith   aa   = a->a;
2882985db425SBarry Smith   ai   = a->i;
2883985db425SBarry Smith   aj   = a->j;
2884985db425SBarry Smith 
2885985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2886985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2887985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2888e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2889985db425SBarry Smith   for (i=0; i<m; i++) {
2890985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2891d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2892985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2893985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2894985db425SBarry Smith       x[i] = 0.0;
2895985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2896985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2897985db425SBarry Smith         for (j=0;j<ncols;j++) {
2898985db425SBarry Smith           if (aj[j] > j) {
2899985db425SBarry Smith             idx[i] = j;
2900985db425SBarry Smith             break;
2901985db425SBarry Smith           }
2902985db425SBarry Smith         }
2903985db425SBarry Smith       }
2904985db425SBarry Smith     }
2905985db425SBarry Smith     for (j=0; j<ncols; j++){
2906985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2907985db425SBarry Smith       aa++; aj++;
2908e34fafa9SBarry Smith     }
2909e34fafa9SBarry Smith   }
2910e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2911e34fafa9SBarry Smith   PetscFunctionReturn(0);
2912e34fafa9SBarry Smith }
2913bbead8a2SBarry Smith 
2914bbead8a2SBarry Smith #include <petscblaslapack.h>
2915bbead8a2SBarry Smith #include <../src/mat/blockinvert.h>
2916bbead8a2SBarry Smith 
2917bbead8a2SBarry Smith #undef __FUNCT__
2918bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
2919713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
2920bbead8a2SBarry Smith {
2921bbead8a2SBarry Smith   Mat_SeqAIJ    *a = (Mat_SeqAIJ*) A->data;
2922bbead8a2SBarry Smith   PetscErrorCode ierr;
292334fc4b71SJed Brown   PetscInt       i,bs = A->rmap->bs,mbs = A->rmap->n/A->rmap->bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
2924bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
2925bbead8a2SBarry Smith   PetscReal      shift = 0.0;
2926bbead8a2SBarry Smith 
2927bbead8a2SBarry Smith   PetscFunctionBegin;
29284a0d0026SBarry Smith   if (a->ibdiagvalid) {
29294a0d0026SBarry Smith     if (values) *values = a->ibdiag;
29304a0d0026SBarry Smith     PetscFunctionReturn(0);
29314a0d0026SBarry Smith   }
2932bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
2933bbead8a2SBarry Smith   if (!a->ibdiag) {
2934bbead8a2SBarry Smith     ierr = PetscMalloc(bs2*mbs*sizeof(PetscScalar),&a->ibdiag);CHKERRQ(ierr);
2935bbead8a2SBarry Smith     ierr = PetscLogObjectMemory(A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
2936bbead8a2SBarry Smith   }
2937bbead8a2SBarry Smith   diag    = a->ibdiag;
2938bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
2939bbead8a2SBarry Smith   /* factor and invert each block */
2940bbead8a2SBarry Smith   switch (bs){
2941bbead8a2SBarry Smith     case 1:
2942bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2943bbead8a2SBarry Smith         ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
2944bbead8a2SBarry Smith         diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
2945bbead8a2SBarry Smith       }
2946bbead8a2SBarry Smith       break;
2947bbead8a2SBarry Smith     case 2:
2948bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2949bbead8a2SBarry Smith         ij[0] = 2*i; ij[1] = 2*i + 1;
2950bbead8a2SBarry Smith         ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
295196b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
295296b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
2953bbead8a2SBarry Smith 	diag  += 4;
2954bbead8a2SBarry Smith       }
2955bbead8a2SBarry Smith       break;
2956bbead8a2SBarry Smith     case 3:
2957bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2958bbead8a2SBarry Smith         ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
2959bbead8a2SBarry Smith         ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
296096b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
296196b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
2962bbead8a2SBarry Smith 	diag    += 9;
2963bbead8a2SBarry Smith       }
2964bbead8a2SBarry Smith       break;
2965bbead8a2SBarry Smith     case 4:
2966bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2967bbead8a2SBarry Smith         ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
2968bbead8a2SBarry Smith         ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
296996b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
297096b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
2971bbead8a2SBarry Smith 	diag  += 16;
2972bbead8a2SBarry Smith       }
2973bbead8a2SBarry Smith       break;
2974bbead8a2SBarry Smith     case 5:
2975bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2976bbead8a2SBarry Smith         ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
2977bbead8a2SBarry Smith         ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
297896b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
297996b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
2980bbead8a2SBarry Smith 	diag  += 25;
2981bbead8a2SBarry Smith       }
2982bbead8a2SBarry Smith       break;
2983bbead8a2SBarry Smith     case 6:
2984bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2985bbead8a2SBarry Smith         ij[0] = 6*i; ij[1] = 6*i + 1; ij[2] = 6*i + 2; ij[3] = 6*i + 3; ij[4] = 6*i + 4; ij[5] = 6*i + 5;
2986bbead8a2SBarry Smith         ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
298796b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
298896b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
2989bbead8a2SBarry Smith 	diag  += 36;
2990bbead8a2SBarry Smith       }
2991bbead8a2SBarry Smith       break;
2992bbead8a2SBarry Smith     case 7:
2993bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2994bbead8a2SBarry Smith         ij[0] = 7*i; ij[1] = 7*i + 1; ij[2] = 7*i + 2; ij[3] = 7*i + 3; ij[4] = 7*i + 4; ij[5] = 7*i + 5; ij[5] = 7*i + 6;
2995bbead8a2SBarry Smith         ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
299696b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
299796b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
2998bbead8a2SBarry Smith 	diag  += 49;
2999bbead8a2SBarry Smith       }
3000bbead8a2SBarry Smith       break;
3001bbead8a2SBarry Smith     default:
3002bbead8a2SBarry Smith       ierr = PetscMalloc3(bs,MatScalar,&v_work,bs,PetscInt,&v_pivots,bs,PetscInt,&IJ);CHKERRQ(ierr);
3003bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
3004bbead8a2SBarry Smith         for (j=0; j<bs; j++) {
3005bbead8a2SBarry Smith           IJ[j] = bs*i + j;
3006bbead8a2SBarry Smith         }
3007bbead8a2SBarry Smith         ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
300896b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
300996b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3010bbead8a2SBarry Smith 	diag  += bs2;
3011bbead8a2SBarry Smith       }
3012bbead8a2SBarry Smith       ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3013bbead8a2SBarry Smith   }
3014bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3015bbead8a2SBarry Smith   PetscFunctionReturn(0);
3016bbead8a2SBarry Smith }
3017bbead8a2SBarry Smith 
30187087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
3019682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
30200a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
3021cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
3022cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
3023cb5b572fSBarry Smith        MatMult_SeqAIJ,
302497304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
30257c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
30267c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
3027db4efbfdSBarry Smith        0,
3028db4efbfdSBarry Smith        0,
3029db4efbfdSBarry Smith        0,
3030db4efbfdSBarry Smith /*10*/ 0,
3031cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
3032cb5b572fSBarry Smith        0,
303341f059aeSBarry Smith        MatSOR_SeqAIJ,
303417ab2063SBarry Smith        MatTranspose_SeqAIJ,
303597304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
3036cb5b572fSBarry Smith        MatEqual_SeqAIJ,
3037cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
3038cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
3039cb5b572fSBarry Smith        MatNorm_SeqAIJ,
304097304618SKris Buschelman /*20*/ 0,
3041cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
3042cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
3043cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
3044d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
3045db4efbfdSBarry Smith        0,
3046db4efbfdSBarry Smith        0,
3047db4efbfdSBarry Smith        0,
3048db4efbfdSBarry Smith        0,
30494994cf47SJed Brown /*29*/ MatSetUp_SeqAIJ,
3050db4efbfdSBarry Smith        0,
3051db4efbfdSBarry Smith        0,
30526c0721eeSBarry Smith        MatGetArray_SeqAIJ,
30536c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
3054d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
3055cb5b572fSBarry Smith        0,
3056cb5b572fSBarry Smith        0,
3057cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
3058cb5b572fSBarry Smith        0,
3059d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
3060cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
3061cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
3062cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
3063cb5b572fSBarry Smith        MatCopy_SeqAIJ,
3064d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
3065cb5b572fSBarry Smith        MatScale_SeqAIJ,
3066cb5b572fSBarry Smith        0,
306779299369SBarry Smith        MatDiagonalSet_SeqAIJ,
30686e169961SBarry Smith        MatZeroRowsColumns_SeqAIJ,
3069f73d5cc4SBarry Smith /*49*/ 0,
30703b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
30713b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
30723b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
3073a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
3074d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
3075b9617806SBarry Smith        0,
30760513a670SBarry Smith        0,
3077cda55fadSBarry Smith        MatPermute_SeqAIJ,
3078cda55fadSBarry Smith        0,
3079d519adbfSMatthew Knepley /*59*/ 0,
3080b9b97703SBarry Smith        MatDestroy_SeqAIJ,
3081b9b97703SBarry Smith        MatView_SeqAIJ,
3082357abbc8SBarry Smith        0,
3083ee4f033dSBarry Smith        0,
3084d519adbfSMatthew Knepley /*64*/ 0,
3085ee4f033dSBarry Smith        0,
3086ee4f033dSBarry Smith        0,
3087ee4f033dSBarry Smith        0,
3088ee4f033dSBarry Smith        0,
3089d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
3090c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
3091ee4f033dSBarry Smith        0,
3092ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
3093dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3094ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
3095dcf5cc72SBarry Smith #else
3096dcf5cc72SBarry Smith        0,
3097dcf5cc72SBarry Smith #endif
3098d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
30993acb8795SBarry Smith        MatFDColoringApply_AIJ,
310097304618SKris Buschelman        0,
310197304618SKris Buschelman        0,
310297304618SKris Buschelman        0,
31036ce1633cSBarry Smith /*79*/ MatFindZeroDiagonals_SeqAIJ,
310497304618SKris Buschelman        0,
310597304618SKris Buschelman        0,
310697304618SKris Buschelman        0,
3107bc011b1eSHong Zhang        MatLoad_SeqAIJ,
3108d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
31091cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
31106284ec50SHong Zhang        0,
31116284ec50SHong Zhang        0,
3112bc011b1eSHong Zhang        0,
3113d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
311426be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
311526be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
3116d439da42SKris Buschelman        MatPtAP_Basic,
31177ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
3118d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
31196fc122caSHong Zhang        MatMatTransposeMult_SeqAIJ_SeqAIJ,
31206fc122caSHong Zhang        MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
31216fc122caSHong Zhang        MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
31227ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
3123d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
3124609c6c4dSKris Buschelman        0,
3125609c6c4dSKris Buschelman        0,
312687d4246cSBarry Smith        MatConjugate_SeqAIJ,
312787d4246cSBarry Smith        0,
3128d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
312999cafbc1SBarry Smith        MatRealPart_SeqAIJ,
3130f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
3131f5edf698SHong Zhang        0,
31322bebee5dSHong Zhang        0,
3133cbd44569SHong Zhang /*109*/MatMatSolve_SeqAIJ,
3134985db425SBarry Smith        0,
31352af78befSBarry Smith        MatGetRowMin_SeqAIJ,
31362af78befSBarry Smith        0,
3137599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
3138d519adbfSMatthew Knepley /*114*/0,
3139599ef60dSHong Zhang        0,
31403c2a7987SHong Zhang        0,
3141fe97e370SBarry Smith        0,
3142fbdbba38SShri Abhyankar        0,
3143fbdbba38SShri Abhyankar /*119*/0,
3144fbdbba38SShri Abhyankar        0,
3145fbdbba38SShri Abhyankar        0,
314682d44351SHong Zhang        0,
3147b3a44c85SBarry Smith        MatGetMultiProcBlock_SeqAIJ,
31480716a85fSBarry Smith /*124*/MatFindNonzeroRows_SeqAIJ,
3149bbead8a2SBarry Smith        MatGetColumnNorms_SeqAIJ,
315037868618SMatthew G Knepley        MatInvertBlockDiagonal_SeqAIJ,
315137868618SMatthew G Knepley        0,
315237868618SMatthew G Knepley        0,
31535df89d91SHong Zhang /*129*/0,
315475648e8dSHong Zhang        MatTransposeMatMult_SeqAIJ_SeqAIJ,
315575648e8dSHong Zhang        MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
315675648e8dSHong Zhang        MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3157b9af6bddSHong Zhang        MatTransposeColoringCreate_SeqAIJ,
3158b9af6bddSHong Zhang /*134*/MatTransColoringApplySpToDen_SeqAIJ,
31592b8ad9a3SHong Zhang        MatTransColoringApplyDenToSp_SeqAIJ,
31602b8ad9a3SHong Zhang        MatRARt_SeqAIJ_SeqAIJ,
31612b8ad9a3SHong Zhang        MatRARtSymbolic_SeqAIJ_SeqAIJ,
31622b8ad9a3SHong Zhang        MatRARtNumeric_SeqAIJ_SeqAIJ
31639e29f15eSvictorle };
316417ab2063SBarry Smith 
3165fb2e594dSBarry Smith EXTERN_C_BEGIN
31664a2ae208SSatish Balay #undef __FUNCT__
31674a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
31687087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3169bef8e0ddSBarry Smith {
3170bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
317197f1f81fSBarry Smith   PetscInt   i,nz,n;
3172bef8e0ddSBarry Smith 
3173bef8e0ddSBarry Smith   PetscFunctionBegin;
3174bef8e0ddSBarry Smith 
3175bef8e0ddSBarry Smith   nz = aij->maxnz;
3176d0f46423SBarry Smith   n  = mat->rmap->n;
3177bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3178bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3179bef8e0ddSBarry Smith   }
3180bef8e0ddSBarry Smith   aij->nz = nz;
3181bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3182bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3183bef8e0ddSBarry Smith   }
3184bef8e0ddSBarry Smith 
3185bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3186bef8e0ddSBarry Smith }
3187fb2e594dSBarry Smith EXTERN_C_END
3188bef8e0ddSBarry Smith 
31894a2ae208SSatish Balay #undef __FUNCT__
31904a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3191bef8e0ddSBarry Smith /*@
3192bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3193bef8e0ddSBarry Smith        in the matrix.
3194bef8e0ddSBarry Smith 
3195bef8e0ddSBarry Smith   Input Parameters:
3196bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3197bef8e0ddSBarry Smith -  indices - the column indices
3198bef8e0ddSBarry Smith 
319915091d37SBarry Smith   Level: advanced
320015091d37SBarry Smith 
3201bef8e0ddSBarry Smith   Notes:
3202bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3203bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3204bef8e0ddSBarry Smith   of the MatSetValues() operation.
3205bef8e0ddSBarry Smith 
3206bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3207d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3208bef8e0ddSBarry Smith 
3209bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3210bef8e0ddSBarry Smith 
3211b9617806SBarry Smith     The indices should start with zero, not one.
3212b9617806SBarry Smith 
3213bef8e0ddSBarry Smith @*/
32147087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3215bef8e0ddSBarry Smith {
32164ac538c5SBarry Smith   PetscErrorCode ierr;
3217bef8e0ddSBarry Smith 
3218bef8e0ddSBarry Smith   PetscFunctionBegin;
32190700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
32204482741eSBarry Smith   PetscValidPointer(indices,2);
32214ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
3222bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3223bef8e0ddSBarry Smith }
3224bef8e0ddSBarry Smith 
3225be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3226be6bf707SBarry Smith 
3227fb2e594dSBarry Smith EXTERN_C_BEGIN
32284a2ae208SSatish Balay #undef __FUNCT__
32294a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
32307087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3231be6bf707SBarry Smith {
3232be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
32336849ba73SBarry Smith   PetscErrorCode ierr;
3234d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3235be6bf707SBarry Smith 
3236be6bf707SBarry Smith   PetscFunctionBegin;
3237be6bf707SBarry Smith   if (aij->nonew != 1) {
3238e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3239be6bf707SBarry Smith   }
3240be6bf707SBarry Smith 
3241be6bf707SBarry Smith   /* allocate space for values if not already there */
3242be6bf707SBarry Smith   if (!aij->saved_values) {
324387828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
32449518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3245be6bf707SBarry Smith   }
3246be6bf707SBarry Smith 
3247be6bf707SBarry Smith   /* copy values over */
324887828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3249be6bf707SBarry Smith   PetscFunctionReturn(0);
3250be6bf707SBarry Smith }
3251fb2e594dSBarry Smith EXTERN_C_END
3252be6bf707SBarry Smith 
32534a2ae208SSatish Balay #undef __FUNCT__
3254b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3255be6bf707SBarry Smith /*@
3256be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3257be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3258be6bf707SBarry Smith        nonlinear portion.
3259be6bf707SBarry Smith 
3260be6bf707SBarry Smith    Collect on Mat
3261be6bf707SBarry Smith 
3262be6bf707SBarry Smith   Input Parameters:
32630e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3264be6bf707SBarry Smith 
326515091d37SBarry Smith   Level: advanced
326615091d37SBarry Smith 
3267be6bf707SBarry Smith   Common Usage, with SNESSolve():
3268be6bf707SBarry Smith $    Create Jacobian matrix
3269be6bf707SBarry Smith $    Set linear terms into matrix
3270be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3271be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3272be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3273512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3274be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3275be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3276be6bf707SBarry Smith $    In your Jacobian routine
3277be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3278be6bf707SBarry Smith $      Set nonlinear terms in matrix
3279be6bf707SBarry Smith 
3280be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3281be6bf707SBarry Smith $    // build linear portion of Jacobian
3282512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3283be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3284be6bf707SBarry Smith $    loop over nonlinear iterations
3285be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3286be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3287be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3288be6bf707SBarry Smith $       Solve linear system with Jacobian
3289be6bf707SBarry Smith $    endloop
3290be6bf707SBarry Smith 
3291be6bf707SBarry Smith   Notes:
3292be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3293512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3294be6bf707SBarry Smith     calling this routine.
3295be6bf707SBarry Smith 
32960c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
32970c468ba9SBarry Smith     and does not allocated additional space.
32980c468ba9SBarry Smith 
3299be6bf707SBarry Smith .seealso: MatRetrieveValues()
3300be6bf707SBarry Smith 
3301be6bf707SBarry Smith @*/
33027087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3303be6bf707SBarry Smith {
33044ac538c5SBarry Smith   PetscErrorCode ierr;
3305be6bf707SBarry Smith 
3306be6bf707SBarry Smith   PetscFunctionBegin;
33070700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3308e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3309e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
33104ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3311be6bf707SBarry Smith   PetscFunctionReturn(0);
3312be6bf707SBarry Smith }
3313be6bf707SBarry Smith 
3314fb2e594dSBarry Smith EXTERN_C_BEGIN
33154a2ae208SSatish Balay #undef __FUNCT__
33164a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
33177087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3318be6bf707SBarry Smith {
3319be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
33206849ba73SBarry Smith   PetscErrorCode ierr;
3321d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3322be6bf707SBarry Smith 
3323be6bf707SBarry Smith   PetscFunctionBegin;
3324be6bf707SBarry Smith   if (aij->nonew != 1) {
3325e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3326be6bf707SBarry Smith   }
3327be6bf707SBarry Smith   if (!aij->saved_values) {
3328e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3329be6bf707SBarry Smith   }
3330be6bf707SBarry Smith   /* copy values over */
333187828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3332be6bf707SBarry Smith   PetscFunctionReturn(0);
3333be6bf707SBarry Smith }
3334fb2e594dSBarry Smith EXTERN_C_END
3335be6bf707SBarry Smith 
33364a2ae208SSatish Balay #undef __FUNCT__
33374a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3338be6bf707SBarry Smith /*@
3339be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3340be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3341be6bf707SBarry Smith        nonlinear portion.
3342be6bf707SBarry Smith 
3343be6bf707SBarry Smith    Collect on Mat
3344be6bf707SBarry Smith 
3345be6bf707SBarry Smith   Input Parameters:
3346be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3347be6bf707SBarry Smith 
334815091d37SBarry Smith   Level: advanced
334915091d37SBarry Smith 
3350be6bf707SBarry Smith .seealso: MatStoreValues()
3351be6bf707SBarry Smith 
3352be6bf707SBarry Smith @*/
33537087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3354be6bf707SBarry Smith {
33554ac538c5SBarry Smith   PetscErrorCode ierr;
3356be6bf707SBarry Smith 
3357be6bf707SBarry Smith   PetscFunctionBegin;
33580700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3359e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3360e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
33614ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3362be6bf707SBarry Smith   PetscFunctionReturn(0);
3363be6bf707SBarry Smith }
3364be6bf707SBarry Smith 
3365f83d6046SBarry Smith 
3366be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
33674a2ae208SSatish Balay #undef __FUNCT__
33684a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
336917ab2063SBarry Smith /*@C
3370682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
33710d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
33726e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
337351c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
33742bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
337517ab2063SBarry Smith 
3376db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3377db81eaa0SLois Curfman McInnes 
337817ab2063SBarry Smith    Input Parameters:
3379db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
338017ab2063SBarry Smith .  m - number of rows
338117ab2063SBarry Smith .  n - number of columns
338217ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
338351c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
33842bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
338517ab2063SBarry Smith 
338617ab2063SBarry Smith    Output Parameter:
3387416022c9SBarry Smith .  A - the matrix
338817ab2063SBarry Smith 
3389175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3390ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3391175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3392175b88e8SBarry Smith 
3393b259b22eSLois Curfman McInnes    Notes:
339449a6f317SBarry Smith    If nnz is given then nz is ignored
339549a6f317SBarry Smith 
339617ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
339717ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
33980002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
339944cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
340017ab2063SBarry Smith 
340117ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3402a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
34033d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
34046da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
340517ab2063SBarry Smith 
3406682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
34074fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3408682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
34096c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
34106c7ebb05SLois Curfman McInnes 
34116c7ebb05SLois Curfman McInnes    Options Database Keys:
3412698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
34139db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
341417ab2063SBarry Smith 
3415027ccd11SLois Curfman McInnes    Level: intermediate
3416027ccd11SLois Curfman McInnes 
341769b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
341836db0b34SBarry Smith 
341917ab2063SBarry Smith @*/
34207087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
342117ab2063SBarry Smith {
3422dfbe8321SBarry Smith   PetscErrorCode ierr;
34236945ee14SBarry Smith 
34243a40ed3dSBarry Smith   PetscFunctionBegin;
3425f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3426117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3427c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3428d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3429273d9f13SBarry Smith   PetscFunctionReturn(0);
3430273d9f13SBarry Smith }
3431273d9f13SBarry Smith 
34324a2ae208SSatish Balay #undef __FUNCT__
34334a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3434273d9f13SBarry Smith /*@C
3435273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3436273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3437273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3438273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3439273d9f13SBarry Smith 
3440273d9f13SBarry Smith    Collective on MPI_Comm
3441273d9f13SBarry Smith 
3442273d9f13SBarry Smith    Input Parameters:
3443117016b1SBarry Smith +  B - The matrix-free
3444273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3445273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
3446273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
3447273d9f13SBarry Smith 
3448273d9f13SBarry Smith    Notes:
344949a6f317SBarry Smith      If nnz is given then nz is ignored
345049a6f317SBarry Smith 
3451273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3452273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3453273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3454273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3455273d9f13SBarry Smith 
3456273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3457273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3458273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3459273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3460273d9f13SBarry Smith 
3461aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3462aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3463aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3464aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3465aa95bbe8SBarry Smith 
3466a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3467a96a251dSBarry Smith    entries or columns indices
3468a96a251dSBarry Smith 
3469273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3470273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3471273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3472273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3473273d9f13SBarry Smith 
3474273d9f13SBarry Smith    Options Database Keys:
3475698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3476698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3477273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3478273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3479273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3480273d9f13SBarry Smith 
3481273d9f13SBarry Smith    Level: intermediate
3482273d9f13SBarry Smith 
348369b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3484273d9f13SBarry Smith 
3485273d9f13SBarry Smith @*/
34867087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3487273d9f13SBarry Smith {
34884ac538c5SBarry Smith   PetscErrorCode ierr;
3489a23d5eceSKris Buschelman 
3490a23d5eceSKris Buschelman   PetscFunctionBegin;
34916ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
34926ba663aaSJed Brown   PetscValidType(B,1);
34934ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3494a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3495a23d5eceSKris Buschelman }
3496a23d5eceSKris Buschelman 
3497a23d5eceSKris Buschelman EXTERN_C_BEGIN
3498a23d5eceSKris Buschelman #undef __FUNCT__
3499a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
35007087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3501a23d5eceSKris Buschelman {
3502273d9f13SBarry Smith   Mat_SeqAIJ     *b;
35032576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
35046849ba73SBarry Smith   PetscErrorCode ierr;
350597f1f81fSBarry Smith   PetscInt       i;
3506273d9f13SBarry Smith 
3507273d9f13SBarry Smith   PetscFunctionBegin;
35082576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3509a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3510c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3511c461c341SBarry Smith     nz             = 0;
3512c461c341SBarry Smith   }
3513c461c341SBarry Smith 
351426283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
351526283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3516899cda47SBarry Smith 
3517435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3518e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3519b73539f3SBarry Smith   if (nnz) {
3520d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3521e32f2f54SBarry 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]);
3522e32f2f54SBarry 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);
3523b73539f3SBarry Smith     }
3524b73539f3SBarry Smith   }
3525b73539f3SBarry Smith 
3526273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3527273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3528273d9f13SBarry Smith 
3529ab93d7beSBarry Smith   if (!skipallocation) {
35302ee49352SLisandro Dalcin     if (!b->imax) {
3531d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3532d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
35332ee49352SLisandro Dalcin     }
3534273d9f13SBarry Smith     if (!nnz) {
3535435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3536c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3537d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3538d0f46423SBarry Smith       nz = nz*B->rmap->n;
3539273d9f13SBarry Smith     } else {
3540273d9f13SBarry Smith       nz = 0;
3541d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3542273d9f13SBarry Smith     }
3543ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3544d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3545ab93d7beSBarry Smith 
3546273d9f13SBarry Smith     /* allocate the matrix space */
35472ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3548d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3549d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3550bfeeae90SHong Zhang     b->i[0] = 0;
3551d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
35525da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
35535da197adSKris Buschelman     }
3554273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3555e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3556e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3557c461c341SBarry Smith   } else {
3558e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3559e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3560c461c341SBarry Smith   }
3561273d9f13SBarry Smith 
3562273d9f13SBarry Smith   b->nz                = 0;
3563273d9f13SBarry Smith   b->maxnz             = nz;
3564273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
35652576faa2SJed Brown   if (realalloc) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);}
3566*78b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
3567*78b84d54SShri Abhyankar   ierr = MatZeroEntries_SeqAIJ(B);CHKERRQ(ierr);
3568*78b84d54SShri Abhyankar #endif
3569273d9f13SBarry Smith   PetscFunctionReturn(0);
3570273d9f13SBarry Smith }
3571a23d5eceSKris Buschelman EXTERN_C_END
3572273d9f13SBarry Smith 
3573a1661176SMatthew Knepley #undef  __FUNCT__
3574a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
357558d36128SBarry Smith /*@
3576a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3577a1661176SMatthew Knepley 
3578a1661176SMatthew Knepley    Input Parameters:
3579a1661176SMatthew Knepley +  B - the matrix
3580a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3581a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3582a1661176SMatthew Knepley -  v - optional values in the matrix
3583a1661176SMatthew Knepley 
3584a1661176SMatthew Knepley    Level: developer
3585a1661176SMatthew Knepley 
358658d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
358758d36128SBarry Smith 
3588a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3589a1661176SMatthew Knepley 
3590a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3591a1661176SMatthew Knepley @*/
3592a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3593a1661176SMatthew Knepley {
3594a1661176SMatthew Knepley   PetscErrorCode ierr;
3595a1661176SMatthew Knepley 
3596a1661176SMatthew Knepley   PetscFunctionBegin;
35970700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
35986ba663aaSJed Brown   PetscValidType(B,1);
35994ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3600a1661176SMatthew Knepley   PetscFunctionReturn(0);
3601a1661176SMatthew Knepley }
3602a1661176SMatthew Knepley 
3603a1661176SMatthew Knepley EXTERN_C_BEGIN
3604a1661176SMatthew Knepley #undef  __FUNCT__
3605a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
36067087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3607a1661176SMatthew Knepley {
3608a1661176SMatthew Knepley   PetscInt       i;
3609a1661176SMatthew Knepley   PetscInt       m,n;
3610a1661176SMatthew Knepley   PetscInt       nz;
3611a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3612a1661176SMatthew Knepley   PetscScalar    *values;
3613a1661176SMatthew Knepley   PetscErrorCode ierr;
3614a1661176SMatthew Knepley 
3615a1661176SMatthew Knepley   PetscFunctionBegin;
361665e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3617779a8d59SSatish Balay 
3618779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3619779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3620779a8d59SSatish Balay 
3621779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3622a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3623a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3624b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3625a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
362665e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3627a1661176SMatthew Knepley     nnz[i] = nz;
3628a1661176SMatthew Knepley   }
3629a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3630a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3631a1661176SMatthew Knepley 
3632a1661176SMatthew Knepley   if (v) {
3633a1661176SMatthew Knepley     values = (PetscScalar*) v;
3634a1661176SMatthew Knepley   } else {
36350e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3636a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3637a1661176SMatthew Knepley   }
3638a1661176SMatthew Knepley 
3639a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3640b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3641b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3642a1661176SMatthew Knepley   }
3643a1661176SMatthew Knepley 
3644a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3645a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3646a1661176SMatthew Knepley 
3647a1661176SMatthew Knepley   if (!v) {
3648a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3649a1661176SMatthew Knepley   }
36507827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3651a1661176SMatthew Knepley   PetscFunctionReturn(0);
3652a1661176SMatthew Knepley }
3653a1661176SMatthew Knepley EXTERN_C_END
3654a1661176SMatthew Knepley 
3655c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3656b45d2f2cSJed Brown #include <petsc-private/petscaxpy.h>
3657170fe5c8SBarry Smith 
3658170fe5c8SBarry Smith #undef __FUNCT__
3659170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3660170fe5c8SBarry Smith /*
3661170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3662170fe5c8SBarry Smith 
3663170fe5c8SBarry Smith                n                       p                          p
3664170fe5c8SBarry Smith         (              )       (              )         (                  )
3665170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3666170fe5c8SBarry Smith         (              )       (              )         (                  )
3667170fe5c8SBarry Smith 
3668170fe5c8SBarry Smith */
3669170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3670170fe5c8SBarry Smith {
3671170fe5c8SBarry Smith   PetscErrorCode     ierr;
3672170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3673170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3674170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
36751de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3676170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3677170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3678170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3679170fe5c8SBarry Smith 
3680170fe5c8SBarry Smith   PetscFunctionBegin;
3681d0f46423SBarry Smith   m = A->rmap->n;
3682d0f46423SBarry Smith   n = A->cmap->n;
3683d0f46423SBarry Smith   p = B->cmap->n;
3684170fe5c8SBarry Smith   a = sub_a->v;
3685170fe5c8SBarry Smith   b = sub_b->a;
3686170fe5c8SBarry Smith   c = sub_c->v;
3687170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3688170fe5c8SBarry Smith 
3689170fe5c8SBarry Smith   ii  = sub_b->i;
3690170fe5c8SBarry Smith   idx = sub_b->j;
3691170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3692170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3693170fe5c8SBarry Smith     while (q-->0) {
3694170fe5c8SBarry Smith       c_q = c + m*(*idx);
3695170fe5c8SBarry Smith       a_q = a + m*i;
3696be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3697170fe5c8SBarry Smith       idx++;
3698170fe5c8SBarry Smith       b++;
3699170fe5c8SBarry Smith     }
3700170fe5c8SBarry Smith   }
3701170fe5c8SBarry Smith   PetscFunctionReturn(0);
3702170fe5c8SBarry Smith }
3703170fe5c8SBarry Smith 
3704170fe5c8SBarry Smith #undef __FUNCT__
3705170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3706170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3707170fe5c8SBarry Smith {
3708170fe5c8SBarry Smith   PetscErrorCode ierr;
3709d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3710170fe5c8SBarry Smith   Mat            Cmat;
3711170fe5c8SBarry Smith 
3712170fe5c8SBarry Smith   PetscFunctionBegin;
3713e32f2f54SBarry 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);
371439804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3715170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3716a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
3717170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3718170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3719170fe5c8SBarry Smith   Cmat->assembled    = PETSC_TRUE;
37208cdbd757SHong Zhang   Cmat->ops->matmult = MatMatMult_SeqDense_SeqAIJ;
3721170fe5c8SBarry Smith   *C = Cmat;
3722170fe5c8SBarry Smith   PetscFunctionReturn(0);
3723170fe5c8SBarry Smith }
3724170fe5c8SBarry Smith 
3725170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3726170fe5c8SBarry Smith #undef __FUNCT__
3727170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3728170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3729170fe5c8SBarry Smith {
3730170fe5c8SBarry Smith   PetscErrorCode ierr;
3731170fe5c8SBarry Smith 
3732170fe5c8SBarry Smith   PetscFunctionBegin;
3733170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3734170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3735170fe5c8SBarry Smith   }
3736170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3737170fe5c8SBarry Smith   PetscFunctionReturn(0);
3738170fe5c8SBarry Smith }
3739170fe5c8SBarry Smith 
3740170fe5c8SBarry Smith 
37410bad9183SKris Buschelman /*MC
3742fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
37430bad9183SKris Buschelman    based on compressed sparse row format.
37440bad9183SKris Buschelman 
37450bad9183SKris Buschelman    Options Database Keys:
37460bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
37470bad9183SKris Buschelman 
37480bad9183SKris Buschelman   Level: beginner
37490bad9183SKris Buschelman 
3750f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
37510bad9183SKris Buschelman M*/
37520bad9183SKris Buschelman 
3753a6175056SHong Zhang EXTERN_C_BEGIN
3754b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3755b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3756b5e56a35SBarry Smith #endif
3757ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3758af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3759af1023dbSSatish Balay #endif
37607087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
37617087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
37627087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
37637087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3764611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
37657087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3766611f576cSBarry Smith #endif
3767611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
37687087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3769611f576cSBarry Smith #endif
3770f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3771f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3772f3c0ef26SHong Zhang #endif
3773611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
37747087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3775611f576cSBarry Smith #endif
3776eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
37777087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3778eb3b5408SSatish Balay #endif
3779586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
37807087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3781586621ddSJed Brown #endif
3782719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
37837087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3784719d5645SBarry Smith #endif
3785b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
37867087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
37877087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
37887087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3789b3866ffcSBarry Smith #endif
379017667f90SBarry Smith EXTERN_C_END
379117667f90SBarry Smith 
3792c0c8ee5eSDmitry Karpeev 
379317667f90SBarry Smith EXTERN_C_BEGIN
37944a2ae208SSatish Balay #undef __FUNCT__
37954a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
37967087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqAIJ(Mat B)
3797273d9f13SBarry Smith {
3798273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3799dfbe8321SBarry Smith   PetscErrorCode ierr;
380038baddfdSBarry Smith   PetscMPIInt    size;
3801273d9f13SBarry Smith 
3802273d9f13SBarry Smith   PetscFunctionBegin;
38037adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3804e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3805273d9f13SBarry Smith 
380638f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3807b0a32e0cSBarry Smith   B->data             = (void*)b;
3808549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3809416022c9SBarry Smith   b->row              = 0;
3810416022c9SBarry Smith   b->col              = 0;
381182bf6240SBarry Smith   b->icol             = 0;
3812b810aeb4SBarry Smith   b->reallocs         = 0;
381336db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3814f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3815416022c9SBarry Smith   b->nonew             = 0;
3816416022c9SBarry Smith   b->diag              = 0;
3817416022c9SBarry Smith   b->solve_work        = 0;
38182a1b7f2aSHong Zhang   B->spptr             = 0;
3819be6bf707SBarry Smith   b->saved_values      = 0;
3820d7f994e1SBarry Smith   b->idiag             = 0;
382171f1c65dSBarry Smith   b->mdiag             = 0;
382271f1c65dSBarry Smith   b->ssor_work         = 0;
382371f1c65dSBarry Smith   b->omega             = 1.0;
382471f1c65dSBarry Smith   b->fshift            = 0.0;
382571f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3826bbead8a2SBarry Smith   b->ibdiagvalid       = PETSC_FALSE;
3827a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3828a30b2313SHong Zhang   b->xtoy              = 0;
3829a30b2313SHong Zhang   b->XtoY              = 0;
383088e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
383117ab2063SBarry Smith 
383235d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3833b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3834700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3835b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3836b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3837b3866ffcSBarry Smith #endif
3838b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3839700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3840b5e56a35SBarry Smith #endif
3841ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3842700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3843719d5645SBarry Smith #endif
3844611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3845700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3846611f576cSBarry Smith #endif
3847f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3848700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3849f3c0ef26SHong Zhang #endif
3850611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3851700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C","MatGetFactor_seqaij_spooles",MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3852611f576cSBarry Smith #endif
3853611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3854700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr);
3855611f576cSBarry Smith #endif
3856eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3857700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3858eb3b5408SSatish Balay #endif
3859586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3860700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3861586621ddSJed Brown #endif
3862719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3863700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_seqaij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3864719d5645SBarry Smith #endif
3865700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3866700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3867700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
3868700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3869700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3870700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3871700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3872700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3873700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3874700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3875700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3876700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3877700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3878700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
3879700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3880700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3881700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3882700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
38834108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
388417667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
38853a40ed3dSBarry Smith   PetscFunctionReturn(0);
388617ab2063SBarry Smith }
3887273d9f13SBarry Smith EXTERN_C_END
388817ab2063SBarry Smith 
38894a2ae208SSatish Balay #undef __FUNCT__
3890b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3891b24902e0SBarry Smith /*
3892b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3893b24902e0SBarry Smith */
3894ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
389517ab2063SBarry Smith {
3896416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
38976849ba73SBarry Smith   PetscErrorCode ierr;
3898d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
389917ab2063SBarry Smith 
39003a40ed3dSBarry Smith   PetscFunctionBegin;
3901273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3902273d9f13SBarry Smith 
3903d5f3da31SBarry Smith   C->factortype     = A->factortype;
3904416022c9SBarry Smith   c->row            = 0;
3905416022c9SBarry Smith   c->col            = 0;
390682bf6240SBarry Smith   c->icol           = 0;
39076ad4291fSHong Zhang   c->reallocs       = 0;
390817ab2063SBarry Smith 
39096ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
391017ab2063SBarry Smith 
3911aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
3912aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
3913eec197d1SBarry Smith 
391433b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
39159518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
391617ab2063SBarry Smith   for (i=0; i<m; i++) {
3917416022c9SBarry Smith     c->imax[i] = a->imax[i];
3918416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
391917ab2063SBarry Smith   }
392017ab2063SBarry Smith 
392117ab2063SBarry Smith   /* allocate the matrix space */
3922f77e22a1SHong Zhang   if (mallocmatspace){
3923a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
39249518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3925f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
392697f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
392717ab2063SBarry Smith     if (m > 0) {
392897f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3929be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3930bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3931be6bf707SBarry Smith       } else {
3932bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
393317ab2063SBarry Smith       }
393408480c60SBarry Smith     }
3935f77e22a1SHong Zhang   }
393617ab2063SBarry Smith 
39376ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3938416022c9SBarry Smith   c->roworiented       = a->roworiented;
3939416022c9SBarry Smith   c->nonew             = a->nonew;
3940416022c9SBarry Smith   if (a->diag) {
394197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
394252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
394317ab2063SBarry Smith     for (i=0; i<m; i++) {
3944416022c9SBarry Smith       c->diag[i] = a->diag[i];
394517ab2063SBarry Smith     }
39463a40ed3dSBarry Smith   } else c->diag           = 0;
39476ad4291fSHong Zhang   c->solve_work            = 0;
39486ad4291fSHong Zhang   c->saved_values          = 0;
39496ad4291fSHong Zhang   c->idiag                 = 0;
395071f1c65dSBarry Smith   c->ssor_work             = 0;
3951a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3952e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3953e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
39546ad4291fSHong Zhang   c->xtoy                  = 0;
39556ad4291fSHong Zhang   c->XtoY                  = 0;
39566ad4291fSHong Zhang 
3957893ad86cSHong Zhang   c->rmax               = a->rmax;
3958416022c9SBarry Smith   c->nz                 = a->nz;
39598ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3960273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3961754ec7b1SSatish Balay 
39626ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
39636ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
3964cd6b891eSBarry Smith   c->compressedrow.check   = a->compressedrow.check;
3965cd6b891eSBarry Smith   if (a->compressedrow.use){
39666ad4291fSHong Zhang     i = a->compressedrow.nrows;
39670e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
39686ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
39696ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
397027ea64f8SHong Zhang   } else {
397127ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
397227ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
397327ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
39746ad4291fSHong Zhang   }
397588e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
39764108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
39774846f1f5SKris Buschelman 
39787adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
39793a40ed3dSBarry Smith   PetscFunctionReturn(0);
398017ab2063SBarry Smith }
398117ab2063SBarry Smith 
39824a2ae208SSatish Balay #undef __FUNCT__
3983b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3984b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3985b24902e0SBarry Smith {
3986b24902e0SBarry Smith   PetscErrorCode ierr;
3987b24902e0SBarry Smith 
3988b24902e0SBarry Smith   PetscFunctionBegin;
3989b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
39904b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3991a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
3992a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
3993f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3994b24902e0SBarry Smith   PetscFunctionReturn(0);
3995b24902e0SBarry Smith }
3996b24902e0SBarry Smith 
3997b24902e0SBarry Smith #undef __FUNCT__
39984a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3999112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4000fbdbba38SShri Abhyankar {
4001fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4002fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4003fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4004fbdbba38SShri Abhyankar   int            fd;
4005fbdbba38SShri Abhyankar   PetscMPIInt    size;
4006fbdbba38SShri Abhyankar   MPI_Comm       comm;
4007bbead8a2SBarry Smith   PetscInt       bs = 1;
4008fbdbba38SShri Abhyankar 
4009fbdbba38SShri Abhyankar   PetscFunctionBegin;
4010fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4011fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4012fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4013bbead8a2SBarry Smith 
4014bbead8a2SBarry Smith   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
4015bbead8a2SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
4016bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4017bbead8a2SBarry Smith 
4018fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4019fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4020fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4021fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4022fbdbba38SShri Abhyankar 
4023bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4024fbdbba38SShri Abhyankar 
4025fbdbba38SShri Abhyankar   /* read in row lengths */
4026fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
4027fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4028fbdbba38SShri Abhyankar 
4029fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4030fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
4031fbdbba38SShri 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);
4032fbdbba38SShri Abhyankar 
4033fbdbba38SShri Abhyankar   /* set global size if not set already*/
4034f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4035fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4036aabbc4fbSShri Abhyankar   } else {
4037fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
4038fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
40394c5b953cSHong Zhang     if (rows < 0 && cols < 0){ /* user might provide local size instead of global size */
40404c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
40414c5b953cSHong Zhang     }
4042f501eaabSShri 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);
4043aabbc4fbSShri Abhyankar   }
4044fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4045fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
4046fbdbba38SShri Abhyankar 
4047fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4048fbdbba38SShri Abhyankar 
4049fbdbba38SShri Abhyankar   /* read in nonzero values */
4050fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4051fbdbba38SShri Abhyankar 
4052fbdbba38SShri Abhyankar   /* set matrix "i" values */
4053fbdbba38SShri Abhyankar   a->i[0] = 0;
4054fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4055fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4056fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4057fbdbba38SShri Abhyankar   }
4058fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4059fbdbba38SShri Abhyankar 
4060fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4061fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4062bbead8a2SBarry Smith   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
4063fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4064fbdbba38SShri Abhyankar }
4065fbdbba38SShri Abhyankar 
4066fbdbba38SShri Abhyankar #undef __FUNCT__
4067b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4068ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
40697264ac53SSatish Balay {
40707264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
4071dfbe8321SBarry Smith   PetscErrorCode ierr;
4072eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4073eeffb40dSHong Zhang   PetscInt k;
4074eeffb40dSHong Zhang #endif
40757264ac53SSatish Balay 
40763a40ed3dSBarry Smith   PetscFunctionBegin;
4077bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4078d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4079ca44d042SBarry Smith     *flg = PETSC_FALSE;
4080ca44d042SBarry Smith     PetscFunctionReturn(0);
4081bcd2baecSBarry Smith   }
40827264ac53SSatish Balay 
40837264ac53SSatish Balay   /* if the a->i are the same */
4084d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4085abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
40867264ac53SSatish Balay 
40877264ac53SSatish Balay   /* if a->j are the same */
408897f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4089abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4090bcd2baecSBarry Smith 
4091bcd2baecSBarry Smith   /* if a->a are the same */
4092eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4093eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
4094eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
4095eeffb40dSHong Zhang       *flg = PETSC_FALSE;
40963a40ed3dSBarry Smith       PetscFunctionReturn(0);
4097eeffb40dSHong Zhang     }
4098eeffb40dSHong Zhang   }
4099eeffb40dSHong Zhang #else
4100eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4101eeffb40dSHong Zhang #endif
4102eeffb40dSHong Zhang   PetscFunctionReturn(0);
41037264ac53SSatish Balay }
410436db0b34SBarry Smith 
41054a2ae208SSatish Balay #undef __FUNCT__
41064a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
410705869f15SSatish Balay /*@
410836db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
410936db0b34SBarry Smith               provided by the user.
411036db0b34SBarry Smith 
4111c75a6043SHong Zhang       Collective on MPI_Comm
411236db0b34SBarry Smith 
411336db0b34SBarry Smith    Input Parameters:
411436db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
411536db0b34SBarry Smith .   m - number of rows
411636db0b34SBarry Smith .   n - number of columns
411736db0b34SBarry Smith .   i - row indices
411836db0b34SBarry Smith .   j - column indices
411936db0b34SBarry Smith -   a - matrix values
412036db0b34SBarry Smith 
412136db0b34SBarry Smith    Output Parameter:
412236db0b34SBarry Smith .   mat - the matrix
412336db0b34SBarry Smith 
412436db0b34SBarry Smith    Level: intermediate
412536db0b34SBarry Smith 
412636db0b34SBarry Smith    Notes:
41270551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4128292fb18eSBarry Smith     once the matrix is destroyed and not before
412936db0b34SBarry Smith 
413036db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
413136db0b34SBarry Smith 
4132bfeeae90SHong Zhang        The i and j indices are 0 based
413336db0b34SBarry Smith 
4134a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4135a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
4136a4552177SSatish Balay     as shown:
4137a4552177SSatish Balay 
4138a4552177SSatish Balay         1 0 0
4139a4552177SSatish Balay         2 0 3
4140a4552177SSatish Balay         4 5 6
4141a4552177SSatish Balay 
4142a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
41439985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4144a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
4145a4552177SSatish Balay 
41469985e31cSBarry Smith 
414769b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
414836db0b34SBarry Smith 
414936db0b34SBarry Smith @*/
41507087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
415136db0b34SBarry Smith {
4152dfbe8321SBarry Smith   PetscErrorCode ierr;
4153cbcfb4deSHong Zhang   PetscInt       ii;
415436db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4155cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4156cbcfb4deSHong Zhang   PetscInt       jj;
4157cbcfb4deSHong Zhang #endif
415836db0b34SBarry Smith 
415936db0b34SBarry Smith   PetscFunctionBegin;
4160a96a251dSBarry Smith   if (i[0]) {
4161e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
416236db0b34SBarry Smith   }
4163f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4164f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4165a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4166ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4167ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4168ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4169ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
4170ab93d7beSBarry Smith 
417136db0b34SBarry Smith   aij->i = i;
417236db0b34SBarry Smith   aij->j = j;
417336db0b34SBarry Smith   aij->a = a;
417436db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
417536db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4176e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4177e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
417836db0b34SBarry Smith 
417936db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
418036db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
41812515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
4182e32f2f54SBarry 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]);
41839985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4184e32f2f54SBarry 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);
4185e32f2f54SBarry 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);
41869985e31cSBarry Smith     }
418736db0b34SBarry Smith #endif
418836db0b34SBarry Smith   }
41892515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
419036db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
4191e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
4192e32f2f54SBarry 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]);
419336db0b34SBarry Smith   }
419436db0b34SBarry Smith #endif
419536db0b34SBarry Smith 
4196b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4197b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
419836db0b34SBarry Smith   PetscFunctionReturn(0);
419936db0b34SBarry Smith }
42008a0b0e6bSVictor Minden #undef __FUNCT__
42018a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
420280ef6e79SMatthew G Knepley /*@C
4203d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
42048a0b0e6bSVictor Minden               provided by the user.
42058a0b0e6bSVictor Minden 
42068a0b0e6bSVictor Minden       Collective on MPI_Comm
42078a0b0e6bSVictor Minden 
42088a0b0e6bSVictor Minden    Input Parameters:
42098a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
42108a0b0e6bSVictor Minden .   m   - number of rows
42118a0b0e6bSVictor Minden .   n   - number of columns
42128a0b0e6bSVictor Minden .   i   - row indices
42138a0b0e6bSVictor Minden .   j   - column indices
42141230e6d1SVictor Minden .   a   - matrix values
42151230e6d1SVictor Minden .   nz  - number of nonzeros
42161230e6d1SVictor Minden -   idx - 0 or 1 based
42178a0b0e6bSVictor Minden 
42188a0b0e6bSVictor Minden    Output Parameter:
42198a0b0e6bSVictor Minden .   mat - the matrix
42208a0b0e6bSVictor Minden 
42218a0b0e6bSVictor Minden    Level: intermediate
42228a0b0e6bSVictor Minden 
42238a0b0e6bSVictor Minden    Notes:
42248a0b0e6bSVictor Minden        The i and j indices are 0 based
42258a0b0e6bSVictor Minden 
42268a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
42278a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
42288a0b0e6bSVictor Minden     as shown:
42298a0b0e6bSVictor Minden 
42308a0b0e6bSVictor Minden         1 0 0
42318a0b0e6bSVictor Minden         2 0 3
42328a0b0e6bSVictor Minden         4 5 6
42338a0b0e6bSVictor Minden 
42348a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
42358a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
42368a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
42378a0b0e6bSVictor Minden 
42388a0b0e6bSVictor Minden 
423969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
42408a0b0e6bSVictor Minden 
42418a0b0e6bSVictor Minden @*/
42421230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
42438a0b0e6bSVictor Minden {
42448a0b0e6bSVictor Minden   PetscErrorCode ierr;
4245d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
42468a0b0e6bSVictor Minden 
42478a0b0e6bSVictor Minden 
42488a0b0e6bSVictor Minden   PetscFunctionBegin;
4249d021a1c5SVictor Minden   ierr = PetscMalloc(m*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
42501230e6d1SVictor Minden   ierr = PetscMemzero(nnz,m*sizeof(PetscInt));CHKERRQ(ierr);
42511230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++){
42521230e6d1SVictor Minden     nnz[i[ii]] += 1;
42531230e6d1SVictor Minden   }
42548a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
42558a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4256a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
42578a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
42581230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
42591230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++){
42601230e6d1SVictor Minden     if (idx){
42611230e6d1SVictor Minden       row = i[ii] - 1;
42621230e6d1SVictor Minden       col = j[ii] - 1;
42631230e6d1SVictor Minden     } else {
42641230e6d1SVictor Minden       row = i[ii];
42651230e6d1SVictor Minden       col = j[ii];
42668a0b0e6bSVictor Minden     }
42671230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
42688a0b0e6bSVictor Minden   }
42698a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
42708a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4271d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
42728a0b0e6bSVictor Minden   PetscFunctionReturn(0);
42738a0b0e6bSVictor Minden }
427436db0b34SBarry Smith 
4275cc8ba8e1SBarry Smith #undef __FUNCT__
4276ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4277dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4278cc8ba8e1SBarry Smith {
4279dfbe8321SBarry Smith   PetscErrorCode ierr;
4280cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
428136db0b34SBarry Smith 
4282cc8ba8e1SBarry Smith   PetscFunctionBegin;
42838ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4284cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4285cc8ba8e1SBarry Smith     a->coloring = coloring;
428612c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
428797f1f81fSBarry Smith     PetscInt             i,*larray;
428812c595b3SBarry Smith     ISColoring      ocoloring;
428908b6dcc0SBarry Smith     ISColoringValue *colors;
429012c595b3SBarry Smith 
429112c595b3SBarry Smith     /* set coloring for diagonal portion */
42920e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
4293d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
429412c595b3SBarry Smith       larray[i] = i;
429512c595b3SBarry Smith     }
4296992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
42970e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4298d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
429912c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
430012c595b3SBarry Smith     }
430112c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4302d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
430312c595b3SBarry Smith     a->coloring = ocoloring;
430412c595b3SBarry Smith   }
4305cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4306cc8ba8e1SBarry Smith }
4307cc8ba8e1SBarry Smith 
4308dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
4309ee4f033dSBarry Smith EXTERN_C_BEGIN
4310c6db04a5SJed Brown #include <adic/ad_utils.h>
4311ee4f033dSBarry Smith EXTERN_C_END
4312cc8ba8e1SBarry Smith 
4313cc8ba8e1SBarry Smith #undef __FUNCT__
4314ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
4315dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
4316cc8ba8e1SBarry Smith {
4317cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4318d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
43194440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
432008b6dcc0SBarry Smith   ISColoringValue *color;
4321cc8ba8e1SBarry Smith 
4322cc8ba8e1SBarry Smith   PetscFunctionBegin;
4323e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
43244440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
4325cc8ba8e1SBarry Smith   color = a->coloring->colors;
4326cc8ba8e1SBarry Smith   /* loop over rows */
4327cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
4328cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
4329cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
4330cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
4331cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
4332cc8ba8e1SBarry Smith     }
43334440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
4334ee4f033dSBarry Smith   }
4335ee4f033dSBarry Smith   PetscFunctionReturn(0);
4336ee4f033dSBarry Smith }
4337ee4f033dSBarry Smith #endif
4338ee4f033dSBarry Smith 
4339ee4f033dSBarry Smith #undef __FUNCT__
4340ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
434197f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4342ee4f033dSBarry Smith {
4343ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4344d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
434554f21887SBarry Smith   MatScalar       *v = a->a;
434654f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
434708b6dcc0SBarry Smith   ISColoringValue *color;
4348ee4f033dSBarry Smith 
4349ee4f033dSBarry Smith   PetscFunctionBegin;
4350e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4351ee4f033dSBarry Smith   color = a->coloring->colors;
4352ee4f033dSBarry Smith   /* loop over rows */
4353ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4354ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4355ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
4356ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
4357ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
4358ee4f033dSBarry Smith     }
4359ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4360cc8ba8e1SBarry Smith   }
4361cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4362cc8ba8e1SBarry Smith }
436336db0b34SBarry Smith 
436481824310SBarry Smith /*
436581824310SBarry Smith     Special version for direct calls from Fortran
436681824310SBarry Smith */
4367b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
436881824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
436981824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
437081824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
437181824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
437281824310SBarry Smith #endif
437381824310SBarry Smith 
437481824310SBarry Smith /* Change these macros so can be used in void function */
437581824310SBarry Smith #undef CHKERRQ
43767adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
437781824310SBarry Smith #undef SETERRQ2
4378e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
43794994cf47SJed Brown #undef SETERRQ3
43804994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
438181824310SBarry Smith 
438281824310SBarry Smith EXTERN_C_BEGIN
438381824310SBarry Smith #undef __FUNCT__
438481824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
43851f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
438681824310SBarry Smith {
438781824310SBarry Smith   Mat            A = *AA;
438881824310SBarry Smith   PetscInt       m = *mm, n = *nn;
438981824310SBarry Smith   InsertMode     is = *isis;
439081824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
439181824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
439281824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
439381824310SBarry Smith   PetscErrorCode ierr;
439481824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
439554f21887SBarry Smith   MatScalar      *ap,value,*aa;
4396ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4397ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
439881824310SBarry Smith 
439981824310SBarry Smith   PetscFunctionBegin;
44004994cf47SJed Brown   MatCheckPreallocated(A,1);
440181824310SBarry Smith   imax = a->imax;
440281824310SBarry Smith   ai = a->i;
440381824310SBarry Smith   ailen = a->ilen;
440481824310SBarry Smith   aj = a->j;
440581824310SBarry Smith   aa = a->a;
440681824310SBarry Smith 
440781824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
440881824310SBarry Smith     row  = im[k];
440981824310SBarry Smith     if (row < 0) continue;
441081824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4411d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
441281824310SBarry Smith #endif
441381824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
441481824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
441581824310SBarry Smith     low  = 0;
441681824310SBarry Smith     high = nrow;
441781824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
441881824310SBarry Smith       if (in[l] < 0) continue;
441981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4420d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
442181824310SBarry Smith #endif
442281824310SBarry Smith       col = in[l];
442381824310SBarry Smith       if (roworiented) {
442481824310SBarry Smith         value = v[l + k*n];
442581824310SBarry Smith       } else {
442681824310SBarry Smith         value = v[k + l*m];
442781824310SBarry Smith       }
442881824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
442981824310SBarry Smith 
443081824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
443181824310SBarry Smith       lastcol = col;
443281824310SBarry Smith       while (high-low > 5) {
443381824310SBarry Smith         t = (low+high)/2;
443481824310SBarry Smith         if (rp[t] > col) high = t;
443581824310SBarry Smith         else             low  = t;
443681824310SBarry Smith       }
443781824310SBarry Smith       for (i=low; i<high; i++) {
443881824310SBarry Smith         if (rp[i] > col) break;
443981824310SBarry Smith         if (rp[i] == col) {
444081824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
444181824310SBarry Smith           else                  ap[i] = value;
444281824310SBarry Smith           goto noinsert;
444381824310SBarry Smith         }
444481824310SBarry Smith       }
444581824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
444681824310SBarry Smith       if (nonew == 1) goto noinsert;
44477adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4448fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
444981824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
445081824310SBarry Smith       /* shift up all the later entries in this row */
445181824310SBarry Smith       for (ii=N; ii>=i; ii--) {
445281824310SBarry Smith         rp[ii+1] = rp[ii];
445381824310SBarry Smith         ap[ii+1] = ap[ii];
445481824310SBarry Smith       }
445581824310SBarry Smith       rp[i] = col;
445681824310SBarry Smith       ap[i] = value;
445781824310SBarry Smith       noinsert:;
445881824310SBarry Smith       low = i + 1;
445981824310SBarry Smith     }
446081824310SBarry Smith     ailen[row] = nrow;
446181824310SBarry Smith   }
446281824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
446381824310SBarry Smith   PetscFunctionReturnVoid();
446481824310SBarry Smith }
446581824310SBarry Smith EXTERN_C_END
446662298a1eSBarry Smith 
4467