xref: /petsc/src/mat/impls/aij/seq/aij.c (revision acf2f550c537e2f6bdddf441d7f975a75b1c3ab0)
1b377110cSBarry Smith 
2d5d45c9bSBarry Smith /*
33369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
4d5d45c9bSBarry Smith   matrix storage format.
5d5d45c9bSBarry Smith */
63369ce9aSBarry Smith 
77c4f633dSBarry Smith 
8c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
9c6db04a5SJed Brown #include <petscblaslapack.h>
10c6db04a5SJed Brown #include <petscbt.h>
11d441b888SJed Brown #include <../src/mat/blocktranspose.h>
1278b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
1378b84d54SShri Abhyankar #include <petscthreadcomm.h>
1478b84d54SShri Abhyankar #endif
150716a85fSBarry Smith 
160716a85fSBarry Smith #undef __FUNCT__
170716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_SeqAIJ"
180716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
190716a85fSBarry Smith {
200716a85fSBarry Smith   PetscErrorCode ierr;
210716a85fSBarry Smith   PetscInt       i,m,n;
220716a85fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
230716a85fSBarry Smith 
240716a85fSBarry Smith   PetscFunctionBegin;
250716a85fSBarry Smith   ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr);
260716a85fSBarry Smith   ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr);
270716a85fSBarry Smith   if (type == NORM_2) {
280716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
290716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
300716a85fSBarry Smith     }
310716a85fSBarry Smith   } else if (type == NORM_1) {
320716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
330716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
340716a85fSBarry Smith     }
350716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
360716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
370716a85fSBarry Smith       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
380716a85fSBarry Smith     }
390716a85fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");
400716a85fSBarry Smith 
410716a85fSBarry Smith   if (type == NORM_2) {
428f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
430716a85fSBarry Smith   }
440716a85fSBarry Smith   PetscFunctionReturn(0);
450716a85fSBarry Smith }
460716a85fSBarry Smith 
474a2ae208SSatish Balay #undef __FUNCT__
48f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ_Private"
49f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows)
506ce1633cSBarry Smith {
516ce1633cSBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
526ce1633cSBarry Smith   const MatScalar   *aa = a->a;
536ce1633cSBarry Smith   PetscInt          i,m=A->rmap->n,cnt = 0;
546ce1633cSBarry Smith   const PetscInt    *jj = a->j,*diag;
556ce1633cSBarry Smith   PetscInt          *rows;
566ce1633cSBarry Smith   PetscErrorCode    ierr;
576ce1633cSBarry Smith 
586ce1633cSBarry Smith   PetscFunctionBegin;
596ce1633cSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
606ce1633cSBarry Smith   diag = a->diag;
616ce1633cSBarry Smith   for (i=0; i<m; i++) {
626ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
636ce1633cSBarry Smith       cnt++;
646ce1633cSBarry Smith     }
656ce1633cSBarry Smith   }
666ce1633cSBarry Smith   ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr);
676ce1633cSBarry Smith   cnt  = 0;
686ce1633cSBarry Smith   for (i=0; i<m; i++) {
696ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
706ce1633cSBarry Smith       rows[cnt++] = i;
716ce1633cSBarry Smith     }
726ce1633cSBarry Smith   }
73f1f41ecbSJed Brown   *nrows = cnt;
74f1f41ecbSJed Brown   *zrows = rows;
75f1f41ecbSJed Brown   PetscFunctionReturn(0);
76f1f41ecbSJed Brown }
77f1f41ecbSJed Brown 
78f1f41ecbSJed Brown #undef __FUNCT__
79f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ"
80f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
81f1f41ecbSJed Brown {
82f1f41ecbSJed Brown   PetscInt       nrows,*rows;
83f1f41ecbSJed Brown   PetscErrorCode ierr;
84f1f41ecbSJed Brown 
85f1f41ecbSJed Brown   PetscFunctionBegin;
86f1f41ecbSJed Brown   *zrows = PETSC_NULL;
87f1f41ecbSJed Brown   ierr = MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);CHKERRQ(ierr);
88f1f41ecbSJed Brown   ierr = ISCreateGeneral(((PetscObject)A)->comm,nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
896ce1633cSBarry Smith   PetscFunctionReturn(0);
906ce1633cSBarry Smith }
916ce1633cSBarry Smith 
926ce1633cSBarry Smith #undef __FUNCT__
93b3a44c85SBarry Smith #define __FUNCT__ "MatFindNonzeroRows_SeqAIJ"
94b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
95b3a44c85SBarry Smith {
96b3a44c85SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
97b3a44c85SBarry Smith   const MatScalar   *aa;
98b3a44c85SBarry Smith   PetscInt          m=A->rmap->n,cnt = 0;
99b3a44c85SBarry Smith   const PetscInt    *ii;
100b3a44c85SBarry Smith   PetscInt          n,i,j,*rows;
101b3a44c85SBarry Smith   PetscErrorCode    ierr;
102b3a44c85SBarry Smith 
103b3a44c85SBarry Smith   PetscFunctionBegin;
104b3a44c85SBarry Smith   *keptrows = 0;
105b3a44c85SBarry Smith   ii        = a->i;
106b3a44c85SBarry Smith   for (i=0; i<m; i++) {
107b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
108b3a44c85SBarry Smith     if (!n) {
109b3a44c85SBarry Smith       cnt++;
110b3a44c85SBarry Smith       goto ok1;
111b3a44c85SBarry Smith     }
112b3a44c85SBarry Smith     aa  = a->a + ii[i];
113b3a44c85SBarry Smith     for (j=0; j<n; j++) {
114b3a44c85SBarry Smith       if (aa[j] != 0.0) goto ok1;
115b3a44c85SBarry Smith     }
116b3a44c85SBarry Smith     cnt++;
117b3a44c85SBarry Smith     ok1:;
118b3a44c85SBarry Smith   }
119b3a44c85SBarry Smith   if (!cnt) PetscFunctionReturn(0);
120b3a44c85SBarry Smith   ierr = PetscMalloc((A->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr);
121b3a44c85SBarry Smith   cnt  = 0;
122b3a44c85SBarry Smith   for (i=0; i<m; i++) {
123b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
124b3a44c85SBarry Smith     if (!n) continue;
125b3a44c85SBarry Smith     aa  = a->a + ii[i];
126b3a44c85SBarry Smith     for (j=0; j<n; j++) {
127b3a44c85SBarry Smith       if (aa[j] != 0.0) {
128b3a44c85SBarry Smith         rows[cnt++] = i;
129b3a44c85SBarry Smith         break;
130b3a44c85SBarry Smith       }
131b3a44c85SBarry Smith     }
132b3a44c85SBarry Smith   }
133b3a44c85SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
134b3a44c85SBarry Smith   PetscFunctionReturn(0);
135b3a44c85SBarry Smith }
136b3a44c85SBarry Smith 
137b3a44c85SBarry Smith #undef __FUNCT__
13879299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1397087cfbeSBarry Smith PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
14079299369SBarry Smith {
14179299369SBarry Smith   PetscErrorCode ierr;
14279299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
143d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
14454f21887SBarry Smith   MatScalar      *aa = aij->a;
14554f21887SBarry Smith   PetscScalar    *v;
146ace3abfcSBarry Smith   PetscBool      missing;
14779299369SBarry Smith 
14879299369SBarry Smith   PetscFunctionBegin;
14909f38230SBarry Smith   if (Y->assembled) {
15009f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
15109f38230SBarry Smith     if (!missing) {
15279299369SBarry Smith       diag = aij->diag;
15379299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
15479299369SBarry Smith       if (is == INSERT_VALUES) {
15579299369SBarry Smith 	for (i=0; i<m; i++) {
15679299369SBarry Smith 	  aa[diag[i]] = v[i];
15779299369SBarry Smith 	}
15879299369SBarry Smith       } else {
15979299369SBarry Smith 	for (i=0; i<m; i++) {
16079299369SBarry Smith 	  aa[diag[i]] += v[i];
16179299369SBarry Smith 	}
16279299369SBarry Smith       }
16379299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
16479299369SBarry Smith       PetscFunctionReturn(0);
16579299369SBarry Smith     }
166*acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
16709f38230SBarry Smith   }
16809f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
16909f38230SBarry Smith   PetscFunctionReturn(0);
17009f38230SBarry Smith }
17179299369SBarry Smith 
17279299369SBarry Smith #undef __FUNCT__
1734a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
1741a83f524SJed Brown PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
17517ab2063SBarry Smith {
176416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
177dfbe8321SBarry Smith   PetscErrorCode ierr;
17897f1f81fSBarry Smith   PetscInt       i,ishift;
17917ab2063SBarry Smith 
1803a40ed3dSBarry Smith   PetscFunctionBegin;
181d0f46423SBarry Smith   *m     = A->rmap->n;
1823a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
183bfeeae90SHong Zhang   ishift = 0;
18453e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
1851a83f524SJed Brown     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
186bfeeae90SHong Zhang   } else if (oshift == 1) {
1871a83f524SJed Brown     PetscInt *tia;
188d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
1893b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
1901a83f524SJed Brown     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),&tia);CHKERRQ(ierr);
1911a83f524SJed Brown     for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1;
1921a83f524SJed Brown     *ia = tia;
193ecc77c7aSBarry Smith     if (ja) {
1941a83f524SJed Brown       PetscInt *tja;
1951a83f524SJed Brown       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&tja);CHKERRQ(ierr);
1961a83f524SJed Brown       for (i=0; i<nz; i++) tja[i] = a->j[i] + 1;
1971a83f524SJed Brown       *ja = tja;
198ecc77c7aSBarry Smith     }
1996945ee14SBarry Smith   } else {
200ecc77c7aSBarry Smith     *ia = a->i;
201ecc77c7aSBarry Smith     if (ja) *ja = a->j;
202a2ce50c7SBarry Smith   }
2033a40ed3dSBarry Smith   PetscFunctionReturn(0);
204a2744918SBarry Smith }
205a2744918SBarry Smith 
2064a2ae208SSatish Balay #undef __FUNCT__
2074a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
2081a83f524SJed Brown PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2096945ee14SBarry Smith {
210dfbe8321SBarry Smith   PetscErrorCode ierr;
2116945ee14SBarry Smith 
2123a40ed3dSBarry Smith   PetscFunctionBegin;
2133a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
214bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
215606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
216ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
217bcd2baecSBarry Smith   }
2183a40ed3dSBarry Smith   PetscFunctionReturn(0);
21917ab2063SBarry Smith }
22017ab2063SBarry Smith 
2214a2ae208SSatish Balay #undef __FUNCT__
2224a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
2231a83f524SJed Brown PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2243b2fbd54SBarry Smith {
2253b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
226dfbe8321SBarry Smith   PetscErrorCode ierr;
227d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
22897f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
2293b2fbd54SBarry Smith 
2303a40ed3dSBarry Smith   PetscFunctionBegin;
231899cda47SBarry Smith   *nn = n;
2323a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2333b2fbd54SBarry Smith   if (symmetric) {
2341a83f524SJed Brown     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
2353b2fbd54SBarry Smith   } else {
23697f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
23797f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
23897f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
23997f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
2403b2fbd54SBarry Smith     jj = a->j;
2413b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
242bfeeae90SHong Zhang       collengths[jj[i]]++;
2433b2fbd54SBarry Smith     }
2443b2fbd54SBarry Smith     cia[0] = oshift;
2453b2fbd54SBarry Smith     for (i=0; i<n; i++) {
2463b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
2473b2fbd54SBarry Smith     }
24897f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
2493b2fbd54SBarry Smith     jj   = a->j;
250a93ec695SBarry Smith     for (row=0; row<m; row++) {
251a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
252a93ec695SBarry Smith       for (i=0; i<mr; i++) {
253bfeeae90SHong Zhang         col = *jj++;
2543b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2553b2fbd54SBarry Smith       }
2563b2fbd54SBarry Smith     }
257606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2583b2fbd54SBarry Smith     *ia = cia; *ja = cja;
2593b2fbd54SBarry Smith   }
2603a40ed3dSBarry Smith   PetscFunctionReturn(0);
2613b2fbd54SBarry Smith }
2623b2fbd54SBarry Smith 
2634a2ae208SSatish Balay #undef __FUNCT__
2644a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
2651a83f524SJed Brown PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2663b2fbd54SBarry Smith {
267dfbe8321SBarry Smith   PetscErrorCode ierr;
268606d414cSSatish Balay 
2693a40ed3dSBarry Smith   PetscFunctionBegin;
2703a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2713b2fbd54SBarry Smith 
272606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
273606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
2743b2fbd54SBarry Smith 
2753a40ed3dSBarry Smith   PetscFunctionReturn(0);
2763b2fbd54SBarry Smith }
2773b2fbd54SBarry Smith 
27887d4246cSBarry Smith #undef __FUNCT__
27987d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
28087d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
28187d4246cSBarry Smith {
28287d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
28387d4246cSBarry Smith   PetscInt       *ai = a->i;
28487d4246cSBarry Smith   PetscErrorCode ierr;
28587d4246cSBarry Smith 
28687d4246cSBarry Smith   PetscFunctionBegin;
28787d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
28887d4246cSBarry Smith   PetscFunctionReturn(0);
28987d4246cSBarry Smith }
29087d4246cSBarry Smith 
2914a2ae208SSatish Balay #undef __FUNCT__
2924a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
29397f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
29417ab2063SBarry Smith {
295416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
296e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
29797f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
2986849ba73SBarry Smith   PetscErrorCode ierr;
299e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
30054f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
301ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
302ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
30317ab2063SBarry Smith 
3043a40ed3dSBarry Smith   PetscFunctionBegin;
30571fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
30617ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
307416022c9SBarry Smith     row  = im[k];
3085ef9f2a5SBarry Smith     if (row < 0) continue;
3092515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
310e32f2f54SBarry 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);
3113b2fbd54SBarry Smith #endif
312bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
31317ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
314416022c9SBarry Smith     low  = 0;
315c71e6ed7SBarry Smith     high = nrow;
31617ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
3175ef9f2a5SBarry Smith       if (in[l] < 0) continue;
3182515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
319e32f2f54SBarry 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);
3203b2fbd54SBarry Smith #endif
321bfeeae90SHong Zhang       col = in[l];
32216371a99SBarry Smith       if (v) {
3234b0e389bSBarry Smith 	if (roworiented) {
3245ef9f2a5SBarry Smith 	  value = v[l + k*n];
325bef8e0ddSBarry Smith 	} else {
3264b0e389bSBarry Smith 	  value = v[k + l*m];
3274b0e389bSBarry Smith 	}
32816371a99SBarry Smith       } else {
32975567043SBarry Smith         value = 0.;
33016371a99SBarry Smith       }
331abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
33236db0b34SBarry Smith 
3337cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
334e2ee6c50SBarry Smith       lastcol = col;
335416022c9SBarry Smith       while (high-low > 5) {
336416022c9SBarry Smith         t = (low+high)/2;
337416022c9SBarry Smith         if (rp[t] > col) high = t;
338416022c9SBarry Smith         else             low  = t;
33917ab2063SBarry Smith       }
340416022c9SBarry Smith       for (i=low; i<high; i++) {
34117ab2063SBarry Smith         if (rp[i] > col) break;
34217ab2063SBarry Smith         if (rp[i] == col) {
343416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
34417ab2063SBarry Smith           else                  ap[i] = value;
345e44c0bd4SBarry Smith           low = i + 1;
34617ab2063SBarry Smith           goto noinsert;
34717ab2063SBarry Smith         }
34817ab2063SBarry Smith       }
349abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
350c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
351e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
352fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
353c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
354416022c9SBarry Smith       /* shift up all the later entries in this row */
355416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
35617ab2063SBarry Smith         rp[ii+1] = rp[ii];
35717ab2063SBarry Smith         ap[ii+1] = ap[ii];
35817ab2063SBarry Smith       }
35917ab2063SBarry Smith       rp[i] = col;
36017ab2063SBarry Smith       ap[i] = value;
361416022c9SBarry Smith       low   = i + 1;
362e44c0bd4SBarry Smith       noinsert:;
36317ab2063SBarry Smith     }
36417ab2063SBarry Smith     ailen[row] = nrow;
36517ab2063SBarry Smith   }
36688e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
3673a40ed3dSBarry Smith   PetscFunctionReturn(0);
36817ab2063SBarry Smith }
36917ab2063SBarry Smith 
37081824310SBarry Smith 
3714a2ae208SSatish Balay #undef __FUNCT__
3724a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
373a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
3747eb43aa7SLois Curfman McInnes {
3757eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
37697f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
37797f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
37854f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
3797eb43aa7SLois Curfman McInnes 
3803a40ed3dSBarry Smith   PetscFunctionBegin;
3817eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
3827eb43aa7SLois Curfman McInnes     row  = im[k];
383e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
384e32f2f54SBarry 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);
385bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
3867eb43aa7SLois Curfman McInnes     nrow = ailen[row];
3877eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
388e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
389e32f2f54SBarry 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);
390bfeeae90SHong Zhang       col = in[l] ;
3917eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
3927eb43aa7SLois Curfman McInnes       while (high-low > 5) {
3937eb43aa7SLois Curfman McInnes         t = (low+high)/2;
3947eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
3957eb43aa7SLois Curfman McInnes         else             low  = t;
3967eb43aa7SLois Curfman McInnes       }
3977eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
3987eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
3997eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
400b49de8d1SLois Curfman McInnes           *v++ = ap[i];
4017eb43aa7SLois Curfman McInnes           goto finished;
4027eb43aa7SLois Curfman McInnes         }
4037eb43aa7SLois Curfman McInnes       }
40497e567efSBarry Smith       *v++ = 0.0;
4057eb43aa7SLois Curfman McInnes       finished:;
4067eb43aa7SLois Curfman McInnes     }
4077eb43aa7SLois Curfman McInnes   }
4083a40ed3dSBarry Smith   PetscFunctionReturn(0);
4097eb43aa7SLois Curfman McInnes }
4107eb43aa7SLois Curfman McInnes 
41117ab2063SBarry Smith 
4124a2ae208SSatish Balay #undef __FUNCT__
4134a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
414dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
41517ab2063SBarry Smith {
416416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4176849ba73SBarry Smith   PetscErrorCode ierr;
4186f69ff64SBarry Smith   PetscInt       i,*col_lens;
4196f69ff64SBarry Smith   int            fd;
420b37d52dbSMark F. Adams   FILE           *file;
42117ab2063SBarry Smith 
4223a40ed3dSBarry Smith   PetscFunctionBegin;
423b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
424d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
4250700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
426d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
427d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
428416022c9SBarry Smith   col_lens[3] = a->nz;
429416022c9SBarry Smith 
430416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
431d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
432416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
43317ab2063SBarry Smith   }
434d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
435606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
436416022c9SBarry Smith 
437416022c9SBarry Smith   /* store column indices (zero start index) */
4386f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
439416022c9SBarry Smith 
440416022c9SBarry Smith   /* store nonzero values */
4416f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
442b37d52dbSMark F. Adams 
443b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
444b37d52dbSMark F. Adams   if (file) {
445b37d52dbSMark F. Adams     fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
446b37d52dbSMark F. Adams   }
447b37d52dbSMark F. Adams 
4483a40ed3dSBarry Smith   PetscFunctionReturn(0);
44917ab2063SBarry Smith }
450416022c9SBarry Smith 
45109573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
452cd155464SBarry Smith 
4534a2ae208SSatish Balay #undef __FUNCT__
4544a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
455dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
456416022c9SBarry Smith {
457416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
458dfbe8321SBarry Smith   PetscErrorCode    ierr;
459d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
460e060cb09SBarry Smith   const char        *name;
461f3ef73ceSBarry Smith   PetscViewerFormat format;
46217ab2063SBarry Smith 
4633a40ed3dSBarry Smith   PetscFunctionBegin;
464b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
46571c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
46697f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
467d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
468d00d2cf4SBarry Smith       nofinalvalue = 1;
469d00d2cf4SBarry Smith     }
470d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
471d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
47277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
47377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
474b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
47517ab2063SBarry Smith 
47617ab2063SBarry Smith     for (i=0; i<m; i++) {
477416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
478aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
47977431f27SBarry 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);
48017ab2063SBarry Smith #else
48177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
48217ab2063SBarry Smith #endif
48317ab2063SBarry Smith       }
48417ab2063SBarry Smith     }
485d00d2cf4SBarry Smith     if (nofinalvalue) {
486d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
487d00d2cf4SBarry Smith     }
488317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
489fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
490d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
49168369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
492cd155464SBarry Smith      PetscFunctionReturn(0);
493fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
494d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4957566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
49644cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
49777431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
49844cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
499aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
50036db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
501ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50236db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
503ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50436db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
505ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
5066831982aSBarry Smith         }
50744cd7ae7SLois Curfman McInnes #else
508ba0e910bSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);}
50944cd7ae7SLois Curfman McInnes #endif
51044cd7ae7SLois Curfman McInnes       }
511b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
51244cd7ae7SLois Curfman McInnes     }
513d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
514fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
51597f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
516d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5177566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
51897f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
519496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
520496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
521496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
522496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
523aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52436db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
525496be53dSLois Curfman McInnes #else
526496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
527496be53dSLois Curfman McInnes #endif
528496be53dSLois Curfman McInnes         }
529496be53dSLois Curfman McInnes       }
530496be53dSLois Curfman McInnes     }
5312e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
53277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
5332e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
53477431f27SBarry 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);}
53577431f27SBarry 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);}
53677431f27SBarry 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);}
53777431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
53877431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
53977431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
540496be53dSLois Curfman McInnes     }
541b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
542606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
543496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
544496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
54577431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
546496be53dSLois Curfman McInnes       }
547b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
548496be53dSLois Curfman McInnes     }
549b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
550496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
551496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
552496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
553aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
55436db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
555b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5566831982aSBarry Smith           }
557496be53dSLois Curfman McInnes #else
558b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
559496be53dSLois Curfman McInnes #endif
560496be53dSLois Curfman McInnes         }
561496be53dSLois Curfman McInnes       }
562b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
563496be53dSLois Curfman McInnes     }
564d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
565fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
56697f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
56787828ca2SBarry Smith     PetscScalar value;
56802594712SBarry Smith 
569d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5707566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
57102594712SBarry Smith     for (i=0; i<m; i++) {
57202594712SBarry Smith       jcnt = 0;
573d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
574e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
57502594712SBarry Smith           value = a->a[cnt++];
576e24b481bSBarry Smith           jcnt++;
57702594712SBarry Smith         } else {
57802594712SBarry Smith           value = 0.0;
57902594712SBarry Smith         }
580aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
581b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
58202594712SBarry Smith #else
583b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
58402594712SBarry Smith #endif
58502594712SBarry Smith       }
586b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
58702594712SBarry Smith     }
588d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5893c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
590d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5917566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
5923c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5933c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
5943c215bfdSMatthew Knepley #else
5953c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
5963c215bfdSMatthew Knepley #endif
597d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
5983c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
5993c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
6003c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
6013c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
602ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g %g\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6033c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
604ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g -%g\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6053c215bfdSMatthew Knepley         } else {
606ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
6073c215bfdSMatthew Knepley         }
6083c215bfdSMatthew Knepley #else
609ba0e910bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+shift, a->j[j]+shift, (double)a->a[j]);CHKERRQ(ierr);
6103c215bfdSMatthew Knepley #endif
6113c215bfdSMatthew Knepley       }
6123c215bfdSMatthew Knepley     }
613d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
6143a40ed3dSBarry Smith   } else {
615d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
6167566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
617d5f3da31SBarry Smith     if (A->factortype){
61816cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
61916cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
62016cd7e1dSShri Abhyankar         /* L part */
62116cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
62216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
62316cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
624ba0e910bSBarry Smith             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) {
626ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
62716cd7e1dSShri Abhyankar           } else {
628ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
62916cd7e1dSShri Abhyankar           }
63016cd7e1dSShri Abhyankar #else
631ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);
63216cd7e1dSShri Abhyankar #endif
63316cd7e1dSShri Abhyankar         }
63416cd7e1dSShri Abhyankar 	/* diagonal */
63516cd7e1dSShri Abhyankar 	j = a->diag[i];
63616cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
63716cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
638ba0e910bSBarry Smith             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);
63916cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
640ba0e910bSBarry Smith             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);
64116cd7e1dSShri Abhyankar           } else {
642ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
64316cd7e1dSShri Abhyankar           }
64416cd7e1dSShri Abhyankar #else
645ba0e910bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)1.0/a->a[j]);CHKERRQ(ierr);
64616cd7e1dSShri Abhyankar #endif
64716cd7e1dSShri Abhyankar 
64816cd7e1dSShri Abhyankar 	/* U part */
64916cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
65016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
65116cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
652ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
65316cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
654ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
65516cd7e1dSShri Abhyankar           } else {
656ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
65716cd7e1dSShri Abhyankar           }
65816cd7e1dSShri Abhyankar #else
659ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);
66016cd7e1dSShri Abhyankar #endif
66116cd7e1dSShri Abhyankar }
66216cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
66316cd7e1dSShri Abhyankar         }
66416cd7e1dSShri Abhyankar     } else {
66517ab2063SBarry Smith       for (i=0; i<m; i++) {
66677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
667416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
668aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
66936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
670ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
67136db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
672ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6733a40ed3dSBarry Smith           } else {
674ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
67517ab2063SBarry Smith           }
67617ab2063SBarry Smith #else
677ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);
67817ab2063SBarry Smith #endif
67917ab2063SBarry Smith         }
680b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
68117ab2063SBarry Smith       }
68216cd7e1dSShri Abhyankar     }
683d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
68417ab2063SBarry Smith   }
685b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6863a40ed3dSBarry Smith   PetscFunctionReturn(0);
687416022c9SBarry Smith }
688416022c9SBarry Smith 
6894a2ae208SSatish Balay #undef __FUNCT__
6904a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
691dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
692416022c9SBarry Smith {
693480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
694416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
695dfbe8321SBarry Smith   PetscErrorCode    ierr;
696d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
69736db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
698b0a32e0cSBarry Smith   PetscViewer       viewer;
699f3ef73ceSBarry Smith   PetscViewerFormat format;
700cddf8d76SBarry Smith 
7013a40ed3dSBarry Smith   PetscFunctionBegin;
702480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
703b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
70419bcc07fSBarry Smith 
705b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
706416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
7070513a670SBarry Smith 
708fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
7090513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
710b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
711416022c9SBarry Smith     for (i=0; i<m; i++) {
712cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
713bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
714bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
715aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
71636db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
717cddf8d76SBarry Smith #else
718cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
719cddf8d76SBarry Smith #endif
720b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
721cddf8d76SBarry Smith       }
722cddf8d76SBarry Smith     }
723b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
724cddf8d76SBarry Smith     for (i=0; i<m; i++) {
725cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
726bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
727bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
728cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
729b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
730cddf8d76SBarry Smith       }
731cddf8d76SBarry Smith     }
732b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
733cddf8d76SBarry Smith     for (i=0; i<m; i++) {
734cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
735bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
736bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
737aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
73836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
739cddf8d76SBarry Smith #else
740cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
741cddf8d76SBarry Smith #endif
742b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
743416022c9SBarry Smith       }
744416022c9SBarry Smith     }
7450513a670SBarry Smith   } else {
7460513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
7470513a670SBarry Smith     /* first determine max of all nonzero values */
74897f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
749b0a32e0cSBarry Smith     PetscDraw   popup;
75036db0b34SBarry Smith     PetscReal scale;
7510513a670SBarry Smith 
7520513a670SBarry Smith     for (i=0; i<nz; i++) {
7530513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
7540513a670SBarry Smith     }
755b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
756b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
757b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
7580513a670SBarry Smith     count = 0;
7590513a670SBarry Smith     for (i=0; i<m; i++) {
7600513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
761bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
762bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
76397f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
764b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
7650513a670SBarry Smith         count++;
7660513a670SBarry Smith       }
7670513a670SBarry Smith     }
7680513a670SBarry Smith   }
769480ef9eaSBarry Smith   PetscFunctionReturn(0);
770480ef9eaSBarry Smith }
771cddf8d76SBarry Smith 
7724a2ae208SSatish Balay #undef __FUNCT__
7734a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
774dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
775480ef9eaSBarry Smith {
776dfbe8321SBarry Smith   PetscErrorCode ierr;
777b0a32e0cSBarry Smith   PetscDraw      draw;
77836db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
779ace3abfcSBarry Smith   PetscBool      isnull;
780480ef9eaSBarry Smith 
781480ef9eaSBarry Smith   PetscFunctionBegin;
782b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
783b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
784480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
785480ef9eaSBarry Smith 
786480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
787d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
788480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
789b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
790b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
791480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
7923a40ed3dSBarry Smith   PetscFunctionReturn(0);
793416022c9SBarry Smith }
794416022c9SBarry Smith 
7954a2ae208SSatish Balay #undef __FUNCT__
7964a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
797dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
798416022c9SBarry Smith {
799dfbe8321SBarry Smith   PetscErrorCode ierr;
800ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
801416022c9SBarry Smith 
8023a40ed3dSBarry Smith   PetscFunctionBegin;
803251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
804251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
805251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
806c45a1595SBarry Smith   if (iascii) {
8073a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
8080f5bd95cSBarry Smith   } else if (isbinary) {
8093a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
8100f5bd95cSBarry Smith   } else if (isdraw) {
8113a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
812913ac41fSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
8134108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
8143a40ed3dSBarry Smith   PetscFunctionReturn(0);
81517ab2063SBarry Smith }
81619bcc07fSBarry Smith 
8174a2ae208SSatish Balay #undef __FUNCT__
8184a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
819dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
82017ab2063SBarry Smith {
821416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8226849ba73SBarry Smith   PetscErrorCode ierr;
82397f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
824d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
82554f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
8263447b6efSHong Zhang   PetscReal      ratio=0.6;
82717ab2063SBarry Smith 
8283a40ed3dSBarry Smith   PetscFunctionBegin;
8293a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
83017ab2063SBarry Smith 
83143ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
83217ab2063SBarry Smith   for (i=1; i<m; i++) {
833416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
83417ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
83594a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
83617ab2063SBarry Smith     if (fshift) {
837bfeeae90SHong Zhang       ip = aj + ai[i] ;
838bfeeae90SHong Zhang       ap = aa + ai[i] ;
83917ab2063SBarry Smith       N  = ailen[i];
84017ab2063SBarry Smith       for (j=0; j<N; j++) {
84117ab2063SBarry Smith         ip[j-fshift] = ip[j];
84217ab2063SBarry Smith         ap[j-fshift] = ap[j];
84317ab2063SBarry Smith       }
84417ab2063SBarry Smith     }
84517ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
84617ab2063SBarry Smith   }
84717ab2063SBarry Smith   if (m) {
84817ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
84917ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
85017ab2063SBarry Smith   }
85117ab2063SBarry Smith   /* reset ilen and imax for each row */
85217ab2063SBarry Smith   for (i=0; i<m; i++) {
85317ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
85417ab2063SBarry Smith   }
855bfeeae90SHong Zhang   a->nz = ai[m];
85665e19b50SBarry 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);
85717ab2063SBarry Smith 
85809f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
859d0f46423SBarry 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);
860ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
861ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
8628e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
863dd5f02e7SSatish Balay   a->reallocs          = 0;
8644e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
86536db0b34SBarry Smith   a->rmax              = rmax;
8664e220ebcSLois Curfman McInnes 
867cd6b891eSBarry Smith   ierr = MatCheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
86888e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
86971c2f376SKris Buschelman 
8704108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
87171f1c65dSBarry Smith 
872*acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
8733a40ed3dSBarry Smith   PetscFunctionReturn(0);
87417ab2063SBarry Smith }
87517ab2063SBarry Smith 
8764a2ae208SSatish Balay #undef __FUNCT__
87799cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
87899cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
87999cafbc1SBarry Smith {
88099cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
88199cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
88254f21887SBarry Smith   MatScalar      *aa = a->a;
883*acf2f550SJed Brown   PetscErrorCode ierr;
88499cafbc1SBarry Smith 
88599cafbc1SBarry Smith   PetscFunctionBegin;
88699cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
887*acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
88899cafbc1SBarry Smith   PetscFunctionReturn(0);
88999cafbc1SBarry Smith }
89099cafbc1SBarry Smith 
89199cafbc1SBarry Smith #undef __FUNCT__
89299cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
89399cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
89499cafbc1SBarry Smith {
89599cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
89699cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
89754f21887SBarry Smith   MatScalar      *aa = a->a;
898*acf2f550SJed Brown   PetscErrorCode ierr;
89999cafbc1SBarry Smith 
90099cafbc1SBarry Smith   PetscFunctionBegin;
90199cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
902*acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
90399cafbc1SBarry Smith   PetscFunctionReturn(0);
90499cafbc1SBarry Smith }
90599cafbc1SBarry Smith 
90678b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
90778b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ_Kernel(PetscInt thread_id,Mat A)
90878b84d54SShri Abhyankar {
90978b84d54SShri Abhyankar   PetscErrorCode ierr;
91078b84d54SShri Abhyankar   PetscInt       *trstarts=A->rmap->trstarts;
91178b84d54SShri Abhyankar   PetscInt       n,start,end;
91278b84d54SShri Abhyankar   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
91378b84d54SShri Abhyankar 
91478b84d54SShri Abhyankar   start = trstarts[thread_id];
91578b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
91619baf141SJed Brown   n     = a->i[end] - a->i[start];
91719baf141SJed Brown   ierr = PetscMemzero(a->a+a->i[start],n*sizeof(PetscScalar));CHKERRQ(ierr);
91878b84d54SShri Abhyankar   return 0;
91978b84d54SShri Abhyankar }
92078b84d54SShri Abhyankar 
92178b84d54SShri Abhyankar #undef __FUNCT__
92278b84d54SShri Abhyankar #define __FUNCT__ "MatZeroEntries_SeqAIJ"
92378b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
92478b84d54SShri Abhyankar {
92578b84d54SShri Abhyankar   PetscErrorCode ierr;
92678b84d54SShri Abhyankar 
92778b84d54SShri Abhyankar   PetscFunctionBegin;
92878b84d54SShri Abhyankar   ierr = PetscThreadCommRunKernel(((PetscObject)A)->comm,(PetscThreadKernel)MatZeroEntries_SeqAIJ_Kernel,1,A);CHKERRQ(ierr);
929*acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
93078b84d54SShri Abhyankar   PetscFunctionReturn(0);
93178b84d54SShri Abhyankar }
93278b84d54SShri Abhyankar #else
93399cafbc1SBarry Smith #undef __FUNCT__
9344a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
935dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
93617ab2063SBarry Smith {
937416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
938dfbe8321SBarry Smith   PetscErrorCode ierr;
9393a40ed3dSBarry Smith 
9403a40ed3dSBarry Smith   PetscFunctionBegin;
941d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
942*acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
9433a40ed3dSBarry Smith   PetscFunctionReturn(0);
94417ab2063SBarry Smith }
94578b84d54SShri Abhyankar #endif
946416022c9SBarry Smith 
9474a2ae208SSatish Balay #undef __FUNCT__
9484a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
949dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
95017ab2063SBarry Smith {
951416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
952dfbe8321SBarry Smith   PetscErrorCode ierr;
953d5d45c9bSBarry Smith 
9543a40ed3dSBarry Smith   PetscFunctionBegin;
955aa482453SBarry Smith #if defined(PETSC_USE_LOG)
956d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
95717ab2063SBarry Smith #endif
958e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
9596bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
9606bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
96105b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
962d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
96305b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
96471f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
96505b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
9666bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
96705b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
9686bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
96905b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
9706bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
971cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
9720b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
973a30b2313SHong Zhang 
9744108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
975bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
976901853e0SKris Buschelman 
977dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
978901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
979901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
980901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
981901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
982901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
9835a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr);
984901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
985901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
986a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
987901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
9883a40ed3dSBarry Smith   PetscFunctionReturn(0);
98917ab2063SBarry Smith }
99017ab2063SBarry Smith 
9914a2ae208SSatish Balay #undef __FUNCT__
9924a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
993ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool  flg)
99417ab2063SBarry Smith {
995416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9964846f1f5SKris Buschelman   PetscErrorCode ierr;
9973a40ed3dSBarry Smith 
9983a40ed3dSBarry Smith   PetscFunctionBegin;
999a65d3064SKris Buschelman   switch (op) {
1000a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
10014e0d8c25SBarry Smith     a->roworiented       = flg;
1002a65d3064SKris Buschelman     break;
1003a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1004a9817697SBarry Smith     a->keepnonzeropattern    = flg;
1005a65d3064SKris Buschelman     break;
1006512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1007512a5fc5SBarry Smith     a->nonew             = (flg ? 0 : 1);
1008a65d3064SKris Buschelman     break;
1009a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
10104e0d8c25SBarry Smith     a->nonew             = (flg ? -1 : 0);
1011a65d3064SKris Buschelman     break;
1012a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
10134e0d8c25SBarry Smith     a->nonew             = (flg ? -2 : 0);
1014a65d3064SKris Buschelman     break;
101528b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
101628b2fa4aSMatthew Knepley     a->nounused          = (flg ? -1 : 0);
101728b2fa4aSMatthew Knepley     break;
1018a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
10194e0d8c25SBarry Smith     a->ignorezeroentries = flg;
10200df259c2SBarry Smith     break;
1021cd6b891eSBarry Smith   case MAT_CHECK_COMPRESSED_ROW:
1022cd6b891eSBarry Smith     a->compressedrow.check = flg;
1023d487561eSHong Zhang     break;
10243d472b54SHong Zhang   case MAT_SPD:
1025b1646e73SJed Brown   case MAT_SYMMETRIC:
1026b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1027b1646e73SJed Brown   case MAT_HERMITIAN:
1028b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
10295021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
10305021d80fSJed Brown     break;
10314e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1032a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1033a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1034290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1035a65d3064SKris Buschelman     break;
1036b87ac2d8SJed Brown   case MAT_USE_INODES:
1037b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1038b87ac2d8SJed Brown     break;
1039a65d3064SKris Buschelman   default:
1040e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1041a65d3064SKris Buschelman   }
10424108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
10433a40ed3dSBarry Smith   PetscFunctionReturn(0);
104417ab2063SBarry Smith }
104517ab2063SBarry Smith 
10464a2ae208SSatish Balay #undef __FUNCT__
10474a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1048dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
104917ab2063SBarry Smith {
1050416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10516849ba73SBarry Smith   PetscErrorCode ierr;
1052d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
105335e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
105417ab2063SBarry Smith 
10553a40ed3dSBarry Smith   PetscFunctionBegin;
1056d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1057e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
105835e7444dSHong Zhang 
1059d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
1060d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
106135e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
10622c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
106335e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
106435e7444dSHong Zhang     PetscFunctionReturn(0);
106535e7444dSHong Zhang   }
106635e7444dSHong Zhang 
10672dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
10681ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
106935e7444dSHong Zhang   for (i=0; i<n; i++) {
107035e7444dSHong Zhang     nz = ai[i+1] - ai[i];
10712f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
107235e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
107335e7444dSHong Zhang       if (aj[j] == i) {
107435e7444dSHong Zhang         x[i] = aa[j];
107517ab2063SBarry Smith         break;
107617ab2063SBarry Smith       }
107717ab2063SBarry Smith     }
107817ab2063SBarry Smith   }
10791ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
10803a40ed3dSBarry Smith   PetscFunctionReturn(0);
108117ab2063SBarry Smith }
108217ab2063SBarry Smith 
1083c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10844a2ae208SSatish Balay #undef __FUNCT__
10854a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1086dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
108717ab2063SBarry Smith {
1088416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
10895c897100SBarry Smith   PetscScalar       *x,*y;
1090dfbe8321SBarry Smith   PetscErrorCode    ierr;
1091d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
10925c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1093a77337e4SBarry Smith   MatScalar         *v;
1094a77337e4SBarry Smith   PetscScalar       alpha;
109504fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
10963447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
1097ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
10985c897100SBarry Smith #endif
109917ab2063SBarry Smith 
11003a40ed3dSBarry Smith   PetscFunctionBegin;
11012e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
11021ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
11031ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
11045c897100SBarry Smith 
11055c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1106bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
11075c897100SBarry Smith #else
11083447b6efSHong Zhang   if (usecprow){
11093447b6efSHong Zhang     m    = cprow.nrows;
11103447b6efSHong Zhang     ii   = cprow.i;
11117b2bb3b9SHong Zhang     ridx = cprow.rindex;
11123447b6efSHong Zhang   } else {
11133447b6efSHong Zhang     ii = a->i;
11143447b6efSHong Zhang   }
111517ab2063SBarry Smith   for (i=0; i<m; i++) {
11163447b6efSHong Zhang     idx   = a->j + ii[i] ;
11173447b6efSHong Zhang     v     = a->a + ii[i] ;
11183447b6efSHong Zhang     n     = ii[i+1] - ii[i];
11193447b6efSHong Zhang     if (usecprow){
11207b2bb3b9SHong Zhang       alpha = x[ridx[i]];
11213447b6efSHong Zhang     } else {
112217ab2063SBarry Smith       alpha = x[i];
11233447b6efSHong Zhang     }
112404fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
112517ab2063SBarry Smith   }
11265c897100SBarry Smith #endif
1127dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
11281ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11291ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11303a40ed3dSBarry Smith   PetscFunctionReturn(0);
113117ab2063SBarry Smith }
113217ab2063SBarry Smith 
11334a2ae208SSatish Balay #undef __FUNCT__
11345c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1135dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
11365c897100SBarry Smith {
1137dfbe8321SBarry Smith   PetscErrorCode ierr;
11385c897100SBarry Smith 
11395c897100SBarry Smith   PetscFunctionBegin;
1140170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
11415c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
11425c897100SBarry Smith   PetscFunctionReturn(0);
11435c897100SBarry Smith }
11445c897100SBarry Smith 
1145c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
114678b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
114778b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ_Kernel(PetscInt thread_id,Mat A,Vec xx,Vec yy)
114878b84d54SShri Abhyankar {
114978b84d54SShri Abhyankar   PetscErrorCode    ierr;
115078b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
115178b84d54SShri Abhyankar   PetscScalar       *y;
115278b84d54SShri Abhyankar   const PetscScalar *x;
115378b84d54SShri Abhyankar   const MatScalar   *aa;
115478b84d54SShri Abhyankar   PetscInt          *trstarts=A->rmap->trstarts;
115578b84d54SShri Abhyankar   PetscInt          n,start,end,i;
115678b84d54SShri Abhyankar   const PetscInt   *aj,*ai;
115778b84d54SShri Abhyankar   PetscScalar      sum;
115878b84d54SShri Abhyankar 
115978b84d54SShri Abhyankar   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
116078b84d54SShri Abhyankar   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
116178b84d54SShri Abhyankar   start = trstarts[thread_id];
116278b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
116378b84d54SShri Abhyankar   aj    = a->j;
116478b84d54SShri Abhyankar   aa    = a->a;
116578b84d54SShri Abhyankar   ai    = a->i;
116678b84d54SShri Abhyankar   for (i=start;i<end;i++) {
116778b84d54SShri Abhyankar     n = ai[i+1] - ai[i];
116878b84d54SShri Abhyankar     aj = a->j + ai[i];
116978b84d54SShri Abhyankar     aa = a->a + ai[i];
117078b84d54SShri Abhyankar     sum = 0.0;
117178b84d54SShri Abhyankar     PetscSparseDensePlusDot(sum,x,aa,aj,n);
117278b84d54SShri Abhyankar     y[i] = sum;
117378b84d54SShri Abhyankar   }
117478b84d54SShri Abhyankar   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
117578b84d54SShri Abhyankar   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
117678b84d54SShri Abhyankar   return 0;
117778b84d54SShri Abhyankar }
117878b84d54SShri Abhyankar 
117978b84d54SShri Abhyankar #undef __FUNCT__
118078b84d54SShri Abhyankar #define __FUNCT__ "MatMult_SeqAIJ"
118178b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
118278b84d54SShri Abhyankar {
118378b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
118478b84d54SShri Abhyankar   PetscScalar       *y;
118578b84d54SShri Abhyankar   const PetscScalar *x;
118678b84d54SShri Abhyankar   const MatScalar   *aa;
118778b84d54SShri Abhyankar   PetscErrorCode    ierr;
118878b84d54SShri Abhyankar   PetscInt          m=A->rmap->n;
118978b84d54SShri Abhyankar   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
119078b84d54SShri Abhyankar   PetscInt          n,i,nonzerorow=0;
119178b84d54SShri Abhyankar   PetscScalar       sum;
119278b84d54SShri Abhyankar   PetscBool         usecprow=a->compressedrow.use;
119378b84d54SShri Abhyankar 
119478b84d54SShri Abhyankar #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
119578b84d54SShri Abhyankar #pragma disjoint(*x,*y,*aa)
119678b84d54SShri Abhyankar #endif
119778b84d54SShri Abhyankar 
119878b84d54SShri Abhyankar   PetscFunctionBegin;
119978b84d54SShri Abhyankar   aj  = a->j;
120078b84d54SShri Abhyankar   aa  = a->a;
120178b84d54SShri Abhyankar   ii  = a->i;
120278b84d54SShri Abhyankar   if (usecprow){ /* use compressed row format */
120378b84d54SShri Abhyankar     ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
120478b84d54SShri Abhyankar     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
120578b84d54SShri Abhyankar     m    = a->compressedrow.nrows;
120678b84d54SShri Abhyankar     ii   = a->compressedrow.i;
120778b84d54SShri Abhyankar     ridx = a->compressedrow.rindex;
120878b84d54SShri Abhyankar     for (i=0; i<m; i++){
120978b84d54SShri Abhyankar       n   = ii[i+1] - ii[i];
121078b84d54SShri Abhyankar       aj  = a->j + ii[i];
121178b84d54SShri Abhyankar       aa  = a->a + ii[i];
121278b84d54SShri Abhyankar       sum = 0.0;
121378b84d54SShri Abhyankar       nonzerorow += (n>0);
121478b84d54SShri Abhyankar       PetscSparseDensePlusDot(sum,x,aa,aj,n);
121578b84d54SShri Abhyankar       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
121678b84d54SShri Abhyankar       y[*ridx++] = sum;
121778b84d54SShri Abhyankar     }
121878b84d54SShri Abhyankar     ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
121978b84d54SShri Abhyankar     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
122078b84d54SShri Abhyankar   } else { /* do not use compressed row format */
122178b84d54SShri Abhyankar #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
122278b84d54SShri Abhyankar     fortranmultaij_(&m,x,ii,aj,aa,y);
122378b84d54SShri Abhyankar #else
122478b84d54SShri Abhyankar     ierr = PetscThreadCommRunKernel(((PetscObject)A)->comm,(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
122578b84d54SShri Abhyankar #endif
122678b84d54SShri Abhyankar   }
122778b84d54SShri Abhyankar   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
122878b84d54SShri Abhyankar   PetscFunctionReturn(0);
122978b84d54SShri Abhyankar }
123078b84d54SShri Abhyankar #else
12315c897100SBarry Smith #undef __FUNCT__
12324a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1233dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
123417ab2063SBarry Smith {
1235416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1236d9fead3dSBarry Smith   PetscScalar       *y;
123754f21887SBarry Smith   const PetscScalar *x;
123854f21887SBarry Smith   const MatScalar   *aa;
1239dfbe8321SBarry Smith   PetscErrorCode    ierr;
1240003131ecSBarry Smith   PetscInt          m=A->rmap->n;
1241003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
12428aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1243362ced78SSatish Balay   PetscScalar       sum;
1244ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
124517ab2063SBarry Smith 
1246b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
124797952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1248fee21e36SBarry Smith #endif
1249fee21e36SBarry Smith 
12503a40ed3dSBarry Smith   PetscFunctionBegin;
12513649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12521ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
125397952fefSHong Zhang   aj  = a->j;
125497952fefSHong Zhang   aa  = a->a;
1255416022c9SBarry Smith   ii  = a->i;
12564eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
125797952fefSHong Zhang     m    = a->compressedrow.nrows;
125897952fefSHong Zhang     ii   = a->compressedrow.i;
125997952fefSHong Zhang     ridx = a->compressedrow.rindex;
126097952fefSHong Zhang     for (i=0; i<m; i++){
126197952fefSHong Zhang       n   = ii[i+1] - ii[i];
126297952fefSHong Zhang       aj  = a->j + ii[i];
126397952fefSHong Zhang       aa  = a->a + ii[i];
126497952fefSHong Zhang       sum = 0.0;
1265a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1266003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1267003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
126897952fefSHong Zhang       y[*ridx++] = sum;
126997952fefSHong Zhang     }
127097952fefSHong Zhang   } else { /* do not use compressed row format */
1271b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1272b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1273b05257ddSBarry Smith #else
127478b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
127578b84d54SShri Abhyankar     ierr = PetscThreadCommRunKernel(((PetscObject)A)->comm,(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
127678b84d54SShri Abhyankar #else
127717ab2063SBarry Smith     for (i=0; i<m; i++) {
1278003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1279003131ecSBarry Smith       aj  = a->j + ii[i];
1280003131ecSBarry Smith       aa  = a->a + ii[i];
128117ab2063SBarry Smith       sum  = 0.0;
1282a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1283003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
128417ab2063SBarry Smith       y[i] = sum;
128517ab2063SBarry Smith     }
12868d195f9aSBarry Smith #endif
128778b84d54SShri Abhyankar #endif
1288b05257ddSBarry Smith   }
1289dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
12903649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12911ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12923a40ed3dSBarry Smith   PetscFunctionReturn(0);
129317ab2063SBarry Smith }
129478b84d54SShri Abhyankar #endif
129517ab2063SBarry Smith 
1296c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
12974a2ae208SSatish Balay #undef __FUNCT__
12984a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1299dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
130017ab2063SBarry Smith {
1301416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1302f15663dcSBarry Smith   PetscScalar       *y,*z;
1303f15663dcSBarry Smith   const PetscScalar *x;
130454f21887SBarry Smith   const MatScalar   *aa;
1305dfbe8321SBarry Smith   PetscErrorCode    ierr;
1306d0f46423SBarry Smith   PetscInt          m = A->rmap->n,*aj,*ii;
1307f15663dcSBarry Smith   PetscInt          n,i,*ridx=PETSC_NULL;
1308362ced78SSatish Balay   PetscScalar       sum;
1309ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
13109ea0dfa2SSatish Balay 
13113a40ed3dSBarry Smith   PetscFunctionBegin;
1312f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
13131ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
13142e8a6d31SBarry Smith   if (zz != yy) {
13151ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
13162e8a6d31SBarry Smith   } else {
13172e8a6d31SBarry Smith     z = y;
13182e8a6d31SBarry Smith   }
1319bfeeae90SHong Zhang 
132097952fefSHong Zhang   aj  = a->j;
132197952fefSHong Zhang   aa  = a->a;
1322cddf8d76SBarry Smith   ii  = a->i;
13234eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
13244eb6d288SHong Zhang     if (zz != yy){
13254eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
13264eb6d288SHong Zhang     }
132797952fefSHong Zhang     m    = a->compressedrow.nrows;
132897952fefSHong Zhang     ii   = a->compressedrow.i;
132997952fefSHong Zhang     ridx = a->compressedrow.rindex;
133097952fefSHong Zhang     for (i=0; i<m; i++){
133197952fefSHong Zhang       n  = ii[i+1] - ii[i];
133297952fefSHong Zhang       aj  = a->j + ii[i];
133397952fefSHong Zhang       aa  = a->a + ii[i];
133497952fefSHong Zhang       sum = y[*ridx];
1335f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
133697952fefSHong Zhang       z[*ridx++] = sum;
133797952fefSHong Zhang     }
133897952fefSHong Zhang   } else { /* do not use compressed row format */
1339f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1340f15663dcSBarry Smith   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1341f15663dcSBarry Smith #else
134217ab2063SBarry Smith     for (i=0; i<m; i++) {
1343f15663dcSBarry Smith       n    = ii[i+1] - ii[i];
1344f15663dcSBarry Smith       aj  = a->j + ii[i];
1345f15663dcSBarry Smith       aa  = a->a + ii[i];
134617ab2063SBarry Smith       sum  = y[i];
1347f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
134817ab2063SBarry Smith       z[i] = sum;
134917ab2063SBarry Smith     }
135002ab625aSSatish Balay #endif
1351f15663dcSBarry Smith   }
1352dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1353f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13541ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13552e8a6d31SBarry Smith   if (zz != yy) {
13561ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
13572e8a6d31SBarry Smith   }
13588154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
13596b375ea7SVictor Minden   /*
1360918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1361918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1362918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
13636b375ea7SVictor Minden   */
1364918e98c3SVictor Minden #endif
13653a40ed3dSBarry Smith   PetscFunctionReturn(0);
136617ab2063SBarry Smith }
136717ab2063SBarry Smith 
136817ab2063SBarry Smith /*
136917ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
137017ab2063SBarry Smith */
13714a2ae208SSatish Balay #undef __FUNCT__
13724a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1373dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
137417ab2063SBarry Smith {
1375416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
13766849ba73SBarry Smith   PetscErrorCode ierr;
1377d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
137817ab2063SBarry Smith 
13793a40ed3dSBarry Smith   PetscFunctionBegin;
138009f38230SBarry Smith   if (!a->diag) {
138109f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
13829518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
138309f38230SBarry Smith   }
1384d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
138509f38230SBarry Smith     a->diag[i] = a->i[i+1];
1386bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1387bfeeae90SHong Zhang       if (a->j[j] == i) {
138809f38230SBarry Smith         a->diag[i] = j;
138917ab2063SBarry Smith         break;
139017ab2063SBarry Smith       }
139117ab2063SBarry Smith     }
139217ab2063SBarry Smith   }
13933a40ed3dSBarry Smith   PetscFunctionReturn(0);
139417ab2063SBarry Smith }
139517ab2063SBarry Smith 
1396be5855fcSBarry Smith /*
1397be5855fcSBarry Smith      Checks for missing diagonals
1398be5855fcSBarry Smith */
13994a2ae208SSatish Balay #undef __FUNCT__
14004a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1401ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1402be5855fcSBarry Smith {
1403be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
140497f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1405be5855fcSBarry Smith 
1406be5855fcSBarry Smith   PetscFunctionBegin;
140709f38230SBarry Smith   *missing = PETSC_FALSE;
1408d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
140909f38230SBarry Smith     *missing  = PETSC_TRUE;
141009f38230SBarry Smith     if (d) *d = 0;
1411358d2f5dSShri Abhyankar     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
141209f38230SBarry Smith   } else {
1413f1e2ffcdSBarry Smith     diag = a->diag;
1414d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1415bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
141609f38230SBarry Smith 	*missing = PETSC_TRUE;
141709f38230SBarry Smith 	if (d) *d = i;
141809f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
1419358d2f5dSShri Abhyankar 	break;
142009f38230SBarry Smith       }
1421be5855fcSBarry Smith     }
1422be5855fcSBarry Smith   }
1423be5855fcSBarry Smith   PetscFunctionReturn(0);
1424be5855fcSBarry Smith }
1425be5855fcSBarry Smith 
142671f1c65dSBarry Smith EXTERN_C_BEGIN
142771f1c65dSBarry Smith #undef __FUNCT__
142871f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
14297087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
143071f1c65dSBarry Smith {
143171f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
143271f1c65dSBarry Smith   PetscErrorCode ierr;
1433d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
143454f21887SBarry Smith   MatScalar      *v = a->a;
143554f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
143671f1c65dSBarry Smith 
143771f1c65dSBarry Smith   PetscFunctionBegin;
143871f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
143971f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
144071f1c65dSBarry Smith   diag = a->diag;
144171f1c65dSBarry Smith   if (!a->idiag) {
144271f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
144371f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
144471f1c65dSBarry Smith     v        = a->a;
144571f1c65dSBarry Smith   }
144671f1c65dSBarry Smith   mdiag = a->mdiag;
144771f1c65dSBarry Smith   idiag = a->idiag;
144871f1c65dSBarry Smith 
1449028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
145071f1c65dSBarry Smith     for (i=0; i<m; i++) {
145171f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1452e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
145371f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
145471f1c65dSBarry Smith     }
145571f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
145671f1c65dSBarry Smith   } else {
145771f1c65dSBarry Smith     for (i=0; i<m; i++) {
145871f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
145971f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
146071f1c65dSBarry Smith     }
1461dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
146271f1c65dSBarry Smith   }
146371f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
146471f1c65dSBarry Smith   PetscFunctionReturn(0);
146571f1c65dSBarry Smith }
14665a9745a3SMatthew Knepley EXTERN_C_END
146771f1c65dSBarry Smith 
1468c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
14694a2ae208SSatish Balay #undef __FUNCT__
147041f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
147141f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
147217ab2063SBarry Smith {
1473416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1474e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1475e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
147654f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1477dfbe8321SBarry Smith   PetscErrorCode     ierr;
1478d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
147997f1f81fSBarry Smith   const PetscInt     *idx,*diag;
148017ab2063SBarry Smith 
14813a40ed3dSBarry Smith   PetscFunctionBegin;
1482b965ef7fSBarry Smith   its = its*lits;
148391723122SBarry Smith 
148471f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
148571f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
148671f1c65dSBarry Smith   a->fshift = fshift;
148771f1c65dSBarry Smith   a->omega  = omega;
1488ed480e8bSBarry Smith 
148971f1c65dSBarry Smith   diag = a->diag;
149071f1c65dSBarry Smith   t     = a->ssor_work;
1491ed480e8bSBarry Smith   idiag = a->idiag;
149271f1c65dSBarry Smith   mdiag = a->mdiag;
1493ed480e8bSBarry Smith 
14941ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
14953649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
149671f1c65dSBarry Smith   CHKMEMQ;
1497ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
149817ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
149917ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1500ed480e8bSBarry Smith     bs = b;
150117ab2063SBarry Smith     for (i=0; i<m; i++) {
150271f1c65dSBarry Smith         d    = fshift + mdiag[i];
1503416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1504ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1505ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
150617ab2063SBarry Smith         sum  = b[i]*d/omega;
1507003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
150817ab2063SBarry Smith         x[i] = sum;
150917ab2063SBarry Smith     }
15101ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15113649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1512efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
15133a40ed3dSBarry Smith     PetscFunctionReturn(0);
151417ab2063SBarry Smith   }
1515c783ea89SBarry Smith 
151648af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1517e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
15183a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
151917ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1520887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
152117ab2063SBarry Smith 
152217ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
152317ab2063SBarry Smith 
1524887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
152517ab2063SBarry Smith     */
152617ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
152717ab2063SBarry Smith 
152817ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
152917ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1530416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1531ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1532ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
153317ab2063SBarry Smith       sum  = b[i];
1534e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1535ed480e8bSBarry Smith       x[i] = sum*idiag[i];
153617ab2063SBarry Smith     }
153717ab2063SBarry Smith 
153817ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1539416022c9SBarry Smith     v = a->a;
1540ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
154117ab2063SBarry Smith 
154217ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1543ed480e8bSBarry Smith     ts = t;
1544416022c9SBarry Smith     diag = a->diag;
154517ab2063SBarry Smith     for (i=0; i<m; i++) {
1546416022c9SBarry Smith       n    = diag[i] - a->i[i];
1547ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1548ed480e8bSBarry Smith       v    = a->a + a->i[i];
154917ab2063SBarry Smith       sum  = t[i];
1550003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1551ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1552733d66baSBarry Smith       /*  x = x + t */
1553733d66baSBarry Smith       x[i] += t[i];
155417ab2063SBarry Smith     }
155517ab2063SBarry Smith 
1556dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
15571ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15583649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
15593a40ed3dSBarry Smith     PetscFunctionReturn(0);
156017ab2063SBarry Smith   }
156117ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
156217ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
156317ab2063SBarry Smith       for (i=0; i<m; i++) {
1564416022c9SBarry Smith         n    = diag[i] - a->i[i];
1565ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1566ed480e8bSBarry Smith         v    = a->a + a->i[i];
156717ab2063SBarry Smith         sum  = b[i];
1568e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
15695c99c7daSBarry Smith         t[i] = sum;
1570ed480e8bSBarry Smith         x[i] = sum*idiag[i];
157117ab2063SBarry Smith       }
15725c99c7daSBarry Smith       xb = t;
1573efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
15743a40ed3dSBarry Smith     } else xb = b;
157517ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
157617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1577416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1578ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1579ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
158017ab2063SBarry Smith         sum  = xb[i];
1581e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
15825c99c7daSBarry Smith         if (xb == b) {
1583ed480e8bSBarry Smith           x[i] = sum*idiag[i];
15845c99c7daSBarry Smith         } else {
15855c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
158617ab2063SBarry Smith         }
15875c99c7daSBarry Smith       }
1588efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
158917ab2063SBarry Smith     }
159017ab2063SBarry Smith     its--;
159117ab2063SBarry Smith   }
159217ab2063SBarry Smith   while (its--) {
159317ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
159417ab2063SBarry Smith       for (i=0; i<m; i++) {
1595416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1596ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1597ed480e8bSBarry Smith         v    = a->a + a->i[i];
159817ab2063SBarry Smith         sum  = b[i];
1599e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1600ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
160117ab2063SBarry Smith       }
16029f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
160317ab2063SBarry Smith     }
160417ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
160517ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1606416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1607ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1608ed480e8bSBarry Smith         v    = a->a + a->i[i];
160917ab2063SBarry Smith         sum  = b[i];
1610e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1611ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
161217ab2063SBarry Smith       }
16139f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
161417ab2063SBarry Smith     }
161517ab2063SBarry Smith   }
16161ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16173649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
161871f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
161917ab2063SBarry Smith }
162017ab2063SBarry Smith 
16212af78befSBarry Smith 
16224a2ae208SSatish Balay #undef __FUNCT__
16234a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1624dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
162517ab2063SBarry Smith {
1626416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
16274e220ebcSLois Curfman McInnes 
16283a40ed3dSBarry Smith   PetscFunctionBegin;
16294e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
16304e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
16314e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
16324e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
16334e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
16348e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
16357adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1636d5f3da31SBarry Smith   if (A->factortype) {
16374e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
16384e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
16394e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
16404e220ebcSLois Curfman McInnes   } else {
16414e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
16424e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
16434e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
16444e220ebcSLois Curfman McInnes   }
16453a40ed3dSBarry Smith   PetscFunctionReturn(0);
164617ab2063SBarry Smith }
164717ab2063SBarry Smith 
16484a2ae208SSatish Balay #undef __FUNCT__
16494a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
16502b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
165117ab2063SBarry Smith {
1652416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
16533b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
16546849ba73SBarry Smith   PetscErrorCode    ierr;
165597b48c8fSBarry Smith   const PetscScalar *xx;
165697b48c8fSBarry Smith   PetscScalar       *bb;
1657ace3abfcSBarry Smith   PetscBool         missing;
165817ab2063SBarry Smith 
16593a40ed3dSBarry Smith   PetscFunctionBegin;
166097b48c8fSBarry Smith   if (x && b) {
166197b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
166297b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
166397b48c8fSBarry Smith     for (i=0; i<N; i++) {
166497b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
166597b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
166697b48c8fSBarry Smith     }
166797b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
166897b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
166997b48c8fSBarry Smith   }
167097b48c8fSBarry Smith 
1671a9817697SBarry Smith   if (a->keepnonzeropattern) {
1672f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1673e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1674bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1675f1e2ffcdSBarry Smith     }
1676f4df32b1SMatthew Knepley     if (diag != 0.0) {
167709f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1678e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1679f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1680f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1681f1e2ffcdSBarry Smith       }
1682f1e2ffcdSBarry Smith     }
168388e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1684f1e2ffcdSBarry Smith   } else {
1685f4df32b1SMatthew Knepley     if (diag != 0.0) {
168617ab2063SBarry Smith       for (i=0; i<N; i++) {
1687e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
16887ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1689416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1690f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1691bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
16927ae801bdSBarry Smith         } else { /* in case row was completely empty */
1693f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
169417ab2063SBarry Smith         }
169517ab2063SBarry Smith       }
16963a40ed3dSBarry Smith     } else {
169717ab2063SBarry Smith       for (i=0; i<N; i++) {
1698e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1699416022c9SBarry Smith         a->ilen[rows[i]] = 0;
170017ab2063SBarry Smith       }
170117ab2063SBarry Smith     }
170288e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1703f1e2ffcdSBarry Smith   }
170443a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17053a40ed3dSBarry Smith   PetscFunctionReturn(0);
170617ab2063SBarry Smith }
170717ab2063SBarry Smith 
17084a2ae208SSatish Balay #undef __FUNCT__
17096e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
17106e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
17116e169961SBarry Smith {
17126e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
17136e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
17146e169961SBarry Smith   PetscErrorCode    ierr;
17152b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
17166e169961SBarry Smith   const PetscScalar *xx;
17176e169961SBarry Smith   PetscScalar       *bb;
17186e169961SBarry Smith 
17196e169961SBarry Smith   PetscFunctionBegin;
17206e169961SBarry Smith   if (x && b) {
17216e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
17226e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
17232b40b63fSBarry Smith     vecs = PETSC_TRUE;
17246e169961SBarry Smith   }
17256e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
17266e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
17276e169961SBarry Smith   for (i=0; i<N; i++) {
17286e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
17296e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
17306e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
17316e169961SBarry Smith   }
17326e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
17336e169961SBarry Smith     if (!zeroed[i]) {
17346e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
17356e169961SBarry Smith         if (zeroed[a->j[j]]) {
17362b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
17376e169961SBarry Smith           a->a[j] = 0.0;
17386e169961SBarry Smith         }
17396e169961SBarry Smith       }
17402b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
17416e169961SBarry Smith   }
17426e169961SBarry Smith   if (x && b) {
17436e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
17446e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
17456e169961SBarry Smith   }
17466e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
17476e169961SBarry Smith   if (diag != 0.0) {
17486e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
17496e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
17506e169961SBarry Smith     for (i=0; i<N; i++) {
17516e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
17526e169961SBarry Smith     }
17536e169961SBarry Smith   }
17546e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
17556e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17566e169961SBarry Smith   PetscFunctionReturn(0);
17576e169961SBarry Smith }
17586e169961SBarry Smith 
17596e169961SBarry Smith #undef __FUNCT__
17604a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1761a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
176217ab2063SBarry Smith {
1763416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
176497f1f81fSBarry Smith   PetscInt   *itmp;
176517ab2063SBarry Smith 
17663a40ed3dSBarry Smith   PetscFunctionBegin;
1767e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
176817ab2063SBarry Smith 
1769416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1770bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
177117ab2063SBarry Smith   if (idx) {
1772bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1773bfeeae90SHong Zhang     if (*nz) {
17744e093b46SBarry Smith       *idx = itmp;
177517ab2063SBarry Smith     }
177617ab2063SBarry Smith     else *idx = 0;
177717ab2063SBarry Smith   }
17783a40ed3dSBarry Smith   PetscFunctionReturn(0);
177917ab2063SBarry Smith }
178017ab2063SBarry Smith 
1781bfeeae90SHong Zhang /* remove this function? */
17824a2ae208SSatish Balay #undef __FUNCT__
17834a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1784a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
178517ab2063SBarry Smith {
17863a40ed3dSBarry Smith   PetscFunctionBegin;
17873a40ed3dSBarry Smith   PetscFunctionReturn(0);
178817ab2063SBarry Smith }
178917ab2063SBarry Smith 
17904a2ae208SSatish Balay #undef __FUNCT__
17914a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1792dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
179317ab2063SBarry Smith {
1794416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
179554f21887SBarry Smith   MatScalar      *v = a->a;
179636db0b34SBarry Smith   PetscReal      sum = 0.0;
17976849ba73SBarry Smith   PetscErrorCode ierr;
179897f1f81fSBarry Smith   PetscInt       i,j;
179917ab2063SBarry Smith 
18003a40ed3dSBarry Smith   PetscFunctionBegin;
180117ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1802416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
180336db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
180417ab2063SBarry Smith     }
18058f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
18063a40ed3dSBarry Smith   } else if (type == NORM_1) {
180736db0b34SBarry Smith     PetscReal *tmp;
180897f1f81fSBarry Smith     PetscInt    *jj = a->j;
1809d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1810d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1811064f8208SBarry Smith     *nrm = 0.0;
1812416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1813bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
181417ab2063SBarry Smith     }
1815d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1816064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
181717ab2063SBarry Smith     }
1818606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
18193a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1820064f8208SBarry Smith     *nrm = 0.0;
1821d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1822bfeeae90SHong Zhang       v = a->a + a->i[j];
182317ab2063SBarry Smith       sum = 0.0;
1824416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1825cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
182617ab2063SBarry Smith       }
1827064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
182817ab2063SBarry Smith     }
18293a40ed3dSBarry Smith   } else {
1830e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
183117ab2063SBarry Smith   }
18323a40ed3dSBarry Smith   PetscFunctionReturn(0);
183317ab2063SBarry Smith }
183417ab2063SBarry Smith 
18354e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
18364e938277SHong Zhang #undef __FUNCT__
18374e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
18384e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
18394e938277SHong Zhang {
18404e938277SHong Zhang   PetscErrorCode ierr;
18414e938277SHong Zhang   PetscInt       i,j,anzj;
18424e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ *)A->data,*b;
18434e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
18444e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
18454e938277SHong Zhang 
18464e938277SHong Zhang   PetscFunctionBegin;
18474e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
18484e938277SHong Zhang   ierr = PetscMalloc((an+1)*sizeof(PetscInt),&ati);CHKERRQ(ierr);
18494e938277SHong Zhang   ierr = PetscMalloc(ai[am]*sizeof(PetscInt),&atj);CHKERRQ(ierr);
18504e938277SHong Zhang   ierr = PetscMalloc(an*sizeof(PetscInt),&atfill);CHKERRQ(ierr);
18514e938277SHong Zhang   ierr = PetscMemzero(ati,(an+1)*sizeof(PetscInt));CHKERRQ(ierr);
18524e938277SHong Zhang 
18534e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
18544e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
18554e938277SHong Zhang   for (i=0;i<ai[am];i++) {
18564e938277SHong Zhang     ati[aj[i]+1] += 1;
18574e938277SHong Zhang   }
18584e938277SHong Zhang   /* Form ati for csr format of A^T. */
18594e938277SHong Zhang   for (i=0;i<an;i++) {
18604e938277SHong Zhang     ati[i+1] += ati[i];
18614e938277SHong Zhang   }
18624e938277SHong Zhang 
18634e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
18644e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
18654e938277SHong Zhang 
18664e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
18674e938277SHong Zhang   for (i=0;i<am;i++) {
18684e938277SHong Zhang     anzj = ai[i+1] - ai[i];
18694e938277SHong Zhang     for (j=0;j<anzj;j++) {
18704e938277SHong Zhang       atj[atfill[*aj]] = i;
18714e938277SHong Zhang       atfill[*aj++]   += 1;
18724e938277SHong Zhang     }
18734e938277SHong Zhang   }
18744e938277SHong Zhang 
18754e938277SHong Zhang   /* Clean up temporary space and complete requests. */
18764e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
18774e938277SHong Zhang   ierr = MatCreateSeqAIJWithArrays(((PetscObject)A)->comm,an,am,ati,atj,PETSC_NULL,B);CHKERRQ(ierr);
1878a2f3521dSMark F. Adams   (*B)->rmap->bs = A->cmap->bs;
1879a2f3521dSMark F. Adams   (*B)->cmap->bs = A->rmap->bs;
1880a2f3521dSMark F. Adams 
18814e938277SHong Zhang   b = (Mat_SeqAIJ *)((*B)->data);
18824e938277SHong Zhang   b->free_a   = PETSC_FALSE;
18834e938277SHong Zhang   b->free_ij  = PETSC_TRUE;
18844e938277SHong Zhang   b->nonew    = 0;
18854e938277SHong Zhang   PetscFunctionReturn(0);
18864e938277SHong Zhang }
18874e938277SHong Zhang 
18884a2ae208SSatish Balay #undef __FUNCT__
18894a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1890fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
189117ab2063SBarry Smith {
1892416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1893416022c9SBarry Smith   Mat            C;
18946849ba73SBarry Smith   PetscErrorCode ierr;
1895d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
189654f21887SBarry Smith   MatScalar      *array = a->a;
189717ab2063SBarry Smith 
18983a40ed3dSBarry Smith   PetscFunctionBegin;
1899e32f2f54SBarry 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");
1900fc4dec0aSBarry Smith 
1901fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1902d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1903d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1904bfeeae90SHong Zhang 
1905bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
19067adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1907d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
1908a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(C,A->cmap->bs,A->rmap->bs);CHKERRQ(ierr);
19097adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1910ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1911606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1912a541d17aSBarry Smith   } else {
1913a541d17aSBarry Smith     C = *B;
1914a541d17aSBarry Smith   }
1915a541d17aSBarry Smith 
191617ab2063SBarry Smith   for (i=0; i<m; i++) {
191717ab2063SBarry Smith     len    = ai[i+1]-ai[i];
191887d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1919b9b97703SBarry Smith     array += len;
1920b9b97703SBarry Smith     aj    += len;
192117ab2063SBarry Smith   }
19226d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19236d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
192417ab2063SBarry Smith 
1925815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1926416022c9SBarry Smith     *B = C;
192717ab2063SBarry Smith   } else {
1928eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
192917ab2063SBarry Smith   }
19303a40ed3dSBarry Smith   PetscFunctionReturn(0);
193117ab2063SBarry Smith }
193217ab2063SBarry Smith 
1933cd0d46ebSvictorle EXTERN_C_BEGIN
1934cd0d46ebSvictorle #undef __FUNCT__
19355fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
19367087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1937cd0d46ebSvictorle {
1938cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
193954f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
194054f21887SBarry Smith   MatScalar      *va,*vb;
19416849ba73SBarry Smith   PetscErrorCode ierr;
194297f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1943cd0d46ebSvictorle 
1944cd0d46ebSvictorle   PetscFunctionBegin;
1945cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1946cd0d46ebSvictorle 
1947cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1948cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
19495485867bSBarry Smith   if (ma!=nb || na!=mb){
19505485867bSBarry Smith     *f = PETSC_FALSE;
19515485867bSBarry Smith     PetscFunctionReturn(0);
19525485867bSBarry Smith   }
1953cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1954cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1955cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
195697f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
195797f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1958cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1959cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1960cd0d46ebSvictorle 
1961cd0d46ebSvictorle   *f = PETSC_TRUE;
1962cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1963cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
196497f1f81fSBarry Smith       PetscInt         idc,idr;
19655485867bSBarry Smith       PetscScalar vc,vr;
1966cd0d46ebSvictorle       /* column/row index/value */
19675485867bSBarry Smith       idc = adx[aptr[i]];
19685485867bSBarry Smith       idr = bdx[bptr[idc]];
19695485867bSBarry Smith       vc  = va[aptr[i]];
19705485867bSBarry Smith       vr  = vb[bptr[idc]];
19715485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
19725485867bSBarry Smith         *f = PETSC_FALSE;
19735485867bSBarry Smith         goto done;
1974cd0d46ebSvictorle       } else {
19755485867bSBarry Smith         aptr[i]++;
19765485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1977cd0d46ebSvictorle       }
1978cd0d46ebSvictorle     }
1979cd0d46ebSvictorle   }
1980cd0d46ebSvictorle  done:
1981cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
19823aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
1983cd0d46ebSvictorle   PetscFunctionReturn(0);
1984cd0d46ebSvictorle }
1985cd0d46ebSvictorle EXTERN_C_END
1986cd0d46ebSvictorle 
19871cbb95d3SBarry Smith EXTERN_C_BEGIN
19881cbb95d3SBarry Smith #undef __FUNCT__
19891cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
19907087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
19911cbb95d3SBarry Smith {
19921cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
199354f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
199454f21887SBarry Smith   MatScalar      *va,*vb;
19951cbb95d3SBarry Smith   PetscErrorCode ierr;
19961cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
19971cbb95d3SBarry Smith 
19981cbb95d3SBarry Smith   PetscFunctionBegin;
19991cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
20001cbb95d3SBarry Smith 
20011cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
20021cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
20031cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
20041cbb95d3SBarry Smith     *f = PETSC_FALSE;
20051cbb95d3SBarry Smith     PetscFunctionReturn(0);
20061cbb95d3SBarry Smith   }
20071cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
20081cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
20091cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
20101cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
20111cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
20121cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
20131cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
20141cbb95d3SBarry Smith 
20151cbb95d3SBarry Smith   *f = PETSC_TRUE;
20161cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
20171cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
20181cbb95d3SBarry Smith       PetscInt         idc,idr;
20191cbb95d3SBarry Smith       PetscScalar vc,vr;
20201cbb95d3SBarry Smith       /* column/row index/value */
20211cbb95d3SBarry Smith       idc = adx[aptr[i]];
20221cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
20231cbb95d3SBarry Smith       vc  = va[aptr[i]];
20241cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
20251cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
20261cbb95d3SBarry Smith         *f = PETSC_FALSE;
20271cbb95d3SBarry Smith         goto done;
20281cbb95d3SBarry Smith       } else {
20291cbb95d3SBarry Smith         aptr[i]++;
20301cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
20311cbb95d3SBarry Smith       }
20321cbb95d3SBarry Smith     }
20331cbb95d3SBarry Smith   }
20341cbb95d3SBarry Smith  done:
20351cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
20361cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
20371cbb95d3SBarry Smith   PetscFunctionReturn(0);
20381cbb95d3SBarry Smith }
20391cbb95d3SBarry Smith EXTERN_C_END
20401cbb95d3SBarry Smith 
20419e29f15eSvictorle #undef __FUNCT__
20429e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2043ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20449e29f15eSvictorle {
2045dfbe8321SBarry Smith   PetscErrorCode ierr;
20469e29f15eSvictorle   PetscFunctionBegin;
20475485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20489e29f15eSvictorle   PetscFunctionReturn(0);
20499e29f15eSvictorle }
20509e29f15eSvictorle 
20514a2ae208SSatish Balay #undef __FUNCT__
20521cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2053ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20541cbb95d3SBarry Smith {
20551cbb95d3SBarry Smith   PetscErrorCode ierr;
20561cbb95d3SBarry Smith   PetscFunctionBegin;
20571cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20581cbb95d3SBarry Smith   PetscFunctionReturn(0);
20591cbb95d3SBarry Smith }
20601cbb95d3SBarry Smith 
20611cbb95d3SBarry Smith #undef __FUNCT__
20624a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2063dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
206417ab2063SBarry Smith {
2065416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
206654f21887SBarry Smith   PetscScalar    *l,*r,x;
206754f21887SBarry Smith   MatScalar      *v;
2068dfbe8321SBarry Smith   PetscErrorCode ierr;
2069d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
207017ab2063SBarry Smith 
20713a40ed3dSBarry Smith   PetscFunctionBegin;
207217ab2063SBarry Smith   if (ll) {
20733ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
20743ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2075e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2076e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
20771ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2078416022c9SBarry Smith     v = a->a;
207917ab2063SBarry Smith     for (i=0; i<m; i++) {
208017ab2063SBarry Smith       x = l[i];
2081416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
208217ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
208317ab2063SBarry Smith     }
20841ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2085efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
208617ab2063SBarry Smith   }
208717ab2063SBarry Smith   if (rr) {
2088e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2089e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
20901ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2091416022c9SBarry Smith     v = a->a; jj = a->j;
209217ab2063SBarry Smith     for (i=0; i<nz; i++) {
2093bfeeae90SHong Zhang       (*v++) *= r[*jj++];
209417ab2063SBarry Smith     }
20951ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2096efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
209717ab2063SBarry Smith   }
2098*acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
20993a40ed3dSBarry Smith   PetscFunctionReturn(0);
210017ab2063SBarry Smith }
210117ab2063SBarry Smith 
21024a2ae208SSatish Balay #undef __FUNCT__
21034a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
210497f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
210517ab2063SBarry Smith {
2106db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
21076849ba73SBarry Smith   PetscErrorCode ierr;
2108d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
210997f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
21105d0c19d7SBarry Smith   const PetscInt *irow,*icol;
21115d0c19d7SBarry Smith   PetscInt       nrows,ncols;
211297f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
211354f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2114416022c9SBarry Smith   Mat            C;
2115ace3abfcSBarry Smith   PetscBool      stride,sorted;
211617ab2063SBarry Smith 
21173a40ed3dSBarry Smith   PetscFunctionBegin;
211814ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
2119e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
212014ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
2121e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
212299141d43SSatish Balay 
212317ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2124b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2125b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
212617ab2063SBarry Smith 
2127fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2128251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2129fee21e36SBarry Smith   if (stride && step == 1) {
213002834360SBarry Smith     /* special case of contiguous rows */
21310e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
213202834360SBarry Smith     /* loop over new rows determining lens and starting points */
213302834360SBarry Smith     for (i=0; i<nrows; i++) {
2134bfeeae90SHong Zhang       kstart  = ai[irow[i]];
2135a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
213602834360SBarry Smith       for (k=kstart; k<kend; k++) {
2137bfeeae90SHong Zhang         if (aj[k] >= first) {
213802834360SBarry Smith           starts[i] = k;
213902834360SBarry Smith           break;
214002834360SBarry Smith 	}
214102834360SBarry Smith       }
2142a2744918SBarry Smith       sum = 0;
214302834360SBarry Smith       while (k < kend) {
2144bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2145a2744918SBarry Smith         sum++;
214602834360SBarry Smith       }
2147a2744918SBarry Smith       lens[i] = sum;
214802834360SBarry Smith     }
214902834360SBarry Smith     /* create submatrix */
2150cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
215197f1f81fSBarry Smith       PetscInt n_cols,n_rows;
215208480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2153e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2154d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
215508480c60SBarry Smith       C = *B;
21563a40ed3dSBarry Smith     } else {
21573bef6203SJed Brown       PetscInt rbs,cbs;
21587adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2159f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
21603bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
21613bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
21623bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
21637adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2164ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
216508480c60SBarry Smith     }
2166db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2167db02288aSLois Curfman McInnes 
216802834360SBarry Smith     /* loop over rows inserting into submatrix */
2169db02288aSLois Curfman McInnes     a_new    = c->a;
2170db02288aSLois Curfman McInnes     j_new    = c->j;
2171db02288aSLois Curfman McInnes     i_new    = c->i;
2172bfeeae90SHong Zhang 
217302834360SBarry Smith     for (i=0; i<nrows; i++) {
2174a2744918SBarry Smith       ii    = starts[i];
2175a2744918SBarry Smith       lensi = lens[i];
2176a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2177a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
217802834360SBarry Smith       }
217987828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2180a2744918SBarry Smith       a_new      += lensi;
2181a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
2182a2744918SBarry Smith       c->ilen[i]  = lensi;
218302834360SBarry Smith     }
21840e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
21853a40ed3dSBarry Smith   } else {
218602834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
21870e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
218897f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
21890e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
21904dcab191SBarry Smith     for (i=0; i<ncols; i++) {
21914dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
21924dcab191SBarry 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);
21934dcab191SBarry Smith #endif
21944dcab191SBarry Smith       smap[icol[i]] = i+1;
21954dcab191SBarry Smith     }
21964dcab191SBarry Smith 
219702834360SBarry Smith     /* determine lens of each row */
219802834360SBarry Smith     for (i=0; i<nrows; i++) {
2199bfeeae90SHong Zhang       kstart  = ai[irow[i]];
220002834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
220102834360SBarry Smith       lens[i] = 0;
220202834360SBarry Smith       for (k=kstart; k<kend; k++) {
2203bfeeae90SHong Zhang         if (smap[aj[k]]) {
220402834360SBarry Smith           lens[i]++;
220502834360SBarry Smith         }
220602834360SBarry Smith       }
220702834360SBarry Smith     }
220817ab2063SBarry Smith     /* Create and fill new matrix */
2209a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2210ace3abfcSBarry Smith       PetscBool  equal;
22110f5bd95cSBarry Smith 
221299141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
2213e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2214d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
22150f5bd95cSBarry Smith       if (!equal) {
2216e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
221799141d43SSatish Balay       }
2218d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
221908480c60SBarry Smith       C = *B;
22203a40ed3dSBarry Smith     } else {
22213bef6203SJed Brown       PetscInt rbs,cbs;
22227adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2223f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
22243bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
22253bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
22263bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
22277adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2228ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
222908480c60SBarry Smith     }
223099141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
223117ab2063SBarry Smith     for (i=0; i<nrows; i++) {
223299141d43SSatish Balay       row    = irow[i];
2233bfeeae90SHong Zhang       kstart = ai[row];
223499141d43SSatish Balay       kend   = kstart + a->ilen[row];
2235bfeeae90SHong Zhang       mat_i  = c->i[i];
223699141d43SSatish Balay       mat_j  = c->j + mat_i;
223799141d43SSatish Balay       mat_a  = c->a + mat_i;
223899141d43SSatish Balay       mat_ilen = c->ilen + i;
223917ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2240bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2241ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
224299141d43SSatish Balay           *mat_a++ = a->a[k];
224399141d43SSatish Balay           (*mat_ilen)++;
224499141d43SSatish Balay 
224517ab2063SBarry Smith         }
224617ab2063SBarry Smith       }
224717ab2063SBarry Smith     }
224802834360SBarry Smith     /* Free work space */
224902834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2250606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2251606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
225202834360SBarry Smith   }
22536d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22546d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
225517ab2063SBarry Smith 
225617ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2257416022c9SBarry Smith   *B = C;
22583a40ed3dSBarry Smith   PetscFunctionReturn(0);
225917ab2063SBarry Smith }
226017ab2063SBarry Smith 
22611df811f5SHong Zhang #undef __FUNCT__
226282d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2263fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat* subMat)
226482d44351SHong Zhang {
226582d44351SHong Zhang   PetscErrorCode ierr;
226682d44351SHong Zhang   Mat            B;
226782d44351SHong Zhang 
226882d44351SHong Zhang   PetscFunctionBegin;
226982d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
227082d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
2271a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(B,mat->rmap->bs,mat->cmap->bs);CHKERRQ(ierr);
227282d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
227382d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
227482d44351SHong Zhang   *subMat = B;
227582d44351SHong Zhang   PetscFunctionReturn(0);
227682d44351SHong Zhang }
227782d44351SHong Zhang 
227882d44351SHong Zhang #undef __FUNCT__
22794a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
22800481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2281a871dcd8SBarry Smith {
228263b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2283dfbe8321SBarry Smith   PetscErrorCode ierr;
228463b91edcSBarry Smith   Mat            outA;
2285ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
228663b91edcSBarry Smith 
22873a40ed3dSBarry Smith   PetscFunctionBegin;
2288e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
22891df811f5SHong Zhang 
2290b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2291b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2292a871dcd8SBarry Smith 
229363b91edcSBarry Smith   outA              = inA;
2294d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
2295c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
22966bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
2297c3122656SLisandro Dalcin   a->row = row;
2298c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
22996bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
2300c3122656SLisandro Dalcin   a->col = col;
230163b91edcSBarry Smith 
230236db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
23036bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
23044c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
230552e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2306f0ec6fceSSatish Balay 
230794a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2308d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2309d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
231094a9d846SBarry Smith   }
231163b91edcSBarry Smith 
2312f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2313137fb511SHong Zhang   if (row_identity && col_identity) {
2314ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2315137fb511SHong Zhang   } else {
2316719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2317137fb511SHong Zhang   }
23183a40ed3dSBarry Smith   PetscFunctionReturn(0);
2319a871dcd8SBarry Smith }
2320a871dcd8SBarry Smith 
23214a2ae208SSatish Balay #undef __FUNCT__
23224a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2323f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2324f0b747eeSBarry Smith {
2325f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2326f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2327efee365bSSatish Balay   PetscErrorCode ierr;
23280805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
23293a40ed3dSBarry Smith 
23303a40ed3dSBarry Smith   PetscFunctionBegin;
2331f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
2332efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2333*acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
23343a40ed3dSBarry Smith   PetscFunctionReturn(0);
2335f0b747eeSBarry Smith }
2336f0b747eeSBarry Smith 
23374a2ae208SSatish Balay #undef __FUNCT__
23384a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
233997f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2340cddf8d76SBarry Smith {
2341dfbe8321SBarry Smith   PetscErrorCode ierr;
234297f1f81fSBarry Smith   PetscInt       i;
2343cddf8d76SBarry Smith 
23443a40ed3dSBarry Smith   PetscFunctionBegin;
2345cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2346b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2347cddf8d76SBarry Smith   }
2348cddf8d76SBarry Smith 
2349cddf8d76SBarry Smith   for (i=0; i<n; i++) {
23506a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2351cddf8d76SBarry Smith   }
23523a40ed3dSBarry Smith   PetscFunctionReturn(0);
2353cddf8d76SBarry Smith }
2354cddf8d76SBarry Smith 
23554a2ae208SSatish Balay #undef __FUNCT__
23564a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
235797f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
23584dcbc457SBarry Smith {
2359e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
23606849ba73SBarry Smith   PetscErrorCode ierr;
23615d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
23625d0c19d7SBarry Smith   const PetscInt *idx;
236397f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2364f1af5d2fSBarry Smith   PetscBT        table;
2365bbd702dbSSatish Balay 
23663a40ed3dSBarry Smith   PetscFunctionBegin;
2367d0f46423SBarry Smith   m     = A->rmap->n;
2368e4d965acSSatish Balay   ai    = a->i;
2369bfeeae90SHong Zhang   aj    = a->j;
23708a047759SSatish Balay 
2371e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
237206763907SSatish Balay 
237397f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
237453b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
237506763907SSatish Balay 
2376e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2377b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2378e4d965acSSatish Balay     isz  = 0;
23796831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2380e4d965acSSatish Balay 
2381e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
23824dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2383b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2384e4d965acSSatish Balay 
2385dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2386e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2387f1af5d2fSBarry Smith       if (!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
23884dcbc457SBarry Smith     }
238906763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
23906bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2391e4d965acSSatish Balay 
239204a348a9SBarry Smith     k = 0;
239304a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
239404a348a9SBarry Smith       n = isz;
239506763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2396e4d965acSSatish Balay         row   = nidx[k];
2397e4d965acSSatish Balay         start = ai[row];
2398e4d965acSSatish Balay         end   = ai[row+1];
239904a348a9SBarry Smith         for (l = start; l<end ; l++){
2400efb16452SHong Zhang           val = aj[l] ;
2401f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2402e4d965acSSatish Balay         }
2403e4d965acSSatish Balay       }
2404e4d965acSSatish Balay     }
240570b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2406e4d965acSSatish Balay   }
240794bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2408606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
24093a40ed3dSBarry Smith   PetscFunctionReturn(0);
24104dcbc457SBarry Smith }
241117ab2063SBarry Smith 
24120513a670SBarry Smith /* -------------------------------------------------------------- */
24134a2ae208SSatish Balay #undef __FUNCT__
24144a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2415dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
24160513a670SBarry Smith {
24170513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
24186849ba73SBarry Smith   PetscErrorCode ierr;
24193b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
24205d0c19d7SBarry Smith   const PetscInt *row,*col;
24215d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
242256cd22aeSBarry Smith   IS             icolp,irowp;
24233b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
24243b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
24250513a670SBarry Smith 
24263a40ed3dSBarry Smith   PetscFunctionBegin;
24274c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
242856cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
24294c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
243056cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
24310513a670SBarry Smith 
24320513a670SBarry Smith   /* determine lengths of permuted rows */
243397f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
24340513a670SBarry Smith   for (i=0; i<m; i++) {
24350513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
24360513a670SBarry Smith   }
24377adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2438f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
2439a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
24407adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2441ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2442606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
24430513a670SBarry Smith 
244497f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
24450513a670SBarry Smith   for (i=0; i<m; i++) {
244632ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24470513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2448cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
244932ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24500513a670SBarry Smith   }
2451606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
24523c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
24530513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24540513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
245556cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
245656cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
24576bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
24586bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
24593a40ed3dSBarry Smith   PetscFunctionReturn(0);
24600513a670SBarry Smith }
24610513a670SBarry Smith 
24624a2ae208SSatish Balay #undef __FUNCT__
24634a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2464dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2465cb5b572fSBarry Smith {
2466dfbe8321SBarry Smith   PetscErrorCode ierr;
2467cb5b572fSBarry Smith 
2468cb5b572fSBarry Smith   PetscFunctionBegin;
246933f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
247033f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2471be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2472be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2473be6bf707SBarry Smith 
2474700c5bfcSBarry 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");
2475d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2476cb5b572fSBarry Smith   } else {
2477cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2478cb5b572fSBarry Smith   }
2479cb5b572fSBarry Smith   PetscFunctionReturn(0);
2480cb5b572fSBarry Smith }
2481cb5b572fSBarry Smith 
24824a2ae208SSatish Balay #undef __FUNCT__
24834994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
24844994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2485273d9f13SBarry Smith {
2486dfbe8321SBarry Smith   PetscErrorCode ierr;
2487273d9f13SBarry Smith 
2488273d9f13SBarry Smith   PetscFunctionBegin;
2489ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2490273d9f13SBarry Smith   PetscFunctionReturn(0);
2491273d9f13SBarry Smith }
2492273d9f13SBarry Smith 
24934a2ae208SSatish Balay #undef __FUNCT__
24948c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray_SeqAIJ"
24958c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
24966c0721eeSBarry Smith {
24976c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
24986c0721eeSBarry Smith   PetscFunctionBegin;
24996c0721eeSBarry Smith   *array = a->a;
25006c0721eeSBarry Smith   PetscFunctionReturn(0);
25016c0721eeSBarry Smith }
25026c0721eeSBarry Smith 
25034a2ae208SSatish Balay #undef __FUNCT__
25048c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray_SeqAIJ"
25058c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
25066c0721eeSBarry Smith {
25076c0721eeSBarry Smith   PetscFunctionBegin;
25086c0721eeSBarry Smith   PetscFunctionReturn(0);
25096c0721eeSBarry Smith }
2510273d9f13SBarry Smith 
2511ee4f033dSBarry Smith #undef __FUNCT__
2512ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2513dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2514ee4f033dSBarry Smith {
25156849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
25166849ba73SBarry Smith   PetscErrorCode ierr;
25174e269d77SPeter Brune   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow;
2518efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
251987828ca2SBarry Smith   PetscScalar    *vscale_array;
2520ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2521ee4f033dSBarry Smith   Vec            w1,w2,w3;
2522ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2523ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2524ee4f033dSBarry Smith 
2525ee4f033dSBarry Smith   PetscFunctionBegin;
2526ee4f033dSBarry Smith   if (!coloring->w1) {
2527ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
252852e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2529ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
253052e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2531ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
253252e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2533ee4f033dSBarry Smith   }
2534ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2535ee4f033dSBarry Smith 
2536ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2537acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2538ee4f033dSBarry Smith   if (flg) {
2539ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2540ee4f033dSBarry Smith   } else {
2541ace3abfcSBarry Smith     PetscBool  assembled;
25420b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
25430b9b6f31SBarry Smith     if (assembled) {
2544ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2545ee4f033dSBarry Smith     }
25460b9b6f31SBarry Smith   }
2547ee4f033dSBarry Smith 
2548ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2549ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2550ee4f033dSBarry Smith 
25514e269d77SPeter Brune   if (!coloring->fset) {
255266f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2553ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
255466f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
25554e269d77SPeter Brune   } else {
25564e269d77SPeter Brune     coloring->fset = PETSC_FALSE;
2557ee4f033dSBarry Smith   }
2558ee4f033dSBarry Smith 
2559ee4f033dSBarry Smith   /*
2560ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2561ee4f033dSBarry Smith   */
25621ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
25631ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2564ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2565ee4f033dSBarry Smith     /*
2566ee4f033dSBarry Smith        Loop over each column associated with color adding the
2567ee4f033dSBarry Smith        perturbation to the vector w3.
2568ee4f033dSBarry Smith     */
2569ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2570ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2571ee4f033dSBarry Smith       dx  = xx[col];
2572ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2573ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2574ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2575ee4f033dSBarry Smith       dx                *= epsilon;
2576ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2577ee4f033dSBarry Smith     }
2578ee4f033dSBarry Smith   }
25791ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2580ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2581ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2582ee4f033dSBarry Smith 
2583ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2584ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2585ee4f033dSBarry Smith 
2586ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2587ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2588ee4f033dSBarry Smith 
25891ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2590ee4f033dSBarry Smith   /*
2591ee4f033dSBarry Smith       Loop over each color
2592ee4f033dSBarry Smith   */
2593ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
259449b058dcSBarry Smith     coloring->currentcolor = k;
2595ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
25961ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2597ee4f033dSBarry Smith     /*
2598ee4f033dSBarry Smith        Loop over each column associated with color adding the
2599ee4f033dSBarry Smith        perturbation to the vector w3.
2600ee4f033dSBarry Smith     */
2601ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2602ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2603ee4f033dSBarry Smith       dx  = xx[col];
26045b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2605ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2606ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2607ee4f033dSBarry Smith       dx            *= epsilon;
2608e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2609ee4f033dSBarry Smith       w3_array[col] += dx;
2610ee4f033dSBarry Smith     }
26111ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2612ee4f033dSBarry Smith 
2613ee4f033dSBarry Smith     /*
2614ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2615ee4f033dSBarry Smith     */
2616ee4f033dSBarry Smith 
261766f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2618ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
261966f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2620efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2621ee4f033dSBarry Smith 
2622ee4f033dSBarry Smith     /*
2623ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2624ee4f033dSBarry Smith     */
26251ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2626ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2627ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2628ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2629ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2630ee4f033dSBarry Smith       srow   = row + start;
2631ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2632ee4f033dSBarry Smith     }
26331ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2634ee4f033dSBarry Smith   }
263549b058dcSBarry Smith   coloring->currentcolor = k;
26361ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
26371ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2638ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2639ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2640ee4f033dSBarry Smith   PetscFunctionReturn(0);
2641ee4f033dSBarry Smith }
2642ee4f033dSBarry Smith 
26438229c054SShri Abhyankar /*
26448229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
26458229c054SShri Abhyankar    have different nonzero structure.
26468229c054SShri Abhyankar */
2647ac90fabeSBarry Smith #undef __FUNCT__
26488229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
26498229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2650ec7775f6SShri Abhyankar {
26518229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2652ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2653ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2654ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2655ec7775f6SShri Abhyankar 
2656ec7775f6SShri Abhyankar   PetscFunctionBegin;
2657ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2658ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
26598af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
26608af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
26618af7cee1SJed Brown     nnz[i] = 0;
26628af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
26638af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
26648af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
26658af7cee1SJed Brown       nnz[i]++;
26668af7cee1SJed Brown     }
26678af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2668ec7775f6SShri Abhyankar   }
2669ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2670ec7775f6SShri Abhyankar }
2671ec7775f6SShri Abhyankar 
2672ec7775f6SShri Abhyankar #undef __FUNCT__
2673ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2674f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2675ac90fabeSBarry Smith {
2676dfbe8321SBarry Smith   PetscErrorCode ierr;
267797f1f81fSBarry Smith   PetscInt       i;
2678ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
26790805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2680ac90fabeSBarry Smith 
2681ac90fabeSBarry Smith   PetscFunctionBegin;
2682ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2683f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2684f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2685*acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2686c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2687a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2688a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
26896bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2690a30b2313SHong Zhang     }
2691a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2692d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2693a30b2313SHong Zhang       y->XtoY = X;
2694407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2695c537a176SHong Zhang     }
2696f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2697ba0e910bSBarry Smith     ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %d/%d = %g\n",x->nz,y->nz,(double)(PetscReal)(x->nz)/(y->nz+1));CHKERRQ(ierr);
2698ac90fabeSBarry Smith   } else {
26998229c054SShri Abhyankar     Mat      B;
27008229c054SShri Abhyankar     PetscInt *nnz;
270116b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2702ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2703bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
27044aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2705a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr);
2706176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
27078229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2708ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2709ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2710ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
27118229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2712ac90fabeSBarry Smith   }
2713ac90fabeSBarry Smith   PetscFunctionReturn(0);
2714ac90fabeSBarry Smith }
2715ac90fabeSBarry Smith 
2716521d7252SBarry Smith #undef __FUNCT__
2717354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
27187087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2719354c94deSBarry Smith {
2720354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2721354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2722354c94deSBarry Smith   PetscInt    i,nz;
2723354c94deSBarry Smith   PetscScalar *a;
2724354c94deSBarry Smith 
2725354c94deSBarry Smith   PetscFunctionBegin;
2726354c94deSBarry Smith   nz = aij->nz;
2727354c94deSBarry Smith   a  = aij->a;
2728354c94deSBarry Smith   for (i=0; i<nz; i++) {
2729354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2730354c94deSBarry Smith   }
2731354c94deSBarry Smith #else
2732354c94deSBarry Smith   PetscFunctionBegin;
2733354c94deSBarry Smith #endif
2734354c94deSBarry Smith   PetscFunctionReturn(0);
2735354c94deSBarry Smith }
2736354c94deSBarry Smith 
2737e34fafa9SBarry Smith #undef __FUNCT__
2738985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2739985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2740e34fafa9SBarry Smith {
2741e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2742e34fafa9SBarry Smith   PetscErrorCode ierr;
2743d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2744e34fafa9SBarry Smith   PetscReal      atmp;
2745985db425SBarry Smith   PetscScalar    *x;
2746e34fafa9SBarry Smith   MatScalar      *aa;
2747e34fafa9SBarry Smith 
2748e34fafa9SBarry Smith   PetscFunctionBegin;
2749e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2750e34fafa9SBarry Smith   aa   = a->a;
2751e34fafa9SBarry Smith   ai   = a->i;
2752e34fafa9SBarry Smith   aj   = a->j;
2753e34fafa9SBarry Smith 
2754985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2755e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2756e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2757e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2758e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2759e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
27609189402eSHong Zhang     x[i] = 0.0;
2761e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2762985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2763985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2764985db425SBarry Smith       aa++; aj++;
2765985db425SBarry Smith     }
2766985db425SBarry Smith   }
2767985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2768985db425SBarry Smith   PetscFunctionReturn(0);
2769985db425SBarry Smith }
2770985db425SBarry Smith 
2771985db425SBarry Smith #undef __FUNCT__
2772985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2773985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2774985db425SBarry Smith {
2775985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2776985db425SBarry Smith   PetscErrorCode ierr;
2777d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2778985db425SBarry Smith   PetscScalar    *x;
2779985db425SBarry Smith   MatScalar      *aa;
2780985db425SBarry Smith 
2781985db425SBarry Smith   PetscFunctionBegin;
2782e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2783985db425SBarry Smith   aa   = a->a;
2784985db425SBarry Smith   ai   = a->i;
2785985db425SBarry Smith   aj   = a->j;
2786985db425SBarry Smith 
2787985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2788985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2789985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2790e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2791985db425SBarry Smith   for (i=0; i<m; i++) {
2792985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2793d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2794985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2795985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2796985db425SBarry Smith       x[i] = 0.0;
2797985db425SBarry Smith       if (idx) {
2798985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2799985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2800985db425SBarry Smith           if (aj[j] > j) {
2801985db425SBarry Smith             idx[i] = j;
2802985db425SBarry Smith             break;
2803985db425SBarry Smith           }
2804985db425SBarry Smith         }
2805985db425SBarry Smith       }
2806985db425SBarry Smith     }
2807985db425SBarry Smith     for (j=0; j<ncols; j++){
2808985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2809985db425SBarry Smith       aa++; aj++;
2810985db425SBarry Smith     }
2811985db425SBarry Smith   }
2812985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2813985db425SBarry Smith   PetscFunctionReturn(0);
2814985db425SBarry Smith }
2815985db425SBarry Smith 
2816985db425SBarry Smith #undef __FUNCT__
2817c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2818c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2819c87e5d42SMatthew Knepley {
2820c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2821c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2822c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2823c87e5d42SMatthew Knepley   PetscReal      atmp;
2824c87e5d42SMatthew Knepley   PetscScalar    *x;
2825c87e5d42SMatthew Knepley   MatScalar      *aa;
2826c87e5d42SMatthew Knepley 
2827c87e5d42SMatthew Knepley   PetscFunctionBegin;
2828e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2829c87e5d42SMatthew Knepley   aa   = a->a;
2830c87e5d42SMatthew Knepley   ai   = a->i;
2831c87e5d42SMatthew Knepley   aj   = a->j;
2832c87e5d42SMatthew Knepley 
2833c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2834c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2835c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
28363bb78c5cSMatthew G Knepley   if (n != A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector, %d vs. %d rows", A->rmap->n, n);
2837c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2838c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2839289a08f5SMatthew Knepley     if (ncols) {
2840289a08f5SMatthew Knepley       /* Get first nonzero */
2841289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
2842289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2843289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2844289a08f5SMatthew Knepley       }
284512431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2846289a08f5SMatthew Knepley     } else {
2847289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2848289a08f5SMatthew Knepley     }
2849c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
2850c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2851289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2852c87e5d42SMatthew Knepley       aa++; aj++;
2853c87e5d42SMatthew Knepley     }
2854c87e5d42SMatthew Knepley   }
2855c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2856c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2857c87e5d42SMatthew Knepley }
2858c87e5d42SMatthew Knepley 
2859c87e5d42SMatthew Knepley #undef __FUNCT__
2860985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2861985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2862985db425SBarry Smith {
2863985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2864985db425SBarry Smith   PetscErrorCode ierr;
2865d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2866985db425SBarry Smith   PetscScalar    *x;
2867985db425SBarry Smith   MatScalar      *aa;
2868985db425SBarry Smith 
2869985db425SBarry Smith   PetscFunctionBegin;
2870e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2871985db425SBarry Smith   aa   = a->a;
2872985db425SBarry Smith   ai   = a->i;
2873985db425SBarry Smith   aj   = a->j;
2874985db425SBarry Smith 
2875985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2876985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2877985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2878e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2879985db425SBarry Smith   for (i=0; i<m; i++) {
2880985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2881d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2882985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2883985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2884985db425SBarry Smith       x[i] = 0.0;
2885985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2886985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2887985db425SBarry Smith         for (j=0;j<ncols;j++) {
2888985db425SBarry Smith           if (aj[j] > j) {
2889985db425SBarry Smith             idx[i] = j;
2890985db425SBarry Smith             break;
2891985db425SBarry Smith           }
2892985db425SBarry Smith         }
2893985db425SBarry Smith       }
2894985db425SBarry Smith     }
2895985db425SBarry Smith     for (j=0; j<ncols; j++){
2896985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2897985db425SBarry Smith       aa++; aj++;
2898e34fafa9SBarry Smith     }
2899e34fafa9SBarry Smith   }
2900e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2901e34fafa9SBarry Smith   PetscFunctionReturn(0);
2902e34fafa9SBarry Smith }
2903bbead8a2SBarry Smith 
2904bbead8a2SBarry Smith #include <petscblaslapack.h>
2905bbead8a2SBarry Smith #include <../src/mat/blockinvert.h>
2906bbead8a2SBarry Smith 
2907bbead8a2SBarry Smith #undef __FUNCT__
2908bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
2909713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
2910bbead8a2SBarry Smith {
2911bbead8a2SBarry Smith   Mat_SeqAIJ    *a = (Mat_SeqAIJ*) A->data;
2912bbead8a2SBarry Smith   PetscErrorCode ierr;
291334fc4b71SJed 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;
2914bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
2915bbead8a2SBarry Smith   PetscReal      shift = 0.0;
2916bbead8a2SBarry Smith 
2917bbead8a2SBarry Smith   PetscFunctionBegin;
29184a0d0026SBarry Smith   if (a->ibdiagvalid) {
29194a0d0026SBarry Smith     if (values) *values = a->ibdiag;
29204a0d0026SBarry Smith     PetscFunctionReturn(0);
29214a0d0026SBarry Smith   }
2922bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
2923bbead8a2SBarry Smith   if (!a->ibdiag) {
2924bbead8a2SBarry Smith     ierr = PetscMalloc(bs2*mbs*sizeof(PetscScalar),&a->ibdiag);CHKERRQ(ierr);
2925bbead8a2SBarry Smith     ierr = PetscLogObjectMemory(A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
2926bbead8a2SBarry Smith   }
2927bbead8a2SBarry Smith   diag    = a->ibdiag;
2928bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
2929bbead8a2SBarry Smith   /* factor and invert each block */
2930bbead8a2SBarry Smith   switch (bs){
2931bbead8a2SBarry Smith     case 1:
2932bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2933bbead8a2SBarry Smith         ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
2934bbead8a2SBarry Smith         diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
2935bbead8a2SBarry Smith       }
2936bbead8a2SBarry Smith       break;
2937bbead8a2SBarry Smith     case 2:
2938bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2939bbead8a2SBarry Smith         ij[0] = 2*i; ij[1] = 2*i + 1;
2940bbead8a2SBarry Smith         ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
294196b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
294296b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
2943bbead8a2SBarry Smith 	diag  += 4;
2944bbead8a2SBarry Smith       }
2945bbead8a2SBarry Smith       break;
2946bbead8a2SBarry Smith     case 3:
2947bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2948bbead8a2SBarry Smith         ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
2949bbead8a2SBarry Smith         ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
295096b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
295196b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
2952bbead8a2SBarry Smith 	diag    += 9;
2953bbead8a2SBarry Smith       }
2954bbead8a2SBarry Smith       break;
2955bbead8a2SBarry Smith     case 4:
2956bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2957bbead8a2SBarry Smith         ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
2958bbead8a2SBarry Smith         ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
295996b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
296096b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
2961bbead8a2SBarry Smith 	diag  += 16;
2962bbead8a2SBarry Smith       }
2963bbead8a2SBarry Smith       break;
2964bbead8a2SBarry Smith     case 5:
2965bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2966bbead8a2SBarry 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;
2967bbead8a2SBarry Smith         ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
296896b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
296996b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
2970bbead8a2SBarry Smith 	diag  += 25;
2971bbead8a2SBarry Smith       }
2972bbead8a2SBarry Smith       break;
2973bbead8a2SBarry Smith     case 6:
2974bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2975bbead8a2SBarry 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;
2976bbead8a2SBarry Smith         ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
297796b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
297896b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
2979bbead8a2SBarry Smith 	diag  += 36;
2980bbead8a2SBarry Smith       }
2981bbead8a2SBarry Smith       break;
2982bbead8a2SBarry Smith     case 7:
2983bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2984bbead8a2SBarry 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;
2985bbead8a2SBarry Smith         ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
298696b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
298796b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
2988bbead8a2SBarry Smith 	diag  += 49;
2989bbead8a2SBarry Smith       }
2990bbead8a2SBarry Smith       break;
2991bbead8a2SBarry Smith     default:
2992bbead8a2SBarry Smith       ierr = PetscMalloc3(bs,MatScalar,&v_work,bs,PetscInt,&v_pivots,bs,PetscInt,&IJ);CHKERRQ(ierr);
2993bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2994bbead8a2SBarry Smith         for (j=0; j<bs; j++) {
2995bbead8a2SBarry Smith           IJ[j] = bs*i + j;
2996bbead8a2SBarry Smith         }
2997bbead8a2SBarry Smith         ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
299896b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
299996b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3000bbead8a2SBarry Smith 	diag  += bs2;
3001bbead8a2SBarry Smith       }
3002bbead8a2SBarry Smith       ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3003bbead8a2SBarry Smith   }
3004bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3005bbead8a2SBarry Smith   PetscFunctionReturn(0);
3006bbead8a2SBarry Smith }
3007bbead8a2SBarry Smith 
300873a71a0fSBarry Smith #undef __FUNCT__
300973a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_SeqAIJ"
301073a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
301173a71a0fSBarry Smith {
301273a71a0fSBarry Smith   PetscErrorCode ierr;
301373a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
301473a71a0fSBarry Smith   PetscScalar    a;
301573a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
301673a71a0fSBarry Smith 
301773a71a0fSBarry Smith   PetscFunctionBegin;
301873a71a0fSBarry Smith   if (!x->assembled) {
301973a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
302073a71a0fSBarry Smith     for (i=0; i<m; i++) {
302173a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
302273a71a0fSBarry Smith         ierr  = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
302373a71a0fSBarry Smith         col   = (PetscInt)(n*PetscRealPart(a));
302473a71a0fSBarry Smith         ierr  = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
302573a71a0fSBarry Smith       }
302673a71a0fSBarry Smith     }
302773a71a0fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
302873a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
302973a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
303073a71a0fSBarry Smith   PetscFunctionReturn(0);
303173a71a0fSBarry Smith }
303273a71a0fSBarry Smith 
30337087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
3034682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
30350a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
3036cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
3037cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
3038cb5b572fSBarry Smith        MatMult_SeqAIJ,
303997304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
30407c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
30417c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
3042db4efbfdSBarry Smith        0,
3043db4efbfdSBarry Smith        0,
3044db4efbfdSBarry Smith        0,
3045db4efbfdSBarry Smith /*10*/ 0,
3046cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
3047cb5b572fSBarry Smith        0,
304841f059aeSBarry Smith        MatSOR_SeqAIJ,
304917ab2063SBarry Smith        MatTranspose_SeqAIJ,
305097304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
3051cb5b572fSBarry Smith        MatEqual_SeqAIJ,
3052cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
3053cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
3054cb5b572fSBarry Smith        MatNorm_SeqAIJ,
305597304618SKris Buschelman /*20*/ 0,
3056cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
3057cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
3058cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
3059d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
3060db4efbfdSBarry Smith        0,
3061db4efbfdSBarry Smith        0,
3062db4efbfdSBarry Smith        0,
3063db4efbfdSBarry Smith        0,
30644994cf47SJed Brown /*29*/ MatSetUp_SeqAIJ,
3065db4efbfdSBarry Smith        0,
3066db4efbfdSBarry Smith        0,
30678c778c55SBarry Smith        0,
30688c778c55SBarry Smith        0,
3069d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
3070cb5b572fSBarry Smith        0,
3071cb5b572fSBarry Smith        0,
3072cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
3073cb5b572fSBarry Smith        0,
3074d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
3075cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
3076cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
3077cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
3078cb5b572fSBarry Smith        MatCopy_SeqAIJ,
3079d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
3080cb5b572fSBarry Smith        MatScale_SeqAIJ,
3081cb5b572fSBarry Smith        0,
308279299369SBarry Smith        MatDiagonalSet_SeqAIJ,
30836e169961SBarry Smith        MatZeroRowsColumns_SeqAIJ,
308473a71a0fSBarry Smith /*49*/ MatSetRandom_SeqAIJ,
30853b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
30863b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
30873b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
3088a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
3089d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
3090b9617806SBarry Smith        0,
30910513a670SBarry Smith        0,
3092cda55fadSBarry Smith        MatPermute_SeqAIJ,
3093cda55fadSBarry Smith        0,
3094d519adbfSMatthew Knepley /*59*/ 0,
3095b9b97703SBarry Smith        MatDestroy_SeqAIJ,
3096b9b97703SBarry Smith        MatView_SeqAIJ,
3097357abbc8SBarry Smith        0,
3098321b30b9SSatish Balay        MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3099321b30b9SSatish Balay /*64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3100321b30b9SSatish Balay        MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3101ee4f033dSBarry Smith        0,
3102ee4f033dSBarry Smith        0,
3103ee4f033dSBarry Smith        0,
3104d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
3105c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
3106ee4f033dSBarry Smith        0,
3107ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
3108dcf5cc72SBarry Smith        0,
3109d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
31103acb8795SBarry Smith        MatFDColoringApply_AIJ,
311197304618SKris Buschelman        0,
311297304618SKris Buschelman        0,
311397304618SKris Buschelman        0,
31146ce1633cSBarry Smith /*79*/ MatFindZeroDiagonals_SeqAIJ,
311597304618SKris Buschelman        0,
311697304618SKris Buschelman        0,
311797304618SKris Buschelman        0,
3118bc011b1eSHong Zhang        MatLoad_SeqAIJ,
3119d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
31201cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
31216284ec50SHong Zhang        0,
31226284ec50SHong Zhang        0,
3123bc011b1eSHong Zhang        0,
3124d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
312526be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
312626be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
312765e8a0caSHong Zhang        MatPtAP_SeqAIJ_SeqAIJ,
312865e8a0caSHong Zhang        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
312965e8a0caSHong Zhang /*94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
31306fc122caSHong Zhang        MatMatTransposeMult_SeqAIJ_SeqAIJ,
31316fc122caSHong Zhang        MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
31326fc122caSHong Zhang        MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
31332121bac1SHong Zhang        0,
31342121bac1SHong Zhang /*99*/ 0,
3135609c6c4dSKris Buschelman        0,
3136609c6c4dSKris Buschelman        0,
313787d4246cSBarry Smith        MatConjugate_SeqAIJ,
313887d4246cSBarry Smith        0,
3139d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
314099cafbc1SBarry Smith        MatRealPart_SeqAIJ,
3141f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
3142f5edf698SHong Zhang        0,
31432bebee5dSHong Zhang        0,
3144cbd44569SHong Zhang /*109*/MatMatSolve_SeqAIJ,
3145985db425SBarry Smith        0,
31462af78befSBarry Smith        MatGetRowMin_SeqAIJ,
31472af78befSBarry Smith        0,
3148599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
3149d519adbfSMatthew Knepley /*114*/0,
3150599ef60dSHong Zhang        0,
31513c2a7987SHong Zhang        0,
3152fe97e370SBarry Smith        0,
3153fbdbba38SShri Abhyankar        0,
3154fbdbba38SShri Abhyankar /*119*/0,
3155fbdbba38SShri Abhyankar        0,
3156fbdbba38SShri Abhyankar        0,
315782d44351SHong Zhang        0,
3158b3a44c85SBarry Smith        MatGetMultiProcBlock_SeqAIJ,
31590716a85fSBarry Smith /*124*/MatFindNonzeroRows_SeqAIJ,
3160bbead8a2SBarry Smith        MatGetColumnNorms_SeqAIJ,
316137868618SMatthew G Knepley        MatInvertBlockDiagonal_SeqAIJ,
316237868618SMatthew G Knepley        0,
316337868618SMatthew G Knepley        0,
31645df89d91SHong Zhang /*129*/0,
316575648e8dSHong Zhang        MatTransposeMatMult_SeqAIJ_SeqAIJ,
316675648e8dSHong Zhang        MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
316775648e8dSHong Zhang        MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3168b9af6bddSHong Zhang        MatTransposeColoringCreate_SeqAIJ,
3169b9af6bddSHong Zhang /*134*/MatTransColoringApplySpToDen_SeqAIJ,
31702b8ad9a3SHong Zhang        MatTransColoringApplyDenToSp_SeqAIJ,
31712b8ad9a3SHong Zhang        MatRARt_SeqAIJ_SeqAIJ,
31722b8ad9a3SHong Zhang        MatRARtSymbolic_SeqAIJ_SeqAIJ,
31732b8ad9a3SHong Zhang        MatRARtNumeric_SeqAIJ_SeqAIJ
31749e29f15eSvictorle };
317517ab2063SBarry Smith 
3176fb2e594dSBarry Smith EXTERN_C_BEGIN
31774a2ae208SSatish Balay #undef __FUNCT__
31784a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
31797087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3180bef8e0ddSBarry Smith {
3181bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
318297f1f81fSBarry Smith   PetscInt   i,nz,n;
3183bef8e0ddSBarry Smith 
3184bef8e0ddSBarry Smith   PetscFunctionBegin;
3185bef8e0ddSBarry Smith 
3186bef8e0ddSBarry Smith   nz = aij->maxnz;
3187d0f46423SBarry Smith   n  = mat->rmap->n;
3188bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3189bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3190bef8e0ddSBarry Smith   }
3191bef8e0ddSBarry Smith   aij->nz = nz;
3192bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3193bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3194bef8e0ddSBarry Smith   }
3195bef8e0ddSBarry Smith 
3196bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3197bef8e0ddSBarry Smith }
3198fb2e594dSBarry Smith EXTERN_C_END
3199bef8e0ddSBarry Smith 
32004a2ae208SSatish Balay #undef __FUNCT__
32014a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3202bef8e0ddSBarry Smith /*@
3203bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3204bef8e0ddSBarry Smith        in the matrix.
3205bef8e0ddSBarry Smith 
3206bef8e0ddSBarry Smith   Input Parameters:
3207bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3208bef8e0ddSBarry Smith -  indices - the column indices
3209bef8e0ddSBarry Smith 
321015091d37SBarry Smith   Level: advanced
321115091d37SBarry Smith 
3212bef8e0ddSBarry Smith   Notes:
3213bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3214bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3215bef8e0ddSBarry Smith   of the MatSetValues() operation.
3216bef8e0ddSBarry Smith 
3217bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3218d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3219bef8e0ddSBarry Smith 
3220bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3221bef8e0ddSBarry Smith 
3222b9617806SBarry Smith     The indices should start with zero, not one.
3223b9617806SBarry Smith 
3224bef8e0ddSBarry Smith @*/
32257087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3226bef8e0ddSBarry Smith {
32274ac538c5SBarry Smith   PetscErrorCode ierr;
3228bef8e0ddSBarry Smith 
3229bef8e0ddSBarry Smith   PetscFunctionBegin;
32300700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
32314482741eSBarry Smith   PetscValidPointer(indices,2);
32324ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
3233bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3234bef8e0ddSBarry Smith }
3235bef8e0ddSBarry Smith 
3236be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3237be6bf707SBarry Smith 
3238fb2e594dSBarry Smith EXTERN_C_BEGIN
32394a2ae208SSatish Balay #undef __FUNCT__
32404a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
32417087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3242be6bf707SBarry Smith {
3243be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
32446849ba73SBarry Smith   PetscErrorCode ierr;
3245d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3246be6bf707SBarry Smith 
3247be6bf707SBarry Smith   PetscFunctionBegin;
3248be6bf707SBarry Smith   if (aij->nonew != 1) {
3249e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3250be6bf707SBarry Smith   }
3251be6bf707SBarry Smith 
3252be6bf707SBarry Smith   /* allocate space for values if not already there */
3253be6bf707SBarry Smith   if (!aij->saved_values) {
325487828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
32559518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3256be6bf707SBarry Smith   }
3257be6bf707SBarry Smith 
3258be6bf707SBarry Smith   /* copy values over */
325987828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3260be6bf707SBarry Smith   PetscFunctionReturn(0);
3261be6bf707SBarry Smith }
3262fb2e594dSBarry Smith EXTERN_C_END
3263be6bf707SBarry Smith 
32644a2ae208SSatish Balay #undef __FUNCT__
3265b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3266be6bf707SBarry Smith /*@
3267be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3268be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3269be6bf707SBarry Smith        nonlinear portion.
3270be6bf707SBarry Smith 
3271be6bf707SBarry Smith    Collect on Mat
3272be6bf707SBarry Smith 
3273be6bf707SBarry Smith   Input Parameters:
32740e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3275be6bf707SBarry Smith 
327615091d37SBarry Smith   Level: advanced
327715091d37SBarry Smith 
3278be6bf707SBarry Smith   Common Usage, with SNESSolve():
3279be6bf707SBarry Smith $    Create Jacobian matrix
3280be6bf707SBarry Smith $    Set linear terms into matrix
3281be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3282be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3283be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3284512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3285be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3286be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3287be6bf707SBarry Smith $    In your Jacobian routine
3288be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3289be6bf707SBarry Smith $      Set nonlinear terms in matrix
3290be6bf707SBarry Smith 
3291be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3292be6bf707SBarry Smith $    // build linear portion of Jacobian
3293512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3294be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3295be6bf707SBarry Smith $    loop over nonlinear iterations
3296be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3297be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3298be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3299be6bf707SBarry Smith $       Solve linear system with Jacobian
3300be6bf707SBarry Smith $    endloop
3301be6bf707SBarry Smith 
3302be6bf707SBarry Smith   Notes:
3303be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3304512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3305be6bf707SBarry Smith     calling this routine.
3306be6bf707SBarry Smith 
33070c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
33080c468ba9SBarry Smith     and does not allocated additional space.
33090c468ba9SBarry Smith 
3310be6bf707SBarry Smith .seealso: MatRetrieveValues()
3311be6bf707SBarry Smith 
3312be6bf707SBarry Smith @*/
33137087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3314be6bf707SBarry Smith {
33154ac538c5SBarry Smith   PetscErrorCode ierr;
3316be6bf707SBarry Smith 
3317be6bf707SBarry Smith   PetscFunctionBegin;
33180700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3319e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3320e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
33214ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3322be6bf707SBarry Smith   PetscFunctionReturn(0);
3323be6bf707SBarry Smith }
3324be6bf707SBarry Smith 
3325fb2e594dSBarry Smith EXTERN_C_BEGIN
33264a2ae208SSatish Balay #undef __FUNCT__
33274a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
33287087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3329be6bf707SBarry Smith {
3330be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
33316849ba73SBarry Smith   PetscErrorCode ierr;
3332d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3333be6bf707SBarry Smith 
3334be6bf707SBarry Smith   PetscFunctionBegin;
3335be6bf707SBarry Smith   if (aij->nonew != 1) {
3336e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3337be6bf707SBarry Smith   }
3338be6bf707SBarry Smith   if (!aij->saved_values) {
3339e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3340be6bf707SBarry Smith   }
3341be6bf707SBarry Smith   /* copy values over */
334287828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3343be6bf707SBarry Smith   PetscFunctionReturn(0);
3344be6bf707SBarry Smith }
3345fb2e594dSBarry Smith EXTERN_C_END
3346be6bf707SBarry Smith 
33474a2ae208SSatish Balay #undef __FUNCT__
33484a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3349be6bf707SBarry Smith /*@
3350be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3351be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3352be6bf707SBarry Smith        nonlinear portion.
3353be6bf707SBarry Smith 
3354be6bf707SBarry Smith    Collect on Mat
3355be6bf707SBarry Smith 
3356be6bf707SBarry Smith   Input Parameters:
3357be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3358be6bf707SBarry Smith 
335915091d37SBarry Smith   Level: advanced
336015091d37SBarry Smith 
3361be6bf707SBarry Smith .seealso: MatStoreValues()
3362be6bf707SBarry Smith 
3363be6bf707SBarry Smith @*/
33647087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3365be6bf707SBarry Smith {
33664ac538c5SBarry Smith   PetscErrorCode ierr;
3367be6bf707SBarry Smith 
3368be6bf707SBarry Smith   PetscFunctionBegin;
33690700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3370e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3371e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
33724ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3373be6bf707SBarry Smith   PetscFunctionReturn(0);
3374be6bf707SBarry Smith }
3375be6bf707SBarry Smith 
3376f83d6046SBarry Smith 
3377be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
33784a2ae208SSatish Balay #undef __FUNCT__
33794a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
338017ab2063SBarry Smith /*@C
3381682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
33820d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
33836e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
338451c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
33852bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
338617ab2063SBarry Smith 
3387db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3388db81eaa0SLois Curfman McInnes 
338917ab2063SBarry Smith    Input Parameters:
3390db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
339117ab2063SBarry Smith .  m - number of rows
339217ab2063SBarry Smith .  n - number of columns
339317ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
339451c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
33952bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
339617ab2063SBarry Smith 
339717ab2063SBarry Smith    Output Parameter:
3398416022c9SBarry Smith .  A - the matrix
339917ab2063SBarry Smith 
3400175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3401ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3402175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3403175b88e8SBarry Smith 
3404b259b22eSLois Curfman McInnes    Notes:
340549a6f317SBarry Smith    If nnz is given then nz is ignored
340649a6f317SBarry Smith 
340717ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
340817ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
34090002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
341044cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
341117ab2063SBarry Smith 
341217ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3413a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
34143d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
34156da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
341617ab2063SBarry Smith 
3417682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
34184fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3419682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
34206c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
34216c7ebb05SLois Curfman McInnes 
34226c7ebb05SLois Curfman McInnes    Options Database Keys:
3423698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
34249db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
342517ab2063SBarry Smith 
3426027ccd11SLois Curfman McInnes    Level: intermediate
3427027ccd11SLois Curfman McInnes 
342869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
342936db0b34SBarry Smith 
343017ab2063SBarry Smith @*/
34317087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
343217ab2063SBarry Smith {
3433dfbe8321SBarry Smith   PetscErrorCode ierr;
34346945ee14SBarry Smith 
34353a40ed3dSBarry Smith   PetscFunctionBegin;
3436f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3437117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3438c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3439d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3440273d9f13SBarry Smith   PetscFunctionReturn(0);
3441273d9f13SBarry Smith }
3442273d9f13SBarry Smith 
34434a2ae208SSatish Balay #undef __FUNCT__
34444a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3445273d9f13SBarry Smith /*@C
3446273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3447273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3448273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3449273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3450273d9f13SBarry Smith 
3451273d9f13SBarry Smith    Collective on MPI_Comm
3452273d9f13SBarry Smith 
3453273d9f13SBarry Smith    Input Parameters:
3454117016b1SBarry Smith +  B - The matrix-free
3455273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3456273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
3457273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
3458273d9f13SBarry Smith 
3459273d9f13SBarry Smith    Notes:
346049a6f317SBarry Smith      If nnz is given then nz is ignored
346149a6f317SBarry Smith 
3462273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3463273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3464273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3465273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3466273d9f13SBarry Smith 
3467273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3468273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3469273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3470273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3471273d9f13SBarry Smith 
3472aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3473aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3474aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3475aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3476aa95bbe8SBarry Smith 
3477a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3478a96a251dSBarry Smith    entries or columns indices
3479a96a251dSBarry Smith 
3480273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3481273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3482273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3483273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3484273d9f13SBarry Smith 
3485273d9f13SBarry Smith    Options Database Keys:
3486698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3487698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3488273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3489273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3490273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3491273d9f13SBarry Smith 
3492273d9f13SBarry Smith    Level: intermediate
3493273d9f13SBarry Smith 
349469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3495273d9f13SBarry Smith 
3496273d9f13SBarry Smith @*/
34977087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3498273d9f13SBarry Smith {
34994ac538c5SBarry Smith   PetscErrorCode ierr;
3500a23d5eceSKris Buschelman 
3501a23d5eceSKris Buschelman   PetscFunctionBegin;
35026ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
35036ba663aaSJed Brown   PetscValidType(B,1);
35044ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3505a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3506a23d5eceSKris Buschelman }
3507a23d5eceSKris Buschelman 
3508a23d5eceSKris Buschelman EXTERN_C_BEGIN
3509a23d5eceSKris Buschelman #undef __FUNCT__
3510a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
35117087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3512a23d5eceSKris Buschelman {
3513273d9f13SBarry Smith   Mat_SeqAIJ     *b;
35142576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
35156849ba73SBarry Smith   PetscErrorCode ierr;
351697f1f81fSBarry Smith   PetscInt       i;
3517273d9f13SBarry Smith 
3518273d9f13SBarry Smith   PetscFunctionBegin;
35192576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3520a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3521c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3522c461c341SBarry Smith     nz             = 0;
3523c461c341SBarry Smith   }
3524c461c341SBarry Smith 
352526283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
352626283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3527899cda47SBarry Smith 
3528435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3529e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3530b73539f3SBarry Smith   if (nnz) {
3531d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3532e32f2f54SBarry 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]);
3533e32f2f54SBarry 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);
3534b73539f3SBarry Smith     }
3535b73539f3SBarry Smith   }
3536b73539f3SBarry Smith 
3537273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3538273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3539273d9f13SBarry Smith 
3540ab93d7beSBarry Smith   if (!skipallocation) {
35412ee49352SLisandro Dalcin     if (!b->imax) {
3542d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3543d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
35442ee49352SLisandro Dalcin     }
3545273d9f13SBarry Smith     if (!nnz) {
3546435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3547c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3548d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3549d0f46423SBarry Smith       nz = nz*B->rmap->n;
3550273d9f13SBarry Smith     } else {
3551273d9f13SBarry Smith       nz = 0;
3552d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3553273d9f13SBarry Smith     }
3554ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3555d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3556ab93d7beSBarry Smith 
3557273d9f13SBarry Smith     /* allocate the matrix space */
35582ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3559d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3560d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3561bfeeae90SHong Zhang     b->i[0] = 0;
3562d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
35635da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
35645da197adSKris Buschelman     }
3565273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3566e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3567e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3568b31eba2aSShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
3569b31eba2aSShri Abhyankar   ierr = MatZeroEntries_SeqAIJ(B);CHKERRQ(ierr);
3570b31eba2aSShri Abhyankar #endif
3571c461c341SBarry Smith   } else {
3572e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3573e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3574c461c341SBarry Smith   }
3575273d9f13SBarry Smith 
3576273d9f13SBarry Smith   b->nz                = 0;
3577273d9f13SBarry Smith   b->maxnz             = nz;
3578273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
35792576faa2SJed Brown   if (realalloc) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);}
3580273d9f13SBarry Smith   PetscFunctionReturn(0);
3581273d9f13SBarry Smith }
3582a23d5eceSKris Buschelman EXTERN_C_END
3583273d9f13SBarry Smith 
3584a1661176SMatthew Knepley #undef  __FUNCT__
3585a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
358658d36128SBarry Smith /*@
3587a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3588a1661176SMatthew Knepley 
3589a1661176SMatthew Knepley    Input Parameters:
3590a1661176SMatthew Knepley +  B - the matrix
3591a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3592a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3593a1661176SMatthew Knepley -  v - optional values in the matrix
3594a1661176SMatthew Knepley 
3595a1661176SMatthew Knepley    Level: developer
3596a1661176SMatthew Knepley 
359758d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
359858d36128SBarry Smith 
3599a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3600a1661176SMatthew Knepley 
3601a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3602a1661176SMatthew Knepley @*/
3603a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3604a1661176SMatthew Knepley {
3605a1661176SMatthew Knepley   PetscErrorCode ierr;
3606a1661176SMatthew Knepley 
3607a1661176SMatthew Knepley   PetscFunctionBegin;
36080700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
36096ba663aaSJed Brown   PetscValidType(B,1);
36104ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3611a1661176SMatthew Knepley   PetscFunctionReturn(0);
3612a1661176SMatthew Knepley }
3613a1661176SMatthew Knepley 
3614a1661176SMatthew Knepley EXTERN_C_BEGIN
3615a1661176SMatthew Knepley #undef  __FUNCT__
3616a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
36177087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3618a1661176SMatthew Knepley {
3619a1661176SMatthew Knepley   PetscInt       i;
3620a1661176SMatthew Knepley   PetscInt       m,n;
3621a1661176SMatthew Knepley   PetscInt       nz;
3622a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3623a1661176SMatthew Knepley   PetscScalar    *values;
3624a1661176SMatthew Knepley   PetscErrorCode ierr;
3625a1661176SMatthew Knepley 
3626a1661176SMatthew Knepley   PetscFunctionBegin;
362765e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3628779a8d59SSatish Balay 
3629779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3630779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3631779a8d59SSatish Balay 
3632779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3633a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3634a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3635b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3636a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
363765e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3638a1661176SMatthew Knepley     nnz[i] = nz;
3639a1661176SMatthew Knepley   }
3640a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3641a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3642a1661176SMatthew Knepley 
3643a1661176SMatthew Knepley   if (v) {
3644a1661176SMatthew Knepley     values = (PetscScalar*) v;
3645a1661176SMatthew Knepley   } else {
36460e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3647a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3648a1661176SMatthew Knepley   }
3649a1661176SMatthew Knepley 
3650a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3651b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3652b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3653a1661176SMatthew Knepley   }
3654a1661176SMatthew Knepley 
3655a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3656a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3657a1661176SMatthew Knepley 
3658a1661176SMatthew Knepley   if (!v) {
3659a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3660a1661176SMatthew Knepley   }
36617827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3662a1661176SMatthew Knepley   PetscFunctionReturn(0);
3663a1661176SMatthew Knepley }
3664a1661176SMatthew Knepley EXTERN_C_END
3665a1661176SMatthew Knepley 
3666c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3667b45d2f2cSJed Brown #include <petsc-private/petscaxpy.h>
3668170fe5c8SBarry Smith 
3669170fe5c8SBarry Smith #undef __FUNCT__
3670170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3671170fe5c8SBarry Smith /*
3672170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3673170fe5c8SBarry Smith 
3674170fe5c8SBarry Smith                n                       p                          p
3675170fe5c8SBarry Smith         (              )       (              )         (                  )
3676170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3677170fe5c8SBarry Smith         (              )       (              )         (                  )
3678170fe5c8SBarry Smith 
3679170fe5c8SBarry Smith */
3680170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3681170fe5c8SBarry Smith {
3682170fe5c8SBarry Smith   PetscErrorCode     ierr;
3683170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3684170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3685170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
36861de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3687170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3688170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3689170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3690170fe5c8SBarry Smith 
3691170fe5c8SBarry Smith   PetscFunctionBegin;
3692d0f46423SBarry Smith   m = A->rmap->n;
3693d0f46423SBarry Smith   n = A->cmap->n;
3694d0f46423SBarry Smith   p = B->cmap->n;
3695170fe5c8SBarry Smith   a = sub_a->v;
3696170fe5c8SBarry Smith   b = sub_b->a;
3697170fe5c8SBarry Smith   c = sub_c->v;
3698170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3699170fe5c8SBarry Smith 
3700170fe5c8SBarry Smith   ii  = sub_b->i;
3701170fe5c8SBarry Smith   idx = sub_b->j;
3702170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3703170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3704170fe5c8SBarry Smith     while (q-->0) {
3705170fe5c8SBarry Smith       c_q = c + m*(*idx);
3706170fe5c8SBarry Smith       a_q = a + m*i;
3707be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3708170fe5c8SBarry Smith       idx++;
3709170fe5c8SBarry Smith       b++;
3710170fe5c8SBarry Smith     }
3711170fe5c8SBarry Smith   }
3712170fe5c8SBarry Smith   PetscFunctionReturn(0);
3713170fe5c8SBarry Smith }
3714170fe5c8SBarry Smith 
3715170fe5c8SBarry Smith #undef __FUNCT__
3716170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3717170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3718170fe5c8SBarry Smith {
3719170fe5c8SBarry Smith   PetscErrorCode ierr;
3720d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3721170fe5c8SBarry Smith   Mat            Cmat;
3722170fe5c8SBarry Smith 
3723170fe5c8SBarry Smith   PetscFunctionBegin;
3724e32f2f54SBarry 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);
372539804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3726170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3727a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
3728170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3729170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3730d73949e8SHong Zhang 
3731d73949e8SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
3732170fe5c8SBarry Smith   *C = Cmat;
3733170fe5c8SBarry Smith   PetscFunctionReturn(0);
3734170fe5c8SBarry Smith }
3735170fe5c8SBarry Smith 
3736170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3737170fe5c8SBarry Smith #undef __FUNCT__
3738170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3739170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3740170fe5c8SBarry Smith {
3741170fe5c8SBarry Smith   PetscErrorCode ierr;
3742170fe5c8SBarry Smith 
3743170fe5c8SBarry Smith   PetscFunctionBegin;
3744170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3745170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3746170fe5c8SBarry Smith   }
3747170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3748170fe5c8SBarry Smith   PetscFunctionReturn(0);
3749170fe5c8SBarry Smith }
3750170fe5c8SBarry Smith 
3751170fe5c8SBarry Smith 
37520bad9183SKris Buschelman /*MC
3753fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
37540bad9183SKris Buschelman    based on compressed sparse row format.
37550bad9183SKris Buschelman 
37560bad9183SKris Buschelman    Options Database Keys:
37570bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
37580bad9183SKris Buschelman 
37590bad9183SKris Buschelman   Level: beginner
37600bad9183SKris Buschelman 
3761f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
37620bad9183SKris Buschelman M*/
37630bad9183SKris Buschelman 
3764ccd284c7SBarry Smith /*MC
3765ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
3766ccd284c7SBarry Smith 
3767ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
3768ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
3769ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
3770ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3771ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3772ccd284c7SBarry Smith 
3773ccd284c7SBarry Smith    Options Database Keys:
3774ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
3775ccd284c7SBarry Smith 
3776ccd284c7SBarry Smith   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
3777ccd284c7SBarry Smith    enough exist.
3778ccd284c7SBarry Smith 
3779ccd284c7SBarry Smith   Level: beginner
3780ccd284c7SBarry Smith 
3781ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3782ccd284c7SBarry Smith M*/
3783ccd284c7SBarry Smith 
3784ccd284c7SBarry Smith /*MC
3785ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
3786ccd284c7SBarry Smith 
3787ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3788ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3789ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3790ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3791ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3792ccd284c7SBarry Smith 
3793ccd284c7SBarry Smith    Options Database Keys:
3794ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3795ccd284c7SBarry Smith 
3796ccd284c7SBarry Smith   Level: beginner
3797ccd284c7SBarry Smith 
3798ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3799ccd284c7SBarry Smith M*/
3800ccd284c7SBarry Smith 
3801a6175056SHong Zhang EXTERN_C_BEGIN
3802b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3803b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3804b5e56a35SBarry Smith #endif
3805ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3806af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3807af1023dbSSatish Balay #endif
38087087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
38097087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
38107087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
38117087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3812611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
38137087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3814611f576cSBarry Smith #endif
3815611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
38167087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3817611f576cSBarry Smith #endif
3818f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3819f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3820f3c0ef26SHong Zhang #endif
3821eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
38227087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3823eb3b5408SSatish Balay #endif
3824586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
38257087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3826586621ddSJed Brown #endif
3827719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
38287087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3829719d5645SBarry Smith #endif
3830b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
38317087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
38327087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
38337087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3834b3866ffcSBarry Smith #endif
383517f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
383617f1a0eaSHong Zhang extern PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
383717f1a0eaSHong Zhang #endif
383817667f90SBarry Smith EXTERN_C_END
383917667f90SBarry Smith 
3840c0c8ee5eSDmitry Karpeev 
38418c778c55SBarry Smith #undef __FUNCT__
38428c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray"
38438c778c55SBarry Smith /*@C
38448c778c55SBarry Smith    MatSeqAIJGetArray - gives access to the array where the data for a SeqSeqAIJ matrix is stored
38458c778c55SBarry Smith 
38468c778c55SBarry Smith    Not Collective
38478c778c55SBarry Smith 
38488c778c55SBarry Smith    Input Parameter:
38498c778c55SBarry Smith .  mat - a MATSEQDENSE matrix
38508c778c55SBarry Smith 
38518c778c55SBarry Smith    Output Parameter:
38528c778c55SBarry Smith .   array - pointer to the data
38538c778c55SBarry Smith 
38548c778c55SBarry Smith    Level: intermediate
38558c778c55SBarry Smith 
38568c778c55SBarry Smith .seealso: MatSeqAIJRestoreArray()
38578c778c55SBarry Smith @*/
38588c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
38598c778c55SBarry Smith {
38608c778c55SBarry Smith   PetscErrorCode ierr;
38618c778c55SBarry Smith 
38628c778c55SBarry Smith   PetscFunctionBegin;
38638c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
38648c778c55SBarry Smith   PetscFunctionReturn(0);
38658c778c55SBarry Smith }
38668c778c55SBarry Smith 
38678c778c55SBarry Smith #undef __FUNCT__
38688c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray"
38698c778c55SBarry Smith /*@C
38708c778c55SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a SeqSeqAIJ matrix is stored obtained by MatSeqAIJGetArray()
38718c778c55SBarry Smith 
38728c778c55SBarry Smith    Not Collective
38738c778c55SBarry Smith 
38748c778c55SBarry Smith    Input Parameters:
38758c778c55SBarry Smith .  mat - a MATSEQDENSE matrix
38768c778c55SBarry Smith .  array - pointer to the data
38778c778c55SBarry Smith 
38788c778c55SBarry Smith    Level: intermediate
38798c778c55SBarry Smith 
38808c778c55SBarry Smith .seealso: MatSeqAIJGetArray()
38818c778c55SBarry Smith @*/
38828c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
38838c778c55SBarry Smith {
38848c778c55SBarry Smith   PetscErrorCode ierr;
38858c778c55SBarry Smith 
38868c778c55SBarry Smith   PetscFunctionBegin;
38878c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
38888c778c55SBarry Smith   PetscFunctionReturn(0);
38898c778c55SBarry Smith }
38908c778c55SBarry Smith 
389117667f90SBarry Smith EXTERN_C_BEGIN
38924a2ae208SSatish Balay #undef __FUNCT__
38934a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
38947087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqAIJ(Mat B)
3895273d9f13SBarry Smith {
3896273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3897dfbe8321SBarry Smith   PetscErrorCode ierr;
389838baddfdSBarry Smith   PetscMPIInt    size;
3899273d9f13SBarry Smith 
3900273d9f13SBarry Smith   PetscFunctionBegin;
39017adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3902e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3903273d9f13SBarry Smith 
390438f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3905b0a32e0cSBarry Smith   B->data             = (void*)b;
3906549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3907416022c9SBarry Smith   b->row              = 0;
3908416022c9SBarry Smith   b->col              = 0;
390982bf6240SBarry Smith   b->icol             = 0;
3910b810aeb4SBarry Smith   b->reallocs         = 0;
391136db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3912f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3913416022c9SBarry Smith   b->nonew             = 0;
3914416022c9SBarry Smith   b->diag              = 0;
3915416022c9SBarry Smith   b->solve_work        = 0;
39162a1b7f2aSHong Zhang   B->spptr             = 0;
3917be6bf707SBarry Smith   b->saved_values      = 0;
3918d7f994e1SBarry Smith   b->idiag             = 0;
391971f1c65dSBarry Smith   b->mdiag             = 0;
392071f1c65dSBarry Smith   b->ssor_work         = 0;
392171f1c65dSBarry Smith   b->omega             = 1.0;
392271f1c65dSBarry Smith   b->fshift            = 0.0;
392371f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3924bbead8a2SBarry Smith   b->ibdiagvalid       = PETSC_FALSE;
3925a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3926a30b2313SHong Zhang   b->xtoy              = 0;
3927a30b2313SHong Zhang   b->XtoY              = 0;
392888e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
392917ab2063SBarry Smith 
393035d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
39318c778c55SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJGetArray_C","MatSeqAIJGetArray_SeqAIJ",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
39328c778c55SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJRestoreArray_C","MatSeqAIJRestoreArray_SeqAIJ",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
39338c778c55SBarry Smith 
3934b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3935700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3936b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3937b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3938b3866ffcSBarry Smith #endif
3939b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3940700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3941b5e56a35SBarry Smith #endif
3942ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3943700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3944719d5645SBarry Smith #endif
3945611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3946700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3947611f576cSBarry Smith #endif
3948f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3949700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3950f3c0ef26SHong Zhang #endif
3951611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3952700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr);
3953611f576cSBarry Smith #endif
3954eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3955700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3956eb3b5408SSatish Balay #endif
3957586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3958700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3959586621ddSJed Brown #endif
3960719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
396117f1a0eaSHong Zhang     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_aij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3962719d5645SBarry Smith #endif
396317f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
396417f1a0eaSHong Zhang     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_clique_C","MatGetFactor_aij_clique",MatGetFactor_aij_clique);CHKERRQ(ierr);
396517f1a0eaSHong Zhang #endif
396617f1a0eaSHong Zhang 
3967700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3968700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3969700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
3970700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3971700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3972700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3973700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3974700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3975700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3976700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3977700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3978700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3979700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3980700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
3981700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3982700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3983700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3984700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
39854108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
398617667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
39873a40ed3dSBarry Smith   PetscFunctionReturn(0);
398817ab2063SBarry Smith }
3989273d9f13SBarry Smith EXTERN_C_END
399017ab2063SBarry Smith 
39914a2ae208SSatish Balay #undef __FUNCT__
3992b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3993b24902e0SBarry Smith /*
3994b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3995b24902e0SBarry Smith */
3996ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
399717ab2063SBarry Smith {
3998416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
39996849ba73SBarry Smith   PetscErrorCode ierr;
4000d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
400117ab2063SBarry Smith 
40023a40ed3dSBarry Smith   PetscFunctionBegin;
4003273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4004273d9f13SBarry Smith 
4005d5f3da31SBarry Smith   C->factortype     = A->factortype;
4006416022c9SBarry Smith   c->row            = 0;
4007416022c9SBarry Smith   c->col            = 0;
400882bf6240SBarry Smith   c->icol           = 0;
40096ad4291fSHong Zhang   c->reallocs       = 0;
401017ab2063SBarry Smith 
40116ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
401217ab2063SBarry Smith 
4013aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4014aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4015eec197d1SBarry Smith 
401633b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
40179518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
401817ab2063SBarry Smith   for (i=0; i<m; i++) {
4019416022c9SBarry Smith     c->imax[i] = a->imax[i];
4020416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
402117ab2063SBarry Smith   }
402217ab2063SBarry Smith 
402317ab2063SBarry Smith   /* allocate the matrix space */
4024f77e22a1SHong Zhang   if (mallocmatspace){
4025a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
40269518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
4027f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
402897f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
402917ab2063SBarry Smith     if (m > 0) {
403097f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4031be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4032bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4033be6bf707SBarry Smith       } else {
4034bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
403517ab2063SBarry Smith       }
403608480c60SBarry Smith     }
4037f77e22a1SHong Zhang   }
403817ab2063SBarry Smith 
40396ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4040416022c9SBarry Smith   c->roworiented       = a->roworiented;
4041416022c9SBarry Smith   c->nonew             = a->nonew;
4042416022c9SBarry Smith   if (a->diag) {
404397f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
404452e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
404517ab2063SBarry Smith     for (i=0; i<m; i++) {
4046416022c9SBarry Smith       c->diag[i] = a->diag[i];
404717ab2063SBarry Smith     }
40483a40ed3dSBarry Smith   } else c->diag           = 0;
40496ad4291fSHong Zhang   c->solve_work            = 0;
40506ad4291fSHong Zhang   c->saved_values          = 0;
40516ad4291fSHong Zhang   c->idiag                 = 0;
405271f1c65dSBarry Smith   c->ssor_work             = 0;
4053a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
4054e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
4055e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
40566ad4291fSHong Zhang   c->xtoy                  = 0;
40576ad4291fSHong Zhang   c->XtoY                  = 0;
40586ad4291fSHong Zhang 
4059893ad86cSHong Zhang   c->rmax               = a->rmax;
4060416022c9SBarry Smith   c->nz                 = a->nz;
40618ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
4062273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
4063754ec7b1SSatish Balay 
40646ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
40656ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
4066cd6b891eSBarry Smith   c->compressedrow.check   = a->compressedrow.check;
4067cd6b891eSBarry Smith   if (a->compressedrow.use){
40686ad4291fSHong Zhang     i = a->compressedrow.nrows;
40690e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
40706ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
40716ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
407227ea64f8SHong Zhang   } else {
407327ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
407427ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
407527ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
40766ad4291fSHong Zhang   }
407788e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
40784108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
40794846f1f5SKris Buschelman 
40807adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
40813a40ed3dSBarry Smith   PetscFunctionReturn(0);
408217ab2063SBarry Smith }
408317ab2063SBarry Smith 
40844a2ae208SSatish Balay #undef __FUNCT__
4085b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
4086b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4087b24902e0SBarry Smith {
4088b24902e0SBarry Smith   PetscErrorCode ierr;
4089b24902e0SBarry Smith 
4090b24902e0SBarry Smith   PetscFunctionBegin;
4091b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
40924b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4093a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
4094a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4095f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4096b24902e0SBarry Smith   PetscFunctionReturn(0);
4097b24902e0SBarry Smith }
4098b24902e0SBarry Smith 
4099b24902e0SBarry Smith #undef __FUNCT__
41004a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
4101112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4102fbdbba38SShri Abhyankar {
4103fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4104fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4105fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4106fbdbba38SShri Abhyankar   int            fd;
4107fbdbba38SShri Abhyankar   PetscMPIInt    size;
4108fbdbba38SShri Abhyankar   MPI_Comm       comm;
4109bbead8a2SBarry Smith   PetscInt       bs = 1;
4110fbdbba38SShri Abhyankar 
4111fbdbba38SShri Abhyankar   PetscFunctionBegin;
4112fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4113fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4114fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4115bbead8a2SBarry Smith 
4116bbead8a2SBarry Smith   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
4117bbead8a2SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
4118bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4119bbead8a2SBarry Smith 
4120fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4121fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4122fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4123fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4124fbdbba38SShri Abhyankar 
4125bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4126fbdbba38SShri Abhyankar 
4127fbdbba38SShri Abhyankar   /* read in row lengths */
4128fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
4129fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4130fbdbba38SShri Abhyankar 
4131fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4132fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
4133fbdbba38SShri 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);
4134fbdbba38SShri Abhyankar 
4135fbdbba38SShri Abhyankar   /* set global size if not set already*/
4136f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4137fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4138aabbc4fbSShri Abhyankar   } else {
4139fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
4140fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
41414c5b953cSHong Zhang     if (rows < 0 && cols < 0){ /* user might provide local size instead of global size */
41424c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
41434c5b953cSHong Zhang     }
4144f501eaabSShri 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);
4145aabbc4fbSShri Abhyankar   }
4146fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4147fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
4148fbdbba38SShri Abhyankar 
4149fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4150fbdbba38SShri Abhyankar 
4151fbdbba38SShri Abhyankar   /* read in nonzero values */
4152fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4153fbdbba38SShri Abhyankar 
4154fbdbba38SShri Abhyankar   /* set matrix "i" values */
4155fbdbba38SShri Abhyankar   a->i[0] = 0;
4156fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4157fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4158fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4159fbdbba38SShri Abhyankar   }
4160fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4161fbdbba38SShri Abhyankar 
4162fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4163fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4164bbead8a2SBarry Smith   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
4165fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4166fbdbba38SShri Abhyankar }
4167fbdbba38SShri Abhyankar 
4168fbdbba38SShri Abhyankar #undef __FUNCT__
4169b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4170ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
41717264ac53SSatish Balay {
41727264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
4173dfbe8321SBarry Smith   PetscErrorCode ierr;
4174eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4175eeffb40dSHong Zhang   PetscInt       k;
4176eeffb40dSHong Zhang #endif
41777264ac53SSatish Balay 
41783a40ed3dSBarry Smith   PetscFunctionBegin;
4179bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4180d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4181ca44d042SBarry Smith     *flg = PETSC_FALSE;
4182ca44d042SBarry Smith     PetscFunctionReturn(0);
4183bcd2baecSBarry Smith   }
41847264ac53SSatish Balay 
41857264ac53SSatish Balay   /* if the a->i are the same */
4186d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4187abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
41887264ac53SSatish Balay 
41897264ac53SSatish Balay   /* if a->j are the same */
419097f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4191abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4192bcd2baecSBarry Smith 
4193bcd2baecSBarry Smith   /* if a->a are the same */
4194eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4195eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
4196eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
4197eeffb40dSHong Zhang       *flg = PETSC_FALSE;
41983a40ed3dSBarry Smith       PetscFunctionReturn(0);
4199eeffb40dSHong Zhang     }
4200eeffb40dSHong Zhang   }
4201eeffb40dSHong Zhang #else
4202eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4203eeffb40dSHong Zhang #endif
4204eeffb40dSHong Zhang   PetscFunctionReturn(0);
42057264ac53SSatish Balay }
420636db0b34SBarry Smith 
42074a2ae208SSatish Balay #undef __FUNCT__
42084a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
420905869f15SSatish Balay /*@
421036db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
421136db0b34SBarry Smith               provided by the user.
421236db0b34SBarry Smith 
4213c75a6043SHong Zhang       Collective on MPI_Comm
421436db0b34SBarry Smith 
421536db0b34SBarry Smith    Input Parameters:
421636db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
421736db0b34SBarry Smith .   m - number of rows
421836db0b34SBarry Smith .   n - number of columns
421936db0b34SBarry Smith .   i - row indices
422036db0b34SBarry Smith .   j - column indices
422136db0b34SBarry Smith -   a - matrix values
422236db0b34SBarry Smith 
422336db0b34SBarry Smith    Output Parameter:
422436db0b34SBarry Smith .   mat - the matrix
422536db0b34SBarry Smith 
422636db0b34SBarry Smith    Level: intermediate
422736db0b34SBarry Smith 
422836db0b34SBarry Smith    Notes:
42290551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4230292fb18eSBarry Smith     once the matrix is destroyed and not before
423136db0b34SBarry Smith 
423236db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
423336db0b34SBarry Smith 
4234bfeeae90SHong Zhang        The i and j indices are 0 based
423536db0b34SBarry Smith 
4236a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4237a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
4238a4552177SSatish Balay     as shown:
4239a4552177SSatish Balay 
4240a4552177SSatish Balay         1 0 0
4241a4552177SSatish Balay         2 0 3
4242a4552177SSatish Balay         4 5 6
4243a4552177SSatish Balay 
4244a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
42459985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4246a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
4247a4552177SSatish Balay 
42489985e31cSBarry Smith 
424969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
425036db0b34SBarry Smith 
425136db0b34SBarry Smith @*/
42527087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
425336db0b34SBarry Smith {
4254dfbe8321SBarry Smith   PetscErrorCode ierr;
4255cbcfb4deSHong Zhang   PetscInt       ii;
425636db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4257cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4258cbcfb4deSHong Zhang   PetscInt       jj;
4259cbcfb4deSHong Zhang #endif
426036db0b34SBarry Smith 
426136db0b34SBarry Smith   PetscFunctionBegin;
4262a96a251dSBarry Smith   if (i[0]) {
4263e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
426436db0b34SBarry Smith   }
4265f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4266f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4267a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4268ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4269ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4270ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4271ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
4272ab93d7beSBarry Smith 
427336db0b34SBarry Smith   aij->i = i;
427436db0b34SBarry Smith   aij->j = j;
427536db0b34SBarry Smith   aij->a = a;
427636db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
427736db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4278e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4279e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
428036db0b34SBarry Smith 
428136db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
428236db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
42832515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
4284e32f2f54SBarry 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]);
42859985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4286e32f2f54SBarry 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);
4287e32f2f54SBarry 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);
42889985e31cSBarry Smith     }
428936db0b34SBarry Smith #endif
429036db0b34SBarry Smith   }
42912515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
429236db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
4293e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
4294e32f2f54SBarry 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]);
429536db0b34SBarry Smith   }
429636db0b34SBarry Smith #endif
429736db0b34SBarry Smith 
4298b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4299b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
430036db0b34SBarry Smith   PetscFunctionReturn(0);
430136db0b34SBarry Smith }
43028a0b0e6bSVictor Minden #undef __FUNCT__
43038a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
430480ef6e79SMatthew G Knepley /*@C
4305d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
43068a0b0e6bSVictor Minden               provided by the user.
43078a0b0e6bSVictor Minden 
43088a0b0e6bSVictor Minden       Collective on MPI_Comm
43098a0b0e6bSVictor Minden 
43108a0b0e6bSVictor Minden    Input Parameters:
43118a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
43128a0b0e6bSVictor Minden .   m   - number of rows
43138a0b0e6bSVictor Minden .   n   - number of columns
43148a0b0e6bSVictor Minden .   i   - row indices
43158a0b0e6bSVictor Minden .   j   - column indices
43161230e6d1SVictor Minden .   a   - matrix values
43171230e6d1SVictor Minden .   nz  - number of nonzeros
43181230e6d1SVictor Minden -   idx - 0 or 1 based
43198a0b0e6bSVictor Minden 
43208a0b0e6bSVictor Minden    Output Parameter:
43218a0b0e6bSVictor Minden .   mat - the matrix
43228a0b0e6bSVictor Minden 
43238a0b0e6bSVictor Minden    Level: intermediate
43248a0b0e6bSVictor Minden 
43258a0b0e6bSVictor Minden    Notes:
43268a0b0e6bSVictor Minden        The i and j indices are 0 based
43278a0b0e6bSVictor Minden 
43288a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
43298a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
43308a0b0e6bSVictor Minden     as shown:
43318a0b0e6bSVictor Minden 
43328a0b0e6bSVictor Minden         1 0 0
43338a0b0e6bSVictor Minden         2 0 3
43348a0b0e6bSVictor Minden         4 5 6
43358a0b0e6bSVictor Minden 
43368a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
43378a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
43388a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
43398a0b0e6bSVictor Minden 
43408a0b0e6bSVictor Minden 
434169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
43428a0b0e6bSVictor Minden 
43438a0b0e6bSVictor Minden @*/
43441230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
43458a0b0e6bSVictor Minden {
43468a0b0e6bSVictor Minden   PetscErrorCode ierr;
4347d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
43488a0b0e6bSVictor Minden 
43498a0b0e6bSVictor Minden 
43508a0b0e6bSVictor Minden   PetscFunctionBegin;
4351d021a1c5SVictor Minden   ierr = PetscMalloc(m*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
43521230e6d1SVictor Minden   ierr = PetscMemzero(nnz,m*sizeof(PetscInt));CHKERRQ(ierr);
43531230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++){
43541230e6d1SVictor Minden     nnz[i[ii]] += 1;
43551230e6d1SVictor Minden   }
43568a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
43578a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4358a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
43598a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
43601230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
43611230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++){
43621230e6d1SVictor Minden     if (idx){
43631230e6d1SVictor Minden       row = i[ii] - 1;
43641230e6d1SVictor Minden       col = j[ii] - 1;
43651230e6d1SVictor Minden     } else {
43661230e6d1SVictor Minden       row = i[ii];
43671230e6d1SVictor Minden       col = j[ii];
43688a0b0e6bSVictor Minden     }
43691230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
43708a0b0e6bSVictor Minden   }
43718a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
43728a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4373d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
43748a0b0e6bSVictor Minden   PetscFunctionReturn(0);
43758a0b0e6bSVictor Minden }
437636db0b34SBarry Smith 
4377cc8ba8e1SBarry Smith #undef __FUNCT__
4378ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4379dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4380cc8ba8e1SBarry Smith {
4381dfbe8321SBarry Smith   PetscErrorCode ierr;
4382cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
438336db0b34SBarry Smith 
4384cc8ba8e1SBarry Smith   PetscFunctionBegin;
43858ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4386cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4387cc8ba8e1SBarry Smith     a->coloring = coloring;
438812c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
438997f1f81fSBarry Smith     PetscInt             i,*larray;
439012c595b3SBarry Smith     ISColoring      ocoloring;
439108b6dcc0SBarry Smith     ISColoringValue *colors;
439212c595b3SBarry Smith 
439312c595b3SBarry Smith     /* set coloring for diagonal portion */
43940e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
4395d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
439612c595b3SBarry Smith       larray[i] = i;
439712c595b3SBarry Smith     }
4398992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
43990e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4400d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
440112c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
440212c595b3SBarry Smith     }
440312c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4404d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
440512c595b3SBarry Smith     a->coloring = ocoloring;
440612c595b3SBarry Smith   }
4407cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4408cc8ba8e1SBarry Smith }
4409cc8ba8e1SBarry Smith 
4410ee4f033dSBarry Smith #undef __FUNCT__
4411ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
441297f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4413ee4f033dSBarry Smith {
4414ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4415d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
441654f21887SBarry Smith   MatScalar       *v = a->a;
441754f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
441808b6dcc0SBarry Smith   ISColoringValue *color;
4419ee4f033dSBarry Smith 
4420ee4f033dSBarry Smith   PetscFunctionBegin;
4421e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4422ee4f033dSBarry Smith   color = a->coloring->colors;
4423ee4f033dSBarry Smith   /* loop over rows */
4424ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4425ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4426ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
4427ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
4428ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
4429ee4f033dSBarry Smith     }
4430ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4431cc8ba8e1SBarry Smith   }
4432cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4433cc8ba8e1SBarry Smith }
443436db0b34SBarry Smith 
4435*acf2f550SJed Brown #undef __FUNCT__
4436*acf2f550SJed Brown #define __FUNCT__ "MatSeqAIJInvalidateDiagonal"
4437*acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4438*acf2f550SJed Brown {
4439*acf2f550SJed Brown   Mat_SeqAIJ      *a=(Mat_SeqAIJ*)A->data;
4440*acf2f550SJed Brown   PetscErrorCode ierr;
4441*acf2f550SJed Brown 
4442*acf2f550SJed Brown   PetscFunctionBegin;
4443*acf2f550SJed Brown   a->idiagvalid = PETSC_FALSE;
4444*acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
4445*acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4446*acf2f550SJed Brown   PetscFunctionReturn(0);
4447*acf2f550SJed Brown }
4448*acf2f550SJed Brown 
444981824310SBarry Smith /*
445081824310SBarry Smith     Special version for direct calls from Fortran
445181824310SBarry Smith */
4452b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
445381824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
445481824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
445581824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
445681824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
445781824310SBarry Smith #endif
445881824310SBarry Smith 
445981824310SBarry Smith /* Change these macros so can be used in void function */
446081824310SBarry Smith #undef CHKERRQ
44617adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
446281824310SBarry Smith #undef SETERRQ2
4463e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
44644994cf47SJed Brown #undef SETERRQ3
44654994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
446681824310SBarry Smith 
446781824310SBarry Smith EXTERN_C_BEGIN
446881824310SBarry Smith #undef __FUNCT__
446981824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
44701f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
447181824310SBarry Smith {
447281824310SBarry Smith   Mat            A = *AA;
447381824310SBarry Smith   PetscInt       m = *mm, n = *nn;
447481824310SBarry Smith   InsertMode     is = *isis;
447581824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
447681824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
447781824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
447881824310SBarry Smith   PetscErrorCode ierr;
447981824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
448054f21887SBarry Smith   MatScalar      *ap,value,*aa;
4481ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4482ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
448381824310SBarry Smith 
448481824310SBarry Smith   PetscFunctionBegin;
44854994cf47SJed Brown   MatCheckPreallocated(A,1);
448681824310SBarry Smith   imax = a->imax;
448781824310SBarry Smith   ai = a->i;
448881824310SBarry Smith   ailen = a->ilen;
448981824310SBarry Smith   aj = a->j;
449081824310SBarry Smith   aa = a->a;
449181824310SBarry Smith 
449281824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
449381824310SBarry Smith     row  = im[k];
449481824310SBarry Smith     if (row < 0) continue;
449581824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4496d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
449781824310SBarry Smith #endif
449881824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
449981824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
450081824310SBarry Smith     low  = 0;
450181824310SBarry Smith     high = nrow;
450281824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
450381824310SBarry Smith       if (in[l] < 0) continue;
450481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4505d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
450681824310SBarry Smith #endif
450781824310SBarry Smith       col = in[l];
450881824310SBarry Smith       if (roworiented) {
450981824310SBarry Smith         value = v[l + k*n];
451081824310SBarry Smith       } else {
451181824310SBarry Smith         value = v[k + l*m];
451281824310SBarry Smith       }
451381824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
451481824310SBarry Smith 
451581824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
451681824310SBarry Smith       lastcol = col;
451781824310SBarry Smith       while (high-low > 5) {
451881824310SBarry Smith         t = (low+high)/2;
451981824310SBarry Smith         if (rp[t] > col) high = t;
452081824310SBarry Smith         else             low  = t;
452181824310SBarry Smith       }
452281824310SBarry Smith       for (i=low; i<high; i++) {
452381824310SBarry Smith         if (rp[i] > col) break;
452481824310SBarry Smith         if (rp[i] == col) {
452581824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
452681824310SBarry Smith           else                  ap[i] = value;
452781824310SBarry Smith           goto noinsert;
452881824310SBarry Smith         }
452981824310SBarry Smith       }
453081824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
453181824310SBarry Smith       if (nonew == 1) goto noinsert;
45327adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4533fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
453481824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
453581824310SBarry Smith       /* shift up all the later entries in this row */
453681824310SBarry Smith       for (ii=N; ii>=i; ii--) {
453781824310SBarry Smith         rp[ii+1] = rp[ii];
453881824310SBarry Smith         ap[ii+1] = ap[ii];
453981824310SBarry Smith       }
454081824310SBarry Smith       rp[i] = col;
454181824310SBarry Smith       ap[i] = value;
454281824310SBarry Smith       noinsert:;
454381824310SBarry Smith       low = i + 1;
454481824310SBarry Smith     }
454581824310SBarry Smith     ailen[row] = nrow;
454681824310SBarry Smith   }
454781824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
454881824310SBarry Smith   PetscFunctionReturnVoid();
454981824310SBarry Smith }
455081824310SBarry Smith EXTERN_C_END
455162298a1eSBarry Smith 
4552