xref: /petsc/src/mat/impls/aij/seq/aij.c (revision b37d52dbc59334e524284a0f84b46088623bf642)
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>
120716a85fSBarry Smith 
130716a85fSBarry Smith #undef __FUNCT__
140716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_SeqAIJ"
150716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
160716a85fSBarry Smith {
170716a85fSBarry Smith   PetscErrorCode ierr;
180716a85fSBarry Smith   PetscInt       i,m,n;
190716a85fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
200716a85fSBarry Smith 
210716a85fSBarry Smith   PetscFunctionBegin;
220716a85fSBarry Smith   ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr);
230716a85fSBarry Smith   ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr);
240716a85fSBarry Smith   if (type == NORM_2) {
250716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
260716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
270716a85fSBarry Smith     }
280716a85fSBarry Smith   } else if (type == NORM_1) {
290716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
300716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
310716a85fSBarry Smith     }
320716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
330716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
340716a85fSBarry Smith       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
350716a85fSBarry Smith     }
360716a85fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");
370716a85fSBarry Smith 
380716a85fSBarry Smith   if (type == NORM_2) {
398f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
400716a85fSBarry Smith   }
410716a85fSBarry Smith   PetscFunctionReturn(0);
420716a85fSBarry Smith }
430716a85fSBarry Smith 
444a2ae208SSatish Balay #undef __FUNCT__
456ce1633cSBarry Smith #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ"
466ce1633cSBarry Smith PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
476ce1633cSBarry Smith {
486ce1633cSBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
496ce1633cSBarry Smith   const MatScalar   *aa = a->a;
506ce1633cSBarry Smith   PetscInt          i,m=A->rmap->n,cnt = 0;
516ce1633cSBarry Smith   const PetscInt    *jj = a->j,*diag;
526ce1633cSBarry Smith   PetscInt          *rows;
536ce1633cSBarry Smith   PetscErrorCode    ierr;
546ce1633cSBarry Smith 
556ce1633cSBarry Smith   PetscFunctionBegin;
566ce1633cSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
576ce1633cSBarry Smith   diag = a->diag;
586ce1633cSBarry Smith   for (i=0; i<m; i++) {
596ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
606ce1633cSBarry Smith       cnt++;
616ce1633cSBarry Smith     }
626ce1633cSBarry Smith   }
636ce1633cSBarry Smith   ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr);
646ce1633cSBarry Smith   cnt  = 0;
656ce1633cSBarry Smith   for (i=0; i<m; i++) {
666ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
676ce1633cSBarry Smith       rows[cnt++] = i;
686ce1633cSBarry Smith     }
696ce1633cSBarry Smith   }
706ce1633cSBarry Smith   ierr = ISCreateGeneral(((PetscObject)A)->comm,cnt,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
716ce1633cSBarry Smith   PetscFunctionReturn(0);
726ce1633cSBarry Smith }
736ce1633cSBarry Smith 
746ce1633cSBarry Smith #undef __FUNCT__
75b3a44c85SBarry Smith #define __FUNCT__ "MatFindNonzeroRows_SeqAIJ"
76b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
77b3a44c85SBarry Smith {
78b3a44c85SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
79b3a44c85SBarry Smith   const MatScalar   *aa;
80b3a44c85SBarry Smith   PetscInt          m=A->rmap->n,cnt = 0;
81b3a44c85SBarry Smith   const PetscInt    *ii;
82b3a44c85SBarry Smith   PetscInt          n,i,j,*rows;
83b3a44c85SBarry Smith   PetscErrorCode    ierr;
84b3a44c85SBarry Smith 
85b3a44c85SBarry Smith   PetscFunctionBegin;
86b3a44c85SBarry Smith   *keptrows = 0;
87b3a44c85SBarry Smith   ii        = a->i;
88b3a44c85SBarry Smith   for (i=0; i<m; i++) {
89b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
90b3a44c85SBarry Smith     if (!n) {
91b3a44c85SBarry Smith       cnt++;
92b3a44c85SBarry Smith       goto ok1;
93b3a44c85SBarry Smith     }
94b3a44c85SBarry Smith     aa  = a->a + ii[i];
95b3a44c85SBarry Smith     for (j=0; j<n; j++) {
96b3a44c85SBarry Smith       if (aa[j] != 0.0) goto ok1;
97b3a44c85SBarry Smith     }
98b3a44c85SBarry Smith     cnt++;
99b3a44c85SBarry Smith     ok1:;
100b3a44c85SBarry Smith   }
101b3a44c85SBarry Smith   if (!cnt) PetscFunctionReturn(0);
102b3a44c85SBarry Smith   ierr = PetscMalloc((A->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr);
103b3a44c85SBarry Smith   cnt  = 0;
104b3a44c85SBarry Smith   for (i=0; i<m; i++) {
105b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
106b3a44c85SBarry Smith     if (!n) continue;
107b3a44c85SBarry Smith     aa  = a->a + ii[i];
108b3a44c85SBarry Smith     for (j=0; j<n; j++) {
109b3a44c85SBarry Smith       if (aa[j] != 0.0) {
110b3a44c85SBarry Smith         rows[cnt++] = i;
111b3a44c85SBarry Smith         break;
112b3a44c85SBarry Smith       }
113b3a44c85SBarry Smith     }
114b3a44c85SBarry Smith   }
115b3a44c85SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
116b3a44c85SBarry Smith   PetscFunctionReturn(0);
117b3a44c85SBarry Smith }
118b3a44c85SBarry Smith 
119b3a44c85SBarry Smith #undef __FUNCT__
12079299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1217087cfbeSBarry Smith PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
12279299369SBarry Smith {
12379299369SBarry Smith   PetscErrorCode ierr;
12479299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
125d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
12654f21887SBarry Smith   MatScalar      *aa = aij->a;
12754f21887SBarry Smith   PetscScalar    *v;
128ace3abfcSBarry Smith   PetscBool      missing;
12979299369SBarry Smith 
13079299369SBarry Smith   PetscFunctionBegin;
13109f38230SBarry Smith   if (Y->assembled) {
13209f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
13309f38230SBarry Smith     if (!missing) {
13479299369SBarry Smith       diag = aij->diag;
13579299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
13679299369SBarry Smith       if (is == INSERT_VALUES) {
13779299369SBarry Smith 	for (i=0; i<m; i++) {
13879299369SBarry Smith 	  aa[diag[i]] = v[i];
13979299369SBarry Smith 	}
14079299369SBarry Smith       } else {
14179299369SBarry Smith 	for (i=0; i<m; i++) {
14279299369SBarry Smith 	  aa[diag[i]] += v[i];
14379299369SBarry Smith 	}
14479299369SBarry Smith       }
14579299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
14679299369SBarry Smith       PetscFunctionReturn(0);
14779299369SBarry Smith     }
14886c113feSBarry Smith     aij->idiagvalid  = PETSC_FALSE;
14986c113feSBarry Smith     aij->ibdiagvalid = PETSC_FALSE;
15009f38230SBarry Smith   }
15109f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
15209f38230SBarry Smith   PetscFunctionReturn(0);
15309f38230SBarry Smith }
15479299369SBarry Smith 
15579299369SBarry Smith #undef __FUNCT__
1564a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
157ace3abfcSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
15817ab2063SBarry Smith {
159416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
160dfbe8321SBarry Smith   PetscErrorCode ierr;
16197f1f81fSBarry Smith   PetscInt       i,ishift;
16217ab2063SBarry Smith 
1633a40ed3dSBarry Smith   PetscFunctionBegin;
164d0f46423SBarry Smith   *m     = A->rmap->n;
1653a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
166bfeeae90SHong Zhang   ishift = 0;
16753e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
168d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
169bfeeae90SHong Zhang   } else if (oshift == 1) {
170d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
1713b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
172d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
173d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
174ecc77c7aSBarry Smith     if (ja) {
17597f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
1763b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
177ecc77c7aSBarry Smith     }
1786945ee14SBarry Smith   } else {
179ecc77c7aSBarry Smith     *ia = a->i;
180ecc77c7aSBarry Smith     if (ja) *ja = a->j;
181a2ce50c7SBarry Smith   }
1823a40ed3dSBarry Smith   PetscFunctionReturn(0);
183a2744918SBarry Smith }
184a2744918SBarry Smith 
1854a2ae208SSatish Balay #undef __FUNCT__
1864a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
187ace3abfcSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
1886945ee14SBarry Smith {
189dfbe8321SBarry Smith   PetscErrorCode ierr;
1906945ee14SBarry Smith 
1913a40ed3dSBarry Smith   PetscFunctionBegin;
1923a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
193bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
194606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
195ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
196bcd2baecSBarry Smith   }
1973a40ed3dSBarry Smith   PetscFunctionReturn(0);
19817ab2063SBarry Smith }
19917ab2063SBarry Smith 
2004a2ae208SSatish Balay #undef __FUNCT__
2014a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
202ace3abfcSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
2033b2fbd54SBarry Smith {
2043b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
205dfbe8321SBarry Smith   PetscErrorCode ierr;
206d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
20797f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
2083b2fbd54SBarry Smith 
2093a40ed3dSBarry Smith   PetscFunctionBegin;
210899cda47SBarry Smith   *nn = n;
2113a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2123b2fbd54SBarry Smith   if (symmetric) {
213d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
2143b2fbd54SBarry Smith   } else {
21597f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
21697f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
21797f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
21897f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
2193b2fbd54SBarry Smith     jj = a->j;
2203b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
221bfeeae90SHong Zhang       collengths[jj[i]]++;
2223b2fbd54SBarry Smith     }
2233b2fbd54SBarry Smith     cia[0] = oshift;
2243b2fbd54SBarry Smith     for (i=0; i<n; i++) {
2253b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
2263b2fbd54SBarry Smith     }
22797f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
2283b2fbd54SBarry Smith     jj   = a->j;
229a93ec695SBarry Smith     for (row=0; row<m; row++) {
230a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
231a93ec695SBarry Smith       for (i=0; i<mr; i++) {
232bfeeae90SHong Zhang         col = *jj++;
2333b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2343b2fbd54SBarry Smith       }
2353b2fbd54SBarry Smith     }
236606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2373b2fbd54SBarry Smith     *ia = cia; *ja = cja;
2383b2fbd54SBarry Smith   }
2393a40ed3dSBarry Smith   PetscFunctionReturn(0);
2403b2fbd54SBarry Smith }
2413b2fbd54SBarry Smith 
2424a2ae208SSatish Balay #undef __FUNCT__
2434a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
244ace3abfcSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
2453b2fbd54SBarry Smith {
246dfbe8321SBarry Smith   PetscErrorCode ierr;
247606d414cSSatish Balay 
2483a40ed3dSBarry Smith   PetscFunctionBegin;
2493a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2503b2fbd54SBarry Smith 
251606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
252606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
2533b2fbd54SBarry Smith 
2543a40ed3dSBarry Smith   PetscFunctionReturn(0);
2553b2fbd54SBarry Smith }
2563b2fbd54SBarry Smith 
25787d4246cSBarry Smith #undef __FUNCT__
25887d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
25987d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
26087d4246cSBarry Smith {
26187d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
26287d4246cSBarry Smith   PetscInt       *ai = a->i;
26387d4246cSBarry Smith   PetscErrorCode ierr;
26487d4246cSBarry Smith 
26587d4246cSBarry Smith   PetscFunctionBegin;
26687d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
26787d4246cSBarry Smith   PetscFunctionReturn(0);
26887d4246cSBarry Smith }
26987d4246cSBarry Smith 
2704a2ae208SSatish Balay #undef __FUNCT__
2714a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
27297f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
27317ab2063SBarry Smith {
274416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
275e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
27697f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
2776849ba73SBarry Smith   PetscErrorCode ierr;
278e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
27954f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
280ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
281ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
28217ab2063SBarry Smith 
2833a40ed3dSBarry Smith   PetscFunctionBegin;
28471fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
28517ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
286416022c9SBarry Smith     row  = im[k];
2875ef9f2a5SBarry Smith     if (row < 0) continue;
2882515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
289e32f2f54SBarry 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);
2903b2fbd54SBarry Smith #endif
291bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
29217ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
293416022c9SBarry Smith     low  = 0;
294c71e6ed7SBarry Smith     high = nrow;
29517ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
2965ef9f2a5SBarry Smith       if (in[l] < 0) continue;
2972515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
298e32f2f54SBarry 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);
2993b2fbd54SBarry Smith #endif
300bfeeae90SHong Zhang       col = in[l];
30116371a99SBarry Smith       if (v) {
3024b0e389bSBarry Smith 	if (roworiented) {
3035ef9f2a5SBarry Smith 	  value = v[l + k*n];
304bef8e0ddSBarry Smith 	} else {
3054b0e389bSBarry Smith 	  value = v[k + l*m];
3064b0e389bSBarry Smith 	}
30716371a99SBarry Smith       } else {
30875567043SBarry Smith         value = 0.;
30916371a99SBarry Smith       }
310abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
31136db0b34SBarry Smith 
3127cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
313e2ee6c50SBarry Smith       lastcol = col;
314416022c9SBarry Smith       while (high-low > 5) {
315416022c9SBarry Smith         t = (low+high)/2;
316416022c9SBarry Smith         if (rp[t] > col) high = t;
317416022c9SBarry Smith         else             low  = t;
31817ab2063SBarry Smith       }
319416022c9SBarry Smith       for (i=low; i<high; i++) {
32017ab2063SBarry Smith         if (rp[i] > col) break;
32117ab2063SBarry Smith         if (rp[i] == col) {
322416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
32317ab2063SBarry Smith           else                  ap[i] = value;
324e44c0bd4SBarry Smith           low = i + 1;
32517ab2063SBarry Smith           goto noinsert;
32617ab2063SBarry Smith         }
32717ab2063SBarry Smith       }
328abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
329c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
330e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
331fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
332c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
333416022c9SBarry Smith       /* shift up all the later entries in this row */
334416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
33517ab2063SBarry Smith         rp[ii+1] = rp[ii];
33617ab2063SBarry Smith         ap[ii+1] = ap[ii];
33717ab2063SBarry Smith       }
33817ab2063SBarry Smith       rp[i] = col;
33917ab2063SBarry Smith       ap[i] = value;
340416022c9SBarry Smith       low   = i + 1;
341e44c0bd4SBarry Smith       noinsert:;
34217ab2063SBarry Smith     }
34317ab2063SBarry Smith     ailen[row] = nrow;
34417ab2063SBarry Smith   }
34588e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
3463a40ed3dSBarry Smith   PetscFunctionReturn(0);
34717ab2063SBarry Smith }
34817ab2063SBarry Smith 
34981824310SBarry Smith 
3504a2ae208SSatish Balay #undef __FUNCT__
3514a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
352a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
3537eb43aa7SLois Curfman McInnes {
3547eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
35597f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
35697f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
35754f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
3587eb43aa7SLois Curfman McInnes 
3593a40ed3dSBarry Smith   PetscFunctionBegin;
3607eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
3617eb43aa7SLois Curfman McInnes     row  = im[k];
362e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
363e32f2f54SBarry 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);
364bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
3657eb43aa7SLois Curfman McInnes     nrow = ailen[row];
3667eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
367e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
368e32f2f54SBarry 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);
369bfeeae90SHong Zhang       col = in[l] ;
3707eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
3717eb43aa7SLois Curfman McInnes       while (high-low > 5) {
3727eb43aa7SLois Curfman McInnes         t = (low+high)/2;
3737eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
3747eb43aa7SLois Curfman McInnes         else             low  = t;
3757eb43aa7SLois Curfman McInnes       }
3767eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
3777eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
3787eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
379b49de8d1SLois Curfman McInnes           *v++ = ap[i];
3807eb43aa7SLois Curfman McInnes           goto finished;
3817eb43aa7SLois Curfman McInnes         }
3827eb43aa7SLois Curfman McInnes       }
38397e567efSBarry Smith       *v++ = 0.0;
3847eb43aa7SLois Curfman McInnes       finished:;
3857eb43aa7SLois Curfman McInnes     }
3867eb43aa7SLois Curfman McInnes   }
3873a40ed3dSBarry Smith   PetscFunctionReturn(0);
3887eb43aa7SLois Curfman McInnes }
3897eb43aa7SLois Curfman McInnes 
39017ab2063SBarry Smith 
3914a2ae208SSatish Balay #undef __FUNCT__
3924a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
393dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
39417ab2063SBarry Smith {
395416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3966849ba73SBarry Smith   PetscErrorCode ierr;
3976f69ff64SBarry Smith   PetscInt       i,*col_lens;
3986f69ff64SBarry Smith   int            fd;
399*b37d52dbSMark F. Adams   FILE           *file;
40017ab2063SBarry Smith 
4013a40ed3dSBarry Smith   PetscFunctionBegin;
402b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
403d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
4040700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
405d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
406d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
407416022c9SBarry Smith   col_lens[3] = a->nz;
408416022c9SBarry Smith 
409416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
410d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
411416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
41217ab2063SBarry Smith   }
413d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
414606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
415416022c9SBarry Smith 
416416022c9SBarry Smith   /* store column indices (zero start index) */
4176f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
418416022c9SBarry Smith 
419416022c9SBarry Smith   /* store nonzero values */
4206f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
421*b37d52dbSMark F. Adams 
422*b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
423*b37d52dbSMark F. Adams   if (file) {
424*b37d52dbSMark F. Adams     fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
425*b37d52dbSMark F. Adams   }
426*b37d52dbSMark F. Adams 
4273a40ed3dSBarry Smith   PetscFunctionReturn(0);
42817ab2063SBarry Smith }
429416022c9SBarry Smith 
43009573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
431cd155464SBarry Smith 
4324a2ae208SSatish Balay #undef __FUNCT__
4334a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
434dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
435416022c9SBarry Smith {
436416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
437dfbe8321SBarry Smith   PetscErrorCode    ierr;
438d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
439e060cb09SBarry Smith   const char        *name;
440f3ef73ceSBarry Smith   PetscViewerFormat format;
44117ab2063SBarry Smith 
4423a40ed3dSBarry Smith   PetscFunctionBegin;
443b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
44471c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
44597f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
446d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
447d00d2cf4SBarry Smith       nofinalvalue = 1;
448d00d2cf4SBarry Smith     }
449d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
450d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
45177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
45277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
453b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
45417ab2063SBarry Smith 
45517ab2063SBarry Smith     for (i=0; i<m; i++) {
456416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
457aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
45877431f27SBarry 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);
45917ab2063SBarry Smith #else
46077431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
46117ab2063SBarry Smith #endif
46217ab2063SBarry Smith       }
46317ab2063SBarry Smith     }
464d00d2cf4SBarry Smith     if (nofinalvalue) {
465d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
466d00d2cf4SBarry Smith     }
467317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
468fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
469d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
47068369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
471cd155464SBarry Smith      PetscFunctionReturn(0);
472fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
473d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4747566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
47544cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
47677431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
47744cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
478aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
47936db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
480a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48136db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
482a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48336db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
484a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4856831982aSBarry Smith         }
48644cd7ae7SLois Curfman McInnes #else
487a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
48844cd7ae7SLois Curfman McInnes #endif
48944cd7ae7SLois Curfman McInnes       }
490b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
49144cd7ae7SLois Curfman McInnes     }
492d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
493fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
49497f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
495d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4967566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
49797f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
498496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
499496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
500496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
501496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
502aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
50336db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
504496be53dSLois Curfman McInnes #else
505496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
506496be53dSLois Curfman McInnes #endif
507496be53dSLois Curfman McInnes         }
508496be53dSLois Curfman McInnes       }
509496be53dSLois Curfman McInnes     }
5102e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
51177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
5122e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
51377431f27SBarry 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);}
51477431f27SBarry 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);}
51577431f27SBarry 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);}
51677431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
51777431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
51877431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
519496be53dSLois Curfman McInnes     }
520b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
521606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
522496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
523496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
52477431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
525496be53dSLois Curfman McInnes       }
526b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
527496be53dSLois Curfman McInnes     }
528b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
529496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
530496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
531496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
532aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
53336db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
534b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5356831982aSBarry Smith           }
536496be53dSLois Curfman McInnes #else
537b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
538496be53dSLois Curfman McInnes #endif
539496be53dSLois Curfman McInnes         }
540496be53dSLois Curfman McInnes       }
541b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
542496be53dSLois Curfman McInnes     }
543d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
544fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
54597f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
54687828ca2SBarry Smith     PetscScalar value;
54702594712SBarry Smith 
548d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5497566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
55002594712SBarry Smith     for (i=0; i<m; i++) {
55102594712SBarry Smith       jcnt = 0;
552d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
553e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
55402594712SBarry Smith           value = a->a[cnt++];
555e24b481bSBarry Smith           jcnt++;
55602594712SBarry Smith         } else {
55702594712SBarry Smith           value = 0.0;
55802594712SBarry Smith         }
559aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
560b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
56102594712SBarry Smith #else
562b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
56302594712SBarry Smith #endif
56402594712SBarry Smith       }
565b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
56602594712SBarry Smith     }
567d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5683c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
569d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5707566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
5713c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5723c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
5733c215bfdSMatthew Knepley #else
5743c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
5753c215bfdSMatthew Knepley #endif
576d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
5773c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
5783c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5793c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5803c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
5813c215bfdSMatthew 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);
5823c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
5833c215bfdSMatthew 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);
5843c215bfdSMatthew Knepley         } else {
5853c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
5863c215bfdSMatthew Knepley         }
5873c215bfdSMatthew Knepley #else
5883c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
5893c215bfdSMatthew Knepley #endif
5903c215bfdSMatthew Knepley       }
5913c215bfdSMatthew Knepley     }
592d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5933a40ed3dSBarry Smith   } else {
594d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5957566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
596d5f3da31SBarry Smith     if (A->factortype){
59716cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
59816cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
59916cd7e1dSShri Abhyankar         /* L part */
60016cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
60116cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
60216cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
60316cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
60416cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
60516cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
60616cd7e1dSShri Abhyankar           } else {
60716cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
60816cd7e1dSShri Abhyankar           }
60916cd7e1dSShri Abhyankar #else
61016cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
61116cd7e1dSShri Abhyankar #endif
61216cd7e1dSShri Abhyankar         }
61316cd7e1dSShri Abhyankar 	/* diagonal */
61416cd7e1dSShri Abhyankar 	j = a->diag[i];
61516cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
61616cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
6172c990fa1SHong 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);
61816cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
6192c990fa1SHong 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);
62016cd7e1dSShri Abhyankar           } else {
6212c990fa1SHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
62216cd7e1dSShri Abhyankar           }
62316cd7e1dSShri Abhyankar #else
6242c990fa1SHong Zhang           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,1.0/a->a[j]);CHKERRQ(ierr);
62516cd7e1dSShri Abhyankar #endif
62616cd7e1dSShri Abhyankar 
62716cd7e1dSShri Abhyankar 	/* U part */
62816cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
62916cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
63016cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
63116cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
63216cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
63316cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
63416cd7e1dSShri Abhyankar           } else {
63516cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
63616cd7e1dSShri Abhyankar           }
63716cd7e1dSShri Abhyankar #else
63816cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
63916cd7e1dSShri Abhyankar #endif
64016cd7e1dSShri Abhyankar }
64116cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
64216cd7e1dSShri Abhyankar         }
64316cd7e1dSShri Abhyankar     } else {
64417ab2063SBarry Smith       for (i=0; i<m; i++) {
64577431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
646416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
647aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
64836db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
649a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
65036db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
651a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6523a40ed3dSBarry Smith           } else {
653a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
65417ab2063SBarry Smith           }
65517ab2063SBarry Smith #else
656a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
65717ab2063SBarry Smith #endif
65817ab2063SBarry Smith         }
659b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
66017ab2063SBarry Smith       }
66116cd7e1dSShri Abhyankar     }
662d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
66317ab2063SBarry Smith   }
664b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6653a40ed3dSBarry Smith   PetscFunctionReturn(0);
666416022c9SBarry Smith }
667416022c9SBarry Smith 
6684a2ae208SSatish Balay #undef __FUNCT__
6694a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
670dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
671416022c9SBarry Smith {
672480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
673416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
674dfbe8321SBarry Smith   PetscErrorCode    ierr;
675d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
67636db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
677b0a32e0cSBarry Smith   PetscViewer       viewer;
678f3ef73ceSBarry Smith   PetscViewerFormat format;
679cddf8d76SBarry Smith 
6803a40ed3dSBarry Smith   PetscFunctionBegin;
681480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
682b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
68319bcc07fSBarry Smith 
684b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
685416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
6860513a670SBarry Smith 
687fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
6880513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
689b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
690416022c9SBarry Smith     for (i=0; i<m; i++) {
691cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
692bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
693bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
694aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
69536db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
696cddf8d76SBarry Smith #else
697cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
698cddf8d76SBarry Smith #endif
699b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
700cddf8d76SBarry Smith       }
701cddf8d76SBarry Smith     }
702b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
703cddf8d76SBarry Smith     for (i=0; i<m; i++) {
704cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
705bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
706bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
707cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
708b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
709cddf8d76SBarry Smith       }
710cddf8d76SBarry Smith     }
711b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
712cddf8d76SBarry Smith     for (i=0; i<m; i++) {
713cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
714bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
715bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
716aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
71736db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
718cddf8d76SBarry Smith #else
719cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
720cddf8d76SBarry Smith #endif
721b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
722416022c9SBarry Smith       }
723416022c9SBarry Smith     }
7240513a670SBarry Smith   } else {
7250513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
7260513a670SBarry Smith     /* first determine max of all nonzero values */
72797f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
728b0a32e0cSBarry Smith     PetscDraw   popup;
72936db0b34SBarry Smith     PetscReal scale;
7300513a670SBarry Smith 
7310513a670SBarry Smith     for (i=0; i<nz; i++) {
7320513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
7330513a670SBarry Smith     }
734b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
735b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
736b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
7370513a670SBarry Smith     count = 0;
7380513a670SBarry Smith     for (i=0; i<m; i++) {
7390513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
740bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
741bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
74297f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
743b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
7440513a670SBarry Smith         count++;
7450513a670SBarry Smith       }
7460513a670SBarry Smith     }
7470513a670SBarry Smith   }
748480ef9eaSBarry Smith   PetscFunctionReturn(0);
749480ef9eaSBarry Smith }
750cddf8d76SBarry Smith 
7514a2ae208SSatish Balay #undef __FUNCT__
7524a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
753dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
754480ef9eaSBarry Smith {
755dfbe8321SBarry Smith   PetscErrorCode ierr;
756b0a32e0cSBarry Smith   PetscDraw      draw;
75736db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
758ace3abfcSBarry Smith   PetscBool      isnull;
759480ef9eaSBarry Smith 
760480ef9eaSBarry Smith   PetscFunctionBegin;
761b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
762b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
763480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
764480ef9eaSBarry Smith 
765480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
766d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
767480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
768b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
769b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
770480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
7713a40ed3dSBarry Smith   PetscFunctionReturn(0);
772416022c9SBarry Smith }
773416022c9SBarry Smith 
7744a2ae208SSatish Balay #undef __FUNCT__
7754a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
776dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
777416022c9SBarry Smith {
778dfbe8321SBarry Smith   PetscErrorCode ierr;
779ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
780416022c9SBarry Smith 
7813a40ed3dSBarry Smith   PetscFunctionBegin;
782251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
783251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
784251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
785c45a1595SBarry Smith   if (iascii) {
7863a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
7870f5bd95cSBarry Smith   } else if (isbinary) {
7883a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
7890f5bd95cSBarry Smith   } else if (isdraw) {
7903a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
791913ac41fSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
7924108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
7933a40ed3dSBarry Smith   PetscFunctionReturn(0);
79417ab2063SBarry Smith }
79519bcc07fSBarry Smith 
7964a2ae208SSatish Balay #undef __FUNCT__
7974a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
798dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
79917ab2063SBarry Smith {
800416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8016849ba73SBarry Smith   PetscErrorCode ierr;
80297f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
803d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
80454f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
8053447b6efSHong Zhang   PetscReal      ratio=0.6;
80617ab2063SBarry Smith 
8073a40ed3dSBarry Smith   PetscFunctionBegin;
8083a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
80917ab2063SBarry Smith 
81043ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
81117ab2063SBarry Smith   for (i=1; i<m; i++) {
812416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
81317ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
81494a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
81517ab2063SBarry Smith     if (fshift) {
816bfeeae90SHong Zhang       ip = aj + ai[i] ;
817bfeeae90SHong Zhang       ap = aa + ai[i] ;
81817ab2063SBarry Smith       N  = ailen[i];
81917ab2063SBarry Smith       for (j=0; j<N; j++) {
82017ab2063SBarry Smith         ip[j-fshift] = ip[j];
82117ab2063SBarry Smith         ap[j-fshift] = ap[j];
82217ab2063SBarry Smith       }
82317ab2063SBarry Smith     }
82417ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
82517ab2063SBarry Smith   }
82617ab2063SBarry Smith   if (m) {
82717ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
82817ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
82917ab2063SBarry Smith   }
83017ab2063SBarry Smith   /* reset ilen and imax for each row */
83117ab2063SBarry Smith   for (i=0; i<m; i++) {
83217ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
83317ab2063SBarry Smith   }
834bfeeae90SHong Zhang   a->nz = ai[m];
83565e19b50SBarry 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);
83617ab2063SBarry Smith 
83709f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
838d0f46423SBarry 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);
839ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
840ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
8418e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
842dd5f02e7SSatish Balay   a->reallocs          = 0;
8434e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
84436db0b34SBarry Smith   a->rmax              = rmax;
8454e220ebcSLois Curfman McInnes 
846cd6b891eSBarry Smith   ierr = MatCheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
84788e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
84871c2f376SKris Buschelman 
8494108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
85071f1c65dSBarry Smith 
85171f1c65dSBarry Smith   a->idiagvalid  = PETSC_FALSE;
852bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_FALSE;
8533a40ed3dSBarry Smith   PetscFunctionReturn(0);
85417ab2063SBarry Smith }
85517ab2063SBarry Smith 
8564a2ae208SSatish Balay #undef __FUNCT__
85799cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
85899cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
85999cafbc1SBarry Smith {
86099cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
86199cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
86254f21887SBarry Smith   MatScalar      *aa = a->a;
86399cafbc1SBarry Smith 
86499cafbc1SBarry Smith   PetscFunctionBegin;
86599cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
86686c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
86786c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
86899cafbc1SBarry Smith   PetscFunctionReturn(0);
86999cafbc1SBarry Smith }
87099cafbc1SBarry Smith 
87199cafbc1SBarry Smith #undef __FUNCT__
87299cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
87399cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
87499cafbc1SBarry Smith {
87599cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
87699cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
87754f21887SBarry Smith   MatScalar      *aa = a->a;
87899cafbc1SBarry Smith 
87999cafbc1SBarry Smith   PetscFunctionBegin;
88099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
88186c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
88286c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
88399cafbc1SBarry Smith   PetscFunctionReturn(0);
88499cafbc1SBarry Smith }
88599cafbc1SBarry Smith 
88699cafbc1SBarry Smith #undef __FUNCT__
8874a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
888dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
88917ab2063SBarry Smith {
890416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
891dfbe8321SBarry Smith   PetscErrorCode ierr;
8923a40ed3dSBarry Smith 
8933a40ed3dSBarry Smith   PetscFunctionBegin;
894d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
89586c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
89686c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
8973a40ed3dSBarry Smith   PetscFunctionReturn(0);
89817ab2063SBarry Smith }
899416022c9SBarry Smith 
9004a2ae208SSatish Balay #undef __FUNCT__
9014a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
902dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
90317ab2063SBarry Smith {
904416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
905dfbe8321SBarry Smith   PetscErrorCode ierr;
906d5d45c9bSBarry Smith 
9073a40ed3dSBarry Smith   PetscFunctionBegin;
908aa482453SBarry Smith #if defined(PETSC_USE_LOG)
909d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
91017ab2063SBarry Smith #endif
911e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
9126bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
9136bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
91405b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
915d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
91605b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
91771f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
91805b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
9196bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
92005b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
9216bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
92205b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
9236bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
924cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
9250b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
926a30b2313SHong Zhang 
9274108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
928bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
929901853e0SKris Buschelman 
930dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
931901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
932901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
933901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
934901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
935901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
9365a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr);
937901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
938901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
939a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
940901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
9413a40ed3dSBarry Smith   PetscFunctionReturn(0);
94217ab2063SBarry Smith }
94317ab2063SBarry Smith 
9444a2ae208SSatish Balay #undef __FUNCT__
9454a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
946ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool  flg)
94717ab2063SBarry Smith {
948416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9494846f1f5SKris Buschelman   PetscErrorCode ierr;
9503a40ed3dSBarry Smith 
9513a40ed3dSBarry Smith   PetscFunctionBegin;
952a65d3064SKris Buschelman   switch (op) {
953a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
9544e0d8c25SBarry Smith       a->roworiented       = flg;
955a65d3064SKris Buschelman       break;
956a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
957a9817697SBarry Smith       a->keepnonzeropattern    = flg;
958a65d3064SKris Buschelman       break;
959512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
960512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
961a65d3064SKris Buschelman       break;
962a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
9634e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
964a65d3064SKris Buschelman       break;
965a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
9664e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
967a65d3064SKris Buschelman       break;
96828b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
96928b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
97028b2fa4aSMatthew Knepley       break;
971a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
9724e0d8c25SBarry Smith       a->ignorezeroentries = flg;
9730df259c2SBarry Smith       break;
974cd6b891eSBarry Smith     case MAT_CHECK_COMPRESSED_ROW:
975cd6b891eSBarry Smith       a->compressedrow.check = flg;
976d487561eSHong Zhang       break;
9773d472b54SHong Zhang     case MAT_SPD:
9783d472b54SHong Zhang       A->spd_set                         = PETSC_TRUE;
9793d472b54SHong Zhang       A->spd                             = flg;
9803d472b54SHong Zhang       if (flg) {
9813d472b54SHong Zhang         A->symmetric                     = PETSC_TRUE;
9823d472b54SHong Zhang         A->structurally_symmetric        = PETSC_TRUE;
9833d472b54SHong Zhang         A->symmetric_set                 = PETSC_TRUE;
9843d472b54SHong Zhang         A->structurally_symmetric_set    = PETSC_TRUE;
9853d472b54SHong Zhang       }
9863d472b54SHong Zhang       break;
987b1646e73SJed Brown     case MAT_SYMMETRIC:
988b1646e73SJed Brown     case MAT_STRUCTURALLY_SYMMETRIC:
989b1646e73SJed Brown     case MAT_HERMITIAN:
990b1646e73SJed Brown     case MAT_SYMMETRY_ETERNAL:
9914e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
992a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
993a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
994290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
995a65d3064SKris Buschelman       break;
996b87ac2d8SJed Brown     case MAT_USE_INODES:
997b87ac2d8SJed Brown       /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
998b87ac2d8SJed Brown       break;
999a65d3064SKris Buschelman     default:
1000e32f2f54SBarry Smith       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1001a65d3064SKris Buschelman   }
10024108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
10033a40ed3dSBarry Smith   PetscFunctionReturn(0);
100417ab2063SBarry Smith }
100517ab2063SBarry Smith 
10064a2ae208SSatish Balay #undef __FUNCT__
10074a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1008dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
100917ab2063SBarry Smith {
1010416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10116849ba73SBarry Smith   PetscErrorCode ierr;
1012d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
101335e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
101417ab2063SBarry Smith 
10153a40ed3dSBarry Smith   PetscFunctionBegin;
1016d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1017e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
101835e7444dSHong Zhang 
1019d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
1020d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
102135e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
10222c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
102335e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
102435e7444dSHong Zhang     PetscFunctionReturn(0);
102535e7444dSHong Zhang   }
102635e7444dSHong Zhang 
10272dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
10281ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
102935e7444dSHong Zhang   for (i=0; i<n; i++) {
103035e7444dSHong Zhang     nz = ai[i+1] - ai[i];
10312f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
103235e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
103335e7444dSHong Zhang       if (aj[j] == i) {
103435e7444dSHong Zhang         x[i] = aa[j];
103517ab2063SBarry Smith         break;
103617ab2063SBarry Smith       }
103717ab2063SBarry Smith     }
103817ab2063SBarry Smith   }
10391ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
10403a40ed3dSBarry Smith   PetscFunctionReturn(0);
104117ab2063SBarry Smith }
104217ab2063SBarry Smith 
1043c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10444a2ae208SSatish Balay #undef __FUNCT__
10454a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1046dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
104717ab2063SBarry Smith {
1048416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
10495c897100SBarry Smith   PetscScalar       *x,*y;
1050dfbe8321SBarry Smith   PetscErrorCode    ierr;
1051d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
10525c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1053a77337e4SBarry Smith   MatScalar         *v;
1054a77337e4SBarry Smith   PetscScalar       alpha;
105504fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
10563447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
1057ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
10585c897100SBarry Smith #endif
105917ab2063SBarry Smith 
10603a40ed3dSBarry Smith   PetscFunctionBegin;
10612e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
10621ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10631ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10645c897100SBarry Smith 
10655c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1066bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
10675c897100SBarry Smith #else
10683447b6efSHong Zhang   if (usecprow){
10693447b6efSHong Zhang     m    = cprow.nrows;
10703447b6efSHong Zhang     ii   = cprow.i;
10717b2bb3b9SHong Zhang     ridx = cprow.rindex;
10723447b6efSHong Zhang   } else {
10733447b6efSHong Zhang     ii = a->i;
10743447b6efSHong Zhang   }
107517ab2063SBarry Smith   for (i=0; i<m; i++) {
10763447b6efSHong Zhang     idx   = a->j + ii[i] ;
10773447b6efSHong Zhang     v     = a->a + ii[i] ;
10783447b6efSHong Zhang     n     = ii[i+1] - ii[i];
10793447b6efSHong Zhang     if (usecprow){
10807b2bb3b9SHong Zhang       alpha = x[ridx[i]];
10813447b6efSHong Zhang     } else {
108217ab2063SBarry Smith       alpha = x[i];
10833447b6efSHong Zhang     }
108404fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
108517ab2063SBarry Smith   }
10865c897100SBarry Smith #endif
1087dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10881ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10891ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10903a40ed3dSBarry Smith   PetscFunctionReturn(0);
109117ab2063SBarry Smith }
109217ab2063SBarry Smith 
10934a2ae208SSatish Balay #undef __FUNCT__
10945c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1095dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
10965c897100SBarry Smith {
1097dfbe8321SBarry Smith   PetscErrorCode ierr;
10985c897100SBarry Smith 
10995c897100SBarry Smith   PetscFunctionBegin;
1100170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
11015c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
11025c897100SBarry Smith   PetscFunctionReturn(0);
11035c897100SBarry Smith }
11045c897100SBarry Smith 
1105c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
11065c897100SBarry Smith #undef __FUNCT__
11074a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1108dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
110917ab2063SBarry Smith {
1110416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1111d9fead3dSBarry Smith   PetscScalar       *y;
111254f21887SBarry Smith   const PetscScalar *x;
111354f21887SBarry Smith   const MatScalar   *aa;
1114dfbe8321SBarry Smith   PetscErrorCode    ierr;
1115003131ecSBarry Smith   PetscInt          m=A->rmap->n;
1116003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
11178aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1118362ced78SSatish Balay   PetscScalar       sum;
1119ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
112017ab2063SBarry Smith 
1121b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
112297952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1123fee21e36SBarry Smith #endif
1124fee21e36SBarry Smith 
11253a40ed3dSBarry Smith   PetscFunctionBegin;
11263649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
11271ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
112897952fefSHong Zhang   aj  = a->j;
112997952fefSHong Zhang   aa  = a->a;
1130416022c9SBarry Smith   ii  = a->i;
11314eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
113297952fefSHong Zhang     m    = a->compressedrow.nrows;
113397952fefSHong Zhang     ii   = a->compressedrow.i;
113497952fefSHong Zhang     ridx = a->compressedrow.rindex;
113597952fefSHong Zhang     for (i=0; i<m; i++){
113697952fefSHong Zhang       n   = ii[i+1] - ii[i];
113797952fefSHong Zhang       aj  = a->j + ii[i];
113897952fefSHong Zhang       aa  = a->a + ii[i];
113997952fefSHong Zhang       sum = 0.0;
1140a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1141003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1142003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
114397952fefSHong Zhang       y[*ridx++] = sum;
114497952fefSHong Zhang     }
114597952fefSHong Zhang   } else { /* do not use compressed row format */
1146b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1147b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1148b05257ddSBarry Smith #else
114917ab2063SBarry Smith     for (i=0; i<m; i++) {
1150003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1151003131ecSBarry Smith       aj  = a->j + ii[i];
1152003131ecSBarry Smith       aa  = a->a + ii[i];
115317ab2063SBarry Smith       sum  = 0.0;
1154a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1155003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
115617ab2063SBarry Smith       y[i] = sum;
115717ab2063SBarry Smith     }
11588d195f9aSBarry Smith #endif
1159b05257ddSBarry Smith   }
1160dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
11613649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
11621ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11633a40ed3dSBarry Smith   PetscFunctionReturn(0);
116417ab2063SBarry Smith }
116517ab2063SBarry Smith 
1166c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
11674a2ae208SSatish Balay #undef __FUNCT__
11684a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1169dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
117017ab2063SBarry Smith {
1171416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1172f15663dcSBarry Smith   PetscScalar       *y,*z;
1173f15663dcSBarry Smith   const PetscScalar *x;
117454f21887SBarry Smith   const MatScalar   *aa;
1175dfbe8321SBarry Smith   PetscErrorCode    ierr;
1176d0f46423SBarry Smith   PetscInt          m = A->rmap->n,*aj,*ii;
1177f15663dcSBarry Smith   PetscInt          n,i,*ridx=PETSC_NULL;
1178362ced78SSatish Balay   PetscScalar       sum;
1179ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
11809ea0dfa2SSatish Balay 
11813a40ed3dSBarry Smith   PetscFunctionBegin;
1182f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
11831ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
11842e8a6d31SBarry Smith   if (zz != yy) {
11851ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
11862e8a6d31SBarry Smith   } else {
11872e8a6d31SBarry Smith     z = y;
11882e8a6d31SBarry Smith   }
1189bfeeae90SHong Zhang 
119097952fefSHong Zhang   aj  = a->j;
119197952fefSHong Zhang   aa  = a->a;
1192cddf8d76SBarry Smith   ii  = a->i;
11934eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
11944eb6d288SHong Zhang     if (zz != yy){
11954eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
11964eb6d288SHong Zhang     }
119797952fefSHong Zhang     m    = a->compressedrow.nrows;
119897952fefSHong Zhang     ii   = a->compressedrow.i;
119997952fefSHong Zhang     ridx = a->compressedrow.rindex;
120097952fefSHong Zhang     for (i=0; i<m; i++){
120197952fefSHong Zhang       n  = ii[i+1] - ii[i];
120297952fefSHong Zhang       aj  = a->j + ii[i];
120397952fefSHong Zhang       aa  = a->a + ii[i];
120497952fefSHong Zhang       sum = y[*ridx];
1205f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
120697952fefSHong Zhang       z[*ridx++] = sum;
120797952fefSHong Zhang     }
120897952fefSHong Zhang   } else { /* do not use compressed row format */
1209f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1210f15663dcSBarry Smith   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1211f15663dcSBarry Smith #else
121217ab2063SBarry Smith     for (i=0; i<m; i++) {
1213f15663dcSBarry Smith       n    = ii[i+1] - ii[i];
1214f15663dcSBarry Smith       aj  = a->j + ii[i];
1215f15663dcSBarry Smith       aa  = a->a + ii[i];
121617ab2063SBarry Smith       sum  = y[i];
1217f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
121817ab2063SBarry Smith       z[i] = sum;
121917ab2063SBarry Smith     }
122002ab625aSSatish Balay #endif
1221f15663dcSBarry Smith   }
1222dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1223f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12241ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12252e8a6d31SBarry Smith   if (zz != yy) {
12261ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
12272e8a6d31SBarry Smith   }
12288154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
12296b375ea7SVictor Minden   /*
1230918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1231918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1232918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
12336b375ea7SVictor Minden   */
1234918e98c3SVictor Minden #endif
12353a40ed3dSBarry Smith   PetscFunctionReturn(0);
123617ab2063SBarry Smith }
123717ab2063SBarry Smith 
123817ab2063SBarry Smith /*
123917ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
124017ab2063SBarry Smith */
12414a2ae208SSatish Balay #undef __FUNCT__
12424a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1243dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
124417ab2063SBarry Smith {
1245416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12466849ba73SBarry Smith   PetscErrorCode ierr;
1247d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
124817ab2063SBarry Smith 
12493a40ed3dSBarry Smith   PetscFunctionBegin;
125009f38230SBarry Smith   if (!a->diag) {
125109f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
12529518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
125309f38230SBarry Smith   }
1254d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
125509f38230SBarry Smith     a->diag[i] = a->i[i+1];
1256bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1257bfeeae90SHong Zhang       if (a->j[j] == i) {
125809f38230SBarry Smith         a->diag[i] = j;
125917ab2063SBarry Smith         break;
126017ab2063SBarry Smith       }
126117ab2063SBarry Smith     }
126217ab2063SBarry Smith   }
12633a40ed3dSBarry Smith   PetscFunctionReturn(0);
126417ab2063SBarry Smith }
126517ab2063SBarry Smith 
1266be5855fcSBarry Smith /*
1267be5855fcSBarry Smith      Checks for missing diagonals
1268be5855fcSBarry Smith */
12694a2ae208SSatish Balay #undef __FUNCT__
12704a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1271ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1272be5855fcSBarry Smith {
1273be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
127497f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1275be5855fcSBarry Smith 
1276be5855fcSBarry Smith   PetscFunctionBegin;
127709f38230SBarry Smith   *missing = PETSC_FALSE;
1278d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
127909f38230SBarry Smith     *missing  = PETSC_TRUE;
128009f38230SBarry Smith     if (d) *d = 0;
1281358d2f5dSShri Abhyankar     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
128209f38230SBarry Smith   } else {
1283f1e2ffcdSBarry Smith     diag = a->diag;
1284d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1285bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
128609f38230SBarry Smith 	*missing = PETSC_TRUE;
128709f38230SBarry Smith 	if (d) *d = i;
128809f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
1289358d2f5dSShri Abhyankar 	break;
129009f38230SBarry Smith       }
1291be5855fcSBarry Smith     }
1292be5855fcSBarry Smith   }
1293be5855fcSBarry Smith   PetscFunctionReturn(0);
1294be5855fcSBarry Smith }
1295be5855fcSBarry Smith 
129671f1c65dSBarry Smith EXTERN_C_BEGIN
129771f1c65dSBarry Smith #undef __FUNCT__
129871f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
12997087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
130071f1c65dSBarry Smith {
130171f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
130271f1c65dSBarry Smith   PetscErrorCode ierr;
1303d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
130454f21887SBarry Smith   MatScalar      *v = a->a;
130554f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
130671f1c65dSBarry Smith 
130771f1c65dSBarry Smith   PetscFunctionBegin;
130871f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
130971f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
131071f1c65dSBarry Smith   diag = a->diag;
131171f1c65dSBarry Smith   if (!a->idiag) {
131271f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
131371f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
131471f1c65dSBarry Smith     v        = a->a;
131571f1c65dSBarry Smith   }
131671f1c65dSBarry Smith   mdiag = a->mdiag;
131771f1c65dSBarry Smith   idiag = a->idiag;
131871f1c65dSBarry Smith 
1319028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
132071f1c65dSBarry Smith     for (i=0; i<m; i++) {
132171f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1322e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
132371f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
132471f1c65dSBarry Smith     }
132571f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
132671f1c65dSBarry Smith   } else {
132771f1c65dSBarry Smith     for (i=0; i<m; i++) {
132871f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
132971f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
133071f1c65dSBarry Smith     }
1331dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
133271f1c65dSBarry Smith   }
133371f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
133471f1c65dSBarry Smith   PetscFunctionReturn(0);
133571f1c65dSBarry Smith }
13365a9745a3SMatthew Knepley EXTERN_C_END
133771f1c65dSBarry Smith 
1338c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
13394a2ae208SSatish Balay #undef __FUNCT__
134041f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
134141f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
134217ab2063SBarry Smith {
1343416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1344e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1345e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
134654f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1347dfbe8321SBarry Smith   PetscErrorCode     ierr;
1348d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
134997f1f81fSBarry Smith   const PetscInt     *idx,*diag;
135017ab2063SBarry Smith 
13513a40ed3dSBarry Smith   PetscFunctionBegin;
1352b965ef7fSBarry Smith   its = its*lits;
135391723122SBarry Smith 
135471f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
135571f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
135671f1c65dSBarry Smith   a->fshift = fshift;
135771f1c65dSBarry Smith   a->omega  = omega;
1358ed480e8bSBarry Smith 
135971f1c65dSBarry Smith   diag = a->diag;
136071f1c65dSBarry Smith   t     = a->ssor_work;
1361ed480e8bSBarry Smith   idiag = a->idiag;
136271f1c65dSBarry Smith   mdiag = a->mdiag;
1363ed480e8bSBarry Smith 
13641ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
13653649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
136671f1c65dSBarry Smith   CHKMEMQ;
1367ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
136817ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
136917ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1370ed480e8bSBarry Smith     bs = b;
137117ab2063SBarry Smith     for (i=0; i<m; i++) {
137271f1c65dSBarry Smith         d    = fshift + mdiag[i];
1373416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1374ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1375ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
137617ab2063SBarry Smith         sum  = b[i]*d/omega;
1377003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
137817ab2063SBarry Smith         x[i] = sum;
137917ab2063SBarry Smith     }
13801ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13813649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1382efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
13833a40ed3dSBarry Smith     PetscFunctionReturn(0);
138417ab2063SBarry Smith   }
1385c783ea89SBarry Smith 
138648af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1387e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
13883a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
138917ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1390887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
139117ab2063SBarry Smith 
139217ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
139317ab2063SBarry Smith 
1394887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
139517ab2063SBarry Smith     */
139617ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
139717ab2063SBarry Smith 
139817ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
139917ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1400416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1401ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1402ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
140317ab2063SBarry Smith       sum  = b[i];
1404e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1405ed480e8bSBarry Smith       x[i] = sum*idiag[i];
140617ab2063SBarry Smith     }
140717ab2063SBarry Smith 
140817ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1409416022c9SBarry Smith     v = a->a;
1410ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
141117ab2063SBarry Smith 
141217ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1413ed480e8bSBarry Smith     ts = t;
1414416022c9SBarry Smith     diag = a->diag;
141517ab2063SBarry Smith     for (i=0; i<m; i++) {
1416416022c9SBarry Smith       n    = diag[i] - a->i[i];
1417ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1418ed480e8bSBarry Smith       v    = a->a + a->i[i];
141917ab2063SBarry Smith       sum  = t[i];
1420003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1421ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1422733d66baSBarry Smith       /*  x = x + t */
1423733d66baSBarry Smith       x[i] += t[i];
142417ab2063SBarry Smith     }
142517ab2063SBarry Smith 
1426dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
14271ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
14283649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
14293a40ed3dSBarry Smith     PetscFunctionReturn(0);
143017ab2063SBarry Smith   }
143117ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
143217ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
143317ab2063SBarry Smith       for (i=0; i<m; i++) {
1434416022c9SBarry Smith         n    = diag[i] - a->i[i];
1435ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1436ed480e8bSBarry Smith         v    = a->a + a->i[i];
143717ab2063SBarry Smith         sum  = b[i];
1438e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
14395c99c7daSBarry Smith         t[i] = sum;
1440ed480e8bSBarry Smith         x[i] = sum*idiag[i];
144117ab2063SBarry Smith       }
14425c99c7daSBarry Smith       xb = t;
1443efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
14443a40ed3dSBarry Smith     } else xb = b;
144517ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
144617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1447416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1448ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1449ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
145017ab2063SBarry Smith         sum  = xb[i];
1451e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
14525c99c7daSBarry Smith         if (xb == b) {
1453ed480e8bSBarry Smith           x[i] = sum*idiag[i];
14545c99c7daSBarry Smith         } else {
14555c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
145617ab2063SBarry Smith         }
14575c99c7daSBarry Smith       }
1458efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
145917ab2063SBarry Smith     }
146017ab2063SBarry Smith     its--;
146117ab2063SBarry Smith   }
146217ab2063SBarry Smith   while (its--) {
146317ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
146417ab2063SBarry Smith       for (i=0; i<m; i++) {
1465416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1466ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1467ed480e8bSBarry Smith         v    = a->a + a->i[i];
146817ab2063SBarry Smith         sum  = b[i];
1469e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1470ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
147117ab2063SBarry Smith       }
14729f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
147317ab2063SBarry Smith     }
147417ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
147517ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1476416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1477ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1478ed480e8bSBarry Smith         v    = a->a + a->i[i];
147917ab2063SBarry Smith         sum  = b[i];
1480e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1481ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
148217ab2063SBarry Smith       }
14839f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
148417ab2063SBarry Smith     }
148517ab2063SBarry Smith   }
14861ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
14873649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
148871f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
148917ab2063SBarry Smith }
149017ab2063SBarry Smith 
14912af78befSBarry Smith 
14924a2ae208SSatish Balay #undef __FUNCT__
14934a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1494dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
149517ab2063SBarry Smith {
1496416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
14974e220ebcSLois Curfman McInnes 
14983a40ed3dSBarry Smith   PetscFunctionBegin;
14994e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
15004e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
15014e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
15024e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
15034e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
15048e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
15057adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1506d5f3da31SBarry Smith   if (A->factortype) {
15074e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
15084e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
15094e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
15104e220ebcSLois Curfman McInnes   } else {
15114e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
15124e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
15134e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
15144e220ebcSLois Curfman McInnes   }
15153a40ed3dSBarry Smith   PetscFunctionReturn(0);
151617ab2063SBarry Smith }
151717ab2063SBarry Smith 
15184a2ae208SSatish Balay #undef __FUNCT__
15194a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
15202b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
152117ab2063SBarry Smith {
1522416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
15233b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
15246849ba73SBarry Smith   PetscErrorCode    ierr;
152597b48c8fSBarry Smith   const PetscScalar *xx;
152697b48c8fSBarry Smith   PetscScalar       *bb;
1527ace3abfcSBarry Smith   PetscBool         missing;
152817ab2063SBarry Smith 
15293a40ed3dSBarry Smith   PetscFunctionBegin;
153097b48c8fSBarry Smith   if (x && b) {
153197b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
153297b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
153397b48c8fSBarry Smith     for (i=0; i<N; i++) {
153497b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
153597b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
153697b48c8fSBarry Smith     }
153797b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
153897b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
153997b48c8fSBarry Smith   }
154097b48c8fSBarry Smith 
1541a9817697SBarry Smith   if (a->keepnonzeropattern) {
1542f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1543e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1544bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1545f1e2ffcdSBarry Smith     }
1546f4df32b1SMatthew Knepley     if (diag != 0.0) {
154709f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1548e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1549f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1550f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1551f1e2ffcdSBarry Smith       }
1552f1e2ffcdSBarry Smith     }
155388e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1554f1e2ffcdSBarry Smith   } else {
1555f4df32b1SMatthew Knepley     if (diag != 0.0) {
155617ab2063SBarry Smith       for (i=0; i<N; i++) {
1557e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
15587ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1559416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1560f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1561bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
15627ae801bdSBarry Smith         } else { /* in case row was completely empty */
1563f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
156417ab2063SBarry Smith         }
156517ab2063SBarry Smith       }
15663a40ed3dSBarry Smith     } else {
156717ab2063SBarry Smith       for (i=0; i<N; i++) {
1568e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1569416022c9SBarry Smith         a->ilen[rows[i]] = 0;
157017ab2063SBarry Smith       }
157117ab2063SBarry Smith     }
157288e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1573f1e2ffcdSBarry Smith   }
157443a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15753a40ed3dSBarry Smith   PetscFunctionReturn(0);
157617ab2063SBarry Smith }
157717ab2063SBarry Smith 
15784a2ae208SSatish Balay #undef __FUNCT__
15796e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
15806e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
15816e169961SBarry Smith {
15826e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
15836e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
15846e169961SBarry Smith   PetscErrorCode    ierr;
15852b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
15866e169961SBarry Smith   const PetscScalar *xx;
15876e169961SBarry Smith   PetscScalar       *bb;
15886e169961SBarry Smith 
15896e169961SBarry Smith   PetscFunctionBegin;
15906e169961SBarry Smith   if (x && b) {
15916e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
15926e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
15932b40b63fSBarry Smith     vecs = PETSC_TRUE;
15946e169961SBarry Smith   }
15956e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
15966e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
15976e169961SBarry Smith   for (i=0; i<N; i++) {
15986e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
15996e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
16006e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
16016e169961SBarry Smith   }
16026e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
16036e169961SBarry Smith     if (!zeroed[i]) {
16046e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
16056e169961SBarry Smith         if (zeroed[a->j[j]]) {
16062b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
16076e169961SBarry Smith           a->a[j] = 0.0;
16086e169961SBarry Smith         }
16096e169961SBarry Smith       }
16102b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
16116e169961SBarry Smith   }
16126e169961SBarry Smith   if (x && b) {
16136e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
16146e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
16156e169961SBarry Smith   }
16166e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
16176e169961SBarry Smith   if (diag != 0.0) {
16186e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
16196e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
16206e169961SBarry Smith     for (i=0; i<N; i++) {
16216e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
16226e169961SBarry Smith     }
16236e169961SBarry Smith   }
16246e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
16256e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16266e169961SBarry Smith   PetscFunctionReturn(0);
16276e169961SBarry Smith }
16286e169961SBarry Smith 
16296e169961SBarry Smith #undef __FUNCT__
16304a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1631a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
163217ab2063SBarry Smith {
1633416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
163497f1f81fSBarry Smith   PetscInt   *itmp;
163517ab2063SBarry Smith 
16363a40ed3dSBarry Smith   PetscFunctionBegin;
1637e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
163817ab2063SBarry Smith 
1639416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1640bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
164117ab2063SBarry Smith   if (idx) {
1642bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1643bfeeae90SHong Zhang     if (*nz) {
16444e093b46SBarry Smith       *idx = itmp;
164517ab2063SBarry Smith     }
164617ab2063SBarry Smith     else *idx = 0;
164717ab2063SBarry Smith   }
16483a40ed3dSBarry Smith   PetscFunctionReturn(0);
164917ab2063SBarry Smith }
165017ab2063SBarry Smith 
1651bfeeae90SHong Zhang /* remove this function? */
16524a2ae208SSatish Balay #undef __FUNCT__
16534a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1654a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
165517ab2063SBarry Smith {
16563a40ed3dSBarry Smith   PetscFunctionBegin;
16573a40ed3dSBarry Smith   PetscFunctionReturn(0);
165817ab2063SBarry Smith }
165917ab2063SBarry Smith 
16604a2ae208SSatish Balay #undef __FUNCT__
16614a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1662dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
166317ab2063SBarry Smith {
1664416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
166554f21887SBarry Smith   MatScalar      *v = a->a;
166636db0b34SBarry Smith   PetscReal      sum = 0.0;
16676849ba73SBarry Smith   PetscErrorCode ierr;
166897f1f81fSBarry Smith   PetscInt       i,j;
166917ab2063SBarry Smith 
16703a40ed3dSBarry Smith   PetscFunctionBegin;
167117ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1672416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1673aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
167436db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
167517ab2063SBarry Smith #else
167617ab2063SBarry Smith       sum += (*v)*(*v); v++;
167717ab2063SBarry Smith #endif
167817ab2063SBarry Smith     }
16798f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
16803a40ed3dSBarry Smith   } else if (type == NORM_1) {
168136db0b34SBarry Smith     PetscReal *tmp;
168297f1f81fSBarry Smith     PetscInt    *jj = a->j;
1683d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1684d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1685064f8208SBarry Smith     *nrm = 0.0;
1686416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1687bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
168817ab2063SBarry Smith     }
1689d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1690064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
169117ab2063SBarry Smith     }
1692606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
16933a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1694064f8208SBarry Smith     *nrm = 0.0;
1695d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1696bfeeae90SHong Zhang       v = a->a + a->i[j];
169717ab2063SBarry Smith       sum = 0.0;
1698416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1699cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
170017ab2063SBarry Smith       }
1701064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
170217ab2063SBarry Smith     }
17033a40ed3dSBarry Smith   } else {
1704e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
170517ab2063SBarry Smith   }
17063a40ed3dSBarry Smith   PetscFunctionReturn(0);
170717ab2063SBarry Smith }
170817ab2063SBarry Smith 
17094e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
17104e938277SHong Zhang #undef __FUNCT__
17114e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
17124e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
17134e938277SHong Zhang {
17144e938277SHong Zhang   PetscErrorCode ierr;
17154e938277SHong Zhang   PetscInt       i,j,anzj;
17164e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ *)A->data,*b;
17174e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
17184e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
17194e938277SHong Zhang 
17204e938277SHong Zhang   PetscFunctionBegin;
17214e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
17224e938277SHong Zhang   ierr = PetscMalloc((an+1)*sizeof(PetscInt),&ati);CHKERRQ(ierr);
17234e938277SHong Zhang   ierr = PetscMalloc(ai[am]*sizeof(PetscInt),&atj);CHKERRQ(ierr);
17244e938277SHong Zhang   ierr = PetscMalloc(an*sizeof(PetscInt),&atfill);CHKERRQ(ierr);
17254e938277SHong Zhang   ierr = PetscMemzero(ati,(an+1)*sizeof(PetscInt));CHKERRQ(ierr);
17264e938277SHong Zhang 
17274e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
17284e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
17294e938277SHong Zhang   for (i=0;i<ai[am];i++) {
17304e938277SHong Zhang     ati[aj[i]+1] += 1;
17314e938277SHong Zhang   }
17324e938277SHong Zhang   /* Form ati for csr format of A^T. */
17334e938277SHong Zhang   for (i=0;i<an;i++) {
17344e938277SHong Zhang     ati[i+1] += ati[i];
17354e938277SHong Zhang   }
17364e938277SHong Zhang 
17374e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
17384e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
17394e938277SHong Zhang 
17404e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
17414e938277SHong Zhang   for (i=0;i<am;i++) {
17424e938277SHong Zhang     anzj = ai[i+1] - ai[i];
17434e938277SHong Zhang     for (j=0;j<anzj;j++) {
17444e938277SHong Zhang       atj[atfill[*aj]] = i;
17454e938277SHong Zhang       atfill[*aj++]   += 1;
17464e938277SHong Zhang     }
17474e938277SHong Zhang   }
17484e938277SHong Zhang 
17494e938277SHong Zhang   /* Clean up temporary space and complete requests. */
17504e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
17514e938277SHong Zhang   ierr = MatCreateSeqAIJWithArrays(((PetscObject)A)->comm,an,am,ati,atj,PETSC_NULL,B);CHKERRQ(ierr);
1752a2f3521dSMark F. Adams   (*B)->rmap->bs = A->cmap->bs;
1753a2f3521dSMark F. Adams   (*B)->cmap->bs = A->rmap->bs;
1754a2f3521dSMark F. Adams 
17554e938277SHong Zhang   b = (Mat_SeqAIJ *)((*B)->data);
17564e938277SHong Zhang   b->free_a   = PETSC_FALSE;
17574e938277SHong Zhang   b->free_ij  = PETSC_TRUE;
17584e938277SHong Zhang   b->nonew    = 0;
17594e938277SHong Zhang   PetscFunctionReturn(0);
17604e938277SHong Zhang }
17614e938277SHong Zhang 
17624a2ae208SSatish Balay #undef __FUNCT__
17634a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1764fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
176517ab2063SBarry Smith {
1766416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1767416022c9SBarry Smith   Mat            C;
17686849ba73SBarry Smith   PetscErrorCode ierr;
1769d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
177054f21887SBarry Smith   MatScalar      *array = a->a;
177117ab2063SBarry Smith 
17723a40ed3dSBarry Smith   PetscFunctionBegin;
1773e32f2f54SBarry 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");
1774fc4dec0aSBarry Smith 
1775fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1776d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1777d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1778bfeeae90SHong Zhang 
1779bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
17807adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1781d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
1782a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(C,A->cmap->bs,A->rmap->bs);CHKERRQ(ierr);
17837adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1784ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1785606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1786a541d17aSBarry Smith   } else {
1787a541d17aSBarry Smith     C = *B;
1788a541d17aSBarry Smith   }
1789a541d17aSBarry Smith 
179017ab2063SBarry Smith   for (i=0; i<m; i++) {
179117ab2063SBarry Smith     len    = ai[i+1]-ai[i];
179287d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1793b9b97703SBarry Smith     array += len;
1794b9b97703SBarry Smith     aj    += len;
179517ab2063SBarry Smith   }
17966d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17976d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
179817ab2063SBarry Smith 
1799815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1800416022c9SBarry Smith     *B = C;
180117ab2063SBarry Smith   } else {
1802eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
180317ab2063SBarry Smith   }
18043a40ed3dSBarry Smith   PetscFunctionReturn(0);
180517ab2063SBarry Smith }
180617ab2063SBarry Smith 
1807cd0d46ebSvictorle EXTERN_C_BEGIN
1808cd0d46ebSvictorle #undef __FUNCT__
18095fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
18107087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1811cd0d46ebSvictorle {
1812cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
181354f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
181454f21887SBarry Smith   MatScalar      *va,*vb;
18156849ba73SBarry Smith   PetscErrorCode ierr;
181697f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1817cd0d46ebSvictorle 
1818cd0d46ebSvictorle   PetscFunctionBegin;
1819cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1820cd0d46ebSvictorle 
1821cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1822cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
18235485867bSBarry Smith   if (ma!=nb || na!=mb){
18245485867bSBarry Smith     *f = PETSC_FALSE;
18255485867bSBarry Smith     PetscFunctionReturn(0);
18265485867bSBarry Smith   }
1827cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1828cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1829cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
183097f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
183197f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1832cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1833cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1834cd0d46ebSvictorle 
1835cd0d46ebSvictorle   *f = PETSC_TRUE;
1836cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1837cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
183897f1f81fSBarry Smith       PetscInt         idc,idr;
18395485867bSBarry Smith       PetscScalar vc,vr;
1840cd0d46ebSvictorle       /* column/row index/value */
18415485867bSBarry Smith       idc = adx[aptr[i]];
18425485867bSBarry Smith       idr = bdx[bptr[idc]];
18435485867bSBarry Smith       vc  = va[aptr[i]];
18445485867bSBarry Smith       vr  = vb[bptr[idc]];
18455485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
18465485867bSBarry Smith         *f = PETSC_FALSE;
18475485867bSBarry Smith         goto done;
1848cd0d46ebSvictorle       } else {
18495485867bSBarry Smith         aptr[i]++;
18505485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1851cd0d46ebSvictorle       }
1852cd0d46ebSvictorle     }
1853cd0d46ebSvictorle   }
1854cd0d46ebSvictorle  done:
1855cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
18563aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
1857cd0d46ebSvictorle   PetscFunctionReturn(0);
1858cd0d46ebSvictorle }
1859cd0d46ebSvictorle EXTERN_C_END
1860cd0d46ebSvictorle 
18611cbb95d3SBarry Smith EXTERN_C_BEGIN
18621cbb95d3SBarry Smith #undef __FUNCT__
18631cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
18647087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
18651cbb95d3SBarry Smith {
18661cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
186754f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
186854f21887SBarry Smith   MatScalar      *va,*vb;
18691cbb95d3SBarry Smith   PetscErrorCode ierr;
18701cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
18711cbb95d3SBarry Smith 
18721cbb95d3SBarry Smith   PetscFunctionBegin;
18731cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
18741cbb95d3SBarry Smith 
18751cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
18761cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
18771cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
18781cbb95d3SBarry Smith     *f = PETSC_FALSE;
18791cbb95d3SBarry Smith     PetscFunctionReturn(0);
18801cbb95d3SBarry Smith   }
18811cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
18821cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
18831cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
18841cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
18851cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
18861cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
18871cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
18881cbb95d3SBarry Smith 
18891cbb95d3SBarry Smith   *f = PETSC_TRUE;
18901cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
18911cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
18921cbb95d3SBarry Smith       PetscInt         idc,idr;
18931cbb95d3SBarry Smith       PetscScalar vc,vr;
18941cbb95d3SBarry Smith       /* column/row index/value */
18951cbb95d3SBarry Smith       idc = adx[aptr[i]];
18961cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
18971cbb95d3SBarry Smith       vc  = va[aptr[i]];
18981cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
18991cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
19001cbb95d3SBarry Smith         *f = PETSC_FALSE;
19011cbb95d3SBarry Smith         goto done;
19021cbb95d3SBarry Smith       } else {
19031cbb95d3SBarry Smith         aptr[i]++;
19041cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
19051cbb95d3SBarry Smith       }
19061cbb95d3SBarry Smith     }
19071cbb95d3SBarry Smith   }
19081cbb95d3SBarry Smith  done:
19091cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
19101cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
19111cbb95d3SBarry Smith   PetscFunctionReturn(0);
19121cbb95d3SBarry Smith }
19131cbb95d3SBarry Smith EXTERN_C_END
19141cbb95d3SBarry Smith 
19159e29f15eSvictorle #undef __FUNCT__
19169e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1917ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
19189e29f15eSvictorle {
1919dfbe8321SBarry Smith   PetscErrorCode ierr;
19209e29f15eSvictorle   PetscFunctionBegin;
19215485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
19229e29f15eSvictorle   PetscFunctionReturn(0);
19239e29f15eSvictorle }
19249e29f15eSvictorle 
19254a2ae208SSatish Balay #undef __FUNCT__
19261cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
1927ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
19281cbb95d3SBarry Smith {
19291cbb95d3SBarry Smith   PetscErrorCode ierr;
19301cbb95d3SBarry Smith   PetscFunctionBegin;
19311cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
19321cbb95d3SBarry Smith   PetscFunctionReturn(0);
19331cbb95d3SBarry Smith }
19341cbb95d3SBarry Smith 
19351cbb95d3SBarry Smith #undef __FUNCT__
19364a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1937dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
193817ab2063SBarry Smith {
1939416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
194054f21887SBarry Smith   PetscScalar    *l,*r,x;
194154f21887SBarry Smith   MatScalar      *v;
1942dfbe8321SBarry Smith   PetscErrorCode ierr;
1943d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
194417ab2063SBarry Smith 
19453a40ed3dSBarry Smith   PetscFunctionBegin;
194617ab2063SBarry Smith   if (ll) {
19473ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
19483ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1949e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1950e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
19511ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1952416022c9SBarry Smith     v = a->a;
195317ab2063SBarry Smith     for (i=0; i<m; i++) {
195417ab2063SBarry Smith       x = l[i];
1955416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
195617ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
195717ab2063SBarry Smith     }
19581ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1959efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
196017ab2063SBarry Smith   }
196117ab2063SBarry Smith   if (rr) {
1962e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1963e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
19641ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1965416022c9SBarry Smith     v = a->a; jj = a->j;
196617ab2063SBarry Smith     for (i=0; i<nz; i++) {
1967bfeeae90SHong Zhang       (*v++) *= r[*jj++];
196817ab2063SBarry Smith     }
19691ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1970efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
197117ab2063SBarry Smith   }
197286c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
197386c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
19743a40ed3dSBarry Smith   PetscFunctionReturn(0);
197517ab2063SBarry Smith }
197617ab2063SBarry Smith 
19774a2ae208SSatish Balay #undef __FUNCT__
19784a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
197997f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
198017ab2063SBarry Smith {
1981db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
19826849ba73SBarry Smith   PetscErrorCode ierr;
1983d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
198497f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
19855d0c19d7SBarry Smith   const PetscInt *irow,*icol;
19865d0c19d7SBarry Smith   PetscInt       nrows,ncols;
198797f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
198854f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1989416022c9SBarry Smith   Mat            C;
1990ace3abfcSBarry Smith   PetscBool      stride,sorted;
199117ab2063SBarry Smith 
19923a40ed3dSBarry Smith   PetscFunctionBegin;
199314ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
1994e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
199514ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
1996e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
199799141d43SSatish Balay 
199817ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1999b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2000b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
200117ab2063SBarry Smith 
2002fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2003251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2004fee21e36SBarry Smith   if (stride && step == 1) {
200502834360SBarry Smith     /* special case of contiguous rows */
20060e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
200702834360SBarry Smith     /* loop over new rows determining lens and starting points */
200802834360SBarry Smith     for (i=0; i<nrows; i++) {
2009bfeeae90SHong Zhang       kstart  = ai[irow[i]];
2010a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
201102834360SBarry Smith       for (k=kstart; k<kend; k++) {
2012bfeeae90SHong Zhang         if (aj[k] >= first) {
201302834360SBarry Smith           starts[i] = k;
201402834360SBarry Smith           break;
201502834360SBarry Smith 	}
201602834360SBarry Smith       }
2017a2744918SBarry Smith       sum = 0;
201802834360SBarry Smith       while (k < kend) {
2019bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2020a2744918SBarry Smith         sum++;
202102834360SBarry Smith       }
2022a2744918SBarry Smith       lens[i] = sum;
202302834360SBarry Smith     }
202402834360SBarry Smith     /* create submatrix */
2025cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
202697f1f81fSBarry Smith       PetscInt n_cols,n_rows;
202708480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2028e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2029d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
203008480c60SBarry Smith       C = *B;
20313a40ed3dSBarry Smith     } else {
20323bef6203SJed Brown       PetscInt rbs,cbs;
20337adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2034f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
20353bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
20363bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
20373bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
20387adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2039ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
204008480c60SBarry Smith     }
2041db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2042db02288aSLois Curfman McInnes 
204302834360SBarry Smith     /* loop over rows inserting into submatrix */
2044db02288aSLois Curfman McInnes     a_new    = c->a;
2045db02288aSLois Curfman McInnes     j_new    = c->j;
2046db02288aSLois Curfman McInnes     i_new    = c->i;
2047bfeeae90SHong Zhang 
204802834360SBarry Smith     for (i=0; i<nrows; i++) {
2049a2744918SBarry Smith       ii    = starts[i];
2050a2744918SBarry Smith       lensi = lens[i];
2051a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2052a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
205302834360SBarry Smith       }
205487828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2055a2744918SBarry Smith       a_new      += lensi;
2056a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
2057a2744918SBarry Smith       c->ilen[i]  = lensi;
205802834360SBarry Smith     }
20590e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
20603a40ed3dSBarry Smith   } else {
206102834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
20620e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
206397f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
20640e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
20654dcab191SBarry Smith     for (i=0; i<ncols; i++) {
20664dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
20674dcab191SBarry 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);
20684dcab191SBarry Smith #endif
20694dcab191SBarry Smith       smap[icol[i]] = i+1;
20704dcab191SBarry Smith     }
20714dcab191SBarry Smith 
207202834360SBarry Smith     /* determine lens of each row */
207302834360SBarry Smith     for (i=0; i<nrows; i++) {
2074bfeeae90SHong Zhang       kstart  = ai[irow[i]];
207502834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
207602834360SBarry Smith       lens[i] = 0;
207702834360SBarry Smith       for (k=kstart; k<kend; k++) {
2078bfeeae90SHong Zhang         if (smap[aj[k]]) {
207902834360SBarry Smith           lens[i]++;
208002834360SBarry Smith         }
208102834360SBarry Smith       }
208202834360SBarry Smith     }
208317ab2063SBarry Smith     /* Create and fill new matrix */
2084a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2085ace3abfcSBarry Smith       PetscBool  equal;
20860f5bd95cSBarry Smith 
208799141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
2088e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2089d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
20900f5bd95cSBarry Smith       if (!equal) {
2091e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
209299141d43SSatish Balay       }
2093d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
209408480c60SBarry Smith       C = *B;
20953a40ed3dSBarry Smith     } else {
20963bef6203SJed Brown       PetscInt rbs,cbs;
20977adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2098f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
20993bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
21003bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
21013bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
21027adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2103ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
210408480c60SBarry Smith     }
210599141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
210617ab2063SBarry Smith     for (i=0; i<nrows; i++) {
210799141d43SSatish Balay       row    = irow[i];
2108bfeeae90SHong Zhang       kstart = ai[row];
210999141d43SSatish Balay       kend   = kstart + a->ilen[row];
2110bfeeae90SHong Zhang       mat_i  = c->i[i];
211199141d43SSatish Balay       mat_j  = c->j + mat_i;
211299141d43SSatish Balay       mat_a  = c->a + mat_i;
211399141d43SSatish Balay       mat_ilen = c->ilen + i;
211417ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2115bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2116ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
211799141d43SSatish Balay           *mat_a++ = a->a[k];
211899141d43SSatish Balay           (*mat_ilen)++;
211999141d43SSatish Balay 
212017ab2063SBarry Smith         }
212117ab2063SBarry Smith       }
212217ab2063SBarry Smith     }
212302834360SBarry Smith     /* Free work space */
212402834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2125606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2126606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
212702834360SBarry Smith   }
21286d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21296d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
213017ab2063SBarry Smith 
213117ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2132416022c9SBarry Smith   *B = C;
21333a40ed3dSBarry Smith   PetscFunctionReturn(0);
213417ab2063SBarry Smith }
213517ab2063SBarry Smith 
21361df811f5SHong Zhang #undef __FUNCT__
213782d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2138fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat* subMat)
213982d44351SHong Zhang {
214082d44351SHong Zhang   PetscErrorCode ierr;
214182d44351SHong Zhang   Mat            B;
214282d44351SHong Zhang 
214382d44351SHong Zhang   PetscFunctionBegin;
214482d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
214582d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
2146a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(B,mat->rmap->bs,mat->cmap->bs); CHKERRQ(ierr);
214782d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
214882d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
214982d44351SHong Zhang   *subMat = B;
215082d44351SHong Zhang   PetscFunctionReturn(0);
215182d44351SHong Zhang }
215282d44351SHong Zhang 
215382d44351SHong Zhang #undef __FUNCT__
21544a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
21550481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2156a871dcd8SBarry Smith {
215763b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2158dfbe8321SBarry Smith   PetscErrorCode ierr;
215963b91edcSBarry Smith   Mat            outA;
2160ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
216163b91edcSBarry Smith 
21623a40ed3dSBarry Smith   PetscFunctionBegin;
2163e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
21641df811f5SHong Zhang 
2165b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2166b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2167a871dcd8SBarry Smith 
216863b91edcSBarry Smith   outA              = inA;
2169d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
2170c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
21716bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
2172c3122656SLisandro Dalcin   a->row = row;
2173c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
21746bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
2175c3122656SLisandro Dalcin   a->col = col;
217663b91edcSBarry Smith 
217736db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
21786bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
21794c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
218052e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2181f0ec6fceSSatish Balay 
218294a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2183d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2184d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
218594a9d846SBarry Smith   }
218663b91edcSBarry Smith 
2187f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2188137fb511SHong Zhang   if (row_identity && col_identity) {
2189ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2190137fb511SHong Zhang   } else {
2191719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2192137fb511SHong Zhang   }
21933a40ed3dSBarry Smith   PetscFunctionReturn(0);
2194a871dcd8SBarry Smith }
2195a871dcd8SBarry Smith 
21964a2ae208SSatish Balay #undef __FUNCT__
21974a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2198f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2199f0b747eeSBarry Smith {
2200f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2201f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2202efee365bSSatish Balay   PetscErrorCode ierr;
22030805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
22043a40ed3dSBarry Smith 
22053a40ed3dSBarry Smith   PetscFunctionBegin;
2206f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
2207efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
220886c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
220986c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
22103a40ed3dSBarry Smith   PetscFunctionReturn(0);
2211f0b747eeSBarry Smith }
2212f0b747eeSBarry Smith 
22134a2ae208SSatish Balay #undef __FUNCT__
22144a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
221597f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2216cddf8d76SBarry Smith {
2217dfbe8321SBarry Smith   PetscErrorCode ierr;
221897f1f81fSBarry Smith   PetscInt       i;
2219cddf8d76SBarry Smith 
22203a40ed3dSBarry Smith   PetscFunctionBegin;
2221cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2222b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2223cddf8d76SBarry Smith   }
2224cddf8d76SBarry Smith 
2225cddf8d76SBarry Smith   for (i=0; i<n; i++) {
22266a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2227cddf8d76SBarry Smith   }
22283a40ed3dSBarry Smith   PetscFunctionReturn(0);
2229cddf8d76SBarry Smith }
2230cddf8d76SBarry Smith 
22314a2ae208SSatish Balay #undef __FUNCT__
22324a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
223397f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
22344dcbc457SBarry Smith {
2235e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
22366849ba73SBarry Smith   PetscErrorCode ierr;
22375d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
22385d0c19d7SBarry Smith   const PetscInt *idx;
223997f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2240f1af5d2fSBarry Smith   PetscBT        table;
2241bbd702dbSSatish Balay 
22423a40ed3dSBarry Smith   PetscFunctionBegin;
2243d0f46423SBarry Smith   m     = A->rmap->n;
2244e4d965acSSatish Balay   ai    = a->i;
2245bfeeae90SHong Zhang   aj    = a->j;
22468a047759SSatish Balay 
2247e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
224806763907SSatish Balay 
224997f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
225053b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
225106763907SSatish Balay 
2252e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2253b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2254e4d965acSSatish Balay     isz  = 0;
22556831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2256e4d965acSSatish Balay 
2257e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
22584dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2259b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2260e4d965acSSatish Balay 
2261dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2262e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2263f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
22644dcbc457SBarry Smith     }
226506763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
22666bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2267e4d965acSSatish Balay 
226804a348a9SBarry Smith     k = 0;
226904a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
227004a348a9SBarry Smith       n = isz;
227106763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2272e4d965acSSatish Balay         row   = nidx[k];
2273e4d965acSSatish Balay         start = ai[row];
2274e4d965acSSatish Balay         end   = ai[row+1];
227504a348a9SBarry Smith         for (l = start; l<end ; l++){
2276efb16452SHong Zhang           val = aj[l] ;
2277f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2278e4d965acSSatish Balay         }
2279e4d965acSSatish Balay       }
2280e4d965acSSatish Balay     }
228170b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2282e4d965acSSatish Balay   }
228394bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2284606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
22853a40ed3dSBarry Smith   PetscFunctionReturn(0);
22864dcbc457SBarry Smith }
228717ab2063SBarry Smith 
22880513a670SBarry Smith /* -------------------------------------------------------------- */
22894a2ae208SSatish Balay #undef __FUNCT__
22904a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2291dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
22920513a670SBarry Smith {
22930513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
22946849ba73SBarry Smith   PetscErrorCode ierr;
22953b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
22965d0c19d7SBarry Smith   const PetscInt *row,*col;
22975d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
229856cd22aeSBarry Smith   IS             icolp,irowp;
22993b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
23003b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
23010513a670SBarry Smith 
23023a40ed3dSBarry Smith   PetscFunctionBegin;
23034c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
230456cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
23054c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
230656cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
23070513a670SBarry Smith 
23080513a670SBarry Smith   /* determine lengths of permuted rows */
230997f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
23100513a670SBarry Smith   for (i=0; i<m; i++) {
23110513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
23120513a670SBarry Smith   }
23137adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2314f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
2315a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
23167adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2317ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2318606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
23190513a670SBarry Smith 
232097f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
23210513a670SBarry Smith   for (i=0; i<m; i++) {
232232ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
23230513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2324cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
232532ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
23260513a670SBarry Smith   }
2327606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
23283c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
23290513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23300513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
233156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
233256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
23336bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
23346bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
23353a40ed3dSBarry Smith   PetscFunctionReturn(0);
23360513a670SBarry Smith }
23370513a670SBarry Smith 
23384a2ae208SSatish Balay #undef __FUNCT__
23394a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2340dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2341cb5b572fSBarry Smith {
2342dfbe8321SBarry Smith   PetscErrorCode ierr;
2343cb5b572fSBarry Smith 
2344cb5b572fSBarry Smith   PetscFunctionBegin;
234533f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
234633f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2347be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2348be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2349be6bf707SBarry Smith 
2350700c5bfcSBarry 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");
2351d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2352cb5b572fSBarry Smith   } else {
2353cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2354cb5b572fSBarry Smith   }
2355cb5b572fSBarry Smith   PetscFunctionReturn(0);
2356cb5b572fSBarry Smith }
2357cb5b572fSBarry Smith 
23584a2ae208SSatish Balay #undef __FUNCT__
23594994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
23604994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2361273d9f13SBarry Smith {
2362dfbe8321SBarry Smith   PetscErrorCode ierr;
2363273d9f13SBarry Smith 
2364273d9f13SBarry Smith   PetscFunctionBegin;
2365ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2366273d9f13SBarry Smith   PetscFunctionReturn(0);
2367273d9f13SBarry Smith }
2368273d9f13SBarry Smith 
23694a2ae208SSatish Balay #undef __FUNCT__
23704a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2371a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
23726c0721eeSBarry Smith {
23736c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
23746c0721eeSBarry Smith   PetscFunctionBegin;
23756c0721eeSBarry Smith   *array = a->a;
23766c0721eeSBarry Smith   PetscFunctionReturn(0);
23776c0721eeSBarry Smith }
23786c0721eeSBarry Smith 
23794a2ae208SSatish Balay #undef __FUNCT__
23804a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2381dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
23826c0721eeSBarry Smith {
23836c0721eeSBarry Smith   PetscFunctionBegin;
23846c0721eeSBarry Smith   PetscFunctionReturn(0);
23856c0721eeSBarry Smith }
2386273d9f13SBarry Smith 
2387ee4f033dSBarry Smith #undef __FUNCT__
2388ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2389dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2390ee4f033dSBarry Smith {
23916849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
23926849ba73SBarry Smith   PetscErrorCode ierr;
23934e269d77SPeter Brune   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow;
2394efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
239587828ca2SBarry Smith   PetscScalar    *vscale_array;
2396ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2397ee4f033dSBarry Smith   Vec            w1,w2,w3;
2398ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2399ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2400ee4f033dSBarry Smith 
2401ee4f033dSBarry Smith   PetscFunctionBegin;
2402ee4f033dSBarry Smith   if (!coloring->w1) {
2403ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
240452e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2405ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
240652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2407ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
240852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2409ee4f033dSBarry Smith   }
2410ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2411ee4f033dSBarry Smith 
2412ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2413acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2414ee4f033dSBarry Smith   if (flg) {
2415ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2416ee4f033dSBarry Smith   } else {
2417ace3abfcSBarry Smith     PetscBool  assembled;
24180b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
24190b9b6f31SBarry Smith     if (assembled) {
2420ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2421ee4f033dSBarry Smith     }
24220b9b6f31SBarry Smith   }
2423ee4f033dSBarry Smith 
2424ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2425ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2426ee4f033dSBarry Smith 
24274e269d77SPeter Brune   if (!coloring->fset) {
242866f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2429ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
243066f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
24314e269d77SPeter Brune   } else {
24324e269d77SPeter Brune     coloring->fset = PETSC_FALSE;
2433ee4f033dSBarry Smith   }
2434ee4f033dSBarry Smith 
2435ee4f033dSBarry Smith   /*
2436ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2437ee4f033dSBarry Smith   */
24381ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
24391ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2440ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2441ee4f033dSBarry Smith     /*
2442ee4f033dSBarry Smith        Loop over each column associated with color adding the
2443ee4f033dSBarry Smith        perturbation to the vector w3.
2444ee4f033dSBarry Smith     */
2445ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2446ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2447ee4f033dSBarry Smith       dx  = xx[col];
2448ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2449ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2450ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2451ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2452ee4f033dSBarry Smith #else
2453ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2454ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2455ee4f033dSBarry Smith #endif
2456ee4f033dSBarry Smith       dx                *= epsilon;
2457ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2458ee4f033dSBarry Smith     }
2459ee4f033dSBarry Smith   }
24601ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2461ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2462ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2463ee4f033dSBarry Smith 
2464ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2465ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2466ee4f033dSBarry Smith 
2467ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2468ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2469ee4f033dSBarry Smith 
24701ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2471ee4f033dSBarry Smith   /*
2472ee4f033dSBarry Smith       Loop over each color
2473ee4f033dSBarry Smith   */
2474ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
247549b058dcSBarry Smith     coloring->currentcolor = k;
2476ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
24771ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2478ee4f033dSBarry Smith     /*
2479ee4f033dSBarry Smith        Loop over each column associated with color adding the
2480ee4f033dSBarry Smith        perturbation to the vector w3.
2481ee4f033dSBarry Smith     */
2482ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2483ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2484ee4f033dSBarry Smith       dx  = xx[col];
24855b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2486ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2487ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2488ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2489ee4f033dSBarry Smith #else
2490ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2491ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2492ee4f033dSBarry Smith #endif
2493ee4f033dSBarry Smith       dx            *= epsilon;
2494e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2495ee4f033dSBarry Smith       w3_array[col] += dx;
2496ee4f033dSBarry Smith     }
24971ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2498ee4f033dSBarry Smith 
2499ee4f033dSBarry Smith     /*
2500ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2501ee4f033dSBarry Smith     */
2502ee4f033dSBarry Smith 
250366f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2504ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
250566f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2506efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2507ee4f033dSBarry Smith 
2508ee4f033dSBarry Smith     /*
2509ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2510ee4f033dSBarry Smith     */
25111ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2512ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2513ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2514ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2515ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2516ee4f033dSBarry Smith       srow   = row + start;
2517ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2518ee4f033dSBarry Smith     }
25191ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2520ee4f033dSBarry Smith   }
252149b058dcSBarry Smith   coloring->currentcolor = k;
25221ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
25231ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2524ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2525ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2526ee4f033dSBarry Smith   PetscFunctionReturn(0);
2527ee4f033dSBarry Smith }
2528ee4f033dSBarry Smith 
25298229c054SShri Abhyankar /*
25308229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
25318229c054SShri Abhyankar    have different nonzero structure.
25328229c054SShri Abhyankar */
2533ac90fabeSBarry Smith #undef __FUNCT__
25348229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
25358229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2536ec7775f6SShri Abhyankar {
25378229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2538ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2539ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2540ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2541ec7775f6SShri Abhyankar 
2542ec7775f6SShri Abhyankar   PetscFunctionBegin;
2543ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2544ec7775f6SShri Abhyankar   for(i=0; i<m; i++) {
25458af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
25468af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
25478af7cee1SJed Brown     nnz[i] = 0;
25488af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
25498af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
25508af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
25518af7cee1SJed Brown       nnz[i]++;
25528af7cee1SJed Brown     }
25538af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2554ec7775f6SShri Abhyankar   }
2555ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2556ec7775f6SShri Abhyankar }
2557ec7775f6SShri Abhyankar 
2558ec7775f6SShri Abhyankar #undef __FUNCT__
2559ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2560f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2561ac90fabeSBarry Smith {
2562dfbe8321SBarry Smith   PetscErrorCode ierr;
256397f1f81fSBarry Smith   PetscInt       i;
2564ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
25650805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2566ac90fabeSBarry Smith 
2567ac90fabeSBarry Smith   PetscFunctionBegin;
2568ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2569f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2570f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
257186c113feSBarry Smith     y->idiagvalid  = PETSC_FALSE;
257286c113feSBarry Smith     y->ibdiagvalid = PETSC_FALSE;
2573c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2574a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2575a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
25766bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2577a30b2313SHong Zhang     }
2578a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2579d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2580a30b2313SHong Zhang       y->XtoY = X;
2581407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2582c537a176SHong Zhang     }
2583f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2584118727c9SMark 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);
2585ac90fabeSBarry Smith   } else {
25868229c054SShri Abhyankar     Mat      B;
25878229c054SShri Abhyankar     PetscInt *nnz;
258816b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2589ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2590bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
25914aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2592a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr);
2593176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
25948229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2595ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2596ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2597ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
25988229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2599ac90fabeSBarry Smith   }
2600ac90fabeSBarry Smith   PetscFunctionReturn(0);
2601ac90fabeSBarry Smith }
2602ac90fabeSBarry Smith 
2603521d7252SBarry Smith #undef __FUNCT__
2604354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
26057087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2606354c94deSBarry Smith {
2607354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2608354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2609354c94deSBarry Smith   PetscInt    i,nz;
2610354c94deSBarry Smith   PetscScalar *a;
2611354c94deSBarry Smith 
2612354c94deSBarry Smith   PetscFunctionBegin;
2613354c94deSBarry Smith   nz = aij->nz;
2614354c94deSBarry Smith   a  = aij->a;
2615354c94deSBarry Smith   for (i=0; i<nz; i++) {
2616354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2617354c94deSBarry Smith   }
2618354c94deSBarry Smith #else
2619354c94deSBarry Smith   PetscFunctionBegin;
2620354c94deSBarry Smith #endif
2621354c94deSBarry Smith   PetscFunctionReturn(0);
2622354c94deSBarry Smith }
2623354c94deSBarry Smith 
2624e34fafa9SBarry Smith #undef __FUNCT__
2625985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2626985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2627e34fafa9SBarry Smith {
2628e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2629e34fafa9SBarry Smith   PetscErrorCode ierr;
2630d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2631e34fafa9SBarry Smith   PetscReal      atmp;
2632985db425SBarry Smith   PetscScalar    *x;
2633e34fafa9SBarry Smith   MatScalar      *aa;
2634e34fafa9SBarry Smith 
2635e34fafa9SBarry Smith   PetscFunctionBegin;
2636e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2637e34fafa9SBarry Smith   aa   = a->a;
2638e34fafa9SBarry Smith   ai   = a->i;
2639e34fafa9SBarry Smith   aj   = a->j;
2640e34fafa9SBarry Smith 
2641985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2642e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2643e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2644e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2645e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2646e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
26479189402eSHong Zhang     x[i] = 0.0;
2648e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2649985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2650985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2651985db425SBarry Smith       aa++; aj++;
2652985db425SBarry Smith     }
2653985db425SBarry Smith   }
2654985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2655985db425SBarry Smith   PetscFunctionReturn(0);
2656985db425SBarry Smith }
2657985db425SBarry Smith 
2658985db425SBarry Smith #undef __FUNCT__
2659985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2660985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2661985db425SBarry Smith {
2662985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2663985db425SBarry Smith   PetscErrorCode ierr;
2664d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2665985db425SBarry Smith   PetscScalar    *x;
2666985db425SBarry Smith   MatScalar      *aa;
2667985db425SBarry Smith 
2668985db425SBarry Smith   PetscFunctionBegin;
2669e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2670985db425SBarry Smith   aa   = a->a;
2671985db425SBarry Smith   ai   = a->i;
2672985db425SBarry Smith   aj   = a->j;
2673985db425SBarry Smith 
2674985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2675985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2676985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2677e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2678985db425SBarry Smith   for (i=0; i<m; i++) {
2679985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2680d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2681985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2682985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2683985db425SBarry Smith       x[i] = 0.0;
2684985db425SBarry Smith       if (idx) {
2685985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2686985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2687985db425SBarry Smith           if (aj[j] > j) {
2688985db425SBarry Smith             idx[i] = j;
2689985db425SBarry Smith             break;
2690985db425SBarry Smith           }
2691985db425SBarry Smith         }
2692985db425SBarry Smith       }
2693985db425SBarry Smith     }
2694985db425SBarry Smith     for (j=0; j<ncols; j++){
2695985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2696985db425SBarry Smith       aa++; aj++;
2697985db425SBarry Smith     }
2698985db425SBarry Smith   }
2699985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2700985db425SBarry Smith   PetscFunctionReturn(0);
2701985db425SBarry Smith }
2702985db425SBarry Smith 
2703985db425SBarry Smith #undef __FUNCT__
2704c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2705c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2706c87e5d42SMatthew Knepley {
2707c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2708c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2709c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2710c87e5d42SMatthew Knepley   PetscReal      atmp;
2711c87e5d42SMatthew Knepley   PetscScalar    *x;
2712c87e5d42SMatthew Knepley   MatScalar      *aa;
2713c87e5d42SMatthew Knepley 
2714c87e5d42SMatthew Knepley   PetscFunctionBegin;
2715e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2716c87e5d42SMatthew Knepley   aa   = a->a;
2717c87e5d42SMatthew Knepley   ai   = a->i;
2718c87e5d42SMatthew Knepley   aj   = a->j;
2719c87e5d42SMatthew Knepley 
2720c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2721c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2722c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
27233bb78c5cSMatthew 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);
2724c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2725c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2726289a08f5SMatthew Knepley     if (ncols) {
2727289a08f5SMatthew Knepley       /* Get first nonzero */
2728289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2729289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2730289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2731289a08f5SMatthew Knepley       }
273212431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2733289a08f5SMatthew Knepley     } else {
2734289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2735289a08f5SMatthew Knepley     }
2736c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2737c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2738289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2739c87e5d42SMatthew Knepley       aa++; aj++;
2740c87e5d42SMatthew Knepley     }
2741c87e5d42SMatthew Knepley   }
2742c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2743c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2744c87e5d42SMatthew Knepley }
2745c87e5d42SMatthew Knepley 
2746c87e5d42SMatthew Knepley #undef __FUNCT__
2747985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2748985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2749985db425SBarry Smith {
2750985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2751985db425SBarry Smith   PetscErrorCode ierr;
2752d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2753985db425SBarry Smith   PetscScalar    *x;
2754985db425SBarry Smith   MatScalar      *aa;
2755985db425SBarry Smith 
2756985db425SBarry Smith   PetscFunctionBegin;
2757e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2758985db425SBarry Smith   aa   = a->a;
2759985db425SBarry Smith   ai   = a->i;
2760985db425SBarry Smith   aj   = a->j;
2761985db425SBarry Smith 
2762985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2763985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2764985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2765e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2766985db425SBarry Smith   for (i=0; i<m; i++) {
2767985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2768d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2769985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2770985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2771985db425SBarry Smith       x[i] = 0.0;
2772985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2773985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2774985db425SBarry Smith         for (j=0;j<ncols;j++) {
2775985db425SBarry Smith           if (aj[j] > j) {
2776985db425SBarry Smith             idx[i] = j;
2777985db425SBarry Smith             break;
2778985db425SBarry Smith           }
2779985db425SBarry Smith         }
2780985db425SBarry Smith       }
2781985db425SBarry Smith     }
2782985db425SBarry Smith     for (j=0; j<ncols; j++){
2783985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2784985db425SBarry Smith       aa++; aj++;
2785e34fafa9SBarry Smith     }
2786e34fafa9SBarry Smith   }
2787e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2788e34fafa9SBarry Smith   PetscFunctionReturn(0);
2789e34fafa9SBarry Smith }
2790bbead8a2SBarry Smith 
2791bbead8a2SBarry Smith #include <petscblaslapack.h>
2792bbead8a2SBarry Smith #include <../src/mat/blockinvert.h>
2793bbead8a2SBarry Smith 
2794bbead8a2SBarry Smith #undef __FUNCT__
2795bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
2796713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
2797bbead8a2SBarry Smith {
2798bbead8a2SBarry Smith   Mat_SeqAIJ    *a = (Mat_SeqAIJ*) A->data;
2799bbead8a2SBarry Smith   PetscErrorCode ierr;
280034fc4b71SJed 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;
2801bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
2802bbead8a2SBarry Smith   PetscReal      shift = 0.0;
2803bbead8a2SBarry Smith 
2804bbead8a2SBarry Smith   PetscFunctionBegin;
28054a0d0026SBarry Smith   if (a->ibdiagvalid) {
28064a0d0026SBarry Smith     if (values) *values = a->ibdiag;
28074a0d0026SBarry Smith     PetscFunctionReturn(0);
28084a0d0026SBarry Smith   }
2809bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
2810bbead8a2SBarry Smith   if (!a->ibdiag) {
2811bbead8a2SBarry Smith     ierr = PetscMalloc(bs2*mbs*sizeof(PetscScalar),&a->ibdiag);CHKERRQ(ierr);
2812bbead8a2SBarry Smith     ierr = PetscLogObjectMemory(A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
2813bbead8a2SBarry Smith   }
2814bbead8a2SBarry Smith   diag    = a->ibdiag;
2815bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
2816bbead8a2SBarry Smith   /* factor and invert each block */
2817bbead8a2SBarry Smith   switch (bs){
2818bbead8a2SBarry Smith     case 1:
2819bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2820bbead8a2SBarry Smith         ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
2821bbead8a2SBarry Smith         diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
2822bbead8a2SBarry Smith       }
2823bbead8a2SBarry Smith       break;
2824bbead8a2SBarry Smith     case 2:
2825bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2826bbead8a2SBarry Smith         ij[0] = 2*i; ij[1] = 2*i + 1;
2827bbead8a2SBarry Smith         ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
282896b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
282996b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
2830bbead8a2SBarry Smith 	diag  += 4;
2831bbead8a2SBarry Smith       }
2832bbead8a2SBarry Smith       break;
2833bbead8a2SBarry Smith     case 3:
2834bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2835bbead8a2SBarry Smith         ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
2836bbead8a2SBarry Smith         ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
283796b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
283896b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
2839bbead8a2SBarry Smith 	diag    += 9;
2840bbead8a2SBarry Smith       }
2841bbead8a2SBarry Smith       break;
2842bbead8a2SBarry Smith     case 4:
2843bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2844bbead8a2SBarry Smith         ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
2845bbead8a2SBarry Smith         ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
284696b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
284796b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
2848bbead8a2SBarry Smith 	diag  += 16;
2849bbead8a2SBarry Smith       }
2850bbead8a2SBarry Smith       break;
2851bbead8a2SBarry Smith     case 5:
2852bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2853bbead8a2SBarry 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;
2854bbead8a2SBarry Smith         ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
285596b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
285696b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
2857bbead8a2SBarry Smith 	diag  += 25;
2858bbead8a2SBarry Smith       }
2859bbead8a2SBarry Smith       break;
2860bbead8a2SBarry Smith     case 6:
2861bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2862bbead8a2SBarry 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;
2863bbead8a2SBarry Smith         ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
286496b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
286596b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
2866bbead8a2SBarry Smith 	diag  += 36;
2867bbead8a2SBarry Smith       }
2868bbead8a2SBarry Smith       break;
2869bbead8a2SBarry Smith     case 7:
2870bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2871bbead8a2SBarry 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;
2872bbead8a2SBarry Smith         ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
287396b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
287496b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
2875bbead8a2SBarry Smith 	diag  += 49;
2876bbead8a2SBarry Smith       }
2877bbead8a2SBarry Smith       break;
2878bbead8a2SBarry Smith     default:
2879bbead8a2SBarry Smith       ierr = PetscMalloc3(bs,MatScalar,&v_work,bs,PetscInt,&v_pivots,bs,PetscInt,&IJ);CHKERRQ(ierr);
2880bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2881bbead8a2SBarry Smith         for (j=0; j<bs; j++) {
2882bbead8a2SBarry Smith           IJ[j] = bs*i + j;
2883bbead8a2SBarry Smith         }
2884bbead8a2SBarry Smith         ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
288596b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
288696b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
2887bbead8a2SBarry Smith 	diag  += bs2;
2888bbead8a2SBarry Smith       }
2889bbead8a2SBarry Smith       ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
2890bbead8a2SBarry Smith   }
2891bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
2892bbead8a2SBarry Smith   PetscFunctionReturn(0);
2893bbead8a2SBarry Smith }
2894bbead8a2SBarry Smith 
28957087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2896682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
28970a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2898cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2899cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2900cb5b572fSBarry Smith        MatMult_SeqAIJ,
290197304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
29027c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
29037c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2904db4efbfdSBarry Smith        0,
2905db4efbfdSBarry Smith        0,
2906db4efbfdSBarry Smith        0,
2907db4efbfdSBarry Smith /*10*/ 0,
2908cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2909cb5b572fSBarry Smith        0,
291041f059aeSBarry Smith        MatSOR_SeqAIJ,
291117ab2063SBarry Smith        MatTranspose_SeqAIJ,
291297304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2913cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2914cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2915cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2916cb5b572fSBarry Smith        MatNorm_SeqAIJ,
291797304618SKris Buschelman /*20*/ 0,
2918cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2919cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2920cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2921d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2922db4efbfdSBarry Smith        0,
2923db4efbfdSBarry Smith        0,
2924db4efbfdSBarry Smith        0,
2925db4efbfdSBarry Smith        0,
29264994cf47SJed Brown /*29*/ MatSetUp_SeqAIJ,
2927db4efbfdSBarry Smith        0,
2928db4efbfdSBarry Smith        0,
29296c0721eeSBarry Smith        MatGetArray_SeqAIJ,
29306c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2931d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2932cb5b572fSBarry Smith        0,
2933cb5b572fSBarry Smith        0,
2934cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2935cb5b572fSBarry Smith        0,
2936d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2937cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2938cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2939cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2940cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2941d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2942cb5b572fSBarry Smith        MatScale_SeqAIJ,
2943cb5b572fSBarry Smith        0,
294479299369SBarry Smith        MatDiagonalSet_SeqAIJ,
29456e169961SBarry Smith        MatZeroRowsColumns_SeqAIJ,
2946f73d5cc4SBarry Smith /*49*/ 0,
29473b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
29483b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
29493b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2950a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2951d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2952b9617806SBarry Smith        0,
29530513a670SBarry Smith        0,
2954cda55fadSBarry Smith        MatPermute_SeqAIJ,
2955cda55fadSBarry Smith        0,
2956d519adbfSMatthew Knepley /*59*/ 0,
2957b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2958b9b97703SBarry Smith        MatView_SeqAIJ,
2959357abbc8SBarry Smith        0,
2960ee4f033dSBarry Smith        0,
2961d519adbfSMatthew Knepley /*64*/ 0,
2962ee4f033dSBarry Smith        0,
2963ee4f033dSBarry Smith        0,
2964ee4f033dSBarry Smith        0,
2965ee4f033dSBarry Smith        0,
2966d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2967c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2968ee4f033dSBarry Smith        0,
2969ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2970dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2971ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2972dcf5cc72SBarry Smith #else
2973dcf5cc72SBarry Smith        0,
2974dcf5cc72SBarry Smith #endif
2975d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
29763acb8795SBarry Smith        MatFDColoringApply_AIJ,
297797304618SKris Buschelman        0,
297897304618SKris Buschelman        0,
297997304618SKris Buschelman        0,
29806ce1633cSBarry Smith /*79*/ MatFindZeroDiagonals_SeqAIJ,
298197304618SKris Buschelman        0,
298297304618SKris Buschelman        0,
298397304618SKris Buschelman        0,
2984bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2985d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
29861cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
29876284ec50SHong Zhang        0,
29886284ec50SHong Zhang        0,
2989bc011b1eSHong Zhang        0,
2990d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
299126be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
299226be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2993d439da42SKris Buschelman        MatPtAP_Basic,
29947ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2995d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
29966fc122caSHong Zhang        MatMatTransposeMult_SeqAIJ_SeqAIJ,
29976fc122caSHong Zhang        MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
29986fc122caSHong Zhang        MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
29997ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
3000d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
3001609c6c4dSKris Buschelman        0,
3002609c6c4dSKris Buschelman        0,
300387d4246cSBarry Smith        MatConjugate_SeqAIJ,
300487d4246cSBarry Smith        0,
3005d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
300699cafbc1SBarry Smith        MatRealPart_SeqAIJ,
3007f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
3008f5edf698SHong Zhang        0,
30092bebee5dSHong Zhang        0,
3010cbd44569SHong Zhang /*109*/MatMatSolve_SeqAIJ,
3011985db425SBarry Smith        0,
30122af78befSBarry Smith        MatGetRowMin_SeqAIJ,
30132af78befSBarry Smith        0,
3014599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
3015d519adbfSMatthew Knepley /*114*/0,
3016599ef60dSHong Zhang        0,
30173c2a7987SHong Zhang        0,
3018fe97e370SBarry Smith        0,
3019fbdbba38SShri Abhyankar        0,
3020fbdbba38SShri Abhyankar /*119*/0,
3021fbdbba38SShri Abhyankar        0,
3022fbdbba38SShri Abhyankar        0,
302382d44351SHong Zhang        0,
3024b3a44c85SBarry Smith        MatGetMultiProcBlock_SeqAIJ,
30250716a85fSBarry Smith /*124*/MatFindNonzeroRows_SeqAIJ,
3026bbead8a2SBarry Smith        MatGetColumnNorms_SeqAIJ,
302737868618SMatthew G Knepley        MatInvertBlockDiagonal_SeqAIJ,
302837868618SMatthew G Knepley        0,
302937868618SMatthew G Knepley        0,
30305df89d91SHong Zhang /*129*/0,
303175648e8dSHong Zhang        MatTransposeMatMult_SeqAIJ_SeqAIJ,
303275648e8dSHong Zhang        MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
303375648e8dSHong Zhang        MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3034b9af6bddSHong Zhang        MatTransposeColoringCreate_SeqAIJ,
3035b9af6bddSHong Zhang /*134*/MatTransColoringApplySpToDen_SeqAIJ,
30362b8ad9a3SHong Zhang        MatTransColoringApplyDenToSp_SeqAIJ,
30372b8ad9a3SHong Zhang        MatRARt_SeqAIJ_SeqAIJ,
30382b8ad9a3SHong Zhang        MatRARtSymbolic_SeqAIJ_SeqAIJ,
30392b8ad9a3SHong Zhang        MatRARtNumeric_SeqAIJ_SeqAIJ
30409e29f15eSvictorle };
304117ab2063SBarry Smith 
3042fb2e594dSBarry Smith EXTERN_C_BEGIN
30434a2ae208SSatish Balay #undef __FUNCT__
30444a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
30457087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3046bef8e0ddSBarry Smith {
3047bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
304897f1f81fSBarry Smith   PetscInt   i,nz,n;
3049bef8e0ddSBarry Smith 
3050bef8e0ddSBarry Smith   PetscFunctionBegin;
3051bef8e0ddSBarry Smith 
3052bef8e0ddSBarry Smith   nz = aij->maxnz;
3053d0f46423SBarry Smith   n  = mat->rmap->n;
3054bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3055bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3056bef8e0ddSBarry Smith   }
3057bef8e0ddSBarry Smith   aij->nz = nz;
3058bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3059bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3060bef8e0ddSBarry Smith   }
3061bef8e0ddSBarry Smith 
3062bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3063bef8e0ddSBarry Smith }
3064fb2e594dSBarry Smith EXTERN_C_END
3065bef8e0ddSBarry Smith 
30664a2ae208SSatish Balay #undef __FUNCT__
30674a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3068bef8e0ddSBarry Smith /*@
3069bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3070bef8e0ddSBarry Smith        in the matrix.
3071bef8e0ddSBarry Smith 
3072bef8e0ddSBarry Smith   Input Parameters:
3073bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3074bef8e0ddSBarry Smith -  indices - the column indices
3075bef8e0ddSBarry Smith 
307615091d37SBarry Smith   Level: advanced
307715091d37SBarry Smith 
3078bef8e0ddSBarry Smith   Notes:
3079bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3080bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3081bef8e0ddSBarry Smith   of the MatSetValues() operation.
3082bef8e0ddSBarry Smith 
3083bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3084d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3085bef8e0ddSBarry Smith 
3086bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3087bef8e0ddSBarry Smith 
3088b9617806SBarry Smith     The indices should start with zero, not one.
3089b9617806SBarry Smith 
3090bef8e0ddSBarry Smith @*/
30917087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3092bef8e0ddSBarry Smith {
30934ac538c5SBarry Smith   PetscErrorCode ierr;
3094bef8e0ddSBarry Smith 
3095bef8e0ddSBarry Smith   PetscFunctionBegin;
30960700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
30974482741eSBarry Smith   PetscValidPointer(indices,2);
30984ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
3099bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3100bef8e0ddSBarry Smith }
3101bef8e0ddSBarry Smith 
3102be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3103be6bf707SBarry Smith 
3104fb2e594dSBarry Smith EXTERN_C_BEGIN
31054a2ae208SSatish Balay #undef __FUNCT__
31064a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
31077087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3108be6bf707SBarry Smith {
3109be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
31106849ba73SBarry Smith   PetscErrorCode ierr;
3111d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3112be6bf707SBarry Smith 
3113be6bf707SBarry Smith   PetscFunctionBegin;
3114be6bf707SBarry Smith   if (aij->nonew != 1) {
3115e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3116be6bf707SBarry Smith   }
3117be6bf707SBarry Smith 
3118be6bf707SBarry Smith   /* allocate space for values if not already there */
3119be6bf707SBarry Smith   if (!aij->saved_values) {
312087828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
31219518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3122be6bf707SBarry Smith   }
3123be6bf707SBarry Smith 
3124be6bf707SBarry Smith   /* copy values over */
312587828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3126be6bf707SBarry Smith   PetscFunctionReturn(0);
3127be6bf707SBarry Smith }
3128fb2e594dSBarry Smith EXTERN_C_END
3129be6bf707SBarry Smith 
31304a2ae208SSatish Balay #undef __FUNCT__
3131b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3132be6bf707SBarry Smith /*@
3133be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3134be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3135be6bf707SBarry Smith        nonlinear portion.
3136be6bf707SBarry Smith 
3137be6bf707SBarry Smith    Collect on Mat
3138be6bf707SBarry Smith 
3139be6bf707SBarry Smith   Input Parameters:
31400e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3141be6bf707SBarry Smith 
314215091d37SBarry Smith   Level: advanced
314315091d37SBarry Smith 
3144be6bf707SBarry Smith   Common Usage, with SNESSolve():
3145be6bf707SBarry Smith $    Create Jacobian matrix
3146be6bf707SBarry Smith $    Set linear terms into matrix
3147be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3148be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3149be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3150512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3151be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3152be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3153be6bf707SBarry Smith $    In your Jacobian routine
3154be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3155be6bf707SBarry Smith $      Set nonlinear terms in matrix
3156be6bf707SBarry Smith 
3157be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3158be6bf707SBarry Smith $    // build linear portion of Jacobian
3159512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3160be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3161be6bf707SBarry Smith $    loop over nonlinear iterations
3162be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3163be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3164be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3165be6bf707SBarry Smith $       Solve linear system with Jacobian
3166be6bf707SBarry Smith $    endloop
3167be6bf707SBarry Smith 
3168be6bf707SBarry Smith   Notes:
3169be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3170512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3171be6bf707SBarry Smith     calling this routine.
3172be6bf707SBarry Smith 
31730c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
31740c468ba9SBarry Smith     and does not allocated additional space.
31750c468ba9SBarry Smith 
3176be6bf707SBarry Smith .seealso: MatRetrieveValues()
3177be6bf707SBarry Smith 
3178be6bf707SBarry Smith @*/
31797087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3180be6bf707SBarry Smith {
31814ac538c5SBarry Smith   PetscErrorCode ierr;
3182be6bf707SBarry Smith 
3183be6bf707SBarry Smith   PetscFunctionBegin;
31840700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3185e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3186e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
31874ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3188be6bf707SBarry Smith   PetscFunctionReturn(0);
3189be6bf707SBarry Smith }
3190be6bf707SBarry Smith 
3191fb2e594dSBarry Smith EXTERN_C_BEGIN
31924a2ae208SSatish Balay #undef __FUNCT__
31934a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
31947087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3195be6bf707SBarry Smith {
3196be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
31976849ba73SBarry Smith   PetscErrorCode ierr;
3198d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3199be6bf707SBarry Smith 
3200be6bf707SBarry Smith   PetscFunctionBegin;
3201be6bf707SBarry Smith   if (aij->nonew != 1) {
3202e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3203be6bf707SBarry Smith   }
3204be6bf707SBarry Smith   if (!aij->saved_values) {
3205e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3206be6bf707SBarry Smith   }
3207be6bf707SBarry Smith   /* copy values over */
320887828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3209be6bf707SBarry Smith   PetscFunctionReturn(0);
3210be6bf707SBarry Smith }
3211fb2e594dSBarry Smith EXTERN_C_END
3212be6bf707SBarry Smith 
32134a2ae208SSatish Balay #undef __FUNCT__
32144a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3215be6bf707SBarry Smith /*@
3216be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3217be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3218be6bf707SBarry Smith        nonlinear portion.
3219be6bf707SBarry Smith 
3220be6bf707SBarry Smith    Collect on Mat
3221be6bf707SBarry Smith 
3222be6bf707SBarry Smith   Input Parameters:
3223be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3224be6bf707SBarry Smith 
322515091d37SBarry Smith   Level: advanced
322615091d37SBarry Smith 
3227be6bf707SBarry Smith .seealso: MatStoreValues()
3228be6bf707SBarry Smith 
3229be6bf707SBarry Smith @*/
32307087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3231be6bf707SBarry Smith {
32324ac538c5SBarry Smith   PetscErrorCode ierr;
3233be6bf707SBarry Smith 
3234be6bf707SBarry Smith   PetscFunctionBegin;
32350700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3236e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3237e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
32384ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3239be6bf707SBarry Smith   PetscFunctionReturn(0);
3240be6bf707SBarry Smith }
3241be6bf707SBarry Smith 
3242f83d6046SBarry Smith 
3243be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
32444a2ae208SSatish Balay #undef __FUNCT__
32454a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
324617ab2063SBarry Smith /*@C
3247682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
32480d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
32496e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
325051c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
32512bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
325217ab2063SBarry Smith 
3253db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3254db81eaa0SLois Curfman McInnes 
325517ab2063SBarry Smith    Input Parameters:
3256db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
325717ab2063SBarry Smith .  m - number of rows
325817ab2063SBarry Smith .  n - number of columns
325917ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
326051c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
32612bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
326217ab2063SBarry Smith 
326317ab2063SBarry Smith    Output Parameter:
3264416022c9SBarry Smith .  A - the matrix
326517ab2063SBarry Smith 
3266175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3267ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3268175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3269175b88e8SBarry Smith 
3270b259b22eSLois Curfman McInnes    Notes:
327149a6f317SBarry Smith    If nnz is given then nz is ignored
327249a6f317SBarry Smith 
327317ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
327417ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
32750002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
327644cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
327717ab2063SBarry Smith 
327817ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3279a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
32803d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
32816da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
328217ab2063SBarry Smith 
3283682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
32844fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3285682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
32866c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
32876c7ebb05SLois Curfman McInnes 
32886c7ebb05SLois Curfman McInnes    Options Database Keys:
3289698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
32909db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
329117ab2063SBarry Smith 
3292027ccd11SLois Curfman McInnes    Level: intermediate
3293027ccd11SLois Curfman McInnes 
329469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
329536db0b34SBarry Smith 
329617ab2063SBarry Smith @*/
32977087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
329817ab2063SBarry Smith {
3299dfbe8321SBarry Smith   PetscErrorCode ierr;
33006945ee14SBarry Smith 
33013a40ed3dSBarry Smith   PetscFunctionBegin;
3302f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3303117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3304c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3305d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3306273d9f13SBarry Smith   PetscFunctionReturn(0);
3307273d9f13SBarry Smith }
3308273d9f13SBarry Smith 
33094a2ae208SSatish Balay #undef __FUNCT__
33104a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3311273d9f13SBarry Smith /*@C
3312273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3313273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3314273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3315273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3316273d9f13SBarry Smith 
3317273d9f13SBarry Smith    Collective on MPI_Comm
3318273d9f13SBarry Smith 
3319273d9f13SBarry Smith    Input Parameters:
3320117016b1SBarry Smith +  B - The matrix-free
3321273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3322273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
3323273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
3324273d9f13SBarry Smith 
3325273d9f13SBarry Smith    Notes:
332649a6f317SBarry Smith      If nnz is given then nz is ignored
332749a6f317SBarry Smith 
3328273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3329273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3330273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3331273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3332273d9f13SBarry Smith 
3333273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3334273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3335273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3336273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3337273d9f13SBarry Smith 
3338aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3339aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3340aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3341aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3342aa95bbe8SBarry Smith 
3343a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3344a96a251dSBarry Smith    entries or columns indices
3345a96a251dSBarry Smith 
3346273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3347273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3348273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3349273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3350273d9f13SBarry Smith 
3351273d9f13SBarry Smith    Options Database Keys:
3352698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3353698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3354273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3355273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3356273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3357273d9f13SBarry Smith 
3358273d9f13SBarry Smith    Level: intermediate
3359273d9f13SBarry Smith 
336069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3361273d9f13SBarry Smith 
3362273d9f13SBarry Smith @*/
33637087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3364273d9f13SBarry Smith {
33654ac538c5SBarry Smith   PetscErrorCode ierr;
3366a23d5eceSKris Buschelman 
3367a23d5eceSKris Buschelman   PetscFunctionBegin;
33686ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
33696ba663aaSJed Brown   PetscValidType(B,1);
33704ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3371a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3372a23d5eceSKris Buschelman }
3373a23d5eceSKris Buschelman 
3374a23d5eceSKris Buschelman EXTERN_C_BEGIN
3375a23d5eceSKris Buschelman #undef __FUNCT__
3376a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
33777087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3378a23d5eceSKris Buschelman {
3379273d9f13SBarry Smith   Mat_SeqAIJ     *b;
33802576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
33816849ba73SBarry Smith   PetscErrorCode ierr;
338297f1f81fSBarry Smith   PetscInt       i;
3383273d9f13SBarry Smith 
3384273d9f13SBarry Smith   PetscFunctionBegin;
33852576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3386a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3387c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3388c461c341SBarry Smith     nz             = 0;
3389c461c341SBarry Smith   }
3390c461c341SBarry Smith 
339126283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
339226283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3393899cda47SBarry Smith 
3394435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3395e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3396b73539f3SBarry Smith   if (nnz) {
3397d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3398e32f2f54SBarry 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]);
3399e32f2f54SBarry 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);
3400b73539f3SBarry Smith     }
3401b73539f3SBarry Smith   }
3402b73539f3SBarry Smith 
3403273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3404273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3405273d9f13SBarry Smith 
3406ab93d7beSBarry Smith   if (!skipallocation) {
34072ee49352SLisandro Dalcin     if (!b->imax) {
3408d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3409d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
34102ee49352SLisandro Dalcin     }
3411273d9f13SBarry Smith     if (!nnz) {
3412435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3413c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3414d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3415d0f46423SBarry Smith       nz = nz*B->rmap->n;
3416273d9f13SBarry Smith     } else {
3417273d9f13SBarry Smith       nz = 0;
3418d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3419273d9f13SBarry Smith     }
3420ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3421d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3422ab93d7beSBarry Smith 
3423273d9f13SBarry Smith     /* allocate the matrix space */
34242ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3425d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3426d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3427bfeeae90SHong Zhang     b->i[0] = 0;
3428d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
34295da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
34305da197adSKris Buschelman     }
3431273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3432e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3433e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3434c461c341SBarry Smith   } else {
3435e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3436e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3437c461c341SBarry Smith   }
3438273d9f13SBarry Smith 
3439273d9f13SBarry Smith   b->nz                = 0;
3440273d9f13SBarry Smith   b->maxnz             = nz;
3441273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
34422576faa2SJed Brown   if (realalloc) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);}
3443273d9f13SBarry Smith   PetscFunctionReturn(0);
3444273d9f13SBarry Smith }
3445a23d5eceSKris Buschelman EXTERN_C_END
3446273d9f13SBarry Smith 
3447a1661176SMatthew Knepley #undef  __FUNCT__
3448a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
344958d36128SBarry Smith /*@
3450a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3451a1661176SMatthew Knepley 
3452a1661176SMatthew Knepley    Input Parameters:
3453a1661176SMatthew Knepley +  B - the matrix
3454a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3455a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3456a1661176SMatthew Knepley -  v - optional values in the matrix
3457a1661176SMatthew Knepley 
3458a1661176SMatthew Knepley    Level: developer
3459a1661176SMatthew Knepley 
346058d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
346158d36128SBarry Smith 
3462a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3463a1661176SMatthew Knepley 
3464a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3465a1661176SMatthew Knepley @*/
3466a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3467a1661176SMatthew Knepley {
3468a1661176SMatthew Knepley   PetscErrorCode ierr;
3469a1661176SMatthew Knepley 
3470a1661176SMatthew Knepley   PetscFunctionBegin;
34710700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
34726ba663aaSJed Brown   PetscValidType(B,1);
34734ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3474a1661176SMatthew Knepley   PetscFunctionReturn(0);
3475a1661176SMatthew Knepley }
3476a1661176SMatthew Knepley 
3477a1661176SMatthew Knepley EXTERN_C_BEGIN
3478a1661176SMatthew Knepley #undef  __FUNCT__
3479a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
34807087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3481a1661176SMatthew Knepley {
3482a1661176SMatthew Knepley   PetscInt       i;
3483a1661176SMatthew Knepley   PetscInt       m,n;
3484a1661176SMatthew Knepley   PetscInt       nz;
3485a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3486a1661176SMatthew Knepley   PetscScalar    *values;
3487a1661176SMatthew Knepley   PetscErrorCode ierr;
3488a1661176SMatthew Knepley 
3489a1661176SMatthew Knepley   PetscFunctionBegin;
349065e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3491779a8d59SSatish Balay 
3492779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3493779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3494779a8d59SSatish Balay 
3495779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3496a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3497a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3498b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3499a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
350065e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3501a1661176SMatthew Knepley     nnz[i] = nz;
3502a1661176SMatthew Knepley   }
3503a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3504a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3505a1661176SMatthew Knepley 
3506a1661176SMatthew Knepley   if (v) {
3507a1661176SMatthew Knepley     values = (PetscScalar*) v;
3508a1661176SMatthew Knepley   } else {
35090e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3510a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3511a1661176SMatthew Knepley   }
3512a1661176SMatthew Knepley 
3513a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3514b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3515b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3516a1661176SMatthew Knepley   }
3517a1661176SMatthew Knepley 
3518a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3519a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3520a1661176SMatthew Knepley 
3521a1661176SMatthew Knepley   if (!v) {
3522a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3523a1661176SMatthew Knepley   }
35247827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3525a1661176SMatthew Knepley   PetscFunctionReturn(0);
3526a1661176SMatthew Knepley }
3527a1661176SMatthew Knepley EXTERN_C_END
3528a1661176SMatthew Knepley 
3529c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3530b45d2f2cSJed Brown #include <petsc-private/petscaxpy.h>
3531170fe5c8SBarry Smith 
3532170fe5c8SBarry Smith #undef __FUNCT__
3533170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3534170fe5c8SBarry Smith /*
3535170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3536170fe5c8SBarry Smith 
3537170fe5c8SBarry Smith                n                       p                          p
3538170fe5c8SBarry Smith         (              )       (              )         (                  )
3539170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3540170fe5c8SBarry Smith         (              )       (              )         (                  )
3541170fe5c8SBarry Smith 
3542170fe5c8SBarry Smith */
3543170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3544170fe5c8SBarry Smith {
3545170fe5c8SBarry Smith   PetscErrorCode     ierr;
3546170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3547170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3548170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
35491de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3550170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3551170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3552170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3553170fe5c8SBarry Smith 
3554170fe5c8SBarry Smith   PetscFunctionBegin;
3555d0f46423SBarry Smith   m = A->rmap->n;
3556d0f46423SBarry Smith   n = A->cmap->n;
3557d0f46423SBarry Smith   p = B->cmap->n;
3558170fe5c8SBarry Smith   a = sub_a->v;
3559170fe5c8SBarry Smith   b = sub_b->a;
3560170fe5c8SBarry Smith   c = sub_c->v;
3561170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3562170fe5c8SBarry Smith 
3563170fe5c8SBarry Smith   ii  = sub_b->i;
3564170fe5c8SBarry Smith   idx = sub_b->j;
3565170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3566170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3567170fe5c8SBarry Smith     while (q-->0) {
3568170fe5c8SBarry Smith       c_q = c + m*(*idx);
3569170fe5c8SBarry Smith       a_q = a + m*i;
3570be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3571170fe5c8SBarry Smith       idx++;
3572170fe5c8SBarry Smith       b++;
3573170fe5c8SBarry Smith     }
3574170fe5c8SBarry Smith   }
3575170fe5c8SBarry Smith   PetscFunctionReturn(0);
3576170fe5c8SBarry Smith }
3577170fe5c8SBarry Smith 
3578170fe5c8SBarry Smith #undef __FUNCT__
3579170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3580170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3581170fe5c8SBarry Smith {
3582170fe5c8SBarry Smith   PetscErrorCode ierr;
3583d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3584170fe5c8SBarry Smith   Mat            Cmat;
3585170fe5c8SBarry Smith 
3586170fe5c8SBarry Smith   PetscFunctionBegin;
3587e32f2f54SBarry 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);
358839804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3589170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3590a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
3591170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3592170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3593170fe5c8SBarry Smith   Cmat->assembled    = PETSC_TRUE;
35948cdbd757SHong Zhang   Cmat->ops->matmult = MatMatMult_SeqDense_SeqAIJ;
3595170fe5c8SBarry Smith   *C = Cmat;
3596170fe5c8SBarry Smith   PetscFunctionReturn(0);
3597170fe5c8SBarry Smith }
3598170fe5c8SBarry Smith 
3599170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3600170fe5c8SBarry Smith #undef __FUNCT__
3601170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3602170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3603170fe5c8SBarry Smith {
3604170fe5c8SBarry Smith   PetscErrorCode ierr;
3605170fe5c8SBarry Smith 
3606170fe5c8SBarry Smith   PetscFunctionBegin;
3607170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3608170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3609170fe5c8SBarry Smith   }
3610170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3611170fe5c8SBarry Smith   PetscFunctionReturn(0);
3612170fe5c8SBarry Smith }
3613170fe5c8SBarry Smith 
3614170fe5c8SBarry Smith 
36150bad9183SKris Buschelman /*MC
3616fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
36170bad9183SKris Buschelman    based on compressed sparse row format.
36180bad9183SKris Buschelman 
36190bad9183SKris Buschelman    Options Database Keys:
36200bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
36210bad9183SKris Buschelman 
36220bad9183SKris Buschelman   Level: beginner
36230bad9183SKris Buschelman 
3624f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
36250bad9183SKris Buschelman M*/
36260bad9183SKris Buschelman 
3627a6175056SHong Zhang EXTERN_C_BEGIN
3628b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3629b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3630b5e56a35SBarry Smith #endif
3631ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3632af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3633af1023dbSSatish Balay #endif
36347087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
36357087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
36367087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
36377087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3638611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
36397087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3640611f576cSBarry Smith #endif
3641611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
36427087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3643611f576cSBarry Smith #endif
3644f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3645f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3646f3c0ef26SHong Zhang #endif
3647611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
36487087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3649611f576cSBarry Smith #endif
3650eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
36517087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3652eb3b5408SSatish Balay #endif
3653586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
36547087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3655586621ddSJed Brown #endif
3656719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
36577087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3658719d5645SBarry Smith #endif
3659b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
36607087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
36617087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
36627087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3663b3866ffcSBarry Smith #endif
366417667f90SBarry Smith EXTERN_C_END
366517667f90SBarry Smith 
3666c0c8ee5eSDmitry Karpeev 
366717667f90SBarry Smith EXTERN_C_BEGIN
36684a2ae208SSatish Balay #undef __FUNCT__
36694a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
36707087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqAIJ(Mat B)
3671273d9f13SBarry Smith {
3672273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3673dfbe8321SBarry Smith   PetscErrorCode ierr;
367438baddfdSBarry Smith   PetscMPIInt    size;
3675273d9f13SBarry Smith 
3676273d9f13SBarry Smith   PetscFunctionBegin;
36777adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3678e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3679273d9f13SBarry Smith 
368038f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3681b0a32e0cSBarry Smith   B->data             = (void*)b;
3682549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3683416022c9SBarry Smith   b->row              = 0;
3684416022c9SBarry Smith   b->col              = 0;
368582bf6240SBarry Smith   b->icol             = 0;
3686b810aeb4SBarry Smith   b->reallocs         = 0;
368736db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3688f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3689416022c9SBarry Smith   b->nonew             = 0;
3690416022c9SBarry Smith   b->diag              = 0;
3691416022c9SBarry Smith   b->solve_work        = 0;
36922a1b7f2aSHong Zhang   B->spptr             = 0;
3693be6bf707SBarry Smith   b->saved_values      = 0;
3694d7f994e1SBarry Smith   b->idiag             = 0;
369571f1c65dSBarry Smith   b->mdiag             = 0;
369671f1c65dSBarry Smith   b->ssor_work         = 0;
369771f1c65dSBarry Smith   b->omega             = 1.0;
369871f1c65dSBarry Smith   b->fshift            = 0.0;
369971f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3700bbead8a2SBarry Smith   b->ibdiagvalid       = PETSC_FALSE;
3701a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3702a30b2313SHong Zhang   b->xtoy              = 0;
3703a30b2313SHong Zhang   b->XtoY              = 0;
370488e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
370517ab2063SBarry Smith 
370635d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3707b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3708700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3709b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3710b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3711b3866ffcSBarry Smith #endif
3712b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3713700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3714b5e56a35SBarry Smith #endif
3715ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3716700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3717719d5645SBarry Smith #endif
3718611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3719700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3720611f576cSBarry Smith #endif
3721f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3722700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3723f3c0ef26SHong Zhang #endif
3724611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3725700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C","MatGetFactor_seqaij_spooles",MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3726611f576cSBarry Smith #endif
3727611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3728700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr);
3729611f576cSBarry Smith #endif
3730eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3731700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3732eb3b5408SSatish Balay #endif
3733586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3734700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3735586621ddSJed Brown #endif
3736719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3737700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_seqaij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3738719d5645SBarry Smith #endif
3739700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3740700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3741700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
3742700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3743700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3744700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3745700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3746700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3747700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3748700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3749700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3750700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3751700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3752700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
3753700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3754700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3755700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3756700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
37574108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
375817667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
37593a40ed3dSBarry Smith   PetscFunctionReturn(0);
376017ab2063SBarry Smith }
3761273d9f13SBarry Smith EXTERN_C_END
376217ab2063SBarry Smith 
37634a2ae208SSatish Balay #undef __FUNCT__
3764b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3765b24902e0SBarry Smith /*
3766b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3767b24902e0SBarry Smith */
3768ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
376917ab2063SBarry Smith {
3770416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
37716849ba73SBarry Smith   PetscErrorCode ierr;
3772d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
377317ab2063SBarry Smith 
37743a40ed3dSBarry Smith   PetscFunctionBegin;
3775273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3776273d9f13SBarry Smith 
3777d5f3da31SBarry Smith   C->factortype     = A->factortype;
3778416022c9SBarry Smith   c->row            = 0;
3779416022c9SBarry Smith   c->col            = 0;
378082bf6240SBarry Smith   c->icol           = 0;
37816ad4291fSHong Zhang   c->reallocs       = 0;
378217ab2063SBarry Smith 
37836ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
378417ab2063SBarry Smith 
3785aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
3786aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
3787eec197d1SBarry Smith 
378833b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
37899518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
379017ab2063SBarry Smith   for (i=0; i<m; i++) {
3791416022c9SBarry Smith     c->imax[i] = a->imax[i];
3792416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
379317ab2063SBarry Smith   }
379417ab2063SBarry Smith 
379517ab2063SBarry Smith   /* allocate the matrix space */
3796f77e22a1SHong Zhang   if (mallocmatspace){
3797a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
37989518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3799f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
380097f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
380117ab2063SBarry Smith     if (m > 0) {
380297f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3803be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3804bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3805be6bf707SBarry Smith       } else {
3806bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
380717ab2063SBarry Smith       }
380808480c60SBarry Smith     }
3809f77e22a1SHong Zhang   }
381017ab2063SBarry Smith 
38116ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3812416022c9SBarry Smith   c->roworiented       = a->roworiented;
3813416022c9SBarry Smith   c->nonew             = a->nonew;
3814416022c9SBarry Smith   if (a->diag) {
381597f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
381652e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
381717ab2063SBarry Smith     for (i=0; i<m; i++) {
3818416022c9SBarry Smith       c->diag[i] = a->diag[i];
381917ab2063SBarry Smith     }
38203a40ed3dSBarry Smith   } else c->diag           = 0;
38216ad4291fSHong Zhang   c->solve_work            = 0;
38226ad4291fSHong Zhang   c->saved_values          = 0;
38236ad4291fSHong Zhang   c->idiag                 = 0;
382471f1c65dSBarry Smith   c->ssor_work             = 0;
3825a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3826e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3827e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
38286ad4291fSHong Zhang   c->xtoy                  = 0;
38296ad4291fSHong Zhang   c->XtoY                  = 0;
38306ad4291fSHong Zhang 
3831893ad86cSHong Zhang   c->rmax               = a->rmax;
3832416022c9SBarry Smith   c->nz                 = a->nz;
38338ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3834273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3835754ec7b1SSatish Balay 
38366ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
38376ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
3838cd6b891eSBarry Smith   c->compressedrow.check   = a->compressedrow.check;
3839cd6b891eSBarry Smith   if (a->compressedrow.use){
38406ad4291fSHong Zhang     i = a->compressedrow.nrows;
38410e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
38426ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
38436ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
384427ea64f8SHong Zhang   } else {
384527ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
384627ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
384727ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
38486ad4291fSHong Zhang   }
384988e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
38504108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
38514846f1f5SKris Buschelman 
38527adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
38533a40ed3dSBarry Smith   PetscFunctionReturn(0);
385417ab2063SBarry Smith }
385517ab2063SBarry Smith 
38564a2ae208SSatish Balay #undef __FUNCT__
3857b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3858b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3859b24902e0SBarry Smith {
3860b24902e0SBarry Smith   PetscErrorCode ierr;
3861b24902e0SBarry Smith 
3862b24902e0SBarry Smith   PetscFunctionBegin;
3863b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
38644b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
3865a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
3866a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
3867f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3868b24902e0SBarry Smith   PetscFunctionReturn(0);
3869b24902e0SBarry Smith }
3870b24902e0SBarry Smith 
3871b24902e0SBarry Smith #undef __FUNCT__
38724a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3873112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
3874fbdbba38SShri Abhyankar {
3875fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
3876fbdbba38SShri Abhyankar   PetscErrorCode ierr;
3877fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
3878fbdbba38SShri Abhyankar   int            fd;
3879fbdbba38SShri Abhyankar   PetscMPIInt    size;
3880fbdbba38SShri Abhyankar   MPI_Comm       comm;
3881bbead8a2SBarry Smith   PetscInt       bs = 1;
3882fbdbba38SShri Abhyankar 
3883fbdbba38SShri Abhyankar   PetscFunctionBegin;
3884fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3885fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3886fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
3887bbead8a2SBarry Smith 
3888bbead8a2SBarry Smith   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
3889bbead8a2SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
3890bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3891bbead8a2SBarry Smith 
3892fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3893fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3894fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
3895fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
3896fbdbba38SShri Abhyankar 
3897bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3898fbdbba38SShri Abhyankar 
3899fbdbba38SShri Abhyankar   /* read in row lengths */
3900fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3901fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
3902fbdbba38SShri Abhyankar 
3903fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
3904fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
3905fbdbba38SShri 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);
3906fbdbba38SShri Abhyankar 
3907fbdbba38SShri Abhyankar   /* set global size if not set already*/
3908f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
3909fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3910aabbc4fbSShri Abhyankar   } else {
3911fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
3912fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
39134c5b953cSHong Zhang     if (rows < 0 && cols < 0){ /* user might provide local size instead of global size */
39144c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
39154c5b953cSHong Zhang     }
3916f501eaabSShri 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);
3917aabbc4fbSShri Abhyankar   }
3918fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
3919fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
3920fbdbba38SShri Abhyankar 
3921fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
3922fbdbba38SShri Abhyankar 
3923fbdbba38SShri Abhyankar   /* read in nonzero values */
3924fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
3925fbdbba38SShri Abhyankar 
3926fbdbba38SShri Abhyankar   /* set matrix "i" values */
3927fbdbba38SShri Abhyankar   a->i[0] = 0;
3928fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
3929fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3930fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
3931fbdbba38SShri Abhyankar   }
3932fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3933fbdbba38SShri Abhyankar 
3934fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3935fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3936bbead8a2SBarry Smith   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
3937fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
3938fbdbba38SShri Abhyankar }
3939fbdbba38SShri Abhyankar 
3940fbdbba38SShri Abhyankar #undef __FUNCT__
3941b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3942ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
39437264ac53SSatish Balay {
39447264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3945dfbe8321SBarry Smith   PetscErrorCode ierr;
3946eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3947eeffb40dSHong Zhang   PetscInt k;
3948eeffb40dSHong Zhang #endif
39497264ac53SSatish Balay 
39503a40ed3dSBarry Smith   PetscFunctionBegin;
3951bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3952d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3953ca44d042SBarry Smith     *flg = PETSC_FALSE;
3954ca44d042SBarry Smith     PetscFunctionReturn(0);
3955bcd2baecSBarry Smith   }
39567264ac53SSatish Balay 
39577264ac53SSatish Balay   /* if the a->i are the same */
3958d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3959abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
39607264ac53SSatish Balay 
39617264ac53SSatish Balay   /* if a->j are the same */
396297f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3963abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3964bcd2baecSBarry Smith 
3965bcd2baecSBarry Smith   /* if a->a are the same */
3966eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3967eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3968eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3969eeffb40dSHong Zhang       *flg = PETSC_FALSE;
39703a40ed3dSBarry Smith       PetscFunctionReturn(0);
3971eeffb40dSHong Zhang     }
3972eeffb40dSHong Zhang   }
3973eeffb40dSHong Zhang #else
3974eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3975eeffb40dSHong Zhang #endif
3976eeffb40dSHong Zhang   PetscFunctionReturn(0);
39777264ac53SSatish Balay }
397836db0b34SBarry Smith 
39794a2ae208SSatish Balay #undef __FUNCT__
39804a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
398105869f15SSatish Balay /*@
398236db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
398336db0b34SBarry Smith               provided by the user.
398436db0b34SBarry Smith 
3985c75a6043SHong Zhang       Collective on MPI_Comm
398636db0b34SBarry Smith 
398736db0b34SBarry Smith    Input Parameters:
398836db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
398936db0b34SBarry Smith .   m - number of rows
399036db0b34SBarry Smith .   n - number of columns
399136db0b34SBarry Smith .   i - row indices
399236db0b34SBarry Smith .   j - column indices
399336db0b34SBarry Smith -   a - matrix values
399436db0b34SBarry Smith 
399536db0b34SBarry Smith    Output Parameter:
399636db0b34SBarry Smith .   mat - the matrix
399736db0b34SBarry Smith 
399836db0b34SBarry Smith    Level: intermediate
399936db0b34SBarry Smith 
400036db0b34SBarry Smith    Notes:
40010551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4002292fb18eSBarry Smith     once the matrix is destroyed and not before
400336db0b34SBarry Smith 
400436db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
400536db0b34SBarry Smith 
4006bfeeae90SHong Zhang        The i and j indices are 0 based
400736db0b34SBarry Smith 
4008a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4009a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
4010a4552177SSatish Balay     as shown:
4011a4552177SSatish Balay 
4012a4552177SSatish Balay         1 0 0
4013a4552177SSatish Balay         2 0 3
4014a4552177SSatish Balay         4 5 6
4015a4552177SSatish Balay 
4016a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
40179985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4018a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
4019a4552177SSatish Balay 
40209985e31cSBarry Smith 
402169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
402236db0b34SBarry Smith 
402336db0b34SBarry Smith @*/
40247087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
402536db0b34SBarry Smith {
4026dfbe8321SBarry Smith   PetscErrorCode ierr;
4027cbcfb4deSHong Zhang   PetscInt       ii;
402836db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4029cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4030cbcfb4deSHong Zhang   PetscInt       jj;
4031cbcfb4deSHong Zhang #endif
403236db0b34SBarry Smith 
403336db0b34SBarry Smith   PetscFunctionBegin;
4034a96a251dSBarry Smith   if (i[0]) {
4035e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
403636db0b34SBarry Smith   }
4037f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4038f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4039a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4040ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4041ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4042ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4043ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
4044ab93d7beSBarry Smith 
404536db0b34SBarry Smith   aij->i = i;
404636db0b34SBarry Smith   aij->j = j;
404736db0b34SBarry Smith   aij->a = a;
404836db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
404936db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4050e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4051e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
405236db0b34SBarry Smith 
405336db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
405436db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
40552515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
4056e32f2f54SBarry 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]);
40579985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4058e32f2f54SBarry 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);
4059e32f2f54SBarry 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);
40609985e31cSBarry Smith     }
406136db0b34SBarry Smith #endif
406236db0b34SBarry Smith   }
40632515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
406436db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
4065e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
4066e32f2f54SBarry 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]);
406736db0b34SBarry Smith   }
406836db0b34SBarry Smith #endif
406936db0b34SBarry Smith 
4070b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4071b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
407236db0b34SBarry Smith   PetscFunctionReturn(0);
407336db0b34SBarry Smith }
40748a0b0e6bSVictor Minden #undef __FUNCT__
40758a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
407680ef6e79SMatthew G Knepley /*@C
4077d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
40788a0b0e6bSVictor Minden               provided by the user.
40798a0b0e6bSVictor Minden 
40808a0b0e6bSVictor Minden       Collective on MPI_Comm
40818a0b0e6bSVictor Minden 
40828a0b0e6bSVictor Minden    Input Parameters:
40838a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
40848a0b0e6bSVictor Minden .   m   - number of rows
40858a0b0e6bSVictor Minden .   n   - number of columns
40868a0b0e6bSVictor Minden .   i   - row indices
40878a0b0e6bSVictor Minden .   j   - column indices
40881230e6d1SVictor Minden .   a   - matrix values
40891230e6d1SVictor Minden .   nz  - number of nonzeros
40901230e6d1SVictor Minden -   idx - 0 or 1 based
40918a0b0e6bSVictor Minden 
40928a0b0e6bSVictor Minden    Output Parameter:
40938a0b0e6bSVictor Minden .   mat - the matrix
40948a0b0e6bSVictor Minden 
40958a0b0e6bSVictor Minden    Level: intermediate
40968a0b0e6bSVictor Minden 
40978a0b0e6bSVictor Minden    Notes:
40988a0b0e6bSVictor Minden        The i and j indices are 0 based
40998a0b0e6bSVictor Minden 
41008a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
41018a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
41028a0b0e6bSVictor Minden     as shown:
41038a0b0e6bSVictor Minden 
41048a0b0e6bSVictor Minden         1 0 0
41058a0b0e6bSVictor Minden         2 0 3
41068a0b0e6bSVictor Minden         4 5 6
41078a0b0e6bSVictor Minden 
41088a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
41098a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
41108a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
41118a0b0e6bSVictor Minden 
41128a0b0e6bSVictor Minden 
411369b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
41148a0b0e6bSVictor Minden 
41158a0b0e6bSVictor Minden @*/
41161230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
41178a0b0e6bSVictor Minden {
41188a0b0e6bSVictor Minden   PetscErrorCode ierr;
4119d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
41208a0b0e6bSVictor Minden 
41218a0b0e6bSVictor Minden 
41228a0b0e6bSVictor Minden   PetscFunctionBegin;
4123d021a1c5SVictor Minden   ierr = PetscMalloc(m*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
41241230e6d1SVictor Minden   ierr = PetscMemzero(nnz,m*sizeof(PetscInt));CHKERRQ(ierr);
41251230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++){
41261230e6d1SVictor Minden     nnz[i[ii]] += 1;
41271230e6d1SVictor Minden   }
41288a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
41298a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4130a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
41318a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
41321230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
41331230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++){
41341230e6d1SVictor Minden     if (idx){
41351230e6d1SVictor Minden       row = i[ii] - 1;
41361230e6d1SVictor Minden       col = j[ii] - 1;
41371230e6d1SVictor Minden     } else {
41381230e6d1SVictor Minden       row = i[ii];
41391230e6d1SVictor Minden       col = j[ii];
41408a0b0e6bSVictor Minden     }
41411230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
41428a0b0e6bSVictor Minden   }
41438a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
41448a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4145d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
41468a0b0e6bSVictor Minden   PetscFunctionReturn(0);
41478a0b0e6bSVictor Minden }
414836db0b34SBarry Smith 
4149cc8ba8e1SBarry Smith #undef __FUNCT__
4150ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4151dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4152cc8ba8e1SBarry Smith {
4153dfbe8321SBarry Smith   PetscErrorCode ierr;
4154cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
415536db0b34SBarry Smith 
4156cc8ba8e1SBarry Smith   PetscFunctionBegin;
41578ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4158cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4159cc8ba8e1SBarry Smith     a->coloring = coloring;
416012c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
416197f1f81fSBarry Smith     PetscInt             i,*larray;
416212c595b3SBarry Smith     ISColoring      ocoloring;
416308b6dcc0SBarry Smith     ISColoringValue *colors;
416412c595b3SBarry Smith 
416512c595b3SBarry Smith     /* set coloring for diagonal portion */
41660e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
4167d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
416812c595b3SBarry Smith       larray[i] = i;
416912c595b3SBarry Smith     }
4170992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
41710e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4172d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
417312c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
417412c595b3SBarry Smith     }
417512c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4176d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
417712c595b3SBarry Smith     a->coloring = ocoloring;
417812c595b3SBarry Smith   }
4179cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4180cc8ba8e1SBarry Smith }
4181cc8ba8e1SBarry Smith 
4182dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
4183ee4f033dSBarry Smith EXTERN_C_BEGIN
4184c6db04a5SJed Brown #include <adic/ad_utils.h>
4185ee4f033dSBarry Smith EXTERN_C_END
4186cc8ba8e1SBarry Smith 
4187cc8ba8e1SBarry Smith #undef __FUNCT__
4188ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
4189dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
4190cc8ba8e1SBarry Smith {
4191cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4192d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
41934440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
419408b6dcc0SBarry Smith   ISColoringValue *color;
4195cc8ba8e1SBarry Smith 
4196cc8ba8e1SBarry Smith   PetscFunctionBegin;
4197e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
41984440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
4199cc8ba8e1SBarry Smith   color = a->coloring->colors;
4200cc8ba8e1SBarry Smith   /* loop over rows */
4201cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
4202cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
4203cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
4204cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
4205cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
4206cc8ba8e1SBarry Smith     }
42074440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
4208ee4f033dSBarry Smith   }
4209ee4f033dSBarry Smith   PetscFunctionReturn(0);
4210ee4f033dSBarry Smith }
4211ee4f033dSBarry Smith #endif
4212ee4f033dSBarry Smith 
4213ee4f033dSBarry Smith #undef __FUNCT__
4214ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
421597f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4216ee4f033dSBarry Smith {
4217ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4218d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
421954f21887SBarry Smith   MatScalar       *v = a->a;
422054f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
422108b6dcc0SBarry Smith   ISColoringValue *color;
4222ee4f033dSBarry Smith 
4223ee4f033dSBarry Smith   PetscFunctionBegin;
4224e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4225ee4f033dSBarry Smith   color = a->coloring->colors;
4226ee4f033dSBarry Smith   /* loop over rows */
4227ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4228ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4229ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
4230ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
4231ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
4232ee4f033dSBarry Smith     }
4233ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4234cc8ba8e1SBarry Smith   }
4235cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4236cc8ba8e1SBarry Smith }
423736db0b34SBarry Smith 
423881824310SBarry Smith /*
423981824310SBarry Smith     Special version for direct calls from Fortran
424081824310SBarry Smith */
4241b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
424281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
424381824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
424481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
424581824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
424681824310SBarry Smith #endif
424781824310SBarry Smith 
424881824310SBarry Smith /* Change these macros so can be used in void function */
424981824310SBarry Smith #undef CHKERRQ
42507adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
425181824310SBarry Smith #undef SETERRQ2
4252e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
42534994cf47SJed Brown #undef SETERRQ3
42544994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
425581824310SBarry Smith 
425681824310SBarry Smith EXTERN_C_BEGIN
425781824310SBarry Smith #undef __FUNCT__
425881824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
42591f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
426081824310SBarry Smith {
426181824310SBarry Smith   Mat            A = *AA;
426281824310SBarry Smith   PetscInt       m = *mm, n = *nn;
426381824310SBarry Smith   InsertMode     is = *isis;
426481824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
426581824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
426681824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
426781824310SBarry Smith   PetscErrorCode ierr;
426881824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
426954f21887SBarry Smith   MatScalar      *ap,value,*aa;
4270ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4271ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
427281824310SBarry Smith 
427381824310SBarry Smith   PetscFunctionBegin;
42744994cf47SJed Brown   MatCheckPreallocated(A,1);
427581824310SBarry Smith   imax = a->imax;
427681824310SBarry Smith   ai = a->i;
427781824310SBarry Smith   ailen = a->ilen;
427881824310SBarry Smith   aj = a->j;
427981824310SBarry Smith   aa = a->a;
428081824310SBarry Smith 
428181824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
428281824310SBarry Smith     row  = im[k];
428381824310SBarry Smith     if (row < 0) continue;
428481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4285d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
428681824310SBarry Smith #endif
428781824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
428881824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
428981824310SBarry Smith     low  = 0;
429081824310SBarry Smith     high = nrow;
429181824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
429281824310SBarry Smith       if (in[l] < 0) continue;
429381824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4294d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
429581824310SBarry Smith #endif
429681824310SBarry Smith       col = in[l];
429781824310SBarry Smith       if (roworiented) {
429881824310SBarry Smith         value = v[l + k*n];
429981824310SBarry Smith       } else {
430081824310SBarry Smith         value = v[k + l*m];
430181824310SBarry Smith       }
430281824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
430381824310SBarry Smith 
430481824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
430581824310SBarry Smith       lastcol = col;
430681824310SBarry Smith       while (high-low > 5) {
430781824310SBarry Smith         t = (low+high)/2;
430881824310SBarry Smith         if (rp[t] > col) high = t;
430981824310SBarry Smith         else             low  = t;
431081824310SBarry Smith       }
431181824310SBarry Smith       for (i=low; i<high; i++) {
431281824310SBarry Smith         if (rp[i] > col) break;
431381824310SBarry Smith         if (rp[i] == col) {
431481824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
431581824310SBarry Smith           else                  ap[i] = value;
431681824310SBarry Smith           goto noinsert;
431781824310SBarry Smith         }
431881824310SBarry Smith       }
431981824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
432081824310SBarry Smith       if (nonew == 1) goto noinsert;
43217adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4322fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
432381824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
432481824310SBarry Smith       /* shift up all the later entries in this row */
432581824310SBarry Smith       for (ii=N; ii>=i; ii--) {
432681824310SBarry Smith         rp[ii+1] = rp[ii];
432781824310SBarry Smith         ap[ii+1] = ap[ii];
432881824310SBarry Smith       }
432981824310SBarry Smith       rp[i] = col;
433081824310SBarry Smith       ap[i] = value;
433181824310SBarry Smith       noinsert:;
433281824310SBarry Smith       low = i + 1;
433381824310SBarry Smith     }
433481824310SBarry Smith     ailen[row] = nrow;
433581824310SBarry Smith   }
433681824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
433781824310SBarry Smith   PetscFunctionReturnVoid();
433881824310SBarry Smith }
433981824310SBarry Smith EXTERN_C_END
434062298a1eSBarry Smith 
4341