xref: /petsc/src/mat/impls/aij/seq/aij.c (revision d441b888adf606fe61a1fa025fea15ff9f3ceae5)
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>
11*d441b888SJed 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;
39917ab2063SBarry Smith 
4003a40ed3dSBarry Smith   PetscFunctionBegin;
401b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
402d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
4030700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
404d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
405d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
406416022c9SBarry Smith   col_lens[3] = a->nz;
407416022c9SBarry Smith 
408416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
409d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
410416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
41117ab2063SBarry Smith   }
412d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
413606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
414416022c9SBarry Smith 
415416022c9SBarry Smith   /* store column indices (zero start index) */
4166f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
417416022c9SBarry Smith 
418416022c9SBarry Smith   /* store nonzero values */
4196f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
4203a40ed3dSBarry Smith   PetscFunctionReturn(0);
42117ab2063SBarry Smith }
422416022c9SBarry Smith 
42309573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
424cd155464SBarry Smith 
4254a2ae208SSatish Balay #undef __FUNCT__
4264a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
427dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
428416022c9SBarry Smith {
429416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
430dfbe8321SBarry Smith   PetscErrorCode    ierr;
431d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
432e060cb09SBarry Smith   const char        *name;
433f3ef73ceSBarry Smith   PetscViewerFormat format;
43417ab2063SBarry Smith 
4353a40ed3dSBarry Smith   PetscFunctionBegin;
436b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
43771c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
43897f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
439d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
440d00d2cf4SBarry Smith       nofinalvalue = 1;
441d00d2cf4SBarry Smith     }
442d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
443d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
44477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
44577431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
446b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
44717ab2063SBarry Smith 
44817ab2063SBarry Smith     for (i=0; i<m; i++) {
449416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
450aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
45177431f27SBarry 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);
45217ab2063SBarry Smith #else
45377431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
45417ab2063SBarry Smith #endif
45517ab2063SBarry Smith       }
45617ab2063SBarry Smith     }
457d00d2cf4SBarry Smith     if (nofinalvalue) {
458d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
459d00d2cf4SBarry Smith     }
460317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
461fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
462d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
46368369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
464cd155464SBarry Smith      PetscFunctionReturn(0);
465fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
466d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4677566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
46844cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
46977431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
47044cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
471aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
47236db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
473a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
47436db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
475a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
47636db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
477a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4786831982aSBarry Smith         }
47944cd7ae7SLois Curfman McInnes #else
480a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
48144cd7ae7SLois Curfman McInnes #endif
48244cd7ae7SLois Curfman McInnes       }
483b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
48444cd7ae7SLois Curfman McInnes     }
485d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
486fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
48797f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
488d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4897566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
49097f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
491496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
492496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
493496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
494496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
495aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
49636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
497496be53dSLois Curfman McInnes #else
498496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
499496be53dSLois Curfman McInnes #endif
500496be53dSLois Curfman McInnes         }
501496be53dSLois Curfman McInnes       }
502496be53dSLois Curfman McInnes     }
5032e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
50477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
5052e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
50677431f27SBarry 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);}
50777431f27SBarry 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);}
50877431f27SBarry 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);}
50977431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
51077431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
51177431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
512496be53dSLois Curfman McInnes     }
513b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
514606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
515496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
516496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
51777431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
518496be53dSLois Curfman McInnes       }
519b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
520496be53dSLois Curfman McInnes     }
521b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");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++) {
524496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
525aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
527b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5286831982aSBarry Smith           }
529496be53dSLois Curfman McInnes #else
530b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
531496be53dSLois Curfman McInnes #endif
532496be53dSLois Curfman McInnes         }
533496be53dSLois Curfman McInnes       }
534b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
535496be53dSLois Curfman McInnes     }
536d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
537fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
53897f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
53987828ca2SBarry Smith     PetscScalar value;
54002594712SBarry Smith 
541d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5427566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
54302594712SBarry Smith     for (i=0; i<m; i++) {
54402594712SBarry Smith       jcnt = 0;
545d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
546e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
54702594712SBarry Smith           value = a->a[cnt++];
548e24b481bSBarry Smith           jcnt++;
54902594712SBarry Smith         } else {
55002594712SBarry Smith           value = 0.0;
55102594712SBarry Smith         }
552aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
553b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
55402594712SBarry Smith #else
555b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
55602594712SBarry Smith #endif
55702594712SBarry Smith       }
558b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
55902594712SBarry Smith     }
560d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5613c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
562d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5637566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
5643c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5653c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
5663c215bfdSMatthew Knepley #else
5673c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
5683c215bfdSMatthew Knepley #endif
569d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
5703c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
5713c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5723c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5733c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
5743c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5753c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
5763c215bfdSMatthew 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);
5773c215bfdSMatthew Knepley         } else {
5783c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
5793c215bfdSMatthew Knepley         }
5803c215bfdSMatthew Knepley #else
5813c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
5823c215bfdSMatthew Knepley #endif
5833c215bfdSMatthew Knepley       }
5843c215bfdSMatthew Knepley     }
585d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5863a40ed3dSBarry Smith   } else {
587d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5887566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
589d5f3da31SBarry Smith     if (A->factortype){
59016cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
59116cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
59216cd7e1dSShri Abhyankar         /* L part */
59316cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
59416cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
59516cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
59616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
59716cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
59816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
59916cd7e1dSShri Abhyankar           } else {
60016cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
60116cd7e1dSShri Abhyankar           }
60216cd7e1dSShri Abhyankar #else
60316cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
60416cd7e1dSShri Abhyankar #endif
60516cd7e1dSShri Abhyankar         }
60616cd7e1dSShri Abhyankar 	/* diagonal */
60716cd7e1dSShri Abhyankar 	j = a->diag[i];
60816cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
60916cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
6102c990fa1SHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(1.0/a->a[j]),PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr);
61116cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
6122c990fa1SHong 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);
61316cd7e1dSShri Abhyankar           } else {
6142c990fa1SHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
61516cd7e1dSShri Abhyankar           }
61616cd7e1dSShri Abhyankar #else
6172c990fa1SHong Zhang           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,1.0/a->a[j]);CHKERRQ(ierr);
61816cd7e1dSShri Abhyankar #endif
61916cd7e1dSShri Abhyankar 
62016cd7e1dSShri Abhyankar 	/* U part */
62116cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
62216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
62316cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
62416cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
62516cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
62616cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
62716cd7e1dSShri Abhyankar           } else {
62816cd7e1dSShri Abhyankar             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
62916cd7e1dSShri Abhyankar           }
63016cd7e1dSShri Abhyankar #else
63116cd7e1dSShri Abhyankar           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
63216cd7e1dSShri Abhyankar #endif
63316cd7e1dSShri Abhyankar }
63416cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
63516cd7e1dSShri Abhyankar         }
63616cd7e1dSShri Abhyankar     } else {
63717ab2063SBarry Smith       for (i=0; i<m; i++) {
63877431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
639416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
640aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
64136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
642a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
64336db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
644a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6453a40ed3dSBarry Smith           } else {
646a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
64717ab2063SBarry Smith           }
64817ab2063SBarry Smith #else
649a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
65017ab2063SBarry Smith #endif
65117ab2063SBarry Smith         }
652b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
65317ab2063SBarry Smith       }
65416cd7e1dSShri Abhyankar     }
655d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
65617ab2063SBarry Smith   }
657b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6583a40ed3dSBarry Smith   PetscFunctionReturn(0);
659416022c9SBarry Smith }
660416022c9SBarry Smith 
6614a2ae208SSatish Balay #undef __FUNCT__
6624a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
663dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
664416022c9SBarry Smith {
665480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
666416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
667dfbe8321SBarry Smith   PetscErrorCode    ierr;
668d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
66936db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
670b0a32e0cSBarry Smith   PetscViewer       viewer;
671f3ef73ceSBarry Smith   PetscViewerFormat format;
672cddf8d76SBarry Smith 
6733a40ed3dSBarry Smith   PetscFunctionBegin;
674480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
675b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
67619bcc07fSBarry Smith 
677b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
678416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
6790513a670SBarry Smith 
680fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
6810513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
682b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
683416022c9SBarry Smith     for (i=0; i<m; i++) {
684cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
685bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
686bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
687aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
68836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
689cddf8d76SBarry Smith #else
690cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
691cddf8d76SBarry Smith #endif
692b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
693cddf8d76SBarry Smith       }
694cddf8d76SBarry Smith     }
695b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
696cddf8d76SBarry Smith     for (i=0; i<m; i++) {
697cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
698bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
699bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
700cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
701b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
702cddf8d76SBarry Smith       }
703cddf8d76SBarry Smith     }
704b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
705cddf8d76SBarry Smith     for (i=0; i<m; i++) {
706cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
707bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
708bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
709aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
71036db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
711cddf8d76SBarry Smith #else
712cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
713cddf8d76SBarry Smith #endif
714b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
715416022c9SBarry Smith       }
716416022c9SBarry Smith     }
7170513a670SBarry Smith   } else {
7180513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
7190513a670SBarry Smith     /* first determine max of all nonzero values */
72097f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
721b0a32e0cSBarry Smith     PetscDraw   popup;
72236db0b34SBarry Smith     PetscReal scale;
7230513a670SBarry Smith 
7240513a670SBarry Smith     for (i=0; i<nz; i++) {
7250513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
7260513a670SBarry Smith     }
727b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
728b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
729b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
7300513a670SBarry Smith     count = 0;
7310513a670SBarry Smith     for (i=0; i<m; i++) {
7320513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
733bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
734bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
73597f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
736b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
7370513a670SBarry Smith         count++;
7380513a670SBarry Smith       }
7390513a670SBarry Smith     }
7400513a670SBarry Smith   }
741480ef9eaSBarry Smith   PetscFunctionReturn(0);
742480ef9eaSBarry Smith }
743cddf8d76SBarry Smith 
7444a2ae208SSatish Balay #undef __FUNCT__
7454a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
746dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
747480ef9eaSBarry Smith {
748dfbe8321SBarry Smith   PetscErrorCode ierr;
749b0a32e0cSBarry Smith   PetscDraw      draw;
75036db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
751ace3abfcSBarry Smith   PetscBool      isnull;
752480ef9eaSBarry Smith 
753480ef9eaSBarry Smith   PetscFunctionBegin;
754b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
755b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
756480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
757480ef9eaSBarry Smith 
758480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
759d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
760480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
761b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
762b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
763480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
7643a40ed3dSBarry Smith   PetscFunctionReturn(0);
765416022c9SBarry Smith }
766416022c9SBarry Smith 
7674a2ae208SSatish Balay #undef __FUNCT__
7684a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
769dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
770416022c9SBarry Smith {
771dfbe8321SBarry Smith   PetscErrorCode ierr;
772ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
773416022c9SBarry Smith 
7743a40ed3dSBarry Smith   PetscFunctionBegin;
7752692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
7762692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
7772692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
778c45a1595SBarry Smith   if (iascii) {
7793a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
7800f5bd95cSBarry Smith   } else if (isbinary) {
7813a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
7820f5bd95cSBarry Smith   } else if (isdraw) {
7833a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
784913ac41fSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
7854108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
7863a40ed3dSBarry Smith   PetscFunctionReturn(0);
78717ab2063SBarry Smith }
78819bcc07fSBarry Smith 
7894a2ae208SSatish Balay #undef __FUNCT__
7904a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
791dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
79217ab2063SBarry Smith {
793416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7946849ba73SBarry Smith   PetscErrorCode ierr;
79597f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
796d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
79754f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
7983447b6efSHong Zhang   PetscReal      ratio=0.6;
79917ab2063SBarry Smith 
8003a40ed3dSBarry Smith   PetscFunctionBegin;
8013a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
80217ab2063SBarry Smith 
80343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
80417ab2063SBarry Smith   for (i=1; i<m; i++) {
805416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
80617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
80794a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
80817ab2063SBarry Smith     if (fshift) {
809bfeeae90SHong Zhang       ip = aj + ai[i] ;
810bfeeae90SHong Zhang       ap = aa + ai[i] ;
81117ab2063SBarry Smith       N  = ailen[i];
81217ab2063SBarry Smith       for (j=0; j<N; j++) {
81317ab2063SBarry Smith         ip[j-fshift] = ip[j];
81417ab2063SBarry Smith         ap[j-fshift] = ap[j];
81517ab2063SBarry Smith       }
81617ab2063SBarry Smith     }
81717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
81817ab2063SBarry Smith   }
81917ab2063SBarry Smith   if (m) {
82017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
82117ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
82217ab2063SBarry Smith   }
82317ab2063SBarry Smith   /* reset ilen and imax for each row */
82417ab2063SBarry Smith   for (i=0; i<m; i++) {
82517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
82617ab2063SBarry Smith   }
827bfeeae90SHong Zhang   a->nz = ai[m];
82865e19b50SBarry Smith   if (fshift && a->nounused == -1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
82917ab2063SBarry Smith 
83009f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
831d0f46423SBarry Smith   ierr = PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap->n,fshift,a->nz);CHKERRQ(ierr);
832ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
833ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
8348e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
835dd5f02e7SSatish Balay   a->reallocs          = 0;
8364e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
83736db0b34SBarry Smith   a->rmax              = rmax;
8384e220ebcSLois Curfman McInnes 
839cd6b891eSBarry Smith   ierr = MatCheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
84088e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
84171c2f376SKris Buschelman 
8424108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
84371f1c65dSBarry Smith 
84471f1c65dSBarry Smith   a->idiagvalid  = PETSC_FALSE;
845bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_FALSE;
8463a40ed3dSBarry Smith   PetscFunctionReturn(0);
84717ab2063SBarry Smith }
84817ab2063SBarry Smith 
8494a2ae208SSatish Balay #undef __FUNCT__
85099cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
85199cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
85299cafbc1SBarry Smith {
85399cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
85499cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
85554f21887SBarry Smith   MatScalar      *aa = a->a;
85699cafbc1SBarry Smith 
85799cafbc1SBarry Smith   PetscFunctionBegin;
85899cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
85986c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
86086c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
86199cafbc1SBarry Smith   PetscFunctionReturn(0);
86299cafbc1SBarry Smith }
86399cafbc1SBarry Smith 
86499cafbc1SBarry Smith #undef __FUNCT__
86599cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
86699cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
86799cafbc1SBarry Smith {
86899cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
86999cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
87054f21887SBarry Smith   MatScalar      *aa = a->a;
87199cafbc1SBarry Smith 
87299cafbc1SBarry Smith   PetscFunctionBegin;
87399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
87486c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
87586c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
87699cafbc1SBarry Smith   PetscFunctionReturn(0);
87799cafbc1SBarry Smith }
87899cafbc1SBarry Smith 
87999cafbc1SBarry Smith #undef __FUNCT__
8804a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
881dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
88217ab2063SBarry Smith {
883416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
884dfbe8321SBarry Smith   PetscErrorCode ierr;
8853a40ed3dSBarry Smith 
8863a40ed3dSBarry Smith   PetscFunctionBegin;
887d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
88886c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
88986c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
8903a40ed3dSBarry Smith   PetscFunctionReturn(0);
89117ab2063SBarry Smith }
892416022c9SBarry Smith 
8934a2ae208SSatish Balay #undef __FUNCT__
8944a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
895dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
89617ab2063SBarry Smith {
897416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
898dfbe8321SBarry Smith   PetscErrorCode ierr;
899d5d45c9bSBarry Smith 
9003a40ed3dSBarry Smith   PetscFunctionBegin;
901aa482453SBarry Smith #if defined(PETSC_USE_LOG)
902d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
90317ab2063SBarry Smith #endif
904e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
9056bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
9066bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
90705b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
908d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
90905b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
91071f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
91105b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
9126bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
91305b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
9146bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
91505b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
9166bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
917cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
918a30b2313SHong Zhang 
9194108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
920bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
921901853e0SKris Buschelman 
922dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
923901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
924901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
925901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
926901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
927901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
9285a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr);
929901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
930901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
931a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
932901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
9333a40ed3dSBarry Smith   PetscFunctionReturn(0);
93417ab2063SBarry Smith }
93517ab2063SBarry Smith 
9364a2ae208SSatish Balay #undef __FUNCT__
9374a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
938ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool  flg)
93917ab2063SBarry Smith {
940416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9414846f1f5SKris Buschelman   PetscErrorCode ierr;
9423a40ed3dSBarry Smith 
9433a40ed3dSBarry Smith   PetscFunctionBegin;
944a65d3064SKris Buschelman   switch (op) {
945a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
9464e0d8c25SBarry Smith       a->roworiented       = flg;
947a65d3064SKris Buschelman       break;
948a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
949a9817697SBarry Smith       a->keepnonzeropattern    = flg;
950a65d3064SKris Buschelman       break;
951512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
952512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
953a65d3064SKris Buschelman       break;
954a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
9554e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
956a65d3064SKris Buschelman       break;
957a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
9584e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
959a65d3064SKris Buschelman       break;
96028b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
96128b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
96228b2fa4aSMatthew Knepley       break;
963a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
9644e0d8c25SBarry Smith       a->ignorezeroentries = flg;
9650df259c2SBarry Smith       break;
966cd6b891eSBarry Smith     case MAT_CHECK_COMPRESSED_ROW:
967cd6b891eSBarry Smith       a->compressedrow.check = flg;
968d487561eSHong Zhang       break;
9693d472b54SHong Zhang     case MAT_SPD:
9703d472b54SHong Zhang       A->spd_set                         = PETSC_TRUE;
9713d472b54SHong Zhang       A->spd                             = flg;
9723d472b54SHong Zhang       if (flg) {
9733d472b54SHong Zhang         A->symmetric                     = PETSC_TRUE;
9743d472b54SHong Zhang         A->structurally_symmetric        = PETSC_TRUE;
9753d472b54SHong Zhang         A->symmetric_set                 = PETSC_TRUE;
9763d472b54SHong Zhang         A->structurally_symmetric_set    = PETSC_TRUE;
9773d472b54SHong Zhang       }
9783d472b54SHong Zhang       break;
979b1646e73SJed Brown     case MAT_SYMMETRIC:
980b1646e73SJed Brown     case MAT_STRUCTURALLY_SYMMETRIC:
981b1646e73SJed Brown     case MAT_HERMITIAN:
982b1646e73SJed Brown     case MAT_SYMMETRY_ETERNAL:
9834e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
984a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
985a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
986290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
987a65d3064SKris Buschelman       break;
988b87ac2d8SJed Brown     case MAT_USE_INODES:
989b87ac2d8SJed Brown       /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
990b87ac2d8SJed Brown       break;
991a65d3064SKris Buschelman     default:
992e32f2f54SBarry Smith       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
993a65d3064SKris Buschelman   }
9944108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
9953a40ed3dSBarry Smith   PetscFunctionReturn(0);
99617ab2063SBarry Smith }
99717ab2063SBarry Smith 
9984a2ae208SSatish Balay #undef __FUNCT__
9994a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1000dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
100117ab2063SBarry Smith {
1002416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10036849ba73SBarry Smith   PetscErrorCode ierr;
1004d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
100535e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
100617ab2063SBarry Smith 
10073a40ed3dSBarry Smith   PetscFunctionBegin;
1008d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1009e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
101035e7444dSHong Zhang 
1011d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
1012d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
101335e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
10142c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
101535e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
101635e7444dSHong Zhang     PetscFunctionReturn(0);
101735e7444dSHong Zhang   }
101835e7444dSHong Zhang 
10192dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
10201ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
102135e7444dSHong Zhang   for (i=0; i<n; i++) {
102235e7444dSHong Zhang     nz = ai[i+1] - ai[i];
10232f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
102435e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
102535e7444dSHong Zhang       if (aj[j] == i) {
102635e7444dSHong Zhang         x[i] = aa[j];
102717ab2063SBarry Smith         break;
102817ab2063SBarry Smith       }
102917ab2063SBarry Smith     }
103017ab2063SBarry Smith   }
10311ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
10323a40ed3dSBarry Smith   PetscFunctionReturn(0);
103317ab2063SBarry Smith }
103417ab2063SBarry Smith 
1035c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10364a2ae208SSatish Balay #undef __FUNCT__
10374a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1038dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
103917ab2063SBarry Smith {
1040416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
10415c897100SBarry Smith   PetscScalar       *x,*y;
1042dfbe8321SBarry Smith   PetscErrorCode    ierr;
1043d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
10445c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1045a77337e4SBarry Smith   MatScalar         *v;
1046a77337e4SBarry Smith   PetscScalar       alpha;
104704fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
10483447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
1049ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
10505c897100SBarry Smith #endif
105117ab2063SBarry Smith 
10523a40ed3dSBarry Smith   PetscFunctionBegin;
10532e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
10541ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
10551ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
10565c897100SBarry Smith 
10575c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1058bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
10595c897100SBarry Smith #else
10603447b6efSHong Zhang   if (usecprow){
10613447b6efSHong Zhang     m    = cprow.nrows;
10623447b6efSHong Zhang     ii   = cprow.i;
10637b2bb3b9SHong Zhang     ridx = cprow.rindex;
10643447b6efSHong Zhang   } else {
10653447b6efSHong Zhang     ii = a->i;
10663447b6efSHong Zhang   }
106717ab2063SBarry Smith   for (i=0; i<m; i++) {
10683447b6efSHong Zhang     idx   = a->j + ii[i] ;
10693447b6efSHong Zhang     v     = a->a + ii[i] ;
10703447b6efSHong Zhang     n     = ii[i+1] - ii[i];
10713447b6efSHong Zhang     if (usecprow){
10727b2bb3b9SHong Zhang       alpha = x[ridx[i]];
10733447b6efSHong Zhang     } else {
107417ab2063SBarry Smith       alpha = x[i];
10753447b6efSHong Zhang     }
107604fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
107717ab2063SBarry Smith   }
10785c897100SBarry Smith #endif
1079dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10801ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10811ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10823a40ed3dSBarry Smith   PetscFunctionReturn(0);
108317ab2063SBarry Smith }
108417ab2063SBarry Smith 
10854a2ae208SSatish Balay #undef __FUNCT__
10865c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1087dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
10885c897100SBarry Smith {
1089dfbe8321SBarry Smith   PetscErrorCode ierr;
10905c897100SBarry Smith 
10915c897100SBarry Smith   PetscFunctionBegin;
1092170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
10935c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
10945c897100SBarry Smith   PetscFunctionReturn(0);
10955c897100SBarry Smith }
10965c897100SBarry Smith 
1097c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10985c897100SBarry Smith #undef __FUNCT__
10994a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1100dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
110117ab2063SBarry Smith {
1102416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1103d9fead3dSBarry Smith   PetscScalar       *y;
110454f21887SBarry Smith   const PetscScalar *x;
110554f21887SBarry Smith   const MatScalar   *aa;
1106dfbe8321SBarry Smith   PetscErrorCode    ierr;
1107003131ecSBarry Smith   PetscInt          m=A->rmap->n;
1108003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
11098aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1110362ced78SSatish Balay   PetscScalar       sum;
1111ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
111217ab2063SBarry Smith 
1113b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
111497952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1115fee21e36SBarry Smith #endif
1116fee21e36SBarry Smith 
11173a40ed3dSBarry Smith   PetscFunctionBegin;
11183649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
11191ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
112097952fefSHong Zhang   aj  = a->j;
112197952fefSHong Zhang   aa  = a->a;
1122416022c9SBarry Smith   ii  = a->i;
11234eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
112497952fefSHong Zhang     m    = a->compressedrow.nrows;
112597952fefSHong Zhang     ii   = a->compressedrow.i;
112697952fefSHong Zhang     ridx = a->compressedrow.rindex;
112797952fefSHong Zhang     for (i=0; i<m; i++){
112897952fefSHong Zhang       n   = ii[i+1] - ii[i];
112997952fefSHong Zhang       aj  = a->j + ii[i];
113097952fefSHong Zhang       aa  = a->a + ii[i];
113197952fefSHong Zhang       sum = 0.0;
1132a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1133003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1134003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
113597952fefSHong Zhang       y[*ridx++] = sum;
113697952fefSHong Zhang     }
113797952fefSHong Zhang   } else { /* do not use compressed row format */
1138b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1139b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1140b05257ddSBarry Smith #else
114117ab2063SBarry Smith     for (i=0; i<m; i++) {
1142003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1143003131ecSBarry Smith       aj  = a->j + ii[i];
1144003131ecSBarry Smith       aa  = a->a + ii[i];
114517ab2063SBarry Smith       sum  = 0.0;
1146a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1147003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
114817ab2063SBarry Smith       y[i] = sum;
114917ab2063SBarry Smith     }
11508d195f9aSBarry Smith #endif
1151b05257ddSBarry Smith   }
1152dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
11533649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
11541ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11553a40ed3dSBarry Smith   PetscFunctionReturn(0);
115617ab2063SBarry Smith }
115717ab2063SBarry Smith 
115863db405bSBarry Smith #if defined(PETSC_HAVE_PTHREADCLASSES)
115963db405bSBarry Smith 
11600c74a584SJed Brown /* ******************* */
1161700485ddSSatish Balay #if defined(PETSC_HAVE_PTHREADCLASSES)
1162cfcfc605SKerry Stevens extern PetscBool    PetscUseThreadPool;
1163fdfc40dbSShri Abhyankar #if defined(PETSC_HAVE_CPU_SET_T)
1164cfcfc605SKerry Stevens void* DoCoreAffinity(void);
1165fdfc40dbSShri Abhyankar #endif
1166cfcfc605SKerry Stevens 
11670ca81413SKerry Stevens typedef struct {
11680ca81413SKerry Stevens   const MatScalar* matdata;
11690ca81413SKerry Stevens   const PetscScalar* vecdata;
11700ca81413SKerry Stevens   PetscScalar* vecout;
11710ca81413SKerry Stevens   const PetscInt* colindnz;
11720ca81413SKerry Stevens   const PetscInt* rownumnz;
11730ca81413SKerry Stevens   PetscInt numrows;
11740ca81413SKerry Stevens   const PetscInt* specidx;
11750ca81413SKerry Stevens   PetscInt nzr;
11760ca81413SKerry Stevens } MatMult_KernelData;
11770ca81413SKerry Stevens 
11780ca81413SKerry Stevens void* MatMult_Kernel(void *arg)
11790ca81413SKerry Stevens {
1180cfcfc605SKerry Stevens   if(PetscUseThreadPool==PETSC_FALSE) {
1181fdfc40dbSShri Abhyankar #if defined(PETSC_HAVE_CPU_SET_T)
1182cfcfc605SKerry Stevens     DoCoreAffinity();
1183fdfc40dbSShri Abhyankar #endif
1184cfcfc605SKerry Stevens   }
11850ca81413SKerry Stevens   MatMult_KernelData *data = (MatMult_KernelData*)arg;
11860ca81413SKerry Stevens   PetscScalar       sum;
11870ca81413SKerry Stevens   const MatScalar   *aabase = data->matdata,*aa;
11880ca81413SKerry Stevens   const PetscScalar *x = data->vecdata;
11890ca81413SKerry Stevens   PetscScalar       *y = data->vecout;
11900ca81413SKerry Stevens   const PetscInt    *ajbase = data->colindnz,*aj;
11910ca81413SKerry Stevens   const PetscInt    *ii = data->rownumnz;
11920ca81413SKerry Stevens   PetscInt          m  = data->numrows;
11930ca81413SKerry Stevens   const PetscInt    *ridx = data->specidx;
11940ca81413SKerry Stevens   PetscInt          i,n,nonzerorow = 0;
11950ca81413SKerry Stevens 
11960ca81413SKerry Stevens   if(ridx!=NULL) {
11970ca81413SKerry Stevens     for (i=0; i<m; i++){
11980ca81413SKerry Stevens       n   = ii[i+1] - ii[i];
11990ca81413SKerry Stevens       aj  = ajbase + ii[i];
12000ca81413SKerry Stevens       aa  = aabase + ii[i];
12010ca81413SKerry Stevens       sum = 0.0;
1202cfcfc605SKerry Stevens       if(n>0) {
120351d315f7SKerry Stevens         PetscSparseDensePlusDot(sum,x,aa,aj,n);
120451d315f7SKerry Stevens         nonzerorow++;
1205cfcfc605SKerry Stevens       }
12060ca81413SKerry Stevens       y[*ridx++] = sum;
12070ca81413SKerry Stevens     }
12080ca81413SKerry Stevens   }
12090ca81413SKerry Stevens   else {
121051d315f7SKerry Stevens     PetscInt ibase = data->nzr;
12110ca81413SKerry Stevens     for (i=0; i<m; i++) {
12120ca81413SKerry Stevens       n   = ii[i+1] - ii[i];
12130ca81413SKerry Stevens       aj  = ajbase + ii[i];
12140ca81413SKerry Stevens       aa  = aabase + ii[i];
12150ca81413SKerry Stevens       sum  = 0.0;
1216cfcfc605SKerry Stevens       if(n>0) {
121751d315f7SKerry Stevens         PetscSparseDensePlusDot(sum,x,aa,aj,n);
121851d315f7SKerry Stevens         nonzerorow++;
1219cfcfc605SKerry Stevens       }
122051d315f7SKerry Stevens       y[i+ibase] = sum;
12210ca81413SKerry Stevens     }
12220ca81413SKerry Stevens   }
12230ca81413SKerry Stevens   data->nzr = nonzerorow;
12240ca81413SKerry Stevens   return NULL;
12250ca81413SKerry Stevens }
1226700485ddSSatish Balay #endif
12270ca81413SKerry Stevens 
12280ca81413SKerry Stevens extern PetscMPIInt PetscMaxThreads;
12294b83fb64SBarry Smith extern PetscErrorCode (*MainJob)(void* (*pFunc)(void*),void**,PetscInt);
123051d315f7SKerry Stevens 
12310ca81413SKerry Stevens #undef __FUNCT__
12327d6a0e61SBarry Smith #define __FUNCT__ "MatMult_SeqAIJPThread"
12337d6a0e61SBarry Smith PetscErrorCode MatMult_SeqAIJPThread(Mat A,Vec xx,Vec yy)
12340ca81413SKerry Stevens {
12350ca81413SKerry Stevens   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
12360ca81413SKerry Stevens   PetscScalar       *y;
12370ca81413SKerry Stevens   const PetscScalar *x;
12380ca81413SKerry Stevens   PetscErrorCode    ierr;
12390ca81413SKerry Stevens   PetscInt          m=A->rmap->n,nonzerorow=0;
12400ca81413SKerry Stevens   PetscBool         usecprow=a->compressedrow.use;
12410ca81413SKerry Stevens 
12420ca81413SKerry Stevens #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
12430ca81413SKerry Stevens #pragma disjoint(*x,*y,*aa)
12440ca81413SKerry Stevens #endif
12450ca81413SKerry Stevens 
12460ca81413SKerry Stevens   PetscFunctionBegin;
12470ca81413SKerry Stevens   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12480ca81413SKerry Stevens   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
12490ca81413SKerry Stevens 
12500ca81413SKerry Stevens   if(usecprow) {
12510ca81413SKerry Stevens     PetscInt          NumPerThread,iindex;
12520ca81413SKerry Stevens     const MatScalar   *aa = a->a;
12530ca81413SKerry Stevens     const PetscInt    *aj = a->j,*ii = a->compressedrow.i,*ridx=a->compressedrow.rindex;
12540ca81413SKerry Stevens     PetscInt          i,iStartVal,iEndVal,iStartIndex,iEndIndex;
12550c74a584SJed Brown     const PetscInt    iNumThreads = PetscMaxThreads;  /* this number could be different */
1256cfcfc605SKerry Stevens     MatMult_KernelData* kerneldatap = (MatMult_KernelData*)malloc(iNumThreads*sizeof(MatMult_KernelData));
1257cfcfc605SKerry Stevens     MatMult_KernelData** pdata = (MatMult_KernelData**)malloc(iNumThreads*sizeof(MatMult_KernelData*));
12580ca81413SKerry Stevens 
12590ca81413SKerry Stevens     m    = a->compressedrow.nrows;
12600ca81413SKerry Stevens     NumPerThread = ii[m]/iNumThreads;
12610ca81413SKerry Stevens     iindex = 0;
12620ca81413SKerry Stevens     for(i=0; i<iNumThreads;i++) {
12630ca81413SKerry Stevens       iStartIndex = iindex;
12640ca81413SKerry Stevens       iStartVal = ii[iStartIndex];
12650ca81413SKerry Stevens       iEndVal = iStartVal;
12660c74a584SJed Brown       /* determine number of rows to process */
12670ca81413SKerry Stevens       while(iEndVal-iStartVal<NumPerThread) {
12680ca81413SKerry Stevens 	iindex++;
12690ca81413SKerry Stevens 	iEndVal = ii[iindex];
12700ca81413SKerry Stevens       }
12710c74a584SJed Brown       /* determine whether to go back 1 */
12720ca81413SKerry Stevens       if(iEndVal-iStartVal-NumPerThread>NumPerThread-(ii[iindex-1]-iStartVal)) {
12730ca81413SKerry Stevens 	iindex--;
12740ca81413SKerry Stevens 	iEndVal = ii[iindex];
12750ca81413SKerry Stevens       }
12760ca81413SKerry Stevens       iEndIndex = iindex;
1277cfcfc605SKerry Stevens       kerneldatap[i].matdata  = aa;
12780ca81413SKerry Stevens       kerneldatap[i].vecdata  = x;
12790ca81413SKerry Stevens       kerneldatap[i].vecout   = y;
12800ca81413SKerry Stevens       kerneldatap[i].colindnz = aj;
12810ca81413SKerry Stevens       kerneldatap[i].rownumnz = ii + iStartIndex;
12820ca81413SKerry Stevens       kerneldatap[i].numrows  = iEndIndex - iStartIndex + 1;
12830ca81413SKerry Stevens       kerneldatap[i].specidx  = ridx + iStartVal;
12840ca81413SKerry Stevens       kerneldatap[i].nzr      = 0;
1285cfcfc605SKerry Stevens       pdata[i] = &kerneldatap[i];
12860ca81413SKerry Stevens       iindex++;
12870ca81413SKerry Stevens     }
1288cfcfc605SKerry Stevens     ierr = MainJob(MatMult_Kernel,(void**)pdata,iNumThreads);
1289cfcfc605SKerry Stevens     /* collect results */
129051d315f7SKerry Stevens     for(i=0; i<iNumThreads; i++) {
1291cfcfc605SKerry Stevens       nonzerorow += kerneldatap[i].nzr;
129251d315f7SKerry Stevens     }
1293cfcfc605SKerry Stevens     free(kerneldatap);
1294cfcfc605SKerry Stevens     free(pdata);
129551d315f7SKerry Stevens   }
129651d315f7SKerry Stevens   else {
129751d315f7SKerry Stevens #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
129851d315f7SKerry Stevens   fortranmultaij_(&m,x,a->i,a->j,a->a,y);
129951d315f7SKerry Stevens #else
130051d315f7SKerry Stevens   PetscInt            i,iindex;
130151d315f7SKerry Stevens     const MatScalar   *aa = a->a;
130251d315f7SKerry Stevens     const PetscInt    *aj = a->j,*ii = a->i;
13030c74a584SJed Brown     const PetscInt    iNumThreads = PetscMaxThreads;  /* this number could be different */
130451d315f7SKerry Stevens     PetscInt          Q = m/iNumThreads;
130551d315f7SKerry Stevens     PetscInt          R = m-Q*iNumThreads;
130651d315f7SKerry Stevens     PetscBool         S;
130751d315f7SKerry Stevens 
130851d315f7SKerry Stevens     MatMult_KernelData* kerneldatap = (MatMult_KernelData*)malloc(iNumThreads*sizeof(MatMult_KernelData));
130951d315f7SKerry Stevens     MatMult_KernelData** pdata = (MatMult_KernelData**)malloc(iNumThreads*sizeof(MatMult_KernelData*));
131051d315f7SKerry Stevens 
131151d315f7SKerry Stevens     iindex = 0;
131251d315f7SKerry Stevens     for(i=0; i<iNumThreads;i++) {
1313b50af74fSBarry Smith       S = (PetscBool)(i<R);
131451d315f7SKerry Stevens       kerneldatap[i].matdata  = aa;
131551d315f7SKerry Stevens       kerneldatap[i].vecdata  = x;
131651d315f7SKerry Stevens       kerneldatap[i].vecout   = y;
131751d315f7SKerry Stevens       kerneldatap[i].colindnz = aj;
131851d315f7SKerry Stevens       kerneldatap[i].rownumnz = ii + iindex;
131951d315f7SKerry Stevens       kerneldatap[i].numrows  = S?Q+1:Q;
132051d315f7SKerry Stevens       kerneldatap[i].specidx  = PETSC_NULL;
13210c74a584SJed Brown       kerneldatap[i].nzr      = iindex; /* serves as the 'base' row (needed to access correctly into output vector y) */
132251d315f7SKerry Stevens       pdata[i] = &kerneldatap[i];
132351d315f7SKerry Stevens       iindex += kerneldatap[i].numrows;
132451d315f7SKerry Stevens     }
13250ca81413SKerry Stevens     MainJob(MatMult_Kernel,(void**)pdata,iNumThreads);
13260c74a584SJed Brown     /* collect results */
13270ca81413SKerry Stevens     for(i=0; i<iNumThreads; i++) {
13280ca81413SKerry Stevens       nonzerorow += kerneldatap[i].nzr;
13290ca81413SKerry Stevens     }
133051d315f7SKerry Stevens     free(kerneldatap);
133151d315f7SKerry Stevens     free(pdata);
13320ca81413SKerry Stevens #endif
13330ca81413SKerry Stevens   }
13340ca81413SKerry Stevens 
13350ca81413SKerry Stevens   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
13360ca81413SKerry Stevens   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13370ca81413SKerry Stevens   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13380ca81413SKerry Stevens   PetscFunctionReturn(0);
13390ca81413SKerry Stevens }
13400c74a584SJed Brown /* ******************* */
1341ba61063dSBarry Smith #endif
13420ca81413SKerry Stevens 
1343c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
13444a2ae208SSatish Balay #undef __FUNCT__
13454a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1346dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
134717ab2063SBarry Smith {
1348416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1349f15663dcSBarry Smith   PetscScalar       *y,*z;
1350f15663dcSBarry Smith   const PetscScalar *x;
135154f21887SBarry Smith   const MatScalar   *aa;
1352dfbe8321SBarry Smith   PetscErrorCode    ierr;
1353d0f46423SBarry Smith   PetscInt          m = A->rmap->n,*aj,*ii;
1354f15663dcSBarry Smith   PetscInt          n,i,*ridx=PETSC_NULL;
1355362ced78SSatish Balay   PetscScalar       sum;
1356ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
13579ea0dfa2SSatish Balay 
13583a40ed3dSBarry Smith   PetscFunctionBegin;
1359f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
13601ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
13612e8a6d31SBarry Smith   if (zz != yy) {
13621ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
13632e8a6d31SBarry Smith   } else {
13642e8a6d31SBarry Smith     z = y;
13652e8a6d31SBarry Smith   }
1366bfeeae90SHong Zhang 
136797952fefSHong Zhang   aj  = a->j;
136897952fefSHong Zhang   aa  = a->a;
1369cddf8d76SBarry Smith   ii  = a->i;
13704eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
13714eb6d288SHong Zhang     if (zz != yy){
13724eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
13734eb6d288SHong Zhang     }
137497952fefSHong Zhang     m    = a->compressedrow.nrows;
137597952fefSHong Zhang     ii   = a->compressedrow.i;
137697952fefSHong Zhang     ridx = a->compressedrow.rindex;
137797952fefSHong Zhang     for (i=0; i<m; i++){
137897952fefSHong Zhang       n  = ii[i+1] - ii[i];
137997952fefSHong Zhang       aj  = a->j + ii[i];
138097952fefSHong Zhang       aa  = a->a + ii[i];
138197952fefSHong Zhang       sum = y[*ridx];
1382f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
138397952fefSHong Zhang       z[*ridx++] = sum;
138497952fefSHong Zhang     }
138597952fefSHong Zhang   } else { /* do not use compressed row format */
1386f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1387f15663dcSBarry Smith   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1388f15663dcSBarry Smith #else
138917ab2063SBarry Smith     for (i=0; i<m; i++) {
1390f15663dcSBarry Smith       n    = ii[i+1] - ii[i];
1391f15663dcSBarry Smith       aj  = a->j + ii[i];
1392f15663dcSBarry Smith       aa  = a->a + ii[i];
139317ab2063SBarry Smith       sum  = y[i];
1394f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
139517ab2063SBarry Smith       z[i] = sum;
139617ab2063SBarry Smith     }
139702ab625aSSatish Balay #endif
1398f15663dcSBarry Smith   }
1399dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1400f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
14011ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
14022e8a6d31SBarry Smith   if (zz != yy) {
14031ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
14042e8a6d31SBarry Smith   }
14058154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
14066b375ea7SVictor Minden   /*
1407918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1408918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1409918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
14106b375ea7SVictor Minden   */
1411918e98c3SVictor Minden #endif
14123a40ed3dSBarry Smith   PetscFunctionReturn(0);
141317ab2063SBarry Smith }
141417ab2063SBarry Smith 
141517ab2063SBarry Smith /*
141617ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
141717ab2063SBarry Smith */
14184a2ae208SSatish Balay #undef __FUNCT__
14194a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1420dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
142117ab2063SBarry Smith {
1422416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
14236849ba73SBarry Smith   PetscErrorCode ierr;
1424d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
142517ab2063SBarry Smith 
14263a40ed3dSBarry Smith   PetscFunctionBegin;
142709f38230SBarry Smith   if (!a->diag) {
142809f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
14299518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
143009f38230SBarry Smith   }
1431d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
143209f38230SBarry Smith     a->diag[i] = a->i[i+1];
1433bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1434bfeeae90SHong Zhang       if (a->j[j] == i) {
143509f38230SBarry Smith         a->diag[i] = j;
143617ab2063SBarry Smith         break;
143717ab2063SBarry Smith       }
143817ab2063SBarry Smith     }
143917ab2063SBarry Smith   }
14403a40ed3dSBarry Smith   PetscFunctionReturn(0);
144117ab2063SBarry Smith }
144217ab2063SBarry Smith 
1443be5855fcSBarry Smith /*
1444be5855fcSBarry Smith      Checks for missing diagonals
1445be5855fcSBarry Smith */
14464a2ae208SSatish Balay #undef __FUNCT__
14474a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1448ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1449be5855fcSBarry Smith {
1450be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
145197f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1452be5855fcSBarry Smith 
1453be5855fcSBarry Smith   PetscFunctionBegin;
145409f38230SBarry Smith   *missing = PETSC_FALSE;
1455d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
145609f38230SBarry Smith     *missing  = PETSC_TRUE;
145709f38230SBarry Smith     if (d) *d = 0;
145809f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
145909f38230SBarry Smith   } else {
1460f1e2ffcdSBarry Smith     diag = a->diag;
1461d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1462bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
146309f38230SBarry Smith 	*missing = PETSC_TRUE;
146409f38230SBarry Smith 	if (d) *d = i;
146509f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
146609f38230SBarry Smith       }
1467be5855fcSBarry Smith     }
1468be5855fcSBarry Smith   }
1469be5855fcSBarry Smith   PetscFunctionReturn(0);
1470be5855fcSBarry Smith }
1471be5855fcSBarry Smith 
147271f1c65dSBarry Smith EXTERN_C_BEGIN
147371f1c65dSBarry Smith #undef __FUNCT__
147471f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
14757087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
147671f1c65dSBarry Smith {
147771f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
147871f1c65dSBarry Smith   PetscErrorCode ierr;
1479d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
148054f21887SBarry Smith   MatScalar      *v = a->a;
148154f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
148271f1c65dSBarry Smith 
148371f1c65dSBarry Smith   PetscFunctionBegin;
148471f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
148571f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
148671f1c65dSBarry Smith   diag = a->diag;
148771f1c65dSBarry Smith   if (!a->idiag) {
148871f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
148971f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
149071f1c65dSBarry Smith     v        = a->a;
149171f1c65dSBarry Smith   }
149271f1c65dSBarry Smith   mdiag = a->mdiag;
149371f1c65dSBarry Smith   idiag = a->idiag;
149471f1c65dSBarry Smith 
1495028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
149671f1c65dSBarry Smith     for (i=0; i<m; i++) {
149771f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1498e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
149971f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
150071f1c65dSBarry Smith     }
150171f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
150271f1c65dSBarry Smith   } else {
150371f1c65dSBarry Smith     for (i=0; i<m; i++) {
150471f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
150571f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
150671f1c65dSBarry Smith     }
1507dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
150871f1c65dSBarry Smith   }
150971f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
151071f1c65dSBarry Smith   PetscFunctionReturn(0);
151171f1c65dSBarry Smith }
15125a9745a3SMatthew Knepley EXTERN_C_END
151371f1c65dSBarry Smith 
1514c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
15154a2ae208SSatish Balay #undef __FUNCT__
151641f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
151741f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
151817ab2063SBarry Smith {
1519416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1520e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1521e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
152254f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1523dfbe8321SBarry Smith   PetscErrorCode     ierr;
1524d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
152597f1f81fSBarry Smith   const PetscInt     *idx,*diag;
152617ab2063SBarry Smith 
15273a40ed3dSBarry Smith   PetscFunctionBegin;
1528b965ef7fSBarry Smith   its = its*lits;
152991723122SBarry Smith 
153071f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
153171f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
153271f1c65dSBarry Smith   a->fshift = fshift;
153371f1c65dSBarry Smith   a->omega  = omega;
1534ed480e8bSBarry Smith 
153571f1c65dSBarry Smith   diag = a->diag;
153671f1c65dSBarry Smith   t     = a->ssor_work;
1537ed480e8bSBarry Smith   idiag = a->idiag;
153871f1c65dSBarry Smith   mdiag = a->mdiag;
1539ed480e8bSBarry Smith 
15401ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
15413649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
154271f1c65dSBarry Smith   CHKMEMQ;
1543ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
154417ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
154517ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1546ed480e8bSBarry Smith     bs = b;
154717ab2063SBarry Smith     for (i=0; i<m; i++) {
154871f1c65dSBarry Smith         d    = fshift + mdiag[i];
1549416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1550ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1551ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
155217ab2063SBarry Smith         sum  = b[i]*d/omega;
1553003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
155417ab2063SBarry Smith         x[i] = sum;
155517ab2063SBarry Smith     }
15561ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15573649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1558efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
15593a40ed3dSBarry Smith     PetscFunctionReturn(0);
156017ab2063SBarry Smith   }
1561c783ea89SBarry Smith 
156248af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1563e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
15643a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
156517ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1566887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
156717ab2063SBarry Smith 
156817ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
156917ab2063SBarry Smith 
1570887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
157117ab2063SBarry Smith     */
157217ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
157317ab2063SBarry Smith 
157417ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
157517ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1576416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1577ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1578ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
157917ab2063SBarry Smith       sum  = b[i];
1580e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1581ed480e8bSBarry Smith       x[i] = sum*idiag[i];
158217ab2063SBarry Smith     }
158317ab2063SBarry Smith 
158417ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1585416022c9SBarry Smith     v = a->a;
1586ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
158717ab2063SBarry Smith 
158817ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1589ed480e8bSBarry Smith     ts = t;
1590416022c9SBarry Smith     diag = a->diag;
159117ab2063SBarry Smith     for (i=0; i<m; i++) {
1592416022c9SBarry Smith       n    = diag[i] - a->i[i];
1593ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1594ed480e8bSBarry Smith       v    = a->a + a->i[i];
159517ab2063SBarry Smith       sum  = t[i];
1596003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1597ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1598733d66baSBarry Smith       /*  x = x + t */
1599733d66baSBarry Smith       x[i] += t[i];
160017ab2063SBarry Smith     }
160117ab2063SBarry Smith 
1602dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
16031ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16043649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
16053a40ed3dSBarry Smith     PetscFunctionReturn(0);
160617ab2063SBarry Smith   }
160717ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
160817ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
160917ab2063SBarry Smith       for (i=0; i<m; i++) {
1610416022c9SBarry Smith         n    = diag[i] - a->i[i];
1611ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1612ed480e8bSBarry Smith         v    = a->a + a->i[i];
161317ab2063SBarry Smith         sum  = b[i];
1614e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
16155c99c7daSBarry Smith         t[i] = sum;
1616ed480e8bSBarry Smith         x[i] = sum*idiag[i];
161717ab2063SBarry Smith       }
16185c99c7daSBarry Smith       xb = t;
1619efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
16203a40ed3dSBarry Smith     } else xb = b;
162117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
162217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1623416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1624ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1625ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
162617ab2063SBarry Smith         sum  = xb[i];
1627e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
16285c99c7daSBarry Smith         if (xb == b) {
1629ed480e8bSBarry Smith           x[i] = sum*idiag[i];
16305c99c7daSBarry Smith         } else {
16315c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
163217ab2063SBarry Smith         }
16335c99c7daSBarry Smith       }
1634efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
163517ab2063SBarry Smith     }
163617ab2063SBarry Smith     its--;
163717ab2063SBarry Smith   }
163817ab2063SBarry Smith   while (its--) {
163917ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
164017ab2063SBarry Smith       for (i=0; i<m; i++) {
1641416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1642ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1643ed480e8bSBarry Smith         v    = a->a + a->i[i];
164417ab2063SBarry Smith         sum  = b[i];
1645e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1646ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
164717ab2063SBarry Smith       }
16489f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
164917ab2063SBarry Smith     }
165017ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
165117ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1652416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1653ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1654ed480e8bSBarry Smith         v    = a->a + a->i[i];
165517ab2063SBarry Smith         sum  = b[i];
1656e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1657ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
165817ab2063SBarry Smith       }
16599f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
166017ab2063SBarry Smith     }
166117ab2063SBarry Smith   }
16621ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16633649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
166471f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
166517ab2063SBarry Smith }
166617ab2063SBarry Smith 
16672af78befSBarry Smith 
16684a2ae208SSatish Balay #undef __FUNCT__
16694a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1670dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
167117ab2063SBarry Smith {
1672416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
16734e220ebcSLois Curfman McInnes 
16743a40ed3dSBarry Smith   PetscFunctionBegin;
16754e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
16764e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
16774e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
16784e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
16794e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
16808e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
16817adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1682d5f3da31SBarry Smith   if (A->factortype) {
16834e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
16844e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
16854e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
16864e220ebcSLois Curfman McInnes   } else {
16874e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
16884e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
16894e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
16904e220ebcSLois Curfman McInnes   }
16913a40ed3dSBarry Smith   PetscFunctionReturn(0);
169217ab2063SBarry Smith }
169317ab2063SBarry Smith 
16944a2ae208SSatish Balay #undef __FUNCT__
16954a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
16962b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
169717ab2063SBarry Smith {
1698416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
16993b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
17006849ba73SBarry Smith   PetscErrorCode    ierr;
170197b48c8fSBarry Smith   const PetscScalar *xx;
170297b48c8fSBarry Smith   PetscScalar       *bb;
1703ace3abfcSBarry Smith   PetscBool         missing;
170417ab2063SBarry Smith 
17053a40ed3dSBarry Smith   PetscFunctionBegin;
170697b48c8fSBarry Smith   if (x && b) {
170797b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
170897b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
170997b48c8fSBarry Smith     for (i=0; i<N; i++) {
171097b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
171197b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
171297b48c8fSBarry Smith     }
171397b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
171497b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
171597b48c8fSBarry Smith   }
171697b48c8fSBarry Smith 
1717a9817697SBarry Smith   if (a->keepnonzeropattern) {
1718f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1719e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1720bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1721f1e2ffcdSBarry Smith     }
1722f4df32b1SMatthew Knepley     if (diag != 0.0) {
172309f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1724e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1725f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1726f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1727f1e2ffcdSBarry Smith       }
1728f1e2ffcdSBarry Smith     }
172988e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1730f1e2ffcdSBarry Smith   } else {
1731f4df32b1SMatthew Knepley     if (diag != 0.0) {
173217ab2063SBarry Smith       for (i=0; i<N; i++) {
1733e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
17347ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1735416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1736f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1737bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
17387ae801bdSBarry Smith         } else { /* in case row was completely empty */
1739f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
174017ab2063SBarry Smith         }
174117ab2063SBarry Smith       }
17423a40ed3dSBarry Smith     } else {
174317ab2063SBarry Smith       for (i=0; i<N; i++) {
1744e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1745416022c9SBarry Smith         a->ilen[rows[i]] = 0;
174617ab2063SBarry Smith       }
174717ab2063SBarry Smith     }
174888e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1749f1e2ffcdSBarry Smith   }
175043a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17513a40ed3dSBarry Smith   PetscFunctionReturn(0);
175217ab2063SBarry Smith }
175317ab2063SBarry Smith 
17544a2ae208SSatish Balay #undef __FUNCT__
17556e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
17566e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
17576e169961SBarry Smith {
17586e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
17596e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
17606e169961SBarry Smith   PetscErrorCode    ierr;
17612b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
17626e169961SBarry Smith   const PetscScalar *xx;
17636e169961SBarry Smith   PetscScalar       *bb;
17646e169961SBarry Smith 
17656e169961SBarry Smith   PetscFunctionBegin;
17666e169961SBarry Smith   if (x && b) {
17676e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
17686e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
17692b40b63fSBarry Smith     vecs = PETSC_TRUE;
17706e169961SBarry Smith   }
17716e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
17726e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
17736e169961SBarry Smith   for (i=0; i<N; i++) {
17746e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
17756e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
17766e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
17776e169961SBarry Smith   }
17786e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
17796e169961SBarry Smith     if (!zeroed[i]) {
17806e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
17816e169961SBarry Smith         if (zeroed[a->j[j]]) {
17822b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
17836e169961SBarry Smith           a->a[j] = 0.0;
17846e169961SBarry Smith         }
17856e169961SBarry Smith       }
17862b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
17876e169961SBarry Smith   }
17886e169961SBarry Smith   if (x && b) {
17896e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
17906e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
17916e169961SBarry Smith   }
17926e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
17936e169961SBarry Smith   if (diag != 0.0) {
17946e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
17956e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
17966e169961SBarry Smith     for (i=0; i<N; i++) {
17976e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
17986e169961SBarry Smith     }
17996e169961SBarry Smith   }
18006e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
18016e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18026e169961SBarry Smith   PetscFunctionReturn(0);
18036e169961SBarry Smith }
18046e169961SBarry Smith 
18056e169961SBarry Smith #undef __FUNCT__
18064a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1807a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
180817ab2063SBarry Smith {
1809416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
181097f1f81fSBarry Smith   PetscInt   *itmp;
181117ab2063SBarry Smith 
18123a40ed3dSBarry Smith   PetscFunctionBegin;
1813e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
181417ab2063SBarry Smith 
1815416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1816bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
181717ab2063SBarry Smith   if (idx) {
1818bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1819bfeeae90SHong Zhang     if (*nz) {
18204e093b46SBarry Smith       *idx = itmp;
182117ab2063SBarry Smith     }
182217ab2063SBarry Smith     else *idx = 0;
182317ab2063SBarry Smith   }
18243a40ed3dSBarry Smith   PetscFunctionReturn(0);
182517ab2063SBarry Smith }
182617ab2063SBarry Smith 
1827bfeeae90SHong Zhang /* remove this function? */
18284a2ae208SSatish Balay #undef __FUNCT__
18294a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1830a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
183117ab2063SBarry Smith {
18323a40ed3dSBarry Smith   PetscFunctionBegin;
18333a40ed3dSBarry Smith   PetscFunctionReturn(0);
183417ab2063SBarry Smith }
183517ab2063SBarry Smith 
18364a2ae208SSatish Balay #undef __FUNCT__
18374a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1838dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
183917ab2063SBarry Smith {
1840416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
184154f21887SBarry Smith   MatScalar      *v = a->a;
184236db0b34SBarry Smith   PetscReal      sum = 0.0;
18436849ba73SBarry Smith   PetscErrorCode ierr;
184497f1f81fSBarry Smith   PetscInt       i,j;
184517ab2063SBarry Smith 
18463a40ed3dSBarry Smith   PetscFunctionBegin;
184717ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1848416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1849aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
185036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
185117ab2063SBarry Smith #else
185217ab2063SBarry Smith       sum += (*v)*(*v); v++;
185317ab2063SBarry Smith #endif
185417ab2063SBarry Smith     }
18558f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
18563a40ed3dSBarry Smith   } else if (type == NORM_1) {
185736db0b34SBarry Smith     PetscReal *tmp;
185897f1f81fSBarry Smith     PetscInt    *jj = a->j;
1859d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1860d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1861064f8208SBarry Smith     *nrm = 0.0;
1862416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1863bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
186417ab2063SBarry Smith     }
1865d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1866064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
186717ab2063SBarry Smith     }
1868606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
18693a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1870064f8208SBarry Smith     *nrm = 0.0;
1871d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1872bfeeae90SHong Zhang       v = a->a + a->i[j];
187317ab2063SBarry Smith       sum = 0.0;
1874416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1875cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
187617ab2063SBarry Smith       }
1877064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
187817ab2063SBarry Smith     }
18793a40ed3dSBarry Smith   } else {
1880e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
188117ab2063SBarry Smith   }
18823a40ed3dSBarry Smith   PetscFunctionReturn(0);
188317ab2063SBarry Smith }
188417ab2063SBarry Smith 
18854a2ae208SSatish Balay #undef __FUNCT__
18864a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1887fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
188817ab2063SBarry Smith {
1889416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1890416022c9SBarry Smith   Mat            C;
18916849ba73SBarry Smith   PetscErrorCode ierr;
1892d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
189354f21887SBarry Smith   MatScalar      *array = a->a;
189417ab2063SBarry Smith 
18953a40ed3dSBarry Smith   PetscFunctionBegin;
1896e32f2f54SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1897fc4dec0aSBarry Smith 
1898fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1899d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1900d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1901bfeeae90SHong Zhang 
1902bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
19037adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1904d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
19057adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1906ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1907606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1908a541d17aSBarry Smith   } else {
1909a541d17aSBarry Smith     C = *B;
1910a541d17aSBarry Smith   }
1911a541d17aSBarry Smith 
191217ab2063SBarry Smith   for (i=0; i<m; i++) {
191317ab2063SBarry Smith     len    = ai[i+1]-ai[i];
191487d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1915b9b97703SBarry Smith     array += len;
1916b9b97703SBarry Smith     aj    += len;
191717ab2063SBarry Smith   }
19186d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19196d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
192017ab2063SBarry Smith 
1921815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1922416022c9SBarry Smith     *B = C;
192317ab2063SBarry Smith   } else {
1924eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
192517ab2063SBarry Smith   }
19263a40ed3dSBarry Smith   PetscFunctionReturn(0);
192717ab2063SBarry Smith }
192817ab2063SBarry Smith 
1929cd0d46ebSvictorle EXTERN_C_BEGIN
1930cd0d46ebSvictorle #undef __FUNCT__
19315fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
19327087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1933cd0d46ebSvictorle {
1934cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
193554f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
193654f21887SBarry Smith   MatScalar      *va,*vb;
19376849ba73SBarry Smith   PetscErrorCode ierr;
193897f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1939cd0d46ebSvictorle 
1940cd0d46ebSvictorle   PetscFunctionBegin;
1941cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1942cd0d46ebSvictorle 
1943cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1944cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
19455485867bSBarry Smith   if (ma!=nb || na!=mb){
19465485867bSBarry Smith     *f = PETSC_FALSE;
19475485867bSBarry Smith     PetscFunctionReturn(0);
19485485867bSBarry Smith   }
1949cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1950cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1951cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
195297f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
195397f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1954cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1955cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1956cd0d46ebSvictorle 
1957cd0d46ebSvictorle   *f = PETSC_TRUE;
1958cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1959cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
196097f1f81fSBarry Smith       PetscInt         idc,idr;
19615485867bSBarry Smith       PetscScalar vc,vr;
1962cd0d46ebSvictorle       /* column/row index/value */
19635485867bSBarry Smith       idc = adx[aptr[i]];
19645485867bSBarry Smith       idr = bdx[bptr[idc]];
19655485867bSBarry Smith       vc  = va[aptr[i]];
19665485867bSBarry Smith       vr  = vb[bptr[idc]];
19675485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
19685485867bSBarry Smith         *f = PETSC_FALSE;
19695485867bSBarry Smith         goto done;
1970cd0d46ebSvictorle       } else {
19715485867bSBarry Smith         aptr[i]++;
19725485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1973cd0d46ebSvictorle       }
1974cd0d46ebSvictorle     }
1975cd0d46ebSvictorle   }
1976cd0d46ebSvictorle  done:
1977cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
19783aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
1979cd0d46ebSvictorle   PetscFunctionReturn(0);
1980cd0d46ebSvictorle }
1981cd0d46ebSvictorle EXTERN_C_END
1982cd0d46ebSvictorle 
19831cbb95d3SBarry Smith EXTERN_C_BEGIN
19841cbb95d3SBarry Smith #undef __FUNCT__
19851cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
19867087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
19871cbb95d3SBarry Smith {
19881cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
198954f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
199054f21887SBarry Smith   MatScalar      *va,*vb;
19911cbb95d3SBarry Smith   PetscErrorCode ierr;
19921cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
19931cbb95d3SBarry Smith 
19941cbb95d3SBarry Smith   PetscFunctionBegin;
19951cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
19961cbb95d3SBarry Smith 
19971cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
19981cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
19991cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
20001cbb95d3SBarry Smith     *f = PETSC_FALSE;
20011cbb95d3SBarry Smith     PetscFunctionReturn(0);
20021cbb95d3SBarry Smith   }
20031cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
20041cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
20051cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
20061cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
20071cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
20081cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
20091cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
20101cbb95d3SBarry Smith 
20111cbb95d3SBarry Smith   *f = PETSC_TRUE;
20121cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
20131cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
20141cbb95d3SBarry Smith       PetscInt         idc,idr;
20151cbb95d3SBarry Smith       PetscScalar vc,vr;
20161cbb95d3SBarry Smith       /* column/row index/value */
20171cbb95d3SBarry Smith       idc = adx[aptr[i]];
20181cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
20191cbb95d3SBarry Smith       vc  = va[aptr[i]];
20201cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
20211cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
20221cbb95d3SBarry Smith         *f = PETSC_FALSE;
20231cbb95d3SBarry Smith         goto done;
20241cbb95d3SBarry Smith       } else {
20251cbb95d3SBarry Smith         aptr[i]++;
20261cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
20271cbb95d3SBarry Smith       }
20281cbb95d3SBarry Smith     }
20291cbb95d3SBarry Smith   }
20301cbb95d3SBarry Smith  done:
20311cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
20321cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
20331cbb95d3SBarry Smith   PetscFunctionReturn(0);
20341cbb95d3SBarry Smith }
20351cbb95d3SBarry Smith EXTERN_C_END
20361cbb95d3SBarry Smith 
20379e29f15eSvictorle #undef __FUNCT__
20389e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2039ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20409e29f15eSvictorle {
2041dfbe8321SBarry Smith   PetscErrorCode ierr;
20429e29f15eSvictorle   PetscFunctionBegin;
20435485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20449e29f15eSvictorle   PetscFunctionReturn(0);
20459e29f15eSvictorle }
20469e29f15eSvictorle 
20474a2ae208SSatish Balay #undef __FUNCT__
20481cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2049ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20501cbb95d3SBarry Smith {
20511cbb95d3SBarry Smith   PetscErrorCode ierr;
20521cbb95d3SBarry Smith   PetscFunctionBegin;
20531cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20541cbb95d3SBarry Smith   PetscFunctionReturn(0);
20551cbb95d3SBarry Smith }
20561cbb95d3SBarry Smith 
20571cbb95d3SBarry Smith #undef __FUNCT__
20584a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2059dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
206017ab2063SBarry Smith {
2061416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
206254f21887SBarry Smith   PetscScalar    *l,*r,x;
206354f21887SBarry Smith   MatScalar      *v;
2064dfbe8321SBarry Smith   PetscErrorCode ierr;
2065d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
206617ab2063SBarry Smith 
20673a40ed3dSBarry Smith   PetscFunctionBegin;
206817ab2063SBarry Smith   if (ll) {
20693ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
20703ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2071e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2072e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
20731ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2074416022c9SBarry Smith     v = a->a;
207517ab2063SBarry Smith     for (i=0; i<m; i++) {
207617ab2063SBarry Smith       x = l[i];
2077416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
207817ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
207917ab2063SBarry Smith     }
20801ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2081efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
208217ab2063SBarry Smith   }
208317ab2063SBarry Smith   if (rr) {
2084e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2085e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
20861ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2087416022c9SBarry Smith     v = a->a; jj = a->j;
208817ab2063SBarry Smith     for (i=0; i<nz; i++) {
2089bfeeae90SHong Zhang       (*v++) *= r[*jj++];
209017ab2063SBarry Smith     }
20911ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2092efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
209317ab2063SBarry Smith   }
209486c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
209586c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
20963a40ed3dSBarry Smith   PetscFunctionReturn(0);
209717ab2063SBarry Smith }
209817ab2063SBarry Smith 
20994a2ae208SSatish Balay #undef __FUNCT__
21004a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
210197f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
210217ab2063SBarry Smith {
2103db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
21046849ba73SBarry Smith   PetscErrorCode ierr;
2105d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
210697f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
21075d0c19d7SBarry Smith   const PetscInt *irow,*icol;
21085d0c19d7SBarry Smith   PetscInt       nrows,ncols;
210997f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
211054f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2111416022c9SBarry Smith   Mat            C;
2112ace3abfcSBarry Smith   PetscBool      stride,sorted;
211317ab2063SBarry Smith 
21143a40ed3dSBarry Smith   PetscFunctionBegin;
211514ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
2116e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
211714ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
2118e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
211999141d43SSatish Balay 
212017ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2121b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2122b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
212317ab2063SBarry Smith 
2124fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
21250dbe5b1eSSatish Balay   ierr = PetscTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2126fee21e36SBarry Smith   if (stride && step == 1) {
212702834360SBarry Smith     /* special case of contiguous rows */
21280e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
212902834360SBarry Smith     /* loop over new rows determining lens and starting points */
213002834360SBarry Smith     for (i=0; i<nrows; i++) {
2131bfeeae90SHong Zhang       kstart  = ai[irow[i]];
2132a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
213302834360SBarry Smith       for (k=kstart; k<kend; k++) {
2134bfeeae90SHong Zhang         if (aj[k] >= first) {
213502834360SBarry Smith           starts[i] = k;
213602834360SBarry Smith           break;
213702834360SBarry Smith 	}
213802834360SBarry Smith       }
2139a2744918SBarry Smith       sum = 0;
214002834360SBarry Smith       while (k < kend) {
2141bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2142a2744918SBarry Smith         sum++;
214302834360SBarry Smith       }
2144a2744918SBarry Smith       lens[i] = sum;
214502834360SBarry Smith     }
214602834360SBarry Smith     /* create submatrix */
2147cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
214897f1f81fSBarry Smith       PetscInt n_cols,n_rows;
214908480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2150e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2151d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
215208480c60SBarry Smith       C = *B;
21533a40ed3dSBarry Smith     } else {
21547adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2155f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
21567adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2157ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
215808480c60SBarry Smith     }
2159db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2160db02288aSLois Curfman McInnes 
216102834360SBarry Smith     /* loop over rows inserting into submatrix */
2162db02288aSLois Curfman McInnes     a_new    = c->a;
2163db02288aSLois Curfman McInnes     j_new    = c->j;
2164db02288aSLois Curfman McInnes     i_new    = c->i;
2165bfeeae90SHong Zhang 
216602834360SBarry Smith     for (i=0; i<nrows; i++) {
2167a2744918SBarry Smith       ii    = starts[i];
2168a2744918SBarry Smith       lensi = lens[i];
2169a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2170a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
217102834360SBarry Smith       }
217287828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2173a2744918SBarry Smith       a_new      += lensi;
2174a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
2175a2744918SBarry Smith       c->ilen[i]  = lensi;
217602834360SBarry Smith     }
21770e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
21783a40ed3dSBarry Smith   } else {
217902834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
21800e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
218197f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
21820e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
21834dcab191SBarry Smith     for (i=0; i<ncols; i++) {
21844dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
21854dcab191SBarry 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);
21864dcab191SBarry Smith #endif
21874dcab191SBarry Smith       smap[icol[i]] = i+1;
21884dcab191SBarry Smith     }
21894dcab191SBarry Smith 
219002834360SBarry Smith     /* determine lens of each row */
219102834360SBarry Smith     for (i=0; i<nrows; i++) {
2192bfeeae90SHong Zhang       kstart  = ai[irow[i]];
219302834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
219402834360SBarry Smith       lens[i] = 0;
219502834360SBarry Smith       for (k=kstart; k<kend; k++) {
2196bfeeae90SHong Zhang         if (smap[aj[k]]) {
219702834360SBarry Smith           lens[i]++;
219802834360SBarry Smith         }
219902834360SBarry Smith       }
220002834360SBarry Smith     }
220117ab2063SBarry Smith     /* Create and fill new matrix */
2202a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2203ace3abfcSBarry Smith       PetscBool  equal;
22040f5bd95cSBarry Smith 
220599141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
2206e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2207d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
22080f5bd95cSBarry Smith       if (!equal) {
2209e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
221099141d43SSatish Balay       }
2211d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
221208480c60SBarry Smith       C = *B;
22133a40ed3dSBarry Smith     } else {
22147adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2215f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
22167adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2217ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
221808480c60SBarry Smith     }
221999141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
222017ab2063SBarry Smith     for (i=0; i<nrows; i++) {
222199141d43SSatish Balay       row    = irow[i];
2222bfeeae90SHong Zhang       kstart = ai[row];
222399141d43SSatish Balay       kend   = kstart + a->ilen[row];
2224bfeeae90SHong Zhang       mat_i  = c->i[i];
222599141d43SSatish Balay       mat_j  = c->j + mat_i;
222699141d43SSatish Balay       mat_a  = c->a + mat_i;
222799141d43SSatish Balay       mat_ilen = c->ilen + i;
222817ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2229bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2230ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
223199141d43SSatish Balay           *mat_a++ = a->a[k];
223299141d43SSatish Balay           (*mat_ilen)++;
223399141d43SSatish Balay 
223417ab2063SBarry Smith         }
223517ab2063SBarry Smith       }
223617ab2063SBarry Smith     }
223702834360SBarry Smith     /* Free work space */
223802834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2239606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2240606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
224102834360SBarry Smith   }
22426d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22436d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
224417ab2063SBarry Smith 
224517ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2246416022c9SBarry Smith   *B = C;
22473a40ed3dSBarry Smith   PetscFunctionReturn(0);
224817ab2063SBarry Smith }
224917ab2063SBarry Smith 
22501df811f5SHong Zhang #undef __FUNCT__
225182d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
225282d44351SHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,Mat* subMat)
225382d44351SHong Zhang {
225482d44351SHong Zhang   PetscErrorCode ierr;
225582d44351SHong Zhang   Mat            B;
225682d44351SHong Zhang 
225782d44351SHong Zhang   PetscFunctionBegin;
225882d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
225982d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
226082d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
226182d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
226282d44351SHong Zhang   *subMat = B;
226382d44351SHong Zhang   PetscFunctionReturn(0);
226482d44351SHong Zhang }
226582d44351SHong Zhang 
226682d44351SHong Zhang #undef __FUNCT__
22674a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
22680481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2269a871dcd8SBarry Smith {
227063b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2271dfbe8321SBarry Smith   PetscErrorCode ierr;
227263b91edcSBarry Smith   Mat            outA;
2273ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
227463b91edcSBarry Smith 
22753a40ed3dSBarry Smith   PetscFunctionBegin;
2276e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
22771df811f5SHong Zhang 
2278b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2279b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2280a871dcd8SBarry Smith 
228163b91edcSBarry Smith   outA              = inA;
2282d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
2283c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
22846bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
2285c3122656SLisandro Dalcin   a->row = row;
2286c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
22876bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
2288c3122656SLisandro Dalcin   a->col = col;
228963b91edcSBarry Smith 
229036db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
22916bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
22924c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
229352e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2294f0ec6fceSSatish Balay 
229594a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2296d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2297d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
229894a9d846SBarry Smith   }
229963b91edcSBarry Smith 
2300f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2301137fb511SHong Zhang   if (row_identity && col_identity) {
2302ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2303137fb511SHong Zhang   } else {
2304719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2305137fb511SHong Zhang   }
23063a40ed3dSBarry Smith   PetscFunctionReturn(0);
2307a871dcd8SBarry Smith }
2308a871dcd8SBarry Smith 
23094a2ae208SSatish Balay #undef __FUNCT__
23104a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2311f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2312f0b747eeSBarry Smith {
2313f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2314f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2315efee365bSSatish Balay   PetscErrorCode ierr;
23160805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
23173a40ed3dSBarry Smith 
23183a40ed3dSBarry Smith   PetscFunctionBegin;
2319f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
2320efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
232186c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
232286c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
23233a40ed3dSBarry Smith   PetscFunctionReturn(0);
2324f0b747eeSBarry Smith }
2325f0b747eeSBarry Smith 
23264a2ae208SSatish Balay #undef __FUNCT__
23274a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
232897f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2329cddf8d76SBarry Smith {
2330dfbe8321SBarry Smith   PetscErrorCode ierr;
233197f1f81fSBarry Smith   PetscInt       i;
2332cddf8d76SBarry Smith 
23333a40ed3dSBarry Smith   PetscFunctionBegin;
2334cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2335b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2336cddf8d76SBarry Smith   }
2337cddf8d76SBarry Smith 
2338cddf8d76SBarry Smith   for (i=0; i<n; i++) {
23396a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2340cddf8d76SBarry Smith   }
23413a40ed3dSBarry Smith   PetscFunctionReturn(0);
2342cddf8d76SBarry Smith }
2343cddf8d76SBarry Smith 
23444a2ae208SSatish Balay #undef __FUNCT__
23454a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
234697f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
23474dcbc457SBarry Smith {
2348e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
23496849ba73SBarry Smith   PetscErrorCode ierr;
23505d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
23515d0c19d7SBarry Smith   const PetscInt *idx;
235297f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2353f1af5d2fSBarry Smith   PetscBT        table;
2354bbd702dbSSatish Balay 
23553a40ed3dSBarry Smith   PetscFunctionBegin;
2356d0f46423SBarry Smith   m     = A->rmap->n;
2357e4d965acSSatish Balay   ai    = a->i;
2358bfeeae90SHong Zhang   aj    = a->j;
23598a047759SSatish Balay 
2360e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
236106763907SSatish Balay 
236297f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
23636831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
236406763907SSatish Balay 
2365e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2366b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2367e4d965acSSatish Balay     isz  = 0;
23686831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2369e4d965acSSatish Balay 
2370e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
23714dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2372b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2373e4d965acSSatish Balay 
2374dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2375e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2376f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
23774dcbc457SBarry Smith     }
237806763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
23796bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2380e4d965acSSatish Balay 
238104a348a9SBarry Smith     k = 0;
238204a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
238304a348a9SBarry Smith       n = isz;
238406763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2385e4d965acSSatish Balay         row   = nidx[k];
2386e4d965acSSatish Balay         start = ai[row];
2387e4d965acSSatish Balay         end   = ai[row+1];
238804a348a9SBarry Smith         for (l = start; l<end ; l++){
2389efb16452SHong Zhang           val = aj[l] ;
2390f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2391e4d965acSSatish Balay         }
2392e4d965acSSatish Balay       }
2393e4d965acSSatish Balay     }
239470b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2395e4d965acSSatish Balay   }
23966831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
2397606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
23983a40ed3dSBarry Smith   PetscFunctionReturn(0);
23994dcbc457SBarry Smith }
240017ab2063SBarry Smith 
24010513a670SBarry Smith /* -------------------------------------------------------------- */
24024a2ae208SSatish Balay #undef __FUNCT__
24034a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2404dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
24050513a670SBarry Smith {
24060513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
24076849ba73SBarry Smith   PetscErrorCode ierr;
24083b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
24095d0c19d7SBarry Smith   const PetscInt *row,*col;
24105d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
241156cd22aeSBarry Smith   IS             icolp,irowp;
24123b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
24133b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
24140513a670SBarry Smith 
24153a40ed3dSBarry Smith   PetscFunctionBegin;
24164c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
241756cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
24184c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
241956cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
24200513a670SBarry Smith 
24210513a670SBarry Smith   /* determine lengths of permuted rows */
242297f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
24230513a670SBarry Smith   for (i=0; i<m; i++) {
24240513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
24250513a670SBarry Smith   }
24267adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2427f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
24287adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2429ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2430606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
24310513a670SBarry Smith 
243297f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
24330513a670SBarry Smith   for (i=0; i<m; i++) {
243432ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24350513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2436cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
243732ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24380513a670SBarry Smith   }
2439606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
24403c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
24410513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24420513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
244356cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
244456cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
24456bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
24466bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
24473a40ed3dSBarry Smith   PetscFunctionReturn(0);
24480513a670SBarry Smith }
24490513a670SBarry Smith 
24504a2ae208SSatish Balay #undef __FUNCT__
24514a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2452dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2453cb5b572fSBarry Smith {
2454dfbe8321SBarry Smith   PetscErrorCode ierr;
2455cb5b572fSBarry Smith 
2456cb5b572fSBarry Smith   PetscFunctionBegin;
245733f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
245833f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2459be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2460be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2461be6bf707SBarry Smith 
2462700c5bfcSBarry 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");
2463d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2464cb5b572fSBarry Smith   } else {
2465cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2466cb5b572fSBarry Smith   }
2467cb5b572fSBarry Smith   PetscFunctionReturn(0);
2468cb5b572fSBarry Smith }
2469cb5b572fSBarry Smith 
24704a2ae208SSatish Balay #undef __FUNCT__
24714a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2472dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2473273d9f13SBarry Smith {
2474dfbe8321SBarry Smith   PetscErrorCode ierr;
2475273d9f13SBarry Smith 
2476273d9f13SBarry Smith   PetscFunctionBegin;
2477ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2478273d9f13SBarry Smith   PetscFunctionReturn(0);
2479273d9f13SBarry Smith }
2480273d9f13SBarry Smith 
24814a2ae208SSatish Balay #undef __FUNCT__
24824a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2483a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
24846c0721eeSBarry Smith {
24856c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
24866c0721eeSBarry Smith   PetscFunctionBegin;
24876c0721eeSBarry Smith   *array = a->a;
24886c0721eeSBarry Smith   PetscFunctionReturn(0);
24896c0721eeSBarry Smith }
24906c0721eeSBarry Smith 
24914a2ae208SSatish Balay #undef __FUNCT__
24924a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2493dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
24946c0721eeSBarry Smith {
24956c0721eeSBarry Smith   PetscFunctionBegin;
24966c0721eeSBarry Smith   PetscFunctionReturn(0);
24976c0721eeSBarry Smith }
2498273d9f13SBarry Smith 
2499ee4f033dSBarry Smith #undef __FUNCT__
2500ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2501dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2502ee4f033dSBarry Smith {
25036849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
25046849ba73SBarry Smith   PetscErrorCode ierr;
250597f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2506efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
250787828ca2SBarry Smith   PetscScalar    *vscale_array;
2508ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2509ee4f033dSBarry Smith   Vec            w1,w2,w3;
2510ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2511ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2512ee4f033dSBarry Smith 
2513ee4f033dSBarry Smith   PetscFunctionBegin;
2514ee4f033dSBarry Smith   if (!coloring->w1) {
2515ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
251652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2517ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
251852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2519ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
252052e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2521ee4f033dSBarry Smith   }
2522ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2523ee4f033dSBarry Smith 
2524ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2525acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2526ee4f033dSBarry Smith   if (flg) {
2527ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2528ee4f033dSBarry Smith   } else {
2529ace3abfcSBarry Smith     PetscBool  assembled;
25300b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
25310b9b6f31SBarry Smith     if (assembled) {
2532ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2533ee4f033dSBarry Smith     }
25340b9b6f31SBarry Smith   }
2535ee4f033dSBarry Smith 
2536ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2537ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2538ee4f033dSBarry Smith 
2539ee4f033dSBarry Smith   /*
2540ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2541ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2542ee4f033dSBarry Smith   */
2543ee4f033dSBarry Smith   if (coloring->F) {
2544ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2545ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2546ee4f033dSBarry Smith     if (m1 != m2) {
2547ee4f033dSBarry Smith       coloring->F = 0;
2548ee4f033dSBarry Smith     }
2549ee4f033dSBarry Smith   }
2550ee4f033dSBarry Smith 
2551ee4f033dSBarry Smith   if (coloring->F) {
2552ee4f033dSBarry Smith     w1          = coloring->F;
2553ee4f033dSBarry Smith     coloring->F = 0;
2554ee4f033dSBarry Smith   } else {
255566f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2556ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
255766f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2558ee4f033dSBarry Smith   }
2559ee4f033dSBarry Smith 
2560ee4f033dSBarry Smith   /*
2561ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2562ee4f033dSBarry Smith   */
25631ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
25641ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2565ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2566ee4f033dSBarry Smith     /*
2567ee4f033dSBarry Smith        Loop over each column associated with color adding the
2568ee4f033dSBarry Smith        perturbation to the vector w3.
2569ee4f033dSBarry Smith     */
2570ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2571ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2572ee4f033dSBarry Smith       dx  = xx[col];
2573ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2574ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2575ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2576ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2577ee4f033dSBarry Smith #else
2578ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2579ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2580ee4f033dSBarry Smith #endif
2581ee4f033dSBarry Smith       dx                *= epsilon;
2582ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2583ee4f033dSBarry Smith     }
2584ee4f033dSBarry Smith   }
25851ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2586ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2587ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2588ee4f033dSBarry Smith 
2589ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2590ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2591ee4f033dSBarry Smith 
2592ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2593ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2594ee4f033dSBarry Smith 
25951ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2596ee4f033dSBarry Smith   /*
2597ee4f033dSBarry Smith       Loop over each color
2598ee4f033dSBarry Smith   */
2599ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
260049b058dcSBarry Smith     coloring->currentcolor = k;
2601ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
26021ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2603ee4f033dSBarry Smith     /*
2604ee4f033dSBarry Smith        Loop over each column associated with color adding the
2605ee4f033dSBarry Smith        perturbation to the vector w3.
2606ee4f033dSBarry Smith     */
2607ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2608ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2609ee4f033dSBarry Smith       dx  = xx[col];
26105b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2611ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2612ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2613ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2614ee4f033dSBarry Smith #else
2615ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2616ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2617ee4f033dSBarry Smith #endif
2618ee4f033dSBarry Smith       dx            *= epsilon;
2619e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2620ee4f033dSBarry Smith       w3_array[col] += dx;
2621ee4f033dSBarry Smith     }
26221ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2623ee4f033dSBarry Smith 
2624ee4f033dSBarry Smith     /*
2625ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2626ee4f033dSBarry Smith     */
2627ee4f033dSBarry Smith 
262866f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2629ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
263066f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2631efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2632ee4f033dSBarry Smith 
2633ee4f033dSBarry Smith     /*
2634ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2635ee4f033dSBarry Smith     */
26361ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2637ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2638ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2639ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2640ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2641ee4f033dSBarry Smith       srow   = row + start;
2642ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2643ee4f033dSBarry Smith     }
26441ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2645ee4f033dSBarry Smith   }
264649b058dcSBarry Smith   coloring->currentcolor = k;
26471ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
26481ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2649ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2650ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2651ee4f033dSBarry Smith   PetscFunctionReturn(0);
2652ee4f033dSBarry Smith }
2653ee4f033dSBarry Smith 
26548229c054SShri Abhyankar /*
26558229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
26568229c054SShri Abhyankar    have different nonzero structure.
26578229c054SShri Abhyankar */
2658ac90fabeSBarry Smith #undef __FUNCT__
26598229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
26608229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2661ec7775f6SShri Abhyankar {
26628229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2663ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2664ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2665ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2666ec7775f6SShri Abhyankar 
2667ec7775f6SShri Abhyankar   PetscFunctionBegin;
2668ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2669ec7775f6SShri Abhyankar   for(i=0; i<m; i++) {
26708af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
26718af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
26728af7cee1SJed Brown     nnz[i] = 0;
26738af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
26748af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
26758af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
26768af7cee1SJed Brown       nnz[i]++;
26778af7cee1SJed Brown     }
26788af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2679ec7775f6SShri Abhyankar   }
2680ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2681ec7775f6SShri Abhyankar }
2682ec7775f6SShri Abhyankar 
2683ec7775f6SShri Abhyankar #undef __FUNCT__
2684ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2685f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2686ac90fabeSBarry Smith {
2687dfbe8321SBarry Smith   PetscErrorCode ierr;
268897f1f81fSBarry Smith   PetscInt       i;
2689ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
26900805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2691ac90fabeSBarry Smith 
2692ac90fabeSBarry Smith   PetscFunctionBegin;
2693ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2694f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2695f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
269686c113feSBarry Smith     y->idiagvalid  = PETSC_FALSE;
269786c113feSBarry Smith     y->ibdiagvalid = PETSC_FALSE;
2698c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2699a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2700a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
27016bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2702a30b2313SHong Zhang     }
2703a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2704d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2705a30b2313SHong Zhang       y->XtoY = X;
2706407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2707c537a176SHong Zhang     }
2708f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
27091e2582c4SBarry Smith     ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %d/%d = %G\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz);CHKERRQ(ierr);
2710ac90fabeSBarry Smith   } else {
27118229c054SShri Abhyankar     Mat      B;
27128229c054SShri Abhyankar     PetscInt *nnz;
271316b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2714ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2715bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
27164aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2717176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
27188229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
27198229c054SShri Abhyankar     ierr = MatSeqAIJSetPreallocation(B,PETSC_NULL,nnz);CHKERRQ(ierr);
2720ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2721ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
27228229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2723ac90fabeSBarry Smith   }
2724ac90fabeSBarry Smith   PetscFunctionReturn(0);
2725ac90fabeSBarry Smith }
2726ac90fabeSBarry Smith 
2727521d7252SBarry Smith #undef __FUNCT__
2728521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2729521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2730521d7252SBarry Smith {
273141c166b1SJed Brown   PetscErrorCode ierr;
273241c166b1SJed Brown 
2733521d7252SBarry Smith   PetscFunctionBegin;
273441c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
273541c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2736521d7252SBarry Smith   PetscFunctionReturn(0);
2737521d7252SBarry Smith }
2738521d7252SBarry Smith 
2739354c94deSBarry Smith #undef __FUNCT__
2740354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
27417087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2742354c94deSBarry Smith {
2743354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2744354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2745354c94deSBarry Smith   PetscInt    i,nz;
2746354c94deSBarry Smith   PetscScalar *a;
2747354c94deSBarry Smith 
2748354c94deSBarry Smith   PetscFunctionBegin;
2749354c94deSBarry Smith   nz = aij->nz;
2750354c94deSBarry Smith   a  = aij->a;
2751354c94deSBarry Smith   for (i=0; i<nz; i++) {
2752354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2753354c94deSBarry Smith   }
2754354c94deSBarry Smith #else
2755354c94deSBarry Smith   PetscFunctionBegin;
2756354c94deSBarry Smith #endif
2757354c94deSBarry Smith   PetscFunctionReturn(0);
2758354c94deSBarry Smith }
2759354c94deSBarry Smith 
2760e34fafa9SBarry Smith #undef __FUNCT__
2761985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2762985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2763e34fafa9SBarry Smith {
2764e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2765e34fafa9SBarry Smith   PetscErrorCode ierr;
2766d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2767e34fafa9SBarry Smith   PetscReal      atmp;
2768985db425SBarry Smith   PetscScalar    *x;
2769e34fafa9SBarry Smith   MatScalar      *aa;
2770e34fafa9SBarry Smith 
2771e34fafa9SBarry Smith   PetscFunctionBegin;
2772e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2773e34fafa9SBarry Smith   aa   = a->a;
2774e34fafa9SBarry Smith   ai   = a->i;
2775e34fafa9SBarry Smith   aj   = a->j;
2776e34fafa9SBarry Smith 
2777985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2778e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2779e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2780e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2781e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2782e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
27839189402eSHong Zhang     x[i] = 0.0;
2784e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2785985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2786985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2787985db425SBarry Smith       aa++; aj++;
2788985db425SBarry Smith     }
2789985db425SBarry Smith   }
2790985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2791985db425SBarry Smith   PetscFunctionReturn(0);
2792985db425SBarry Smith }
2793985db425SBarry Smith 
2794985db425SBarry Smith #undef __FUNCT__
2795985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2796985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2797985db425SBarry Smith {
2798985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2799985db425SBarry Smith   PetscErrorCode ierr;
2800d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2801985db425SBarry Smith   PetscScalar    *x;
2802985db425SBarry Smith   MatScalar      *aa;
2803985db425SBarry Smith 
2804985db425SBarry Smith   PetscFunctionBegin;
2805e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2806985db425SBarry Smith   aa   = a->a;
2807985db425SBarry Smith   ai   = a->i;
2808985db425SBarry Smith   aj   = a->j;
2809985db425SBarry Smith 
2810985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2811985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2812985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2813e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2814985db425SBarry Smith   for (i=0; i<m; i++) {
2815985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2816d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2817985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2818985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2819985db425SBarry Smith       x[i] = 0.0;
2820985db425SBarry Smith       if (idx) {
2821985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2822985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2823985db425SBarry Smith           if (aj[j] > j) {
2824985db425SBarry Smith             idx[i] = j;
2825985db425SBarry Smith             break;
2826985db425SBarry Smith           }
2827985db425SBarry Smith         }
2828985db425SBarry Smith       }
2829985db425SBarry Smith     }
2830985db425SBarry Smith     for (j=0; j<ncols; j++){
2831985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2832985db425SBarry Smith       aa++; aj++;
2833985db425SBarry Smith     }
2834985db425SBarry Smith   }
2835985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2836985db425SBarry Smith   PetscFunctionReturn(0);
2837985db425SBarry Smith }
2838985db425SBarry Smith 
2839985db425SBarry Smith #undef __FUNCT__
2840c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2841c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2842c87e5d42SMatthew Knepley {
2843c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2844c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2845c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2846c87e5d42SMatthew Knepley   PetscReal      atmp;
2847c87e5d42SMatthew Knepley   PetscScalar    *x;
2848c87e5d42SMatthew Knepley   MatScalar      *aa;
2849c87e5d42SMatthew Knepley 
2850c87e5d42SMatthew Knepley   PetscFunctionBegin;
2851e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2852c87e5d42SMatthew Knepley   aa   = a->a;
2853c87e5d42SMatthew Knepley   ai   = a->i;
2854c87e5d42SMatthew Knepley   aj   = a->j;
2855c87e5d42SMatthew Knepley 
2856c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2857c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2858c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2859e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2860c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2861c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2862289a08f5SMatthew Knepley     if (ncols) {
2863289a08f5SMatthew Knepley       /* Get first nonzero */
2864289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2865289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2866289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2867289a08f5SMatthew Knepley       }
2868289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2869289a08f5SMatthew Knepley     } else {
2870289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2871289a08f5SMatthew Knepley     }
2872c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2873c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2874289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2875c87e5d42SMatthew Knepley       aa++; aj++;
2876c87e5d42SMatthew Knepley     }
2877c87e5d42SMatthew Knepley   }
2878c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2879c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2880c87e5d42SMatthew Knepley }
2881c87e5d42SMatthew Knepley 
2882c87e5d42SMatthew Knepley #undef __FUNCT__
2883985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2884985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2885985db425SBarry Smith {
2886985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2887985db425SBarry Smith   PetscErrorCode ierr;
2888d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2889985db425SBarry Smith   PetscScalar    *x;
2890985db425SBarry Smith   MatScalar      *aa;
2891985db425SBarry Smith 
2892985db425SBarry Smith   PetscFunctionBegin;
2893e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2894985db425SBarry Smith   aa   = a->a;
2895985db425SBarry Smith   ai   = a->i;
2896985db425SBarry Smith   aj   = a->j;
2897985db425SBarry Smith 
2898985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2899985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2900985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2901e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2902985db425SBarry Smith   for (i=0; i<m; i++) {
2903985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2904d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2905985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2906985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2907985db425SBarry Smith       x[i] = 0.0;
2908985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2909985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2910985db425SBarry Smith         for (j=0;j<ncols;j++) {
2911985db425SBarry Smith           if (aj[j] > j) {
2912985db425SBarry Smith             idx[i] = j;
2913985db425SBarry Smith             break;
2914985db425SBarry Smith           }
2915985db425SBarry Smith         }
2916985db425SBarry Smith       }
2917985db425SBarry Smith     }
2918985db425SBarry Smith     for (j=0; j<ncols; j++){
2919985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2920985db425SBarry Smith       aa++; aj++;
2921e34fafa9SBarry Smith     }
2922e34fafa9SBarry Smith   }
2923e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2924e34fafa9SBarry Smith   PetscFunctionReturn(0);
2925e34fafa9SBarry Smith }
2926bbead8a2SBarry Smith 
2927bbead8a2SBarry Smith #include <petscblaslapack.h>
2928bbead8a2SBarry Smith #include <../src/mat/blockinvert.h>
2929bbead8a2SBarry Smith 
2930bbead8a2SBarry Smith #undef __FUNCT__
2931bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
2932bbead8a2SBarry Smith PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,PetscScalar **values)
2933bbead8a2SBarry Smith {
2934bbead8a2SBarry Smith   Mat_SeqAIJ    *a = (Mat_SeqAIJ*) A->data;
2935bbead8a2SBarry Smith   PetscErrorCode ierr;
293634fc4b71SJed 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;
2937bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
2938bbead8a2SBarry Smith   PetscReal      shift = 0.0;
2939bbead8a2SBarry Smith 
2940bbead8a2SBarry Smith   PetscFunctionBegin;
29414a0d0026SBarry Smith   if (a->ibdiagvalid) {
29424a0d0026SBarry Smith     if (values) *values = a->ibdiag;
29434a0d0026SBarry Smith     PetscFunctionReturn(0);
29444a0d0026SBarry Smith   }
2945bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
2946bbead8a2SBarry Smith   if (!a->ibdiag) {
2947bbead8a2SBarry Smith     ierr = PetscMalloc(bs2*mbs*sizeof(PetscScalar),&a->ibdiag);CHKERRQ(ierr);
2948bbead8a2SBarry Smith     ierr = PetscLogObjectMemory(A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
2949bbead8a2SBarry Smith   }
2950bbead8a2SBarry Smith   diag    = a->ibdiag;
2951bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
2952bbead8a2SBarry Smith   /* factor and invert each block */
2953bbead8a2SBarry Smith   switch (bs){
2954bbead8a2SBarry Smith     case 1:
2955bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2956bbead8a2SBarry Smith         ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
2957bbead8a2SBarry Smith         diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
2958bbead8a2SBarry Smith       }
2959bbead8a2SBarry Smith       break;
2960bbead8a2SBarry Smith     case 2:
2961bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2962bbead8a2SBarry Smith         ij[0] = 2*i; ij[1] = 2*i + 1;
2963bbead8a2SBarry Smith         ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
2964bbead8a2SBarry Smith         ierr  = Kernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
2965*d441b888SJed Brown         ierr  = Kernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
2966bbead8a2SBarry Smith 	diag  += 4;
2967bbead8a2SBarry Smith       }
2968bbead8a2SBarry Smith       break;
2969bbead8a2SBarry Smith     case 3:
2970bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2971bbead8a2SBarry Smith         ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
2972bbead8a2SBarry Smith         ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
2973bbead8a2SBarry Smith         ierr  = Kernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
2974*d441b888SJed Brown         ierr  = Kernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
2975bbead8a2SBarry Smith 	diag    += 9;
2976bbead8a2SBarry Smith       }
2977bbead8a2SBarry Smith       break;
2978bbead8a2SBarry Smith     case 4:
2979bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2980bbead8a2SBarry Smith         ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
2981bbead8a2SBarry Smith         ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
2982bbead8a2SBarry Smith         ierr  = Kernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
2983*d441b888SJed Brown         ierr  = Kernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
2984bbead8a2SBarry Smith 	diag  += 16;
2985bbead8a2SBarry Smith       }
2986bbead8a2SBarry Smith       break;
2987bbead8a2SBarry Smith     case 5:
2988bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2989bbead8a2SBarry 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;
2990bbead8a2SBarry Smith         ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
2991bbead8a2SBarry Smith         ierr  = Kernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
2992*d441b888SJed Brown         ierr  = Kernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
2993bbead8a2SBarry Smith 	diag  += 25;
2994bbead8a2SBarry Smith       }
2995bbead8a2SBarry Smith       break;
2996bbead8a2SBarry Smith     case 6:
2997bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2998bbead8a2SBarry 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;
2999bbead8a2SBarry Smith         ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
3000bbead8a2SBarry Smith         ierr  = Kernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
3001*d441b888SJed Brown         ierr  = Kernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3002bbead8a2SBarry Smith 	diag  += 36;
3003bbead8a2SBarry Smith       }
3004bbead8a2SBarry Smith       break;
3005bbead8a2SBarry Smith     case 7:
3006bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
3007bbead8a2SBarry 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;
3008bbead8a2SBarry Smith         ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
3009bbead8a2SBarry Smith         ierr  = Kernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
3010*d441b888SJed Brown         ierr  = Kernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3011bbead8a2SBarry Smith 	diag  += 49;
3012bbead8a2SBarry Smith       }
3013bbead8a2SBarry Smith       break;
3014bbead8a2SBarry Smith     default:
3015bbead8a2SBarry Smith       ierr = PetscMalloc3(bs,MatScalar,&v_work,bs,PetscInt,&v_pivots,bs,PetscInt,&IJ);CHKERRQ(ierr);
3016bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
3017bbead8a2SBarry Smith         for (j=0; j<bs; j++) {
3018bbead8a2SBarry Smith           IJ[j] = bs*i + j;
3019bbead8a2SBarry Smith         }
3020bbead8a2SBarry Smith         ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
3021bbead8a2SBarry Smith         ierr  = Kernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
3022*d441b888SJed Brown         ierr  = Kernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3023bbead8a2SBarry Smith 	diag  += bs2;
3024bbead8a2SBarry Smith       }
3025bbead8a2SBarry Smith       ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3026bbead8a2SBarry Smith   }
3027bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3028bbead8a2SBarry Smith   PetscFunctionReturn(0);
3029bbead8a2SBarry Smith }
3030bbead8a2SBarry Smith 
30317087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
3032682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
30330a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
3034cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
3035cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
3036cb5b572fSBarry Smith        MatMult_SeqAIJ,
303797304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
30387c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
30397c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
3040db4efbfdSBarry Smith        0,
3041db4efbfdSBarry Smith        0,
3042db4efbfdSBarry Smith        0,
3043db4efbfdSBarry Smith /*10*/ 0,
3044cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
3045cb5b572fSBarry Smith        0,
304641f059aeSBarry Smith        MatSOR_SeqAIJ,
304717ab2063SBarry Smith        MatTranspose_SeqAIJ,
304897304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
3049cb5b572fSBarry Smith        MatEqual_SeqAIJ,
3050cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
3051cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
3052cb5b572fSBarry Smith        MatNorm_SeqAIJ,
305397304618SKris Buschelman /*20*/ 0,
3054cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
3055cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
3056cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
3057d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
3058db4efbfdSBarry Smith        0,
3059db4efbfdSBarry Smith        0,
3060db4efbfdSBarry Smith        0,
3061db4efbfdSBarry Smith        0,
3062d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
3063db4efbfdSBarry Smith        0,
3064db4efbfdSBarry Smith        0,
30656c0721eeSBarry Smith        MatGetArray_SeqAIJ,
30666c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
3067d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
3068cb5b572fSBarry Smith        0,
3069cb5b572fSBarry Smith        0,
3070cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
3071cb5b572fSBarry Smith        0,
3072d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
3073cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
3074cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
3075cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
3076cb5b572fSBarry Smith        MatCopy_SeqAIJ,
3077d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
3078cb5b572fSBarry Smith        MatScale_SeqAIJ,
3079cb5b572fSBarry Smith        0,
308079299369SBarry Smith        MatDiagonalSet_SeqAIJ,
30816e169961SBarry Smith        MatZeroRowsColumns_SeqAIJ,
3082d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
30833b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
30843b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
30853b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
3086a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
3087d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
3088b9617806SBarry Smith        0,
30890513a670SBarry Smith        0,
3090cda55fadSBarry Smith        MatPermute_SeqAIJ,
3091cda55fadSBarry Smith        0,
3092d519adbfSMatthew Knepley /*59*/ 0,
3093b9b97703SBarry Smith        MatDestroy_SeqAIJ,
3094b9b97703SBarry Smith        MatView_SeqAIJ,
3095357abbc8SBarry Smith        0,
3096ee4f033dSBarry Smith        0,
3097d519adbfSMatthew Knepley /*64*/ 0,
3098ee4f033dSBarry Smith        0,
3099ee4f033dSBarry Smith        0,
3100ee4f033dSBarry Smith        0,
3101ee4f033dSBarry Smith        0,
3102d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
3103c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
3104ee4f033dSBarry Smith        0,
3105ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
3106dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3107ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
3108dcf5cc72SBarry Smith #else
3109dcf5cc72SBarry Smith        0,
3110dcf5cc72SBarry Smith #endif
3111d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
31123acb8795SBarry Smith        MatFDColoringApply_AIJ,
311397304618SKris Buschelman        0,
311497304618SKris Buschelman        0,
311597304618SKris Buschelman        0,
31166ce1633cSBarry Smith /*79*/ MatFindZeroDiagonals_SeqAIJ,
311797304618SKris Buschelman        0,
311897304618SKris Buschelman        0,
311997304618SKris Buschelman        0,
3120bc011b1eSHong Zhang        MatLoad_SeqAIJ,
3121d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
31221cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
31236284ec50SHong Zhang        0,
31246284ec50SHong Zhang        0,
3125bc011b1eSHong Zhang        0,
3126d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
312726be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
312826be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
3129d439da42SKris Buschelman        MatPtAP_Basic,
31307ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
3131d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
3132bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
3133bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
3134bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
31357ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
3136d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
3137609c6c4dSKris Buschelman        0,
3138609c6c4dSKris Buschelman        0,
313987d4246cSBarry Smith        MatConjugate_SeqAIJ,
314087d4246cSBarry Smith        0,
3141d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
314299cafbc1SBarry Smith        MatRealPart_SeqAIJ,
3143f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
3144f5edf698SHong Zhang        0,
31452bebee5dSHong Zhang        0,
3146cbd44569SHong Zhang /*109*/MatMatSolve_SeqAIJ,
3147985db425SBarry Smith        0,
31482af78befSBarry Smith        MatGetRowMin_SeqAIJ,
31492af78befSBarry Smith        0,
3150599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
3151d519adbfSMatthew Knepley /*114*/0,
3152599ef60dSHong Zhang        0,
31533c2a7987SHong Zhang        0,
3154fe97e370SBarry Smith        0,
3155fbdbba38SShri Abhyankar        0,
3156fbdbba38SShri Abhyankar /*119*/0,
3157fbdbba38SShri Abhyankar        0,
3158fbdbba38SShri Abhyankar        0,
315982d44351SHong Zhang        0,
3160b3a44c85SBarry Smith        MatGetMultiProcBlock_SeqAIJ,
31610716a85fSBarry Smith /*124*/MatFindNonzeroRows_SeqAIJ,
3162bbead8a2SBarry Smith        MatGetColumnNorms_SeqAIJ,
316337868618SMatthew G Knepley        MatInvertBlockDiagonal_SeqAIJ,
316437868618SMatthew G Knepley        0,
316537868618SMatthew G Knepley        0,
31665df89d91SHong Zhang /*129*/0,
31675df89d91SHong Zhang        MatMatTransposeMult_SeqAIJ_SeqAIJ,
31685df89d91SHong Zhang        MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
3169b1683b59SHong Zhang        MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3170b1683b59SHong Zhang        MatMultTransposeColoringCreate_SeqAIJ,
3171c8db22f6SHong Zhang /*134*/MatMultTransposeColoringApply_SeqAIJ
31729e29f15eSvictorle };
317317ab2063SBarry Smith 
3174fb2e594dSBarry Smith EXTERN_C_BEGIN
31754a2ae208SSatish Balay #undef __FUNCT__
31764a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
31777087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3178bef8e0ddSBarry Smith {
3179bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
318097f1f81fSBarry Smith   PetscInt   i,nz,n;
3181bef8e0ddSBarry Smith 
3182bef8e0ddSBarry Smith   PetscFunctionBegin;
3183bef8e0ddSBarry Smith 
3184bef8e0ddSBarry Smith   nz = aij->maxnz;
3185d0f46423SBarry Smith   n  = mat->rmap->n;
3186bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3187bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3188bef8e0ddSBarry Smith   }
3189bef8e0ddSBarry Smith   aij->nz = nz;
3190bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3191bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3192bef8e0ddSBarry Smith   }
3193bef8e0ddSBarry Smith 
3194bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3195bef8e0ddSBarry Smith }
3196fb2e594dSBarry Smith EXTERN_C_END
3197bef8e0ddSBarry Smith 
31984a2ae208SSatish Balay #undef __FUNCT__
31994a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3200bef8e0ddSBarry Smith /*@
3201bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3202bef8e0ddSBarry Smith        in the matrix.
3203bef8e0ddSBarry Smith 
3204bef8e0ddSBarry Smith   Input Parameters:
3205bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3206bef8e0ddSBarry Smith -  indices - the column indices
3207bef8e0ddSBarry Smith 
320815091d37SBarry Smith   Level: advanced
320915091d37SBarry Smith 
3210bef8e0ddSBarry Smith   Notes:
3211bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3212bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3213bef8e0ddSBarry Smith   of the MatSetValues() operation.
3214bef8e0ddSBarry Smith 
3215bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3216d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3217bef8e0ddSBarry Smith 
3218bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3219bef8e0ddSBarry Smith 
3220b9617806SBarry Smith     The indices should start with zero, not one.
3221b9617806SBarry Smith 
3222bef8e0ddSBarry Smith @*/
32237087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3224bef8e0ddSBarry Smith {
32254ac538c5SBarry Smith   PetscErrorCode ierr;
3226bef8e0ddSBarry Smith 
3227bef8e0ddSBarry Smith   PetscFunctionBegin;
32280700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
32294482741eSBarry Smith   PetscValidPointer(indices,2);
32304ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
3231bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3232bef8e0ddSBarry Smith }
3233bef8e0ddSBarry Smith 
3234be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3235be6bf707SBarry Smith 
3236fb2e594dSBarry Smith EXTERN_C_BEGIN
32374a2ae208SSatish Balay #undef __FUNCT__
32384a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
32397087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3240be6bf707SBarry Smith {
3241be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
32426849ba73SBarry Smith   PetscErrorCode ierr;
3243d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3244be6bf707SBarry Smith 
3245be6bf707SBarry Smith   PetscFunctionBegin;
3246be6bf707SBarry Smith   if (aij->nonew != 1) {
3247e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3248be6bf707SBarry Smith   }
3249be6bf707SBarry Smith 
3250be6bf707SBarry Smith   /* allocate space for values if not already there */
3251be6bf707SBarry Smith   if (!aij->saved_values) {
325287828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
32539518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3254be6bf707SBarry Smith   }
3255be6bf707SBarry Smith 
3256be6bf707SBarry Smith   /* copy values over */
325787828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3258be6bf707SBarry Smith   PetscFunctionReturn(0);
3259be6bf707SBarry Smith }
3260fb2e594dSBarry Smith EXTERN_C_END
3261be6bf707SBarry Smith 
32624a2ae208SSatish Balay #undef __FUNCT__
3263b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3264be6bf707SBarry Smith /*@
3265be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3266be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3267be6bf707SBarry Smith        nonlinear portion.
3268be6bf707SBarry Smith 
3269be6bf707SBarry Smith    Collect on Mat
3270be6bf707SBarry Smith 
3271be6bf707SBarry Smith   Input Parameters:
32720e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3273be6bf707SBarry Smith 
327415091d37SBarry Smith   Level: advanced
327515091d37SBarry Smith 
3276be6bf707SBarry Smith   Common Usage, with SNESSolve():
3277be6bf707SBarry Smith $    Create Jacobian matrix
3278be6bf707SBarry Smith $    Set linear terms into matrix
3279be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3280be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3281be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3282512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3283be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3284be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3285be6bf707SBarry Smith $    In your Jacobian routine
3286be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3287be6bf707SBarry Smith $      Set nonlinear terms in matrix
3288be6bf707SBarry Smith 
3289be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3290be6bf707SBarry Smith $    // build linear portion of Jacobian
3291512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3292be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3293be6bf707SBarry Smith $    loop over nonlinear iterations
3294be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3295be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3296be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3297be6bf707SBarry Smith $       Solve linear system with Jacobian
3298be6bf707SBarry Smith $    endloop
3299be6bf707SBarry Smith 
3300be6bf707SBarry Smith   Notes:
3301be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3302512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3303be6bf707SBarry Smith     calling this routine.
3304be6bf707SBarry Smith 
33050c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
33060c468ba9SBarry Smith     and does not allocated additional space.
33070c468ba9SBarry Smith 
3308be6bf707SBarry Smith .seealso: MatRetrieveValues()
3309be6bf707SBarry Smith 
3310be6bf707SBarry Smith @*/
33117087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3312be6bf707SBarry Smith {
33134ac538c5SBarry Smith   PetscErrorCode ierr;
3314be6bf707SBarry Smith 
3315be6bf707SBarry Smith   PetscFunctionBegin;
33160700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3317e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3318e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
33194ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3320be6bf707SBarry Smith   PetscFunctionReturn(0);
3321be6bf707SBarry Smith }
3322be6bf707SBarry Smith 
3323fb2e594dSBarry Smith EXTERN_C_BEGIN
33244a2ae208SSatish Balay #undef __FUNCT__
33254a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
33267087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3327be6bf707SBarry Smith {
3328be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
33296849ba73SBarry Smith   PetscErrorCode ierr;
3330d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3331be6bf707SBarry Smith 
3332be6bf707SBarry Smith   PetscFunctionBegin;
3333be6bf707SBarry Smith   if (aij->nonew != 1) {
3334e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3335be6bf707SBarry Smith   }
3336be6bf707SBarry Smith   if (!aij->saved_values) {
3337e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3338be6bf707SBarry Smith   }
3339be6bf707SBarry Smith   /* copy values over */
334087828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3341be6bf707SBarry Smith   PetscFunctionReturn(0);
3342be6bf707SBarry Smith }
3343fb2e594dSBarry Smith EXTERN_C_END
3344be6bf707SBarry Smith 
33454a2ae208SSatish Balay #undef __FUNCT__
33464a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3347be6bf707SBarry Smith /*@
3348be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3349be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3350be6bf707SBarry Smith        nonlinear portion.
3351be6bf707SBarry Smith 
3352be6bf707SBarry Smith    Collect on Mat
3353be6bf707SBarry Smith 
3354be6bf707SBarry Smith   Input Parameters:
3355be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3356be6bf707SBarry Smith 
335715091d37SBarry Smith   Level: advanced
335815091d37SBarry Smith 
3359be6bf707SBarry Smith .seealso: MatStoreValues()
3360be6bf707SBarry Smith 
3361be6bf707SBarry Smith @*/
33627087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3363be6bf707SBarry Smith {
33644ac538c5SBarry Smith   PetscErrorCode ierr;
3365be6bf707SBarry Smith 
3366be6bf707SBarry Smith   PetscFunctionBegin;
33670700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3368e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3369e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
33704ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3371be6bf707SBarry Smith   PetscFunctionReturn(0);
3372be6bf707SBarry Smith }
3373be6bf707SBarry Smith 
3374f83d6046SBarry Smith 
3375be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
33764a2ae208SSatish Balay #undef __FUNCT__
33774a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
337817ab2063SBarry Smith /*@C
3379682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
33800d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
33816e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
338251c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
33832bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
338417ab2063SBarry Smith 
3385db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3386db81eaa0SLois Curfman McInnes 
338717ab2063SBarry Smith    Input Parameters:
3388db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
338917ab2063SBarry Smith .  m - number of rows
339017ab2063SBarry Smith .  n - number of columns
339117ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
339251c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
33932bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
339417ab2063SBarry Smith 
339517ab2063SBarry Smith    Output Parameter:
3396416022c9SBarry Smith .  A - the matrix
339717ab2063SBarry Smith 
3398175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3399ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3400175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3401175b88e8SBarry Smith 
3402b259b22eSLois Curfman McInnes    Notes:
340349a6f317SBarry Smith    If nnz is given then nz is ignored
340449a6f317SBarry Smith 
340517ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
340617ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
34070002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
340844cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
340917ab2063SBarry Smith 
341017ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3411a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
34123d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
34136da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
341417ab2063SBarry Smith 
3415682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
34164fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3417682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
34186c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
34196c7ebb05SLois Curfman McInnes 
34206c7ebb05SLois Curfman McInnes    Options Database Keys:
3421698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
34229db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
342317ab2063SBarry Smith 
3424027ccd11SLois Curfman McInnes    Level: intermediate
3425027ccd11SLois Curfman McInnes 
342636db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
342736db0b34SBarry Smith 
342817ab2063SBarry Smith @*/
34297087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
343017ab2063SBarry Smith {
3431dfbe8321SBarry Smith   PetscErrorCode ierr;
34326945ee14SBarry Smith 
34333a40ed3dSBarry Smith   PetscFunctionBegin;
3434f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3435117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3436c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3437d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3438273d9f13SBarry Smith   PetscFunctionReturn(0);
3439273d9f13SBarry Smith }
3440273d9f13SBarry Smith 
34414a2ae208SSatish Balay #undef __FUNCT__
34424a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3443273d9f13SBarry Smith /*@C
3444273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3445273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3446273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3447273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3448273d9f13SBarry Smith 
3449273d9f13SBarry Smith    Collective on MPI_Comm
3450273d9f13SBarry Smith 
3451273d9f13SBarry Smith    Input Parameters:
3452117016b1SBarry Smith +  B - The matrix-free
3453273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3454273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
3455273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
3456273d9f13SBarry Smith 
3457273d9f13SBarry Smith    Notes:
345849a6f317SBarry Smith      If nnz is given then nz is ignored
345949a6f317SBarry Smith 
3460273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3461273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3462273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3463273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3464273d9f13SBarry Smith 
3465273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3466273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3467273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3468273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3469273d9f13SBarry Smith 
3470aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3471aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3472aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3473aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3474aa95bbe8SBarry Smith 
3475a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3476a96a251dSBarry Smith    entries or columns indices
3477a96a251dSBarry Smith 
3478273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3479273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3480273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3481273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3482273d9f13SBarry Smith 
3483273d9f13SBarry Smith    Options Database Keys:
3484698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3485698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3486273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3487273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3488273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3489273d9f13SBarry Smith 
3490273d9f13SBarry Smith    Level: intermediate
3491273d9f13SBarry Smith 
3492aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3493273d9f13SBarry Smith 
3494273d9f13SBarry Smith @*/
34957087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3496273d9f13SBarry Smith {
34974ac538c5SBarry Smith   PetscErrorCode ierr;
3498a23d5eceSKris Buschelman 
3499a23d5eceSKris Buschelman   PetscFunctionBegin;
35004ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3501a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3502a23d5eceSKris Buschelman }
3503a23d5eceSKris Buschelman 
3504a23d5eceSKris Buschelman EXTERN_C_BEGIN
3505a23d5eceSKris Buschelman #undef __FUNCT__
3506a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
35077087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3508a23d5eceSKris Buschelman {
3509273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3510ace3abfcSBarry Smith   PetscBool      skipallocation = PETSC_FALSE;
35116849ba73SBarry Smith   PetscErrorCode ierr;
351297f1f81fSBarry Smith   PetscInt       i;
3513273d9f13SBarry Smith 
3514273d9f13SBarry Smith   PetscFunctionBegin;
3515d5d45c9bSBarry Smith 
3516a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3517c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3518c461c341SBarry Smith     nz             = 0;
3519c461c341SBarry Smith   }
3520c461c341SBarry Smith 
352126283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
352226283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
352326283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
352426283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3525899cda47SBarry Smith 
3526435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3527e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3528b73539f3SBarry Smith   if (nnz) {
3529d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3530e32f2f54SBarry 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]);
3531e32f2f54SBarry 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);
3532b73539f3SBarry Smith     }
3533b73539f3SBarry Smith   }
3534b73539f3SBarry Smith 
3535273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3536273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3537273d9f13SBarry Smith 
3538ab93d7beSBarry Smith   if (!skipallocation) {
35392ee49352SLisandro Dalcin     if (!b->imax) {
3540d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3541d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
35422ee49352SLisandro Dalcin     }
3543273d9f13SBarry Smith     if (!nnz) {
3544435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3545c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3546d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3547d0f46423SBarry Smith       nz = nz*B->rmap->n;
3548273d9f13SBarry Smith     } else {
3549273d9f13SBarry Smith       nz = 0;
3550d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3551273d9f13SBarry Smith     }
3552ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3553d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3554ab93d7beSBarry Smith 
3555273d9f13SBarry Smith     /* allocate the matrix space */
35562ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3557d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3558d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3559bfeeae90SHong Zhang     b->i[0] = 0;
3560d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
35615da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
35625da197adSKris Buschelman     }
3563273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3564e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3565e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3566c461c341SBarry Smith   } else {
3567e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3568e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3569c461c341SBarry Smith   }
3570273d9f13SBarry Smith 
3571273d9f13SBarry Smith   b->nz                = 0;
3572273d9f13SBarry Smith   b->maxnz             = nz;
3573273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3574273d9f13SBarry Smith   PetscFunctionReturn(0);
3575273d9f13SBarry Smith }
3576a23d5eceSKris Buschelman EXTERN_C_END
3577273d9f13SBarry Smith 
3578a1661176SMatthew Knepley #undef  __FUNCT__
3579a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
358058d36128SBarry Smith /*@
3581a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3582a1661176SMatthew Knepley 
3583a1661176SMatthew Knepley    Input Parameters:
3584a1661176SMatthew Knepley +  B - the matrix
3585a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3586a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3587a1661176SMatthew Knepley -  v - optional values in the matrix
3588a1661176SMatthew Knepley 
3589a1661176SMatthew Knepley    Level: developer
3590a1661176SMatthew Knepley 
359158d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
359258d36128SBarry Smith 
3593a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3594a1661176SMatthew Knepley 
3595a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3596a1661176SMatthew Knepley @*/
3597a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3598a1661176SMatthew Knepley {
3599a1661176SMatthew Knepley   PetscErrorCode ierr;
3600a1661176SMatthew Knepley 
3601a1661176SMatthew Knepley   PetscFunctionBegin;
36020700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
36034ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3604a1661176SMatthew Knepley   PetscFunctionReturn(0);
3605a1661176SMatthew Knepley }
3606a1661176SMatthew Knepley 
3607a1661176SMatthew Knepley EXTERN_C_BEGIN
3608a1661176SMatthew Knepley #undef  __FUNCT__
3609a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
36107087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3611a1661176SMatthew Knepley {
3612a1661176SMatthew Knepley   PetscInt       i;
3613a1661176SMatthew Knepley   PetscInt       m,n;
3614a1661176SMatthew Knepley   PetscInt       nz;
3615a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3616a1661176SMatthew Knepley   PetscScalar    *values;
3617a1661176SMatthew Knepley   PetscErrorCode ierr;
3618a1661176SMatthew Knepley 
3619a1661176SMatthew Knepley   PetscFunctionBegin;
3620a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3621a1661176SMatthew Knepley 
362265e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3623a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3624a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3625b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3626a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
362765e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3628a1661176SMatthew Knepley     nnz[i] = nz;
3629a1661176SMatthew Knepley   }
3630a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3631a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3632a1661176SMatthew Knepley 
3633a1661176SMatthew Knepley   if (v) {
3634a1661176SMatthew Knepley     values = (PetscScalar*) v;
3635a1661176SMatthew Knepley   } else {
36360e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3637a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3638a1661176SMatthew Knepley   }
3639a1661176SMatthew Knepley 
3640a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3641b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3642b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3643a1661176SMatthew Knepley   }
3644a1661176SMatthew Knepley 
3645a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3646a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3647a1661176SMatthew Knepley 
3648a1661176SMatthew Knepley   if (!v) {
3649a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3650a1661176SMatthew Knepley   }
3651a1661176SMatthew Knepley   PetscFunctionReturn(0);
3652a1661176SMatthew Knepley }
3653a1661176SMatthew Knepley EXTERN_C_END
3654a1661176SMatthew Knepley 
3655c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3656c6db04a5SJed Brown #include <private/petscaxpy.h>
3657170fe5c8SBarry Smith 
3658170fe5c8SBarry Smith #undef __FUNCT__
3659170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3660170fe5c8SBarry Smith /*
3661170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3662170fe5c8SBarry Smith 
3663170fe5c8SBarry Smith                n                       p                          p
3664170fe5c8SBarry Smith         (              )       (              )         (                  )
3665170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3666170fe5c8SBarry Smith         (              )       (              )         (                  )
3667170fe5c8SBarry Smith 
3668170fe5c8SBarry Smith */
3669170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3670170fe5c8SBarry Smith {
3671170fe5c8SBarry Smith   PetscErrorCode     ierr;
3672170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3673170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3674170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
36751de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3676170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3677170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3678170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3679170fe5c8SBarry Smith 
3680170fe5c8SBarry Smith   PetscFunctionBegin;
3681d0f46423SBarry Smith   m = A->rmap->n;
3682d0f46423SBarry Smith   n = A->cmap->n;
3683d0f46423SBarry Smith   p = B->cmap->n;
3684170fe5c8SBarry Smith   a = sub_a->v;
3685170fe5c8SBarry Smith   b = sub_b->a;
3686170fe5c8SBarry Smith   c = sub_c->v;
3687170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3688170fe5c8SBarry Smith 
3689170fe5c8SBarry Smith   ii  = sub_b->i;
3690170fe5c8SBarry Smith   idx = sub_b->j;
3691170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3692170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3693170fe5c8SBarry Smith     while (q-->0) {
3694170fe5c8SBarry Smith       c_q = c + m*(*idx);
3695170fe5c8SBarry Smith       a_q = a + m*i;
3696be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3697170fe5c8SBarry Smith       idx++;
3698170fe5c8SBarry Smith       b++;
3699170fe5c8SBarry Smith     }
3700170fe5c8SBarry Smith   }
3701170fe5c8SBarry Smith   PetscFunctionReturn(0);
3702170fe5c8SBarry Smith }
3703170fe5c8SBarry Smith 
3704170fe5c8SBarry Smith #undef __FUNCT__
3705170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3706170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3707170fe5c8SBarry Smith {
3708170fe5c8SBarry Smith   PetscErrorCode ierr;
3709d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3710170fe5c8SBarry Smith   Mat            Cmat;
3711170fe5c8SBarry Smith 
3712170fe5c8SBarry Smith   PetscFunctionBegin;
3713e32f2f54SBarry Smith   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
371439804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3715170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3716170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3717170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3718170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3719170fe5c8SBarry Smith   *C = Cmat;
3720170fe5c8SBarry Smith   PetscFunctionReturn(0);
3721170fe5c8SBarry Smith }
3722170fe5c8SBarry Smith 
3723170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3724170fe5c8SBarry Smith #undef __FUNCT__
3725170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3726170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3727170fe5c8SBarry Smith {
3728170fe5c8SBarry Smith   PetscErrorCode ierr;
3729170fe5c8SBarry Smith 
3730170fe5c8SBarry Smith   PetscFunctionBegin;
3731170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3732170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3733170fe5c8SBarry Smith   }
3734170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3735170fe5c8SBarry Smith   PetscFunctionReturn(0);
3736170fe5c8SBarry Smith }
3737170fe5c8SBarry Smith 
3738170fe5c8SBarry Smith 
37390bad9183SKris Buschelman /*MC
3740fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
37410bad9183SKris Buschelman    based on compressed sparse row format.
37420bad9183SKris Buschelman 
37430bad9183SKris Buschelman    Options Database Keys:
37440bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
37450bad9183SKris Buschelman 
37460bad9183SKris Buschelman   Level: beginner
37470bad9183SKris Buschelman 
3748f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
37490bad9183SKris Buschelman M*/
37500bad9183SKris Buschelman 
3751a6175056SHong Zhang EXTERN_C_BEGIN
3752b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3753b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3754b5e56a35SBarry Smith #endif
3755ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3756af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3757af1023dbSSatish Balay #endif
37587087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
37597087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
37607087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
37617087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3762611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
37637087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3764611f576cSBarry Smith #endif
3765611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
37667087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3767611f576cSBarry Smith #endif
3768f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3769f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3770f3c0ef26SHong Zhang #endif
3771611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
37727087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3773611f576cSBarry Smith #endif
3774eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
37757087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3776eb3b5408SSatish Balay #endif
3777586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
37787087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3779586621ddSJed Brown #endif
3780719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
37817087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3782719d5645SBarry Smith #endif
3783b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
37847087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
37857087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
37867087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3787b3866ffcSBarry Smith #endif
378817667f90SBarry Smith EXTERN_C_END
378917667f90SBarry Smith 
379017667f90SBarry Smith EXTERN_C_BEGIN
37914a2ae208SSatish Balay #undef __FUNCT__
37924a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
37937087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqAIJ(Mat B)
3794273d9f13SBarry Smith {
3795273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3796dfbe8321SBarry Smith   PetscErrorCode ierr;
379738baddfdSBarry Smith   PetscMPIInt    size;
3798273d9f13SBarry Smith 
3799273d9f13SBarry Smith   PetscFunctionBegin;
38007adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3801e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3802273d9f13SBarry Smith 
380338f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3804b0a32e0cSBarry Smith   B->data             = (void*)b;
3805549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3806416022c9SBarry Smith   b->row              = 0;
3807416022c9SBarry Smith   b->col              = 0;
380882bf6240SBarry Smith   b->icol             = 0;
3809b810aeb4SBarry Smith   b->reallocs         = 0;
381036db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3811f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3812416022c9SBarry Smith   b->nonew             = 0;
3813416022c9SBarry Smith   b->diag              = 0;
3814416022c9SBarry Smith   b->solve_work        = 0;
38152a1b7f2aSHong Zhang   B->spptr             = 0;
3816be6bf707SBarry Smith   b->saved_values      = 0;
3817d7f994e1SBarry Smith   b->idiag             = 0;
381871f1c65dSBarry Smith   b->mdiag             = 0;
381971f1c65dSBarry Smith   b->ssor_work         = 0;
382071f1c65dSBarry Smith   b->omega             = 1.0;
382171f1c65dSBarry Smith   b->fshift            = 0.0;
382271f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3823bbead8a2SBarry Smith   b->ibdiagvalid       = PETSC_FALSE;
3824a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3825a30b2313SHong Zhang   b->xtoy              = 0;
3826a30b2313SHong Zhang   b->XtoY              = 0;
382788e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
382817ab2063SBarry Smith 
382935d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3830b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3831700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3832b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3833b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3834b3866ffcSBarry Smith #endif
3835b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3836700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3837b5e56a35SBarry Smith #endif
3838ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3839700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3840719d5645SBarry Smith #endif
3841611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3842700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3843611f576cSBarry Smith #endif
3844f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3845700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3846f3c0ef26SHong Zhang #endif
3847611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3848700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C","MatGetFactor_seqaij_spooles",MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3849611f576cSBarry Smith #endif
3850611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3851700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr);
3852611f576cSBarry Smith #endif
3853eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3854700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3855eb3b5408SSatish Balay #endif
3856586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3857700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3858586621ddSJed Brown #endif
3859719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3860700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_seqaij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3861719d5645SBarry Smith #endif
3862700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3863700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3864700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
3865700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3866700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3867700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3868700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3869700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3870700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3871700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3872700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3873700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3874700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3875700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
3876700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3877700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3878700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3879700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
38804108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
388117667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
38823a40ed3dSBarry Smith   PetscFunctionReturn(0);
388317ab2063SBarry Smith }
3884273d9f13SBarry Smith EXTERN_C_END
388517ab2063SBarry Smith 
3886ff34cdc8SBarry Smith #if defined(PETSC_HAVE_PTHREADCLASSES)
388751d315f7SKerry Stevens EXTERN_C_BEGIN
388851d315f7SKerry Stevens #undef __FUNCT__
38897d6a0e61SBarry Smith #define __FUNCT__ "MatCreate_SeqAIJPThread"
38907d6a0e61SBarry Smith PetscErrorCode  MatCreate_SeqAIJPThread(Mat B)
389151d315f7SKerry Stevens {
389251d315f7SKerry Stevens   PetscErrorCode ierr;
389351d315f7SKerry Stevens 
389451d315f7SKerry Stevens   PetscFunctionBegin;
389551d315f7SKerry Stevens   ierr = MatCreate_SeqAIJ(B);
389651d315f7SKerry Stevens   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
38977d6a0e61SBarry Smith   B->ops->mult = MatMult_SeqAIJPThread;
38987d6a0e61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJPTHREAD);CHKERRQ(ierr);
389951d315f7SKerry Stevens   PetscFunctionReturn(0);
390051d315f7SKerry Stevens }
390151d315f7SKerry Stevens EXTERN_C_END
3902ba61063dSBarry Smith #endif
390351d315f7SKerry Stevens 
39044a2ae208SSatish Balay #undef __FUNCT__
3905b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3906b24902e0SBarry Smith /*
3907b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3908b24902e0SBarry Smith */
3909ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
391017ab2063SBarry Smith {
3911416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
39126849ba73SBarry Smith   PetscErrorCode ierr;
3913d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
391417ab2063SBarry Smith 
39153a40ed3dSBarry Smith   PetscFunctionBegin;
3916273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3917273d9f13SBarry Smith 
3918d5f3da31SBarry Smith   C->factortype     = A->factortype;
3919416022c9SBarry Smith   c->row            = 0;
3920416022c9SBarry Smith   c->col            = 0;
392182bf6240SBarry Smith   c->icol           = 0;
39226ad4291fSHong Zhang   c->reallocs       = 0;
392317ab2063SBarry Smith 
39246ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
392517ab2063SBarry Smith 
3926aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
3927aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
3928eec197d1SBarry Smith 
392933b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
39309518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
393117ab2063SBarry Smith   for (i=0; i<m; i++) {
3932416022c9SBarry Smith     c->imax[i] = a->imax[i];
3933416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
393417ab2063SBarry Smith   }
393517ab2063SBarry Smith 
393617ab2063SBarry Smith   /* allocate the matrix space */
3937f77e22a1SHong Zhang   if (mallocmatspace){
3938a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
39399518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3940f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
394197f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
394217ab2063SBarry Smith     if (m > 0) {
394397f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3944be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3945bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3946be6bf707SBarry Smith       } else {
3947bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
394817ab2063SBarry Smith       }
394908480c60SBarry Smith     }
3950f77e22a1SHong Zhang   }
395117ab2063SBarry Smith 
39526ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3953416022c9SBarry Smith   c->roworiented       = a->roworiented;
3954416022c9SBarry Smith   c->nonew             = a->nonew;
3955416022c9SBarry Smith   if (a->diag) {
395697f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
395752e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
395817ab2063SBarry Smith     for (i=0; i<m; i++) {
3959416022c9SBarry Smith       c->diag[i] = a->diag[i];
396017ab2063SBarry Smith     }
39613a40ed3dSBarry Smith   } else c->diag           = 0;
39626ad4291fSHong Zhang   c->solve_work            = 0;
39636ad4291fSHong Zhang   c->saved_values          = 0;
39646ad4291fSHong Zhang   c->idiag                 = 0;
396571f1c65dSBarry Smith   c->ssor_work             = 0;
3966a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3967e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3968e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
39696ad4291fSHong Zhang   c->xtoy                  = 0;
39706ad4291fSHong Zhang   c->XtoY                  = 0;
39716ad4291fSHong Zhang 
3972416022c9SBarry Smith   c->nz                 = a->nz;
39738ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
3974273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3975754ec7b1SSatish Balay 
39766ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
39776ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
3978cd6b891eSBarry Smith   c->compressedrow.check   = a->compressedrow.check;
3979cd6b891eSBarry Smith   if (a->compressedrow.use){
39806ad4291fSHong Zhang     i = a->compressedrow.nrows;
39810e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
39826ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
39836ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
398427ea64f8SHong Zhang   } else {
398527ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
398627ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
398727ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
39886ad4291fSHong Zhang   }
398988e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
39904108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
39914846f1f5SKris Buschelman 
39927adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
39933a40ed3dSBarry Smith   PetscFunctionReturn(0);
399417ab2063SBarry Smith }
399517ab2063SBarry Smith 
39964a2ae208SSatish Balay #undef __FUNCT__
3997b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3998b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3999b24902e0SBarry Smith {
4000b24902e0SBarry Smith   PetscErrorCode ierr;
4001b24902e0SBarry Smith 
4002b24902e0SBarry Smith   PetscFunctionBegin;
4003b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
40044b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4005a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4006f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4007b24902e0SBarry Smith   PetscFunctionReturn(0);
4008b24902e0SBarry Smith }
4009b24902e0SBarry Smith 
4010b24902e0SBarry Smith #undef __FUNCT__
40114a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
4012112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4013fbdbba38SShri Abhyankar {
4014fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4015fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4016fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4017fbdbba38SShri Abhyankar   int            fd;
4018fbdbba38SShri Abhyankar   PetscMPIInt    size;
4019fbdbba38SShri Abhyankar   MPI_Comm       comm;
4020bbead8a2SBarry Smith   PetscInt       bs = 1;
4021fbdbba38SShri Abhyankar 
4022fbdbba38SShri Abhyankar   PetscFunctionBegin;
4023fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4024fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4025fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4026bbead8a2SBarry Smith 
4027bbead8a2SBarry Smith   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
4028bbead8a2SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
4029bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4030bbead8a2SBarry Smith 
4031fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4032fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4033fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4034fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4035fbdbba38SShri Abhyankar 
4036bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4037fbdbba38SShri Abhyankar 
4038fbdbba38SShri Abhyankar   /* read in row lengths */
4039fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
4040fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4041fbdbba38SShri Abhyankar 
4042fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4043fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
4044fbdbba38SShri 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);
4045fbdbba38SShri Abhyankar 
4046fbdbba38SShri Abhyankar   /* set global size if not set already*/
4047f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4048fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4049aabbc4fbSShri Abhyankar   } else {
4050fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
4051fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
40524c5b953cSHong Zhang     if (rows < 0 && cols < 0){ /* user might provide local size instead of global size */
40534c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
40544c5b953cSHong Zhang     }
4055f501eaabSShri 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);
4056aabbc4fbSShri Abhyankar   }
4057fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4058fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
4059fbdbba38SShri Abhyankar 
4060fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4061fbdbba38SShri Abhyankar 
4062fbdbba38SShri Abhyankar   /* read in nonzero values */
4063fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4064fbdbba38SShri Abhyankar 
4065fbdbba38SShri Abhyankar   /* set matrix "i" values */
4066fbdbba38SShri Abhyankar   a->i[0] = 0;
4067fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4068fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4069fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4070fbdbba38SShri Abhyankar   }
4071fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4072fbdbba38SShri Abhyankar 
4073fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4074fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4075bbead8a2SBarry Smith   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
4076fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4077fbdbba38SShri Abhyankar }
4078fbdbba38SShri Abhyankar 
4079fbdbba38SShri Abhyankar #undef __FUNCT__
4080b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4081ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
40827264ac53SSatish Balay {
40837264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
4084dfbe8321SBarry Smith   PetscErrorCode ierr;
4085eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4086eeffb40dSHong Zhang   PetscInt k;
4087eeffb40dSHong Zhang #endif
40887264ac53SSatish Balay 
40893a40ed3dSBarry Smith   PetscFunctionBegin;
4090bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4091d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4092ca44d042SBarry Smith     *flg = PETSC_FALSE;
4093ca44d042SBarry Smith     PetscFunctionReturn(0);
4094bcd2baecSBarry Smith   }
40957264ac53SSatish Balay 
40967264ac53SSatish Balay   /* if the a->i are the same */
4097d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4098abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
40997264ac53SSatish Balay 
41007264ac53SSatish Balay   /* if a->j are the same */
410197f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4102abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4103bcd2baecSBarry Smith 
4104bcd2baecSBarry Smith   /* if a->a are the same */
4105eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4106eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
4107eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
4108eeffb40dSHong Zhang       *flg = PETSC_FALSE;
41093a40ed3dSBarry Smith       PetscFunctionReturn(0);
4110eeffb40dSHong Zhang     }
4111eeffb40dSHong Zhang   }
4112eeffb40dSHong Zhang #else
4113eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4114eeffb40dSHong Zhang #endif
4115eeffb40dSHong Zhang   PetscFunctionReturn(0);
41167264ac53SSatish Balay }
411736db0b34SBarry Smith 
41184a2ae208SSatish Balay #undef __FUNCT__
41194a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
412005869f15SSatish Balay /*@
412136db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
412236db0b34SBarry Smith               provided by the user.
412336db0b34SBarry Smith 
4124c75a6043SHong Zhang       Collective on MPI_Comm
412536db0b34SBarry Smith 
412636db0b34SBarry Smith    Input Parameters:
412736db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
412836db0b34SBarry Smith .   m - number of rows
412936db0b34SBarry Smith .   n - number of columns
413036db0b34SBarry Smith .   i - row indices
413136db0b34SBarry Smith .   j - column indices
413236db0b34SBarry Smith -   a - matrix values
413336db0b34SBarry Smith 
413436db0b34SBarry Smith    Output Parameter:
413536db0b34SBarry Smith .   mat - the matrix
413636db0b34SBarry Smith 
413736db0b34SBarry Smith    Level: intermediate
413836db0b34SBarry Smith 
413936db0b34SBarry Smith    Notes:
41400551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4141292fb18eSBarry Smith     once the matrix is destroyed and not before
414236db0b34SBarry Smith 
414336db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
414436db0b34SBarry Smith 
4145bfeeae90SHong Zhang        The i and j indices are 0 based
414636db0b34SBarry Smith 
4147a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4148a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
4149a4552177SSatish Balay     as shown:
4150a4552177SSatish Balay 
4151a4552177SSatish Balay         1 0 0
4152a4552177SSatish Balay         2 0 3
4153a4552177SSatish Balay         4 5 6
4154a4552177SSatish Balay 
4155a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
41569985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4157a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
4158a4552177SSatish Balay 
41599985e31cSBarry Smith 
41602fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
416136db0b34SBarry Smith 
416236db0b34SBarry Smith @*/
41637087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
416436db0b34SBarry Smith {
4165dfbe8321SBarry Smith   PetscErrorCode ierr;
4166cbcfb4deSHong Zhang   PetscInt       ii;
416736db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4168cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4169cbcfb4deSHong Zhang   PetscInt       jj;
4170cbcfb4deSHong Zhang #endif
417136db0b34SBarry Smith 
417236db0b34SBarry Smith   PetscFunctionBegin;
4173a96a251dSBarry Smith   if (i[0]) {
4174e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
417536db0b34SBarry Smith   }
4176f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4177f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4178ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4179ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4180ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4181ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
4182ab93d7beSBarry Smith 
418336db0b34SBarry Smith   aij->i = i;
418436db0b34SBarry Smith   aij->j = j;
418536db0b34SBarry Smith   aij->a = a;
418636db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
418736db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4188e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4189e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
419036db0b34SBarry Smith 
419136db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
419236db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
41932515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
4194e32f2f54SBarry 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]);
41959985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4196e32f2f54SBarry 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);
4197e32f2f54SBarry 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);
41989985e31cSBarry Smith     }
419936db0b34SBarry Smith #endif
420036db0b34SBarry Smith   }
42012515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
420236db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
4203e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
4204e32f2f54SBarry 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]);
420536db0b34SBarry Smith   }
420636db0b34SBarry Smith #endif
420736db0b34SBarry Smith 
4208b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4209b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
421036db0b34SBarry Smith   PetscFunctionReturn(0);
421136db0b34SBarry Smith }
42128a0b0e6bSVictor Minden #undef __FUNCT__
42138a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
421480ef6e79SMatthew G Knepley /*@C
4215d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
42168a0b0e6bSVictor Minden               provided by the user.
42178a0b0e6bSVictor Minden 
42188a0b0e6bSVictor Minden       Collective on MPI_Comm
42198a0b0e6bSVictor Minden 
42208a0b0e6bSVictor Minden    Input Parameters:
42218a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
42228a0b0e6bSVictor Minden .   m   - number of rows
42238a0b0e6bSVictor Minden .   n   - number of columns
42248a0b0e6bSVictor Minden .   i   - row indices
42258a0b0e6bSVictor Minden .   j   - column indices
42261230e6d1SVictor Minden .   a   - matrix values
42271230e6d1SVictor Minden .   nz  - number of nonzeros
42281230e6d1SVictor Minden -   idx - 0 or 1 based
42298a0b0e6bSVictor Minden 
42308a0b0e6bSVictor Minden    Output Parameter:
42318a0b0e6bSVictor Minden .   mat - the matrix
42328a0b0e6bSVictor Minden 
42338a0b0e6bSVictor Minden    Level: intermediate
42348a0b0e6bSVictor Minden 
42358a0b0e6bSVictor Minden    Notes:
42368a0b0e6bSVictor Minden        The i and j indices are 0 based
42378a0b0e6bSVictor Minden 
42388a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
42398a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
42408a0b0e6bSVictor Minden     as shown:
42418a0b0e6bSVictor Minden 
42428a0b0e6bSVictor Minden         1 0 0
42438a0b0e6bSVictor Minden         2 0 3
42448a0b0e6bSVictor Minden         4 5 6
42458a0b0e6bSVictor Minden 
42468a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
42478a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
42488a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
42498a0b0e6bSVictor Minden 
42508a0b0e6bSVictor Minden 
42518a0b0e6bSVictor Minden .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
42528a0b0e6bSVictor Minden 
42538a0b0e6bSVictor Minden @*/
42541230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
42558a0b0e6bSVictor Minden {
42568a0b0e6bSVictor Minden   PetscErrorCode ierr;
4257d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
42588a0b0e6bSVictor Minden 
42598a0b0e6bSVictor Minden 
42608a0b0e6bSVictor Minden   PetscFunctionBegin;
4261d021a1c5SVictor Minden   ierr = PetscMalloc(m*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
42621230e6d1SVictor Minden   ierr = PetscMemzero(nnz,m*sizeof(PetscInt));CHKERRQ(ierr);
42631230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++){
42641230e6d1SVictor Minden     nnz[i[ii]] += 1;
42651230e6d1SVictor Minden   }
42661230e6d1SVictor Minden   //ierr = MatSeqAIJCreate(comm,m,n,0,nnz,mat);CHKERRQ(ierr);
42678a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
42688a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
42698a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
42701230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
42711230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++){
42721230e6d1SVictor Minden     if (idx){
42731230e6d1SVictor Minden       row = i[ii] - 1;
42741230e6d1SVictor Minden       col = j[ii] - 1;
42751230e6d1SVictor Minden     } else {
42761230e6d1SVictor Minden       row = i[ii];
42771230e6d1SVictor Minden       col = j[ii];
42788a0b0e6bSVictor Minden     }
42791230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
42808a0b0e6bSVictor Minden   }
42818a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
42828a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4283d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
42848a0b0e6bSVictor Minden   PetscFunctionReturn(0);
42858a0b0e6bSVictor Minden }
428636db0b34SBarry Smith 
4287cc8ba8e1SBarry Smith #undef __FUNCT__
4288ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4289dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4290cc8ba8e1SBarry Smith {
4291dfbe8321SBarry Smith   PetscErrorCode ierr;
4292cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
429336db0b34SBarry Smith 
4294cc8ba8e1SBarry Smith   PetscFunctionBegin;
42958ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4296cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4297cc8ba8e1SBarry Smith     a->coloring = coloring;
429812c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
429997f1f81fSBarry Smith     PetscInt             i,*larray;
430012c595b3SBarry Smith     ISColoring      ocoloring;
430108b6dcc0SBarry Smith     ISColoringValue *colors;
430212c595b3SBarry Smith 
430312c595b3SBarry Smith     /* set coloring for diagonal portion */
43040e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
4305d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
430612c595b3SBarry Smith       larray[i] = i;
430712c595b3SBarry Smith     }
4308992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
43090e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4310d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
431112c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
431212c595b3SBarry Smith     }
431312c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4314d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
431512c595b3SBarry Smith     a->coloring = ocoloring;
431612c595b3SBarry Smith   }
4317cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4318cc8ba8e1SBarry Smith }
4319cc8ba8e1SBarry Smith 
4320dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
4321ee4f033dSBarry Smith EXTERN_C_BEGIN
4322c6db04a5SJed Brown #include <adic/ad_utils.h>
4323ee4f033dSBarry Smith EXTERN_C_END
4324cc8ba8e1SBarry Smith 
4325cc8ba8e1SBarry Smith #undef __FUNCT__
4326ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
4327dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
4328cc8ba8e1SBarry Smith {
4329cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4330d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
43314440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
433208b6dcc0SBarry Smith   ISColoringValue *color;
4333cc8ba8e1SBarry Smith 
4334cc8ba8e1SBarry Smith   PetscFunctionBegin;
4335e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
43364440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
4337cc8ba8e1SBarry Smith   color = a->coloring->colors;
4338cc8ba8e1SBarry Smith   /* loop over rows */
4339cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
4340cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
4341cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
4342cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
4343cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
4344cc8ba8e1SBarry Smith     }
43454440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
4346ee4f033dSBarry Smith   }
4347ee4f033dSBarry Smith   PetscFunctionReturn(0);
4348ee4f033dSBarry Smith }
4349ee4f033dSBarry Smith #endif
4350ee4f033dSBarry Smith 
4351ee4f033dSBarry Smith #undef __FUNCT__
4352ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
435397f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4354ee4f033dSBarry Smith {
4355ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4356d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
435754f21887SBarry Smith   MatScalar       *v = a->a;
435854f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
435908b6dcc0SBarry Smith   ISColoringValue *color;
4360ee4f033dSBarry Smith 
4361ee4f033dSBarry Smith   PetscFunctionBegin;
4362e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4363ee4f033dSBarry Smith   color = a->coloring->colors;
4364ee4f033dSBarry Smith   /* loop over rows */
4365ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4366ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4367ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
4368ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
4369ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
4370ee4f033dSBarry Smith     }
4371ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4372cc8ba8e1SBarry Smith   }
4373cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4374cc8ba8e1SBarry Smith }
437536db0b34SBarry Smith 
437681824310SBarry Smith /*
437781824310SBarry Smith     Special version for direct calls from Fortran
437881824310SBarry Smith */
4379c6db04a5SJed Brown #include <private/fortranimpl.h>
438081824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
438181824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
438281824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
438381824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
438481824310SBarry Smith #endif
438581824310SBarry Smith 
438681824310SBarry Smith /* Change these macros so can be used in void function */
438781824310SBarry Smith #undef CHKERRQ
43887adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
438981824310SBarry Smith #undef SETERRQ2
4390e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
439181824310SBarry Smith 
439281824310SBarry Smith EXTERN_C_BEGIN
439381824310SBarry Smith #undef __FUNCT__
439481824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
43951f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
439681824310SBarry Smith {
439781824310SBarry Smith   Mat            A = *AA;
439881824310SBarry Smith   PetscInt       m = *mm, n = *nn;
439981824310SBarry Smith   InsertMode     is = *isis;
440081824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
440181824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
440281824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
440381824310SBarry Smith   PetscErrorCode ierr;
440481824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
440554f21887SBarry Smith   MatScalar      *ap,value,*aa;
4406ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4407ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
440881824310SBarry Smith 
440981824310SBarry Smith   PetscFunctionBegin;
4410d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
441181824310SBarry Smith   imax = a->imax;
441281824310SBarry Smith   ai = a->i;
441381824310SBarry Smith   ailen = a->ilen;
441481824310SBarry Smith   aj = a->j;
441581824310SBarry Smith   aa = a->a;
441681824310SBarry Smith 
441781824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
441881824310SBarry Smith     row  = im[k];
441981824310SBarry Smith     if (row < 0) continue;
442081824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4421d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
442281824310SBarry Smith #endif
442381824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
442481824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
442581824310SBarry Smith     low  = 0;
442681824310SBarry Smith     high = nrow;
442781824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
442881824310SBarry Smith       if (in[l] < 0) continue;
442981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4430d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
443181824310SBarry Smith #endif
443281824310SBarry Smith       col = in[l];
443381824310SBarry Smith       if (roworiented) {
443481824310SBarry Smith         value = v[l + k*n];
443581824310SBarry Smith       } else {
443681824310SBarry Smith         value = v[k + l*m];
443781824310SBarry Smith       }
443881824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
443981824310SBarry Smith 
444081824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
444181824310SBarry Smith       lastcol = col;
444281824310SBarry Smith       while (high-low > 5) {
444381824310SBarry Smith         t = (low+high)/2;
444481824310SBarry Smith         if (rp[t] > col) high = t;
444581824310SBarry Smith         else             low  = t;
444681824310SBarry Smith       }
444781824310SBarry Smith       for (i=low; i<high; i++) {
444881824310SBarry Smith         if (rp[i] > col) break;
444981824310SBarry Smith         if (rp[i] == col) {
445081824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
445181824310SBarry Smith           else                  ap[i] = value;
445281824310SBarry Smith           goto noinsert;
445381824310SBarry Smith         }
445481824310SBarry Smith       }
445581824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
445681824310SBarry Smith       if (nonew == 1) goto noinsert;
44577adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4458fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
445981824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
446081824310SBarry Smith       /* shift up all the later entries in this row */
446181824310SBarry Smith       for (ii=N; ii>=i; ii--) {
446281824310SBarry Smith         rp[ii+1] = rp[ii];
446381824310SBarry Smith         ap[ii+1] = ap[ii];
446481824310SBarry Smith       }
446581824310SBarry Smith       rp[i] = col;
446681824310SBarry Smith       ap[i] = value;
446781824310SBarry Smith       noinsert:;
446881824310SBarry Smith       low = i + 1;
446981824310SBarry Smith     }
447081824310SBarry Smith     ailen[row] = nrow;
447181824310SBarry Smith   }
447281824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
447381824310SBarry Smith   PetscFunctionReturnVoid();
447481824310SBarry Smith }
447581824310SBarry Smith EXTERN_C_END
447662298a1eSBarry Smith 
4477