xref: /petsc/src/mat/impls/aij/seq/aij.c (revision b19a5dc2ae7a19790eb567e5db809e9a2407cc6a)
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>
1106873bf2SBarry Smith #include <petsc-private/kernels/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;
860298fd71SBarry Smith   *zrows = NULL;
87f1f41ecbSJed Brown   ierr   = MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);CHKERRQ(ierr);
88ce94432eSBarry Smith   ierr   = ISCreateGeneral(PetscObjectComm((PetscObject)A),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) {
1500298fd71SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,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     }
166acf2f550SJed 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++;
2542205254eSKarl Rupp 
2553b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2563b2fbd54SBarry Smith       }
2573b2fbd54SBarry Smith     }
258606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2593b2fbd54SBarry Smith     *ia  = cia; *ja = cja;
2603b2fbd54SBarry Smith   }
2613a40ed3dSBarry Smith   PetscFunctionReturn(0);
2623b2fbd54SBarry Smith }
2633b2fbd54SBarry Smith 
2644a2ae208SSatish Balay #undef __FUNCT__
2654a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
2661a83f524SJed Brown PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2673b2fbd54SBarry Smith {
268dfbe8321SBarry Smith   PetscErrorCode ierr;
269606d414cSSatish Balay 
2703a40ed3dSBarry Smith   PetscFunctionBegin;
2713a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2723b2fbd54SBarry Smith 
273606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
274606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
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 
3332205254eSKarl Rupp       if (col <= lastcol) low = 0;
3342205254eSKarl Rupp       else high = nrow;
335e2ee6c50SBarry Smith       lastcol = col;
336416022c9SBarry Smith       while (high-low > 5) {
337416022c9SBarry Smith         t = (low+high)/2;
338416022c9SBarry Smith         if (rp[t] > col) high = t;
339416022c9SBarry Smith         else low = t;
34017ab2063SBarry Smith       }
341416022c9SBarry Smith       for (i=low; i<high; i++) {
34217ab2063SBarry Smith         if (rp[i] > col) break;
34317ab2063SBarry Smith         if (rp[i] == col) {
344416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
34517ab2063SBarry Smith           else ap[i] = value;
346e44c0bd4SBarry Smith           low = i + 1;
34717ab2063SBarry Smith           goto noinsert;
34817ab2063SBarry Smith         }
34917ab2063SBarry Smith       }
350abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
351c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
352e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
353fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
354c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
355416022c9SBarry Smith       /* shift up all the later entries in this row */
356416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
35717ab2063SBarry Smith         rp[ii+1] = rp[ii];
35817ab2063SBarry Smith         ap[ii+1] = ap[ii];
35917ab2063SBarry Smith       }
36017ab2063SBarry Smith       rp[i] = col;
36117ab2063SBarry Smith       ap[i] = value;
362416022c9SBarry Smith       low   = i + 1;
363e44c0bd4SBarry Smith noinsert:;
36417ab2063SBarry Smith     }
36517ab2063SBarry Smith     ailen[row] = nrow;
36617ab2063SBarry Smith   }
36788e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
3683a40ed3dSBarry Smith   PetscFunctionReturn(0);
36917ab2063SBarry Smith }
37017ab2063SBarry Smith 
37181824310SBarry Smith 
3724a2ae208SSatish Balay #undef __FUNCT__
3734a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
374a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
3757eb43aa7SLois Curfman McInnes {
3767eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
37797f1f81fSBarry Smith   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
37897f1f81fSBarry Smith   PetscInt   *ai = a->i,*ailen = a->ilen;
37954f21887SBarry Smith   MatScalar  *ap,*aa = a->a;
3807eb43aa7SLois Curfman McInnes 
3813a40ed3dSBarry Smith   PetscFunctionBegin;
3827eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
3837eb43aa7SLois Curfman McInnes     row = im[k];
384e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
385e32f2f54SBarry 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);
386bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
3877eb43aa7SLois Curfman McInnes     nrow = ailen[row];
3887eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
389e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
390e32f2f54SBarry 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);
391bfeeae90SHong Zhang       col  = in[l];
3927eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
3937eb43aa7SLois Curfman McInnes       while (high-low > 5) {
3947eb43aa7SLois Curfman McInnes         t = (low+high)/2;
3957eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
3967eb43aa7SLois Curfman McInnes         else low = t;
3977eb43aa7SLois Curfman McInnes       }
3987eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
3997eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
4007eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
401b49de8d1SLois Curfman McInnes           *v++ = ap[i];
4027eb43aa7SLois Curfman McInnes           goto finished;
4037eb43aa7SLois Curfman McInnes         }
4047eb43aa7SLois Curfman McInnes       }
40597e567efSBarry Smith       *v++ = 0.0;
4067eb43aa7SLois Curfman McInnes finished:;
4077eb43aa7SLois Curfman McInnes     }
4087eb43aa7SLois Curfman McInnes   }
4093a40ed3dSBarry Smith   PetscFunctionReturn(0);
4107eb43aa7SLois Curfman McInnes }
4117eb43aa7SLois Curfman McInnes 
41217ab2063SBarry Smith 
4134a2ae208SSatish Balay #undef __FUNCT__
4144a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
415dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
41617ab2063SBarry Smith {
417416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4186849ba73SBarry Smith   PetscErrorCode ierr;
4196f69ff64SBarry Smith   PetscInt       i,*col_lens;
4206f69ff64SBarry Smith   int            fd;
421b37d52dbSMark F. Adams   FILE           *file;
42217ab2063SBarry Smith 
4233a40ed3dSBarry Smith   PetscFunctionBegin;
424b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
425d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
4262205254eSKarl Rupp 
4270700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
428d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
429d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
430416022c9SBarry Smith   col_lens[3] = a->nz;
431416022c9SBarry Smith 
432416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
433d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
434416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
43517ab2063SBarry Smith   }
436d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
437606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
438416022c9SBarry Smith 
439416022c9SBarry Smith   /* store column indices (zero start index) */
4406f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
441416022c9SBarry Smith 
442416022c9SBarry Smith   /* store nonzero values */
4436f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
444b37d52dbSMark F. Adams 
445b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
446b37d52dbSMark F. Adams   if (file) {
447b37d52dbSMark F. Adams     fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
448b37d52dbSMark F. Adams   }
4493a40ed3dSBarry Smith   PetscFunctionReturn(0);
45017ab2063SBarry Smith }
451416022c9SBarry Smith 
45209573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
453cd155464SBarry Smith 
4544a2ae208SSatish Balay #undef __FUNCT__
4554a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
456dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
457416022c9SBarry Smith {
458416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
459dfbe8321SBarry Smith   PetscErrorCode    ierr;
460d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
461e060cb09SBarry Smith   const char        *name;
462f3ef73ceSBarry Smith   PetscViewerFormat format;
46317ab2063SBarry Smith 
4643a40ed3dSBarry Smith   PetscFunctionBegin;
465b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
46671c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
46797f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
468014b3a6eSMark Adams     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift))) {
469d00d2cf4SBarry Smith       nofinalvalue = 1;
470d00d2cf4SBarry Smith     }
471d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
472d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
47377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
47477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
475b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
47617ab2063SBarry Smith 
47717ab2063SBarry Smith     for (i=0; i<m; i++) {
478416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
479aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
48077431f27SBarry 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);
48117ab2063SBarry Smith #else
48277431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
48317ab2063SBarry Smith #endif
48417ab2063SBarry Smith       }
48517ab2063SBarry Smith     }
486d00d2cf4SBarry Smith     if (nofinalvalue) {
487d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
488d00d2cf4SBarry Smith     }
489317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
490fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
491d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
49268369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
493cd155464SBarry Smith     PetscFunctionReturn(0);
494fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
495d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
496dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer);CHKERRQ(ierr);
49744cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
49877431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
49944cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
500aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
50136db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
502ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50336db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
504ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50536db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
506ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
5076831982aSBarry Smith         }
50844cd7ae7SLois Curfman McInnes #else
509ba0e910bSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);}
51044cd7ae7SLois Curfman McInnes #endif
51144cd7ae7SLois Curfman McInnes       }
512b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
51344cd7ae7SLois Curfman McInnes     }
514d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
515fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
51697f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
517d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
518dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer);CHKERRQ(ierr);
51997f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
520496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
521496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
522496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
523496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
524aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52536db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
526496be53dSLois Curfman McInnes #else
527496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
528496be53dSLois Curfman McInnes #endif
529496be53dSLois Curfman McInnes         }
530496be53dSLois Curfman McInnes       }
531496be53dSLois Curfman McInnes     }
5322e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
53377431f27SBarry Smith     ierr    = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
5342e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
5352205254eSKarl Rupp       if (i+4<m) {
5362205254eSKarl Rupp         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);
5372205254eSKarl Rupp       } else if (i+3<m) {
5382205254eSKarl Rupp         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);
5392205254eSKarl Rupp       } else if (i+2<m) {
5402205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);
5412205254eSKarl Rupp       } else if (i+1<m) {
5422205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);
5432205254eSKarl Rupp       } else if (i<m) {
5442205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);
5452205254eSKarl Rupp       } else {
5462205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);
5472205254eSKarl Rupp       }
548496be53dSLois Curfman McInnes     }
549b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
550606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
551496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
552496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
55377431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
554496be53dSLois Curfman McInnes       }
555b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
556496be53dSLois Curfman McInnes     }
557b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
558496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
559496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
560496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
561aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
56236db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
563b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5646831982aSBarry Smith           }
565496be53dSLois Curfman McInnes #else
566b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
567496be53dSLois Curfman McInnes #endif
568496be53dSLois Curfman McInnes         }
569496be53dSLois Curfman McInnes       }
570b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
571496be53dSLois Curfman McInnes     }
572d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
573fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
57497f1f81fSBarry Smith     PetscInt    cnt = 0,jcnt;
57587828ca2SBarry Smith     PetscScalar value;
57602594712SBarry Smith 
577d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
578dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer);CHKERRQ(ierr);
57902594712SBarry Smith     for (i=0; i<m; i++) {
58002594712SBarry Smith       jcnt = 0;
581d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
582e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
58302594712SBarry Smith           value = a->a[cnt++];
584e24b481bSBarry Smith           jcnt++;
58502594712SBarry Smith         } else {
58602594712SBarry Smith           value = 0.0;
58702594712SBarry Smith         }
588aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
589b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
59002594712SBarry Smith #else
591b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
59202594712SBarry Smith #endif
59302594712SBarry Smith       }
594b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
59502594712SBarry Smith     }
596d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5973c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
598d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
599dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer);CHKERRQ(ierr);
6003c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
6013c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
6023c215bfdSMatthew Knepley #else
6033c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
6043c215bfdSMatthew Knepley #endif
605d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
6063c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
6073c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
6083c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
6093c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
610ba0e910bSBarry 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);
6113c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
612ba0e910bSBarry 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);
6133c215bfdSMatthew Knepley         } else {
614ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
6153c215bfdSMatthew Knepley         }
6163c215bfdSMatthew Knepley #else
617ba0e910bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+shift, a->j[j]+shift, (double)a->a[j]);CHKERRQ(ierr);
6183c215bfdSMatthew Knepley #endif
6193c215bfdSMatthew Knepley       }
6203c215bfdSMatthew Knepley     }
621d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
6223a40ed3dSBarry Smith   } else {
623d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
624dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer);CHKERRQ(ierr);
625d5f3da31SBarry Smith     if (A->factortype) {
62616cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
62716cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
62816cd7e1dSShri Abhyankar         /* L part */
62916cd7e1dSShri Abhyankar         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
63016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
63116cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
632ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
63316cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
634ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
63516cd7e1dSShri Abhyankar           } else {
636ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
63716cd7e1dSShri Abhyankar           }
63816cd7e1dSShri Abhyankar #else
639ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);
64016cd7e1dSShri Abhyankar #endif
64116cd7e1dSShri Abhyankar         }
64216cd7e1dSShri Abhyankar         /* diagonal */
64316cd7e1dSShri Abhyankar         j = a->diag[i];
64416cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
64516cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
646ba0e910bSBarry 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);
64716cd7e1dSShri Abhyankar         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
648ba0e910bSBarry 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);
64916cd7e1dSShri Abhyankar         } else {
650ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
65116cd7e1dSShri Abhyankar         }
65216cd7e1dSShri Abhyankar #else
653ba0e910bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)1.0/a->a[j]);CHKERRQ(ierr);
65416cd7e1dSShri Abhyankar #endif
65516cd7e1dSShri Abhyankar 
65616cd7e1dSShri Abhyankar         /* U part */
65716cd7e1dSShri Abhyankar         for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
65816cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
65916cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
660ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
66116cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
662ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
66316cd7e1dSShri Abhyankar           } else {
664ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
66516cd7e1dSShri Abhyankar           }
66616cd7e1dSShri Abhyankar #else
667ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);
66816cd7e1dSShri Abhyankar #endif
66916cd7e1dSShri Abhyankar         }
67016cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
67116cd7e1dSShri Abhyankar       }
67216cd7e1dSShri Abhyankar     } else {
67317ab2063SBarry Smith       for (i=0; i<m; i++) {
67477431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
675416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
676aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
67736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
678ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
67936db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
680ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6813a40ed3dSBarry Smith           } else {
682ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
68317ab2063SBarry Smith           }
68417ab2063SBarry Smith #else
685ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);
68617ab2063SBarry Smith #endif
68717ab2063SBarry Smith         }
688b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
68917ab2063SBarry Smith       }
69016cd7e1dSShri Abhyankar     }
691d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
69217ab2063SBarry Smith   }
693b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6943a40ed3dSBarry Smith   PetscFunctionReturn(0);
695416022c9SBarry Smith }
696416022c9SBarry Smith 
6979804daf3SBarry Smith #include <petscdraw.h>
6984a2ae208SSatish Balay #undef __FUNCT__
6994a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
700dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
701416022c9SBarry Smith {
702480ef9eaSBarry Smith   Mat               A  = (Mat) Aa;
703416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
704dfbe8321SBarry Smith   PetscErrorCode    ierr;
705d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
70636db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
707b0a32e0cSBarry Smith   PetscViewer       viewer;
708f3ef73ceSBarry Smith   PetscViewerFormat format;
709cddf8d76SBarry Smith 
7103a40ed3dSBarry Smith   PetscFunctionBegin;
711480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
712b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
71319bcc07fSBarry Smith 
714b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
715416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
7160513a670SBarry Smith 
717fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
7180513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
719b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
720416022c9SBarry Smith     for (i=0; i<m; i++) {
721cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
722bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
723bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
72436db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
725b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
726cddf8d76SBarry Smith       }
727cddf8d76SBarry Smith     }
728b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
729cddf8d76SBarry Smith     for (i=0; i<m; i++) {
730cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
731bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
732bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
733cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
734b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
735cddf8d76SBarry Smith       }
736cddf8d76SBarry Smith     }
737b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
738cddf8d76SBarry Smith     for (i=0; i<m; i++) {
739cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
740bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
741bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
74236db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
743b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
744416022c9SBarry Smith       }
745416022c9SBarry Smith     }
7460513a670SBarry Smith   } else {
7470513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
7480513a670SBarry Smith     /* first determine max of all nonzero values */
74997f1f81fSBarry Smith     PetscInt  nz = a->nz,count;
750b0a32e0cSBarry Smith     PetscDraw popup;
75136db0b34SBarry Smith     PetscReal scale;
7520513a670SBarry Smith 
7530513a670SBarry Smith     for (i=0; i<nz; i++) {
7540513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
7550513a670SBarry Smith     }
756b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
757b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
7582205254eSKarl Rupp     if (popup) {
7592205254eSKarl Rupp       ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);
7602205254eSKarl Rupp     }
7610513a670SBarry Smith     count = 0;
7620513a670SBarry Smith     for (i=0; i<m; i++) {
7630513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
764bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
765bfeeae90SHong Zhang         x_l   = a->j[j]; x_r = x_l + 1.0;
76697f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
767b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
7680513a670SBarry Smith         count++;
7690513a670SBarry Smith       }
7700513a670SBarry Smith     }
7710513a670SBarry Smith   }
772480ef9eaSBarry Smith   PetscFunctionReturn(0);
773480ef9eaSBarry Smith }
774cddf8d76SBarry Smith 
7759804daf3SBarry Smith #include <petscdraw.h>
7764a2ae208SSatish Balay #undef __FUNCT__
7774a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
778dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
779480ef9eaSBarry Smith {
780dfbe8321SBarry Smith   PetscErrorCode ierr;
781b0a32e0cSBarry Smith   PetscDraw      draw;
78236db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
783ace3abfcSBarry Smith   PetscBool      isnull;
784480ef9eaSBarry Smith 
785480ef9eaSBarry Smith   PetscFunctionBegin;
786b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
787b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
788480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
789480ef9eaSBarry Smith 
790480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
791d0f46423SBarry Smith   xr   = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
792480ef9eaSBarry Smith   xr  += w;    yr += h;  xl = -w;     yl = -h;
793b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
794b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
7950298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
7963a40ed3dSBarry Smith   PetscFunctionReturn(0);
797416022c9SBarry Smith }
798416022c9SBarry Smith 
7994a2ae208SSatish Balay #undef __FUNCT__
8004a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
801dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
802416022c9SBarry Smith {
803dfbe8321SBarry Smith   PetscErrorCode ierr;
804ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
805416022c9SBarry Smith 
8063a40ed3dSBarry Smith   PetscFunctionBegin;
807251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
808251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
809251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
810c45a1595SBarry Smith   if (iascii) {
8113a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
8120f5bd95cSBarry Smith   } else if (isbinary) {
8133a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
8140f5bd95cSBarry Smith   } else if (isdraw) {
8153a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
81611aeaf0aSBarry Smith   }
8174108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
8183a40ed3dSBarry Smith   PetscFunctionReturn(0);
81917ab2063SBarry Smith }
82019bcc07fSBarry Smith 
8214a2ae208SSatish Balay #undef __FUNCT__
8224a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
823dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
82417ab2063SBarry Smith {
825416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8266849ba73SBarry Smith   PetscErrorCode ierr;
82797f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
828d0f46423SBarry Smith   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
82954f21887SBarry Smith   MatScalar      *aa    = a->a,*ap;
8303447b6efSHong Zhang   PetscReal      ratio  = 0.6;
83117ab2063SBarry Smith 
8323a40ed3dSBarry Smith   PetscFunctionBegin;
8333a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
83417ab2063SBarry Smith 
83543ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
83617ab2063SBarry Smith   for (i=1; i<m; i++) {
837416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
83817ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
83994a9d846SBarry Smith     rmax    = PetscMax(rmax,ailen[i]);
84017ab2063SBarry Smith     if (fshift) {
841bfeeae90SHong Zhang       ip = aj + ai[i];
842bfeeae90SHong Zhang       ap = aa + ai[i];
84317ab2063SBarry Smith       N  = ailen[i];
84417ab2063SBarry Smith       for (j=0; j<N; j++) {
84517ab2063SBarry Smith         ip[j-fshift] = ip[j];
84617ab2063SBarry Smith         ap[j-fshift] = ap[j];
84717ab2063SBarry Smith       }
84817ab2063SBarry Smith     }
84917ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
85017ab2063SBarry Smith   }
85117ab2063SBarry Smith   if (m) {
85217ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
85317ab2063SBarry Smith     ai[m]   = ai[m-1] + ailen[m-1];
85417ab2063SBarry Smith   }
85517ab2063SBarry Smith   /* reset ilen and imax for each row */
85617ab2063SBarry Smith   for (i=0; i<m; i++) {
85717ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
85817ab2063SBarry Smith   }
859bfeeae90SHong Zhang   a->nz = ai[m];
86065e19b50SBarry 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);
86117ab2063SBarry Smith 
86209f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
863d0f46423SBarry 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);
864ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
865ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
8662205254eSKarl Rupp 
8678e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
868dd5f02e7SSatish Balay   a->reallocs         = 0;
8694e220ebcSLois Curfman McInnes   A->info.nz_unneeded = (double)fshift;
87036db0b34SBarry Smith   a->rmax             = rmax;
8714e220ebcSLois Curfman McInnes 
872cd6b891eSBarry Smith   ierr = MatCheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
8732205254eSKarl Rupp 
87488e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
87571c2f376SKris Buschelman 
8764108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
87771f1c65dSBarry Smith 
878acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
8793a40ed3dSBarry Smith   PetscFunctionReturn(0);
88017ab2063SBarry Smith }
88117ab2063SBarry Smith 
8824a2ae208SSatish Balay #undef __FUNCT__
88399cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
88499cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
88599cafbc1SBarry Smith {
88699cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
88799cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
88854f21887SBarry Smith   MatScalar      *aa = a->a;
889acf2f550SJed Brown   PetscErrorCode ierr;
89099cafbc1SBarry Smith 
89199cafbc1SBarry Smith   PetscFunctionBegin;
89299cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
893acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
89499cafbc1SBarry Smith   PetscFunctionReturn(0);
89599cafbc1SBarry Smith }
89699cafbc1SBarry Smith 
89799cafbc1SBarry Smith #undef __FUNCT__
89899cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
89999cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
90099cafbc1SBarry Smith {
90199cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
90299cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
90354f21887SBarry Smith   MatScalar      *aa = a->a;
904acf2f550SJed Brown   PetscErrorCode ierr;
90599cafbc1SBarry Smith 
90699cafbc1SBarry Smith   PetscFunctionBegin;
90799cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
908acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
90999cafbc1SBarry Smith   PetscFunctionReturn(0);
91099cafbc1SBarry Smith }
91199cafbc1SBarry Smith 
91278b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
91378b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ_Kernel(PetscInt thread_id,Mat A)
91478b84d54SShri Abhyankar {
91578b84d54SShri Abhyankar   PetscErrorCode ierr;
91678b84d54SShri Abhyankar   PetscInt       *trstarts=A->rmap->trstarts;
91778b84d54SShri Abhyankar   PetscInt       n,start,end;
91878b84d54SShri Abhyankar   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
91978b84d54SShri Abhyankar 
92078b84d54SShri Abhyankar   start = trstarts[thread_id];
92178b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
92219baf141SJed Brown   n     = a->i[end] - a->i[start];
92319baf141SJed Brown   ierr  = PetscMemzero(a->a+a->i[start],n*sizeof(PetscScalar));CHKERRQ(ierr);
92478b84d54SShri Abhyankar   return 0;
92578b84d54SShri Abhyankar }
92678b84d54SShri Abhyankar 
92778b84d54SShri Abhyankar #undef __FUNCT__
92878b84d54SShri Abhyankar #define __FUNCT__ "MatZeroEntries_SeqAIJ"
92978b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
93078b84d54SShri Abhyankar {
93178b84d54SShri Abhyankar   PetscErrorCode ierr;
93278b84d54SShri Abhyankar 
93378b84d54SShri Abhyankar   PetscFunctionBegin;
934ce94432eSBarry Smith   ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatZeroEntries_SeqAIJ_Kernel,1,A);CHKERRQ(ierr);
935acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
93678b84d54SShri Abhyankar   PetscFunctionReturn(0);
93778b84d54SShri Abhyankar }
93878b84d54SShri Abhyankar #else
93999cafbc1SBarry Smith #undef __FUNCT__
9404a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
941dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
94217ab2063SBarry Smith {
943416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
944dfbe8321SBarry Smith   PetscErrorCode ierr;
9453a40ed3dSBarry Smith 
9463a40ed3dSBarry Smith   PetscFunctionBegin;
947d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
948acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
9493a40ed3dSBarry Smith   PetscFunctionReturn(0);
95017ab2063SBarry Smith }
95178b84d54SShri Abhyankar #endif
952416022c9SBarry Smith 
9534a2ae208SSatish Balay #undef __FUNCT__
9544a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
955dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
95617ab2063SBarry Smith {
957416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
958dfbe8321SBarry Smith   PetscErrorCode ierr;
959d5d45c9bSBarry Smith 
9603a40ed3dSBarry Smith   PetscFunctionBegin;
961aa482453SBarry Smith #if defined(PETSC_USE_LOG)
962d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
96317ab2063SBarry Smith #endif
964e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
9656bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
9666bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
96705b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
968d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
96905b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
97071f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
97105b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
9726bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
97305b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
9746bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
97505b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
9766bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
977cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
9780b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
979a30b2313SHong Zhang 
9804108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
981bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
982901853e0SKris Buschelman 
983dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
984bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
985bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
986bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
987bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr);
988bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr);
989bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr);
990bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
991bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
992bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
993bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr);
9943a40ed3dSBarry Smith   PetscFunctionReturn(0);
99517ab2063SBarry Smith }
99617ab2063SBarry Smith 
9974a2ae208SSatish Balay #undef __FUNCT__
9984a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
999ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
100017ab2063SBarry Smith {
1001416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10024846f1f5SKris Buschelman   PetscErrorCode ierr;
10033a40ed3dSBarry Smith 
10043a40ed3dSBarry Smith   PetscFunctionBegin;
1005a65d3064SKris Buschelman   switch (op) {
1006a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
10074e0d8c25SBarry Smith     a->roworiented = flg;
1008a65d3064SKris Buschelman     break;
1009a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1010a9817697SBarry Smith     a->keepnonzeropattern = flg;
1011a65d3064SKris Buschelman     break;
1012512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1013512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1014a65d3064SKris Buschelman     break;
1015a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
10164e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1017a65d3064SKris Buschelman     break;
1018a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
10194e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1020a65d3064SKris Buschelman     break;
102128b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
102228b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
102328b2fa4aSMatthew Knepley     break;
1024a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
10254e0d8c25SBarry Smith     a->ignorezeroentries = flg;
10260df259c2SBarry Smith     break;
1027cd6b891eSBarry Smith   case MAT_CHECK_COMPRESSED_ROW:
1028cd6b891eSBarry Smith     a->compressedrow.check = flg;
1029d487561eSHong Zhang     break;
10303d472b54SHong Zhang   case MAT_SPD:
1031b1646e73SJed Brown   case MAT_SYMMETRIC:
1032b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1033b1646e73SJed Brown   case MAT_HERMITIAN:
1034b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
10355021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
10365021d80fSJed Brown     break;
10374e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1038a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1039a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1040290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1041a65d3064SKris Buschelman     break;
1042b87ac2d8SJed Brown   case MAT_USE_INODES:
1043b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1044b87ac2d8SJed Brown     break;
1045a65d3064SKris Buschelman   default:
1046e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1047a65d3064SKris Buschelman   }
10484108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
10493a40ed3dSBarry Smith   PetscFunctionReturn(0);
105017ab2063SBarry Smith }
105117ab2063SBarry Smith 
10524a2ae208SSatish Balay #undef __FUNCT__
10534a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1054dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
105517ab2063SBarry Smith {
1056416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10576849ba73SBarry Smith   PetscErrorCode ierr;
1058d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
105935e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
106017ab2063SBarry Smith 
10613a40ed3dSBarry Smith   PetscFunctionBegin;
1062d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1063e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
106435e7444dSHong Zhang 
1065d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1066d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
106735e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
10682c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
106935e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
107035e7444dSHong Zhang     PetscFunctionReturn(0);
107135e7444dSHong Zhang   }
107235e7444dSHong Zhang 
10732dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
10741ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
107535e7444dSHong Zhang   for (i=0; i<n; i++) {
107635e7444dSHong Zhang     nz = ai[i+1] - ai[i];
10772f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
107835e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++) {
107935e7444dSHong Zhang       if (aj[j] == i) {
108035e7444dSHong Zhang         x[i] = aa[j];
108117ab2063SBarry Smith         break;
108217ab2063SBarry Smith       }
108317ab2063SBarry Smith     }
108417ab2063SBarry Smith   }
10851ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
10863a40ed3dSBarry Smith   PetscFunctionReturn(0);
108717ab2063SBarry Smith }
108817ab2063SBarry Smith 
1089c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10904a2ae208SSatish Balay #undef __FUNCT__
10914a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1092dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
109317ab2063SBarry Smith {
1094416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10955c897100SBarry Smith   PetscScalar    *x,*y;
1096dfbe8321SBarry Smith   PetscErrorCode ierr;
1097d0f46423SBarry Smith   PetscInt       m = A->rmap->n;
10985c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1099a77337e4SBarry Smith   MatScalar         *v;
1100a77337e4SBarry Smith   PetscScalar       alpha;
11010298fd71SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=NULL;
11023447b6efSHong Zhang   Mat_CompressedRow cprow    = a->compressedrow;
1103ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
11045c897100SBarry Smith #endif
110517ab2063SBarry Smith 
11063a40ed3dSBarry Smith   PetscFunctionBegin;
11072e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
11081ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
11091ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
11105c897100SBarry Smith 
11115c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1112bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
11135c897100SBarry Smith #else
11143447b6efSHong Zhang   if (usecprow) {
11153447b6efSHong Zhang     m    = cprow.nrows;
11163447b6efSHong Zhang     ii   = cprow.i;
11177b2bb3b9SHong Zhang     ridx = cprow.rindex;
11183447b6efSHong Zhang   } else {
11193447b6efSHong Zhang     ii = a->i;
11203447b6efSHong Zhang   }
112117ab2063SBarry Smith   for (i=0; i<m; i++) {
11223447b6efSHong Zhang     idx = a->j + ii[i];
11233447b6efSHong Zhang     v   = a->a + ii[i];
11243447b6efSHong Zhang     n   = ii[i+1] - ii[i];
11253447b6efSHong Zhang     if (usecprow) {
11267b2bb3b9SHong Zhang       alpha = x[ridx[i]];
11273447b6efSHong Zhang     } else {
112817ab2063SBarry Smith       alpha = x[i];
11293447b6efSHong Zhang     }
113004fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
113117ab2063SBarry Smith   }
11325c897100SBarry Smith #endif
1133dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
11341ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11351ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11363a40ed3dSBarry Smith   PetscFunctionReturn(0);
113717ab2063SBarry Smith }
113817ab2063SBarry Smith 
11394a2ae208SSatish Balay #undef __FUNCT__
11405c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1141dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
11425c897100SBarry Smith {
1143dfbe8321SBarry Smith   PetscErrorCode ierr;
11445c897100SBarry Smith 
11455c897100SBarry Smith   PetscFunctionBegin;
1146170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
11475c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
11485c897100SBarry Smith   PetscFunctionReturn(0);
11495c897100SBarry Smith }
11505c897100SBarry Smith 
1151c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
115278b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
115378b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ_Kernel(PetscInt thread_id,Mat A,Vec xx,Vec yy)
115478b84d54SShri Abhyankar {
115578b84d54SShri Abhyankar   PetscErrorCode    ierr;
115678b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
115778b84d54SShri Abhyankar   PetscScalar       *y;
115878b84d54SShri Abhyankar   const PetscScalar *x;
115978b84d54SShri Abhyankar   const MatScalar   *aa;
116078b84d54SShri Abhyankar   PetscInt          *trstarts=A->rmap->trstarts;
116178b84d54SShri Abhyankar   PetscInt          n,start,end,i;
116278b84d54SShri Abhyankar   const PetscInt    *aj,*ai;
116378b84d54SShri Abhyankar   PetscScalar       sum;
116478b84d54SShri Abhyankar 
116578b84d54SShri Abhyankar   ierr  = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
116678b84d54SShri Abhyankar   ierr  = VecGetArray(yy,&y);CHKERRQ(ierr);
116778b84d54SShri Abhyankar   start = trstarts[thread_id];
116878b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
116978b84d54SShri Abhyankar   aj    = a->j;
117078b84d54SShri Abhyankar   aa    = a->a;
117178b84d54SShri Abhyankar   ai    = a->i;
117278b84d54SShri Abhyankar   for (i=start; i<end; i++) {
117378b84d54SShri Abhyankar     n   = ai[i+1] - ai[i];
117478b84d54SShri Abhyankar     aj  = a->j + ai[i];
117578b84d54SShri Abhyankar     aa  = a->a + ai[i];
117678b84d54SShri Abhyankar     sum = 0.0;
117778b84d54SShri Abhyankar     PetscSparseDensePlusDot(sum,x,aa,aj,n);
117878b84d54SShri Abhyankar     y[i] = sum;
117978b84d54SShri Abhyankar   }
118078b84d54SShri Abhyankar   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
118178b84d54SShri Abhyankar   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
118278b84d54SShri Abhyankar   return 0;
118378b84d54SShri Abhyankar }
118478b84d54SShri Abhyankar 
118578b84d54SShri Abhyankar #undef __FUNCT__
118678b84d54SShri Abhyankar #define __FUNCT__ "MatMult_SeqAIJ"
118778b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
118878b84d54SShri Abhyankar {
118978b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
119078b84d54SShri Abhyankar   PetscScalar       *y;
119178b84d54SShri Abhyankar   const PetscScalar *x;
119278b84d54SShri Abhyankar   const MatScalar   *aa;
119378b84d54SShri Abhyankar   PetscErrorCode    ierr;
119478b84d54SShri Abhyankar   PetscInt          m=A->rmap->n;
11950298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
119678b84d54SShri Abhyankar   PetscInt          n,i,nonzerorow=0;
119778b84d54SShri Abhyankar   PetscScalar       sum;
119878b84d54SShri Abhyankar   PetscBool         usecprow=a->compressedrow.use;
119978b84d54SShri Abhyankar 
120078b84d54SShri Abhyankar #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
120178b84d54SShri Abhyankar #pragma disjoint(*x,*y,*aa)
120278b84d54SShri Abhyankar #endif
120378b84d54SShri Abhyankar 
120478b84d54SShri Abhyankar   PetscFunctionBegin;
120578b84d54SShri Abhyankar   aj = a->j;
120678b84d54SShri Abhyankar   aa = a->a;
120778b84d54SShri Abhyankar   ii = a->i;
120878b84d54SShri Abhyankar   if (usecprow) { /* use compressed row format */
120978b84d54SShri Abhyankar     ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
121078b84d54SShri Abhyankar     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
121178b84d54SShri Abhyankar     m    = a->compressedrow.nrows;
121278b84d54SShri Abhyankar     ii   = a->compressedrow.i;
121378b84d54SShri Abhyankar     ridx = a->compressedrow.rindex;
121478b84d54SShri Abhyankar     for (i=0; i<m; i++) {
121578b84d54SShri Abhyankar       n           = ii[i+1] - ii[i];
121678b84d54SShri Abhyankar       aj          = a->j + ii[i];
121778b84d54SShri Abhyankar       aa          = a->a + ii[i];
121878b84d54SShri Abhyankar       sum         = 0.0;
121978b84d54SShri Abhyankar       nonzerorow += (n>0);
122078b84d54SShri Abhyankar       PetscSparseDensePlusDot(sum,x,aa,aj,n);
122178b84d54SShri Abhyankar       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
122278b84d54SShri Abhyankar       y[*ridx++] = sum;
122378b84d54SShri Abhyankar     }
122478b84d54SShri Abhyankar     ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
122578b84d54SShri Abhyankar     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
122678b84d54SShri Abhyankar   } else { /* do not use compressed row format */
122778b84d54SShri Abhyankar #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
122878b84d54SShri Abhyankar     fortranmultaij_(&m,x,ii,aj,aa,y);
122978b84d54SShri Abhyankar #else
1230ce94432eSBarry Smith     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
123178b84d54SShri Abhyankar #endif
123278b84d54SShri Abhyankar   }
123378b84d54SShri Abhyankar   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
123478b84d54SShri Abhyankar   PetscFunctionReturn(0);
123578b84d54SShri Abhyankar }
123678b84d54SShri Abhyankar #else
12375c897100SBarry Smith #undef __FUNCT__
12384a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1239dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
124017ab2063SBarry Smith {
1241416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1242d9fead3dSBarry Smith   PetscScalar       *y;
124354f21887SBarry Smith   const PetscScalar *x;
124454f21887SBarry Smith   const MatScalar   *aa;
1245dfbe8321SBarry Smith   PetscErrorCode    ierr;
1246003131ecSBarry Smith   PetscInt          m=A->rmap->n;
12470298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
12488aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1249362ced78SSatish Balay   PetscScalar       sum;
1250ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
125117ab2063SBarry Smith 
1252b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
125397952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1254fee21e36SBarry Smith #endif
1255fee21e36SBarry Smith 
12563a40ed3dSBarry Smith   PetscFunctionBegin;
12573649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12581ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
125997952fefSHong Zhang   aj   = a->j;
126097952fefSHong Zhang   aa   = a->a;
1261416022c9SBarry Smith   ii   = a->i;
12624eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
126397952fefSHong Zhang     m    = a->compressedrow.nrows;
126497952fefSHong Zhang     ii   = a->compressedrow.i;
126597952fefSHong Zhang     ridx = a->compressedrow.rindex;
126697952fefSHong Zhang     for (i=0; i<m; i++) {
126797952fefSHong Zhang       n           = ii[i+1] - ii[i];
126897952fefSHong Zhang       aj          = a->j + ii[i];
126997952fefSHong Zhang       aa          = a->a + ii[i];
127097952fefSHong Zhang       sum         = 0.0;
1271a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1272003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1273003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
127497952fefSHong Zhang       y[*ridx++] = sum;
127597952fefSHong Zhang     }
127697952fefSHong Zhang   } else { /* do not use compressed row format */
1277b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1278b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1279b05257ddSBarry Smith #else
128078b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
1281ce94432eSBarry Smith     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
128278b84d54SShri Abhyankar #else
128317ab2063SBarry Smith     for (i=0; i<m; i++) {
1284003131ecSBarry Smith       n           = ii[i+1] - ii[i];
1285003131ecSBarry Smith       aj          = a->j + ii[i];
1286003131ecSBarry Smith       aa          = a->a + ii[i];
128717ab2063SBarry Smith       sum         = 0.0;
1288a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1289003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
129017ab2063SBarry Smith       y[i] = sum;
129117ab2063SBarry Smith     }
12928d195f9aSBarry Smith #endif
129378b84d54SShri Abhyankar #endif
1294b05257ddSBarry Smith   }
1295dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
12963649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12971ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12983a40ed3dSBarry Smith   PetscFunctionReturn(0);
129917ab2063SBarry Smith }
130078b84d54SShri Abhyankar #endif
130117ab2063SBarry Smith 
1302b434eb95SMatthew G. Knepley #undef __FUNCT__
1303b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultMax_SeqAIJ"
1304b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1305b434eb95SMatthew G. Knepley {
1306b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1307b434eb95SMatthew G. Knepley   PetscScalar       *y;
1308b434eb95SMatthew G. Knepley   const PetscScalar *x;
1309b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1310b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1311b434eb95SMatthew G. Knepley   PetscInt          m=A->rmap->n;
1312b434eb95SMatthew G. Knepley   const PetscInt    *aj,*ii,*ridx=NULL;
1313b434eb95SMatthew G. Knepley   PetscInt          n,i,nonzerorow=0;
1314b434eb95SMatthew G. Knepley   PetscScalar       sum;
1315b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1316b434eb95SMatthew G. Knepley 
1317b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1318b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa)
1319b434eb95SMatthew G. Knepley #endif
1320b434eb95SMatthew G. Knepley 
1321b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1322b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1323b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1324b434eb95SMatthew G. Knepley   aj   = a->j;
1325b434eb95SMatthew G. Knepley   aa   = a->a;
1326b434eb95SMatthew G. Knepley   ii   = a->i;
1327b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1328b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1329b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1330b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1331b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1332b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1333b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1334b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1335b434eb95SMatthew G. Knepley       sum         = 0.0;
1336b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1337b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1338b434eb95SMatthew G. Knepley       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1339b434eb95SMatthew G. Knepley       y[*ridx++] = sum;
1340b434eb95SMatthew G. Knepley     }
1341b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
1342b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1343b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1344b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1345b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1346b434eb95SMatthew G. Knepley       sum         = 0.0;
1347b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1348b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1349b434eb95SMatthew G. Knepley       y[i] = sum;
1350b434eb95SMatthew G. Knepley     }
1351b434eb95SMatthew G. Knepley   }
1352b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1353b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1354b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1355b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1356b434eb95SMatthew G. Knepley }
1357b434eb95SMatthew G. Knepley 
1358b434eb95SMatthew G. Knepley #undef __FUNCT__
1359b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultAddMax_SeqAIJ"
1360b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1361b434eb95SMatthew G. Knepley {
1362b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1363b434eb95SMatthew G. Knepley   PetscScalar       *y,*z;
1364b434eb95SMatthew G. Knepley   const PetscScalar *x;
1365b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1366b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1367b434eb95SMatthew G. Knepley   PetscInt          m = A->rmap->n,*aj,*ii;
1368b434eb95SMatthew G. Knepley   PetscInt          n,i,*ridx=NULL;
1369b434eb95SMatthew G. Knepley   PetscScalar       sum;
1370b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1371b434eb95SMatthew G. Knepley 
1372b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1373b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1374b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1375b434eb95SMatthew G. Knepley   if (zz != yy) {
1376b434eb95SMatthew G. Knepley     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
1377b434eb95SMatthew G. Knepley   } else {
1378b434eb95SMatthew G. Knepley     z = y;
1379b434eb95SMatthew G. Knepley   }
1380b434eb95SMatthew G. Knepley 
1381b434eb95SMatthew G. Knepley   aj = a->j;
1382b434eb95SMatthew G. Knepley   aa = a->a;
1383b434eb95SMatthew G. Knepley   ii = a->i;
1384b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1385b434eb95SMatthew G. Knepley     if (zz != yy) {
1386b434eb95SMatthew G. Knepley       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1387b434eb95SMatthew G. Knepley     }
1388b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1389b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1390b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1391b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1392b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1393b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1394b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1395b434eb95SMatthew G. Knepley       sum = y[*ridx];
1396b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1397b434eb95SMatthew G. Knepley       z[*ridx++] = sum;
1398b434eb95SMatthew G. Knepley     }
1399b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
1400b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1401b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1402b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1403b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1404b434eb95SMatthew G. Knepley       sum = y[i];
1405b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1406b434eb95SMatthew G. Knepley       z[i] = sum;
1407b434eb95SMatthew G. Knepley     }
1408b434eb95SMatthew G. Knepley   }
1409b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1410b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1411b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1412b434eb95SMatthew G. Knepley   if (zz != yy) {
1413b434eb95SMatthew G. Knepley     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
1414b434eb95SMatthew G. Knepley   }
1415b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1416b434eb95SMatthew G. Knepley }
1417b434eb95SMatthew G. Knepley 
1418c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
14194a2ae208SSatish Balay #undef __FUNCT__
14204a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1421dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
142217ab2063SBarry Smith {
1423416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1424f15663dcSBarry Smith   PetscScalar       *y,*z;
1425f15663dcSBarry Smith   const PetscScalar *x;
142654f21887SBarry Smith   const MatScalar   *aa;
1427dfbe8321SBarry Smith   PetscErrorCode    ierr;
1428d0f46423SBarry Smith   PetscInt          m = A->rmap->n,*aj,*ii;
14290298fd71SBarry Smith   PetscInt          n,i,*ridx=NULL;
1430362ced78SSatish Balay   PetscScalar       sum;
1431ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
14329ea0dfa2SSatish Balay 
14333a40ed3dSBarry Smith   PetscFunctionBegin;
1434f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
14351ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
14362e8a6d31SBarry Smith   if (zz != yy) {
14371ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
14382e8a6d31SBarry Smith   } else {
14392e8a6d31SBarry Smith     z = y;
14402e8a6d31SBarry Smith   }
1441bfeeae90SHong Zhang 
144297952fefSHong Zhang   aj = a->j;
144397952fefSHong Zhang   aa = a->a;
1444cddf8d76SBarry Smith   ii = a->i;
14454eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
14464eb6d288SHong Zhang     if (zz != yy) {
14474eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
14484eb6d288SHong Zhang     }
144997952fefSHong Zhang     m    = a->compressedrow.nrows;
145097952fefSHong Zhang     ii   = a->compressedrow.i;
145197952fefSHong Zhang     ridx = a->compressedrow.rindex;
145297952fefSHong Zhang     for (i=0; i<m; i++) {
145397952fefSHong Zhang       n   = ii[i+1] - ii[i];
145497952fefSHong Zhang       aj  = a->j + ii[i];
145597952fefSHong Zhang       aa  = a->a + ii[i];
145697952fefSHong Zhang       sum = y[*ridx];
1457f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
145897952fefSHong Zhang       z[*ridx++] = sum;
145997952fefSHong Zhang     }
146097952fefSHong Zhang   } else { /* do not use compressed row format */
1461f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1462f15663dcSBarry Smith     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1463f15663dcSBarry Smith #else
146417ab2063SBarry Smith     for (i=0; i<m; i++) {
1465f15663dcSBarry Smith       n   = ii[i+1] - ii[i];
1466f15663dcSBarry Smith       aj  = a->j + ii[i];
1467f15663dcSBarry Smith       aa  = a->a + ii[i];
146817ab2063SBarry Smith       sum = y[i];
1469f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
147017ab2063SBarry Smith       z[i] = sum;
147117ab2063SBarry Smith     }
147202ab625aSSatish Balay #endif
1473f15663dcSBarry Smith   }
1474dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1475f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
14761ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
14772e8a6d31SBarry Smith   if (zz != yy) {
14781ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
14792e8a6d31SBarry Smith   }
14808154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
14816b375ea7SVictor Minden   /*
1482918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1483918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1484918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
14856b375ea7SVictor Minden   */
1486918e98c3SVictor Minden #endif
14873a40ed3dSBarry Smith   PetscFunctionReturn(0);
148817ab2063SBarry Smith }
148917ab2063SBarry Smith 
149017ab2063SBarry Smith /*
149117ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
149217ab2063SBarry Smith */
14934a2ae208SSatish Balay #undef __FUNCT__
14944a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1495dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
149617ab2063SBarry Smith {
1497416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
14986849ba73SBarry Smith   PetscErrorCode ierr;
1499d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
150017ab2063SBarry Smith 
15013a40ed3dSBarry Smith   PetscFunctionBegin;
150209f38230SBarry Smith   if (!a->diag) {
150309f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
15043bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr);
150509f38230SBarry Smith   }
1506d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
150709f38230SBarry Smith     a->diag[i] = a->i[i+1];
1508bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1509bfeeae90SHong Zhang       if (a->j[j] == i) {
151009f38230SBarry Smith         a->diag[i] = j;
151117ab2063SBarry Smith         break;
151217ab2063SBarry Smith       }
151317ab2063SBarry Smith     }
151417ab2063SBarry Smith   }
15153a40ed3dSBarry Smith   PetscFunctionReturn(0);
151617ab2063SBarry Smith }
151717ab2063SBarry Smith 
1518be5855fcSBarry Smith /*
1519be5855fcSBarry Smith      Checks for missing diagonals
1520be5855fcSBarry Smith */
15214a2ae208SSatish Balay #undef __FUNCT__
15224a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1523ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1524be5855fcSBarry Smith {
1525be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
152697f1f81fSBarry Smith   PetscInt   *diag,*jj = a->j,i;
1527be5855fcSBarry Smith 
1528be5855fcSBarry Smith   PetscFunctionBegin;
152909f38230SBarry Smith   *missing = PETSC_FALSE;
1530d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
153109f38230SBarry Smith     *missing = PETSC_TRUE;
153209f38230SBarry Smith     if (d) *d = 0;
1533358d2f5dSShri Abhyankar     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
153409f38230SBarry Smith   } else {
1535f1e2ffcdSBarry Smith     diag = a->diag;
1536d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1537bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
153809f38230SBarry Smith         *missing = PETSC_TRUE;
153909f38230SBarry Smith         if (d) *d = i;
154009f38230SBarry Smith         PetscInfo1(A,"Matrix is missing diagonal number %D",i);
1541358d2f5dSShri Abhyankar         break;
154209f38230SBarry Smith       }
1543be5855fcSBarry Smith     }
1544be5855fcSBarry Smith   }
1545be5855fcSBarry Smith   PetscFunctionReturn(0);
1546be5855fcSBarry Smith }
1547be5855fcSBarry Smith 
154871f1c65dSBarry Smith #undef __FUNCT__
154971f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
15507087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
155171f1c65dSBarry Smith {
155271f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
155371f1c65dSBarry Smith   PetscErrorCode ierr;
1554d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
155554f21887SBarry Smith   MatScalar      *v = a->a;
155654f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
155771f1c65dSBarry Smith 
155871f1c65dSBarry Smith   PetscFunctionBegin;
155971f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
156071f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
156171f1c65dSBarry Smith   diag = a->diag;
156271f1c65dSBarry Smith   if (!a->idiag) {
156371f1c65dSBarry Smith     ierr = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
15643bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
156571f1c65dSBarry Smith     v    = a->a;
156671f1c65dSBarry Smith   }
156771f1c65dSBarry Smith   mdiag = a->mdiag;
156871f1c65dSBarry Smith   idiag = a->idiag;
156971f1c65dSBarry Smith 
1570028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
157171f1c65dSBarry Smith     for (i=0; i<m; i++) {
157271f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1573e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
157471f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
157571f1c65dSBarry Smith     }
157671f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
157771f1c65dSBarry Smith   } else {
157871f1c65dSBarry Smith     for (i=0; i<m; i++) {
157971f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
158071f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
158171f1c65dSBarry Smith     }
1582dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
158371f1c65dSBarry Smith   }
158471f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
158571f1c65dSBarry Smith   PetscFunctionReturn(0);
158671f1c65dSBarry Smith }
158771f1c65dSBarry Smith 
1588c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
15894a2ae208SSatish Balay #undef __FUNCT__
159041f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
159141f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
159217ab2063SBarry Smith {
1593416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1594e6d1f457SBarry Smith   PetscScalar       *x,d,sum,*t,scale;
1595e6d1f457SBarry Smith   const MatScalar   *v = a->a,*idiag=0,*mdiag;
159654f21887SBarry Smith   const PetscScalar *b, *bs,*xb, *ts;
1597dfbe8321SBarry Smith   PetscErrorCode    ierr;
1598d0f46423SBarry Smith   PetscInt          n = A->cmap->n,m = A->rmap->n,i;
159997f1f81fSBarry Smith   const PetscInt    *idx,*diag;
160017ab2063SBarry Smith 
16013a40ed3dSBarry Smith   PetscFunctionBegin;
1602b965ef7fSBarry Smith   its = its*lits;
160391723122SBarry Smith 
160471f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
160571f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
160671f1c65dSBarry Smith   a->fshift = fshift;
160771f1c65dSBarry Smith   a->omega  = omega;
1608ed480e8bSBarry Smith 
160971f1c65dSBarry Smith   diag  = a->diag;
161071f1c65dSBarry Smith   t     = a->ssor_work;
1611ed480e8bSBarry Smith   idiag = a->idiag;
161271f1c65dSBarry Smith   mdiag = a->mdiag;
1613ed480e8bSBarry Smith 
16141ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
16153649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1616ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
161717ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
161817ab2063SBarry Smith     /* apply (U + D/omega) to the vector */
1619ed480e8bSBarry Smith     bs = b;
162017ab2063SBarry Smith     for (i=0; i<m; i++) {
162171f1c65dSBarry Smith       d   = fshift + mdiag[i];
1622416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1623ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1624ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
162517ab2063SBarry Smith       sum = b[i]*d/omega;
1626003131ecSBarry Smith       PetscSparseDensePlusDot(sum,bs,v,idx,n);
162717ab2063SBarry Smith       x[i] = sum;
162817ab2063SBarry Smith     }
16291ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16303649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1631efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
16323a40ed3dSBarry Smith     PetscFunctionReturn(0);
163317ab2063SBarry Smith   }
1634c783ea89SBarry Smith 
16352205254eSKarl Rupp   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
16362205254eSKarl Rupp   else if (flag & SOR_EISENSTAT) {
163717ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1638887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
163917ab2063SBarry Smith 
164017ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
164117ab2063SBarry Smith 
1642887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
164317ab2063SBarry Smith     */
164417ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
164517ab2063SBarry Smith 
164617ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
164717ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1648416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1649ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1650ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
165117ab2063SBarry Smith       sum = b[i];
1652e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1653ed480e8bSBarry Smith       x[i] = sum*idiag[i];
165417ab2063SBarry Smith     }
165517ab2063SBarry Smith 
165617ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1657416022c9SBarry Smith     v = a->a;
16582205254eSKarl Rupp     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
165917ab2063SBarry Smith 
166017ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1661ed480e8bSBarry Smith     ts   = t;
1662416022c9SBarry Smith     diag = a->diag;
166317ab2063SBarry Smith     for (i=0; i<m; i++) {
1664416022c9SBarry Smith       n   = diag[i] - a->i[i];
1665ed480e8bSBarry Smith       idx = a->j + a->i[i];
1666ed480e8bSBarry Smith       v   = a->a + a->i[i];
166717ab2063SBarry Smith       sum = t[i];
1668003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1669ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1670733d66baSBarry Smith       /*  x = x + t */
1671733d66baSBarry Smith       x[i] += t[i];
167217ab2063SBarry Smith     }
167317ab2063SBarry Smith 
1674dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
16751ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16763649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
16773a40ed3dSBarry Smith     PetscFunctionReturn(0);
167817ab2063SBarry Smith   }
167917ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
168017ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
168117ab2063SBarry Smith       for (i=0; i<m; i++) {
1682416022c9SBarry Smith         n   = diag[i] - a->i[i];
1683ed480e8bSBarry Smith         idx = a->j + a->i[i];
1684ed480e8bSBarry Smith         v   = a->a + a->i[i];
168517ab2063SBarry Smith         sum = b[i];
1686e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
16875c99c7daSBarry Smith         t[i] = sum;
1688ed480e8bSBarry Smith         x[i] = sum*idiag[i];
168917ab2063SBarry Smith       }
16905c99c7daSBarry Smith       xb   = t;
1691efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
16923a40ed3dSBarry Smith     } else xb = b;
169317ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
169417ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1695416022c9SBarry Smith         n   = a->i[i+1] - diag[i] - 1;
1696ed480e8bSBarry Smith         idx = a->j + diag[i] + 1;
1697ed480e8bSBarry Smith         v   = a->a + diag[i] + 1;
169817ab2063SBarry Smith         sum = xb[i];
1699e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
17005c99c7daSBarry Smith         if (xb == b) {
1701ed480e8bSBarry Smith           x[i] = sum*idiag[i];
17025c99c7daSBarry Smith         } else {
1703*b19a5dc2SMark Adams           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
170417ab2063SBarry Smith         }
17055c99c7daSBarry Smith       }
1706*b19a5dc2SMark Adams       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
170717ab2063SBarry Smith     }
170817ab2063SBarry Smith     its--;
170917ab2063SBarry Smith   }
171017ab2063SBarry Smith   while (its--) {
171117ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
171217ab2063SBarry Smith       for (i=0; i<m; i++) {
1713*b19a5dc2SMark Adams         /* lower */
1714*b19a5dc2SMark Adams         n   = diag[i] - a->i[i];
1715ed480e8bSBarry Smith         idx = a->j + a->i[i];
1716ed480e8bSBarry Smith         v   = a->a + a->i[i];
171717ab2063SBarry Smith         sum = b[i];
1718e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1719*b19a5dc2SMark Adams         t[i] = sum;             /* save application of the lower-triangular part */
1720*b19a5dc2SMark Adams         /* upper */
1721*b19a5dc2SMark Adams         n   = a->i[i+1] - diag[i] - 1;
1722*b19a5dc2SMark Adams         idx = a->j + diag[i] + 1;
1723*b19a5dc2SMark Adams         v   = a->a + diag[i] + 1;
1724*b19a5dc2SMark Adams         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1725*b19a5dc2SMark Adams         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
172617ab2063SBarry Smith       }
1727*b19a5dc2SMark Adams       xb   = t;
17289f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1729*b19a5dc2SMark Adams     } else xb = b;
173017ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
173117ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1732*b19a5dc2SMark Adams         sum = xb[i];
1733*b19a5dc2SMark Adams         if (xb == b) {
1734*b19a5dc2SMark Adams           /* whole matrix (no checkpointing available) */
1735416022c9SBarry Smith           n   = a->i[i+1] - a->i[i];
1736ed480e8bSBarry Smith           idx = a->j + a->i[i];
1737ed480e8bSBarry Smith           v   = a->a + a->i[i];
1738e6d1f457SBarry Smith           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1739ed480e8bSBarry Smith           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1740*b19a5dc2SMark Adams         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1741*b19a5dc2SMark Adams           n   = a->i[i+1] - diag[i] - 1;
1742*b19a5dc2SMark Adams           idx = a->j + diag[i] + 1;
1743*b19a5dc2SMark Adams           v   = a->a + diag[i] + 1;
1744*b19a5dc2SMark Adams           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1745*b19a5dc2SMark Adams           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
174617ab2063SBarry Smith         }
1747*b19a5dc2SMark Adams       }
1748*b19a5dc2SMark Adams       if (xb == b) {
17499f863219SBarry Smith         ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1750*b19a5dc2SMark Adams       } else {
1751*b19a5dc2SMark Adams         ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1752*b19a5dc2SMark Adams       }
175317ab2063SBarry Smith     }
175417ab2063SBarry Smith   }
17551ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
17563649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1757365a8a9eSBarry Smith   PetscFunctionReturn(0);
175817ab2063SBarry Smith }
175917ab2063SBarry Smith 
17602af78befSBarry Smith 
17614a2ae208SSatish Balay #undef __FUNCT__
17624a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1763dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
176417ab2063SBarry Smith {
1765416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
17664e220ebcSLois Curfman McInnes 
17673a40ed3dSBarry Smith   PetscFunctionBegin;
17684e220ebcSLois Curfman McInnes   info->block_size   = 1.0;
17694e220ebcSLois Curfman McInnes   info->nz_allocated = (double)a->maxnz;
17704e220ebcSLois Curfman McInnes   info->nz_used      = (double)a->nz;
17714e220ebcSLois Curfman McInnes   info->nz_unneeded  = (double)(a->maxnz - a->nz);
17724e220ebcSLois Curfman McInnes   info->assemblies   = (double)A->num_ass;
17738e58a170SBarry Smith   info->mallocs      = (double)A->info.mallocs;
17747adad957SLisandro Dalcin   info->memory       = ((PetscObject)A)->mem;
1775d5f3da31SBarry Smith   if (A->factortype) {
17764e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
17774e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
17784e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
17794e220ebcSLois Curfman McInnes   } else {
17804e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
17814e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
17824e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
17834e220ebcSLois Curfman McInnes   }
17843a40ed3dSBarry Smith   PetscFunctionReturn(0);
178517ab2063SBarry Smith }
178617ab2063SBarry Smith 
17874a2ae208SSatish Balay #undef __FUNCT__
17884a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
17892b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
179017ab2063SBarry Smith {
1791416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
17923b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
17936849ba73SBarry Smith   PetscErrorCode    ierr;
179497b48c8fSBarry Smith   const PetscScalar *xx;
179597b48c8fSBarry Smith   PetscScalar       *bb;
1796ace3abfcSBarry Smith   PetscBool         missing;
179717ab2063SBarry Smith 
17983a40ed3dSBarry Smith   PetscFunctionBegin;
179997b48c8fSBarry Smith   if (x && b) {
180097b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
180197b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
180297b48c8fSBarry Smith     for (i=0; i<N; i++) {
180397b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
180497b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
180597b48c8fSBarry Smith     }
180697b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
180797b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
180897b48c8fSBarry Smith   }
180997b48c8fSBarry Smith 
1810a9817697SBarry Smith   if (a->keepnonzeropattern) {
1811f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1812e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1813bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1814f1e2ffcdSBarry Smith     }
1815f4df32b1SMatthew Knepley     if (diag != 0.0) {
181609f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1817e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1818f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1819f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1820f1e2ffcdSBarry Smith       }
1821f1e2ffcdSBarry Smith     }
182288e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1823f1e2ffcdSBarry Smith   } else {
1824f4df32b1SMatthew Knepley     if (diag != 0.0) {
182517ab2063SBarry Smith       for (i=0; i<N; i++) {
1826e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
18277ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1828416022c9SBarry Smith           a->ilen[rows[i]]    = 1;
1829f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1830bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
18317ae801bdSBarry Smith         } else { /* in case row was completely empty */
1832f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
183317ab2063SBarry Smith         }
183417ab2063SBarry Smith       }
18353a40ed3dSBarry Smith     } else {
183617ab2063SBarry Smith       for (i=0; i<N; i++) {
1837e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1838416022c9SBarry Smith         a->ilen[rows[i]] = 0;
183917ab2063SBarry Smith       }
184017ab2063SBarry Smith     }
184188e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1842f1e2ffcdSBarry Smith   }
184343a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18443a40ed3dSBarry Smith   PetscFunctionReturn(0);
184517ab2063SBarry Smith }
184617ab2063SBarry Smith 
18474a2ae208SSatish Balay #undef __FUNCT__
18486e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
18496e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
18506e169961SBarry Smith {
18516e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
18526e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
18536e169961SBarry Smith   PetscErrorCode    ierr;
18542b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
18556e169961SBarry Smith   const PetscScalar *xx;
18566e169961SBarry Smith   PetscScalar       *bb;
18576e169961SBarry Smith 
18586e169961SBarry Smith   PetscFunctionBegin;
18596e169961SBarry Smith   if (x && b) {
18606e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
18616e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
18622b40b63fSBarry Smith     vecs = PETSC_TRUE;
18636e169961SBarry Smith   }
18646e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
18656e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
18666e169961SBarry Smith   for (i=0; i<N; i++) {
18676e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
18686e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
18692205254eSKarl Rupp 
18706e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
18716e169961SBarry Smith   }
18726e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
18736e169961SBarry Smith     if (!zeroed[i]) {
18746e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
18756e169961SBarry Smith         if (zeroed[a->j[j]]) {
18762b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
18776e169961SBarry Smith           a->a[j] = 0.0;
18786e169961SBarry Smith         }
18796e169961SBarry Smith       }
18802b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
18816e169961SBarry Smith   }
18826e169961SBarry Smith   if (x && b) {
18836e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
18846e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
18856e169961SBarry Smith   }
18866e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
18876e169961SBarry Smith   if (diag != 0.0) {
18886e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
18896e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
18906e169961SBarry Smith     for (i=0; i<N; i++) {
18916e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
18926e169961SBarry Smith     }
18936e169961SBarry Smith   }
18946e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
18956e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18966e169961SBarry Smith   PetscFunctionReturn(0);
18976e169961SBarry Smith }
18986e169961SBarry Smith 
18996e169961SBarry Smith #undef __FUNCT__
19004a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1901a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
190217ab2063SBarry Smith {
1903416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
190497f1f81fSBarry Smith   PetscInt   *itmp;
190517ab2063SBarry Smith 
19063a40ed3dSBarry Smith   PetscFunctionBegin;
1907e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
190817ab2063SBarry Smith 
1909416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1910bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
191117ab2063SBarry Smith   if (idx) {
1912bfeeae90SHong Zhang     itmp = a->j + a->i[row];
191326fbe8dcSKarl Rupp     if (*nz) *idx = itmp;
191417ab2063SBarry Smith     else *idx = 0;
191517ab2063SBarry Smith   }
19163a40ed3dSBarry Smith   PetscFunctionReturn(0);
191717ab2063SBarry Smith }
191817ab2063SBarry Smith 
1919bfeeae90SHong Zhang /* remove this function? */
19204a2ae208SSatish Balay #undef __FUNCT__
19214a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1922a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
192317ab2063SBarry Smith {
19243a40ed3dSBarry Smith   PetscFunctionBegin;
19253a40ed3dSBarry Smith   PetscFunctionReturn(0);
192617ab2063SBarry Smith }
192717ab2063SBarry Smith 
19284a2ae208SSatish Balay #undef __FUNCT__
19294a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1930dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
193117ab2063SBarry Smith {
1932416022c9SBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
193354f21887SBarry Smith   MatScalar      *v  = a->a;
193436db0b34SBarry Smith   PetscReal      sum = 0.0;
19356849ba73SBarry Smith   PetscErrorCode ierr;
193697f1f81fSBarry Smith   PetscInt       i,j;
193717ab2063SBarry Smith 
19383a40ed3dSBarry Smith   PetscFunctionBegin;
193917ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1940416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
194136db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
194217ab2063SBarry Smith     }
19438f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
19443a40ed3dSBarry Smith   } else if (type == NORM_1) {
194536db0b34SBarry Smith     PetscReal *tmp;
194697f1f81fSBarry Smith     PetscInt  *jj = a->j;
1947d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1948d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1949064f8208SBarry Smith     *nrm = 0.0;
1950416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1951bfeeae90SHong Zhang       tmp[*jj++] += PetscAbsScalar(*v);  v++;
195217ab2063SBarry Smith     }
1953d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1954064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
195517ab2063SBarry Smith     }
1956606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
19573a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1958064f8208SBarry Smith     *nrm = 0.0;
1959d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1960bfeeae90SHong Zhang       v   = a->a + a->i[j];
196117ab2063SBarry Smith       sum = 0.0;
1962416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1963cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
196417ab2063SBarry Smith       }
1965064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
196617ab2063SBarry Smith     }
1967f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
19683a40ed3dSBarry Smith   PetscFunctionReturn(0);
196917ab2063SBarry Smith }
197017ab2063SBarry Smith 
19714e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
19724e938277SHong Zhang #undef __FUNCT__
19734e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
19744e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
19754e938277SHong Zhang {
19764e938277SHong Zhang   PetscErrorCode ierr;
19774e938277SHong Zhang   PetscInt       i,j,anzj;
19784e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
19794e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
19804e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
19814e938277SHong Zhang 
19824e938277SHong Zhang   PetscFunctionBegin;
19834e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
19844e938277SHong Zhang   ierr = PetscMalloc((an+1)*sizeof(PetscInt),&ati);CHKERRQ(ierr);
19854e938277SHong Zhang   ierr = PetscMalloc(ai[am]*sizeof(PetscInt),&atj);CHKERRQ(ierr);
19864e938277SHong Zhang   ierr = PetscMalloc(an*sizeof(PetscInt),&atfill);CHKERRQ(ierr);
19874e938277SHong Zhang   ierr = PetscMemzero(ati,(an+1)*sizeof(PetscInt));CHKERRQ(ierr);
19884e938277SHong Zhang 
19894e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
19904e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
199126fbe8dcSKarl Rupp   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
19924e938277SHong Zhang   /* Form ati for csr format of A^T. */
199326fbe8dcSKarl Rupp   for (i=0;i<an;i++) ati[i+1] += ati[i];
19944e938277SHong Zhang 
19954e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
19964e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
19974e938277SHong Zhang 
19984e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
19994e938277SHong Zhang   for (i=0;i<am;i++) {
20004e938277SHong Zhang     anzj = ai[i+1] - ai[i];
20014e938277SHong Zhang     for (j=0;j<anzj;j++) {
20024e938277SHong Zhang       atj[atfill[*aj]] = i;
20034e938277SHong Zhang       atfill[*aj++]   += 1;
20044e938277SHong Zhang     }
20054e938277SHong Zhang   }
20064e938277SHong Zhang 
20074e938277SHong Zhang   /* Clean up temporary space and complete requests. */
20084e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
2009ce94432eSBarry Smith   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
20102205254eSKarl Rupp 
2011a2f3521dSMark F. Adams   (*B)->rmap->bs = A->cmap->bs;
2012a2f3521dSMark F. Adams   (*B)->cmap->bs = A->rmap->bs;
2013a2f3521dSMark F. Adams 
20144e938277SHong Zhang   b          = (Mat_SeqAIJ*)((*B)->data);
20154e938277SHong Zhang   b->free_a  = PETSC_FALSE;
20164e938277SHong Zhang   b->free_ij = PETSC_TRUE;
20174e938277SHong Zhang   b->nonew   = 0;
20184e938277SHong Zhang   PetscFunctionReturn(0);
20194e938277SHong Zhang }
20204e938277SHong Zhang 
20214a2ae208SSatish Balay #undef __FUNCT__
20224a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
2023fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
202417ab2063SBarry Smith {
2025416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2026416022c9SBarry Smith   Mat            C;
20276849ba73SBarry Smith   PetscErrorCode ierr;
2028d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
202954f21887SBarry Smith   MatScalar      *array = a->a;
203017ab2063SBarry Smith 
20313a40ed3dSBarry Smith   PetscFunctionBegin;
2032e32f2f54SBarry 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");
2033fc4dec0aSBarry Smith 
2034fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
2035d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
2036d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
2037bfeeae90SHong Zhang 
2038bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
2039ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2040d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
2041a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(C,A->cmap->bs,A->rmap->bs);CHKERRQ(ierr);
20427adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2043ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
2044606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
2045a541d17aSBarry Smith   } else {
2046a541d17aSBarry Smith     C = *B;
2047a541d17aSBarry Smith   }
2048a541d17aSBarry Smith 
204917ab2063SBarry Smith   for (i=0; i<m; i++) {
205017ab2063SBarry Smith     len    = ai[i+1]-ai[i];
205187d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
2052b9b97703SBarry Smith     array += len;
2053b9b97703SBarry Smith     aj    += len;
205417ab2063SBarry Smith   }
20556d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20566d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
205717ab2063SBarry Smith 
2058815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
2059416022c9SBarry Smith     *B = C;
206017ab2063SBarry Smith   } else {
2061eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
206217ab2063SBarry Smith   }
20633a40ed3dSBarry Smith   PetscFunctionReturn(0);
206417ab2063SBarry Smith }
206517ab2063SBarry Smith 
2066cd0d46ebSvictorle #undef __FUNCT__
20675fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
20687087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2069cd0d46ebSvictorle {
2070cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
207154f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
207254f21887SBarry Smith   MatScalar      *va,*vb;
20736849ba73SBarry Smith   PetscErrorCode ierr;
207497f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
2075cd0d46ebSvictorle 
2076cd0d46ebSvictorle   PetscFunctionBegin;
2077cd0d46ebSvictorle   bij = (Mat_SeqAIJ*) B->data;
2078cd0d46ebSvictorle 
2079cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2080cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
20815485867bSBarry Smith   if (ma!=nb || na!=mb) {
20825485867bSBarry Smith     *f = PETSC_FALSE;
20835485867bSBarry Smith     PetscFunctionReturn(0);
20845485867bSBarry Smith   }
2085cd0d46ebSvictorle   aii  = aij->i; bii = bij->i;
2086cd0d46ebSvictorle   adx  = aij->j; bdx = bij->j;
2087cd0d46ebSvictorle   va   = aij->a; vb = bij->a;
208897f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
208997f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
2090cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
2091cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
2092cd0d46ebSvictorle 
2093cd0d46ebSvictorle   *f = PETSC_TRUE;
2094cd0d46ebSvictorle   for (i=0; i<ma; i++) {
2095cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
209697f1f81fSBarry Smith       PetscInt    idc,idr;
20975485867bSBarry Smith       PetscScalar vc,vr;
2098cd0d46ebSvictorle       /* column/row index/value */
20995485867bSBarry Smith       idc = adx[aptr[i]];
21005485867bSBarry Smith       idr = bdx[bptr[idc]];
21015485867bSBarry Smith       vc  = va[aptr[i]];
21025485867bSBarry Smith       vr  = vb[bptr[idc]];
21035485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
21045485867bSBarry Smith         *f = PETSC_FALSE;
21055485867bSBarry Smith         goto done;
2106cd0d46ebSvictorle       } else {
21075485867bSBarry Smith         aptr[i]++;
21085485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
2109cd0d46ebSvictorle       }
2110cd0d46ebSvictorle     }
2111cd0d46ebSvictorle   }
2112cd0d46ebSvictorle done:
2113cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
21143aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
2115cd0d46ebSvictorle   PetscFunctionReturn(0);
2116cd0d46ebSvictorle }
2117cd0d46ebSvictorle 
21181cbb95d3SBarry Smith #undef __FUNCT__
21191cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
21207087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
21211cbb95d3SBarry Smith {
21221cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
212354f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
212454f21887SBarry Smith   MatScalar      *va,*vb;
21251cbb95d3SBarry Smith   PetscErrorCode ierr;
21261cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
21271cbb95d3SBarry Smith 
21281cbb95d3SBarry Smith   PetscFunctionBegin;
21291cbb95d3SBarry Smith   bij = (Mat_SeqAIJ*) B->data;
21301cbb95d3SBarry Smith 
21311cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
21321cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
21331cbb95d3SBarry Smith   if (ma!=nb || na!=mb) {
21341cbb95d3SBarry Smith     *f = PETSC_FALSE;
21351cbb95d3SBarry Smith     PetscFunctionReturn(0);
21361cbb95d3SBarry Smith   }
21371cbb95d3SBarry Smith   aii  = aij->i; bii = bij->i;
21381cbb95d3SBarry Smith   adx  = aij->j; bdx = bij->j;
21391cbb95d3SBarry Smith   va   = aij->a; vb = bij->a;
21401cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
21411cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
21421cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
21431cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
21441cbb95d3SBarry Smith 
21451cbb95d3SBarry Smith   *f = PETSC_TRUE;
21461cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
21471cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
21481cbb95d3SBarry Smith       PetscInt    idc,idr;
21491cbb95d3SBarry Smith       PetscScalar vc,vr;
21501cbb95d3SBarry Smith       /* column/row index/value */
21511cbb95d3SBarry Smith       idc = adx[aptr[i]];
21521cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
21531cbb95d3SBarry Smith       vc  = va[aptr[i]];
21541cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
21551cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
21561cbb95d3SBarry Smith         *f = PETSC_FALSE;
21571cbb95d3SBarry Smith         goto done;
21581cbb95d3SBarry Smith       } else {
21591cbb95d3SBarry Smith         aptr[i]++;
21601cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
21611cbb95d3SBarry Smith       }
21621cbb95d3SBarry Smith     }
21631cbb95d3SBarry Smith   }
21641cbb95d3SBarry Smith done:
21651cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
21661cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
21671cbb95d3SBarry Smith   PetscFunctionReturn(0);
21681cbb95d3SBarry Smith }
21691cbb95d3SBarry Smith 
21709e29f15eSvictorle #undef __FUNCT__
21719e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2172ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
21739e29f15eSvictorle {
2174dfbe8321SBarry Smith   PetscErrorCode ierr;
21756e111a19SKarl Rupp 
21769e29f15eSvictorle   PetscFunctionBegin;
21775485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
21789e29f15eSvictorle   PetscFunctionReturn(0);
21799e29f15eSvictorle }
21809e29f15eSvictorle 
21814a2ae208SSatish Balay #undef __FUNCT__
21821cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2183ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
21841cbb95d3SBarry Smith {
21851cbb95d3SBarry Smith   PetscErrorCode ierr;
21866e111a19SKarl Rupp 
21871cbb95d3SBarry Smith   PetscFunctionBegin;
21881cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
21891cbb95d3SBarry Smith   PetscFunctionReturn(0);
21901cbb95d3SBarry Smith }
21911cbb95d3SBarry Smith 
21921cbb95d3SBarry Smith #undef __FUNCT__
21934a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2194dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
219517ab2063SBarry Smith {
2196416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
219754f21887SBarry Smith   PetscScalar    *l,*r,x;
219854f21887SBarry Smith   MatScalar      *v;
2199dfbe8321SBarry Smith   PetscErrorCode ierr;
2200d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
220117ab2063SBarry Smith 
22023a40ed3dSBarry Smith   PetscFunctionBegin;
220317ab2063SBarry Smith   if (ll) {
22043ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
22053ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2206e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2207e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
22081ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2209416022c9SBarry Smith     v    = a->a;
221017ab2063SBarry Smith     for (i=0; i<m; i++) {
221117ab2063SBarry Smith       x = l[i];
2212416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
22132205254eSKarl Rupp       for (j=0; j<M; j++) (*v++) *= x;
221417ab2063SBarry Smith     }
22151ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2216efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
221717ab2063SBarry Smith   }
221817ab2063SBarry Smith   if (rr) {
2219e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2220e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
22211ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2222416022c9SBarry Smith     v    = a->a; jj = a->j;
22232205254eSKarl Rupp     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
22241ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2225efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
222617ab2063SBarry Smith   }
2227acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
22283a40ed3dSBarry Smith   PetscFunctionReturn(0);
222917ab2063SBarry Smith }
223017ab2063SBarry Smith 
22314a2ae208SSatish Balay #undef __FUNCT__
22324a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
223397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
223417ab2063SBarry Smith {
2235db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
22366849ba73SBarry Smith   PetscErrorCode ierr;
2237d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
223897f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
22395d0c19d7SBarry Smith   const PetscInt *irow,*icol;
22405d0c19d7SBarry Smith   PetscInt       nrows,ncols;
224197f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
224254f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2243416022c9SBarry Smith   Mat            C;
2244ace3abfcSBarry Smith   PetscBool      stride,sorted;
224517ab2063SBarry Smith 
22463a40ed3dSBarry Smith   PetscFunctionBegin;
224714ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
2248e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
224914ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
2250e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
225199141d43SSatish Balay 
225217ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2253b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2254b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
225517ab2063SBarry Smith 
2256fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2257251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2258fee21e36SBarry Smith   if (stride && step == 1) {
225902834360SBarry Smith     /* special case of contiguous rows */
22600e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
226102834360SBarry Smith     /* loop over new rows determining lens and starting points */
226202834360SBarry Smith     for (i=0; i<nrows; i++) {
2263bfeeae90SHong Zhang       kstart = ai[irow[i]];
2264a2744918SBarry Smith       kend   = kstart + ailen[irow[i]];
226502834360SBarry Smith       for (k=kstart; k<kend; k++) {
2266bfeeae90SHong Zhang         if (aj[k] >= first) {
226702834360SBarry Smith           starts[i] = k;
226802834360SBarry Smith           break;
226902834360SBarry Smith         }
227002834360SBarry Smith       }
2271a2744918SBarry Smith       sum = 0;
227202834360SBarry Smith       while (k < kend) {
2273bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2274a2744918SBarry Smith         sum++;
227502834360SBarry Smith       }
2276a2744918SBarry Smith       lens[i] = sum;
227702834360SBarry Smith     }
227802834360SBarry Smith     /* create submatrix */
2279cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
228097f1f81fSBarry Smith       PetscInt n_cols,n_rows;
228108480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2282e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2283d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
228408480c60SBarry Smith       C    = *B;
22853a40ed3dSBarry Smith     } else {
22863bef6203SJed Brown       PetscInt rbs,cbs;
2287ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2288f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
22893bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
22903bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
22913bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
22927adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2293ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
229408480c60SBarry Smith     }
2295db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2296db02288aSLois Curfman McInnes 
229702834360SBarry Smith     /* loop over rows inserting into submatrix */
2298db02288aSLois Curfman McInnes     a_new = c->a;
2299db02288aSLois Curfman McInnes     j_new = c->j;
2300db02288aSLois Curfman McInnes     i_new = c->i;
2301bfeeae90SHong Zhang 
230202834360SBarry Smith     for (i=0; i<nrows; i++) {
2303a2744918SBarry Smith       ii    = starts[i];
2304a2744918SBarry Smith       lensi = lens[i];
2305a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2306a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
230702834360SBarry Smith       }
230887828ca2SBarry Smith       ierr       = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2309a2744918SBarry Smith       a_new     += lensi;
2310a2744918SBarry Smith       i_new[i+1] = i_new[i] + lensi;
2311a2744918SBarry Smith       c->ilen[i] = lensi;
231202834360SBarry Smith     }
23130e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
23143a40ed3dSBarry Smith   } else {
231502834360SBarry Smith     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
23160e83c824SBarry Smith     ierr = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
231797f1f81fSBarry Smith     ierr = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
23180e83c824SBarry Smith     ierr = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
23194dcab191SBarry Smith     for (i=0; i<ncols; i++) {
23204dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
23214dcab191SBarry 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);
23224dcab191SBarry Smith #endif
23234dcab191SBarry Smith       smap[icol[i]] = i+1;
23244dcab191SBarry Smith     }
23254dcab191SBarry Smith 
232602834360SBarry Smith     /* determine lens of each row */
232702834360SBarry Smith     for (i=0; i<nrows; i++) {
2328bfeeae90SHong Zhang       kstart  = ai[irow[i]];
232902834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
233002834360SBarry Smith       lens[i] = 0;
233102834360SBarry Smith       for (k=kstart; k<kend; k++) {
2332bfeeae90SHong Zhang         if (smap[aj[k]]) {
233302834360SBarry Smith           lens[i]++;
233402834360SBarry Smith         }
233502834360SBarry Smith       }
233602834360SBarry Smith     }
233717ab2063SBarry Smith     /* Create and fill new matrix */
2338a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2339ace3abfcSBarry Smith       PetscBool equal;
23400f5bd95cSBarry Smith 
234199141d43SSatish Balay       c = (Mat_SeqAIJ*)((*B)->data);
2342e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2343d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
2344f23aa3ddSBarry Smith       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2345d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
234608480c60SBarry Smith       C    = *B;
23473a40ed3dSBarry Smith     } else {
23483bef6203SJed Brown       PetscInt rbs,cbs;
2349ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2350f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
23513bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
23523bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
23533bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
23547adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2355ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
235608480c60SBarry Smith     }
235799141d43SSatish Balay     c = (Mat_SeqAIJ*)(C->data);
235817ab2063SBarry Smith     for (i=0; i<nrows; i++) {
235999141d43SSatish Balay       row      = irow[i];
2360bfeeae90SHong Zhang       kstart   = ai[row];
236199141d43SSatish Balay       kend     = kstart + a->ilen[row];
2362bfeeae90SHong Zhang       mat_i    = c->i[i];
236399141d43SSatish Balay       mat_j    = c->j + mat_i;
236499141d43SSatish Balay       mat_a    = c->a + mat_i;
236599141d43SSatish Balay       mat_ilen = c->ilen + i;
236617ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2367bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2368ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
236999141d43SSatish Balay           *mat_a++ = a->a[k];
237099141d43SSatish Balay           (*mat_ilen)++;
237199141d43SSatish Balay 
237217ab2063SBarry Smith         }
237317ab2063SBarry Smith       }
237417ab2063SBarry Smith     }
237502834360SBarry Smith     /* Free work space */
237602834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2377606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2378606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
237902834360SBarry Smith   }
23806d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23816d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
238217ab2063SBarry Smith 
238317ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2384416022c9SBarry Smith   *B   = C;
23853a40ed3dSBarry Smith   PetscFunctionReturn(0);
238617ab2063SBarry Smith }
238717ab2063SBarry Smith 
23881df811f5SHong Zhang #undef __FUNCT__
238982d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2390fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
239182d44351SHong Zhang {
239282d44351SHong Zhang   PetscErrorCode ierr;
239382d44351SHong Zhang   Mat            B;
239482d44351SHong Zhang 
239582d44351SHong Zhang   PetscFunctionBegin;
2396c2d650bdSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
239782d44351SHong Zhang     ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
239882d44351SHong Zhang     ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
2399a2f3521dSMark F. Adams     ierr    = MatSetBlockSizes(B,mat->rmap->bs,mat->cmap->bs);CHKERRQ(ierr);
240082d44351SHong Zhang     ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
240182d44351SHong Zhang     ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
240282d44351SHong Zhang     *subMat = B;
2403c2d650bdSHong Zhang   } else {
2404c2d650bdSHong Zhang     ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
2405c2d650bdSHong Zhang   }
240682d44351SHong Zhang   PetscFunctionReturn(0);
240782d44351SHong Zhang }
240882d44351SHong Zhang 
240982d44351SHong Zhang #undef __FUNCT__
24104a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
24110481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2412a871dcd8SBarry Smith {
241363b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2414dfbe8321SBarry Smith   PetscErrorCode ierr;
241563b91edcSBarry Smith   Mat            outA;
2416ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
241763b91edcSBarry Smith 
24183a40ed3dSBarry Smith   PetscFunctionBegin;
2419e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
24201df811f5SHong Zhang 
2421b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2422b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2423a871dcd8SBarry Smith 
242463b91edcSBarry Smith   outA             = inA;
2425d5f3da31SBarry Smith   outA->factortype = MAT_FACTOR_LU;
24262205254eSKarl Rupp 
2427c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
24286bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
24292205254eSKarl Rupp 
2430c3122656SLisandro Dalcin   a->row = row;
24312205254eSKarl Rupp 
2432c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
24336bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
24342205254eSKarl Rupp 
2435c3122656SLisandro Dalcin   a->col = col;
243663b91edcSBarry Smith 
243736db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
24386bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
24394c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
24403bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
2441f0ec6fceSSatish Balay 
244294a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2443d0f46423SBarry Smith     ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
24443bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
244594a9d846SBarry Smith   }
244663b91edcSBarry Smith 
2447f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2448137fb511SHong Zhang   if (row_identity && col_identity) {
2449ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2450137fb511SHong Zhang   } else {
2451719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2452137fb511SHong Zhang   }
24533a40ed3dSBarry Smith   PetscFunctionReturn(0);
2454a871dcd8SBarry Smith }
2455a871dcd8SBarry Smith 
24564a2ae208SSatish Balay #undef __FUNCT__
24574a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2458f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2459f0b747eeSBarry Smith {
2460f0b747eeSBarry Smith   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2461f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2462efee365bSSatish Balay   PetscErrorCode ierr;
2463c5df96a5SBarry Smith   PetscBLASInt   one = 1,bnz;
24643a40ed3dSBarry Smith 
24653a40ed3dSBarry Smith   PetscFunctionBegin;
2466c5df96a5SBarry Smith   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
24678b83055fSJed Brown   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2468efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2469acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
24703a40ed3dSBarry Smith   PetscFunctionReturn(0);
2471f0b747eeSBarry Smith }
2472f0b747eeSBarry Smith 
24734a2ae208SSatish Balay #undef __FUNCT__
24744a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
247597f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2476cddf8d76SBarry Smith {
2477dfbe8321SBarry Smith   PetscErrorCode ierr;
247897f1f81fSBarry Smith   PetscInt       i;
2479cddf8d76SBarry Smith 
24803a40ed3dSBarry Smith   PetscFunctionBegin;
2481cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2482b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2483cddf8d76SBarry Smith   }
2484cddf8d76SBarry Smith 
2485cddf8d76SBarry Smith   for (i=0; i<n; i++) {
24866a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2487cddf8d76SBarry Smith   }
24883a40ed3dSBarry Smith   PetscFunctionReturn(0);
2489cddf8d76SBarry Smith }
2490cddf8d76SBarry Smith 
24914a2ae208SSatish Balay #undef __FUNCT__
24924a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
249397f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
24944dcbc457SBarry Smith {
2495e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
24966849ba73SBarry Smith   PetscErrorCode ierr;
24975d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
24985d0c19d7SBarry Smith   const PetscInt *idx;
249997f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2500f1af5d2fSBarry Smith   PetscBT        table;
2501bbd702dbSSatish Balay 
25023a40ed3dSBarry Smith   PetscFunctionBegin;
2503d0f46423SBarry Smith   m  = A->rmap->n;
2504e4d965acSSatish Balay   ai = a->i;
2505bfeeae90SHong Zhang   aj = a->j;
25068a047759SSatish Balay 
2507e32f2f54SBarry Smith   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
250806763907SSatish Balay 
250997f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
251053b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
251106763907SSatish Balay 
2512e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2513b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2514e4d965acSSatish Balay     isz  = 0;
25156831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2516e4d965acSSatish Balay 
2517e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
25184dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2519b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2520e4d965acSSatish Balay 
2521dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2522e4d965acSSatish Balay     for (j=0; j<n; ++j) {
25232205254eSKarl Rupp       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
25244dcbc457SBarry Smith     }
252506763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
25266bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2527e4d965acSSatish Balay 
252804a348a9SBarry Smith     k = 0;
252904a348a9SBarry Smith     for (j=0; j<ov; j++) { /* for each overlap */
253004a348a9SBarry Smith       n = isz;
253106763907SSatish Balay       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2532e4d965acSSatish Balay         row   = nidx[k];
2533e4d965acSSatish Balay         start = ai[row];
2534e4d965acSSatish Balay         end   = ai[row+1];
253504a348a9SBarry Smith         for (l = start; l<end; l++) {
2536efb16452SHong Zhang           val = aj[l];
25372205254eSKarl Rupp           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2538e4d965acSSatish Balay         }
2539e4d965acSSatish Balay       }
2540e4d965acSSatish Balay     }
254170b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2542e4d965acSSatish Balay   }
254394bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2544606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
25453a40ed3dSBarry Smith   PetscFunctionReturn(0);
25464dcbc457SBarry Smith }
254717ab2063SBarry Smith 
25480513a670SBarry Smith /* -------------------------------------------------------------- */
25494a2ae208SSatish Balay #undef __FUNCT__
25504a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2551dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
25520513a670SBarry Smith {
25530513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
25546849ba73SBarry Smith   PetscErrorCode ierr;
25553b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
25565d0c19d7SBarry Smith   const PetscInt *row,*col;
25575d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
255856cd22aeSBarry Smith   IS             icolp,irowp;
25590298fd71SBarry Smith   PetscInt       *cwork = NULL;
25600298fd71SBarry Smith   PetscScalar    *vwork = NULL;
25610513a670SBarry Smith 
25623a40ed3dSBarry Smith   PetscFunctionBegin;
25634c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
256456cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
25654c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
256656cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
25670513a670SBarry Smith 
25680513a670SBarry Smith   /* determine lengths of permuted rows */
256997f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
25702205254eSKarl Rupp   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2571ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2572f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
2573a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
25747adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2575ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2576606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
25770513a670SBarry Smith 
257897f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
25790513a670SBarry Smith   for (i=0; i<m; i++) {
258032ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
25812205254eSKarl Rupp     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2582cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
258332ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
25840513a670SBarry Smith   }
2585606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
25862205254eSKarl Rupp 
25873c7d62e4SBarry Smith   (*B)->assembled = PETSC_FALSE;
25882205254eSKarl Rupp 
25890513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
25900513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
259156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
259256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
25936bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
25946bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
25953a40ed3dSBarry Smith   PetscFunctionReturn(0);
25960513a670SBarry Smith }
25970513a670SBarry Smith 
25984a2ae208SSatish Balay #undef __FUNCT__
25994a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2600dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2601cb5b572fSBarry Smith {
2602dfbe8321SBarry Smith   PetscErrorCode ierr;
2603cb5b572fSBarry Smith 
2604cb5b572fSBarry Smith   PetscFunctionBegin;
260533f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
260633f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2607be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2608be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2609be6bf707SBarry Smith 
2610700c5bfcSBarry 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");
2611d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2612cb5b572fSBarry Smith   } else {
2613cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2614cb5b572fSBarry Smith   }
2615cb5b572fSBarry Smith   PetscFunctionReturn(0);
2616cb5b572fSBarry Smith }
2617cb5b572fSBarry Smith 
26184a2ae208SSatish Balay #undef __FUNCT__
26194994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
26204994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2621273d9f13SBarry Smith {
2622dfbe8321SBarry Smith   PetscErrorCode ierr;
2623273d9f13SBarry Smith 
2624273d9f13SBarry Smith   PetscFunctionBegin;
2625ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2626273d9f13SBarry Smith   PetscFunctionReturn(0);
2627273d9f13SBarry Smith }
2628273d9f13SBarry Smith 
26294a2ae208SSatish Balay #undef __FUNCT__
26308c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray_SeqAIJ"
26318c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
26326c0721eeSBarry Smith {
26336c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
26346e111a19SKarl Rupp 
26356c0721eeSBarry Smith   PetscFunctionBegin;
26366c0721eeSBarry Smith   *array = a->a;
26376c0721eeSBarry Smith   PetscFunctionReturn(0);
26386c0721eeSBarry Smith }
26396c0721eeSBarry Smith 
26404a2ae208SSatish Balay #undef __FUNCT__
26418c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray_SeqAIJ"
26428c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
26436c0721eeSBarry Smith {
26446c0721eeSBarry Smith   PetscFunctionBegin;
26456c0721eeSBarry Smith   PetscFunctionReturn(0);
26466c0721eeSBarry Smith }
2647273d9f13SBarry Smith 
2648ee4f033dSBarry Smith #undef __FUNCT__
2649ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2650dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2651ee4f033dSBarry Smith {
26526849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void*))coloring->f;
26536849ba73SBarry Smith   PetscErrorCode ierr;
26544e269d77SPeter Brune   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow;
2655efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
265687828ca2SBarry Smith   PetscScalar    *vscale_array;
2657ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2658ee4f033dSBarry Smith   Vec            w1,w2,w3;
2659ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2660ace3abfcSBarry Smith   PetscBool      flg   = PETSC_FALSE;
2661ee4f033dSBarry Smith 
2662ee4f033dSBarry Smith   PetscFunctionBegin;
2663ee4f033dSBarry Smith   if (!coloring->w1) {
2664ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
26653bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)coloring,(PetscObject)coloring->w1);CHKERRQ(ierr);
2666ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
26673bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)coloring,(PetscObject)coloring->w2);CHKERRQ(ierr);
2668ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
26693bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)coloring,(PetscObject)coloring->w3);CHKERRQ(ierr);
2670ee4f033dSBarry Smith   }
2671ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2672ee4f033dSBarry Smith 
2673ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
26740298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,NULL);CHKERRQ(ierr);
2675ee4f033dSBarry Smith   if (flg) {
2676ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2677ee4f033dSBarry Smith   } else {
2678ace3abfcSBarry Smith     PetscBool assembled;
26790b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
26800b9b6f31SBarry Smith     if (assembled) {
2681ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2682ee4f033dSBarry Smith     }
26830b9b6f31SBarry Smith   }
2684ee4f033dSBarry Smith 
2685ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2686ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2687ee4f033dSBarry Smith 
26884e269d77SPeter Brune   if (!coloring->fset) {
268966f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2690ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
269166f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
26924e269d77SPeter Brune   } else {
26934e269d77SPeter Brune     coloring->fset = PETSC_FALSE;
2694ee4f033dSBarry Smith   }
2695ee4f033dSBarry Smith 
2696ee4f033dSBarry Smith   /*
2697ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2698ee4f033dSBarry Smith   */
26991ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
27001ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2701ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2702ee4f033dSBarry Smith     /*
2703ee4f033dSBarry Smith        Loop over each column associated with color adding the
2704ee4f033dSBarry Smith        perturbation to the vector w3.
2705ee4f033dSBarry Smith     */
2706ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2707ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2708ee4f033dSBarry Smith       dx  = xx[col];
2709ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2710ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2711ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2712ee4f033dSBarry Smith       dx               *= epsilon;
2713ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2714ee4f033dSBarry Smith     }
2715ee4f033dSBarry Smith   }
27162205254eSKarl Rupp   vscale_array = vscale_array + start;
27172205254eSKarl Rupp 
27182205254eSKarl Rupp   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2719ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2720ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2721ee4f033dSBarry Smith 
2722ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2723ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2724ee4f033dSBarry Smith 
2725ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2726ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2727ee4f033dSBarry Smith 
27281ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2729ee4f033dSBarry Smith   /*
2730ee4f033dSBarry Smith       Loop over each color
2731ee4f033dSBarry Smith   */
2732ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
273349b058dcSBarry Smith     coloring->currentcolor = k;
27342205254eSKarl Rupp 
2735ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
27361ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2737ee4f033dSBarry Smith     /*
2738ee4f033dSBarry Smith        Loop over each column associated with color adding the
2739ee4f033dSBarry Smith        perturbation to the vector w3.
2740ee4f033dSBarry Smith     */
2741ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2742ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2743ee4f033dSBarry Smith       dx  = xx[col];
27445b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2745ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2746ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2747ee4f033dSBarry Smith       dx *= epsilon;
2748e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2749ee4f033dSBarry Smith       w3_array[col] += dx;
2750ee4f033dSBarry Smith     }
27511ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2752ee4f033dSBarry Smith 
2753ee4f033dSBarry Smith     /*
2754ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2755ee4f033dSBarry Smith     */
2756ee4f033dSBarry Smith 
275766f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2758ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
275966f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2760efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2761ee4f033dSBarry Smith 
2762ee4f033dSBarry Smith     /*
2763ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2764ee4f033dSBarry Smith     */
27651ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2766ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2767ee4f033dSBarry Smith       row     = coloring->rows[k][l];
2768ee4f033dSBarry Smith       col     = coloring->columnsforrow[k][l];
2769ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2770ee4f033dSBarry Smith       srow    = row + start;
2771ee4f033dSBarry Smith       ierr    = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2772ee4f033dSBarry Smith     }
27731ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2774ee4f033dSBarry Smith   }
277549b058dcSBarry Smith   coloring->currentcolor = k;
27762205254eSKarl Rupp 
27771ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
27782205254eSKarl Rupp   xx   = xx + start;
27792205254eSKarl Rupp   ierr = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2780ee4f033dSBarry Smith   ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2781ee4f033dSBarry Smith   ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2782ee4f033dSBarry Smith   PetscFunctionReturn(0);
2783ee4f033dSBarry Smith }
2784ee4f033dSBarry Smith 
27858229c054SShri Abhyankar /*
27868229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
27878229c054SShri Abhyankar    have different nonzero structure.
27888229c054SShri Abhyankar */
2789ac90fabeSBarry Smith #undef __FUNCT__
27908229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
27918229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2792ec7775f6SShri Abhyankar {
27938229c054SShri Abhyankar   PetscInt       i,m=Y->rmap->N;
2794ec7775f6SShri Abhyankar   Mat_SeqAIJ     *x  = (Mat_SeqAIJ*)X->data;
2795ec7775f6SShri Abhyankar   Mat_SeqAIJ     *y  = (Mat_SeqAIJ*)Y->data;
2796ec7775f6SShri Abhyankar   const PetscInt *xi = x->i,*yi = y->i;
2797ec7775f6SShri Abhyankar 
2798ec7775f6SShri Abhyankar   PetscFunctionBegin;
2799ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2800ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
28018af7cee1SJed Brown     PetscInt       j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
28028af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
28038af7cee1SJed Brown     nnz[i] = 0;
28048af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
28058af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
28068af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
28078af7cee1SJed Brown       nnz[i]++;
28088af7cee1SJed Brown     }
28098af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2810ec7775f6SShri Abhyankar   }
2811ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2812ec7775f6SShri Abhyankar }
2813ec7775f6SShri Abhyankar 
2814ec7775f6SShri Abhyankar #undef __FUNCT__
2815ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2816f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2817ac90fabeSBarry Smith {
2818dfbe8321SBarry Smith   PetscErrorCode ierr;
281997f1f81fSBarry Smith   PetscInt       i;
2820ac90fabeSBarry Smith   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2821c5df96a5SBarry Smith   PetscBLASInt   one=1,bnz;
2822ac90fabeSBarry Smith 
2823ac90fabeSBarry Smith   PetscFunctionBegin;
2824c5df96a5SBarry Smith   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2825ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2826f4df32b1SMatthew Knepley     PetscScalar alpha = a;
28278b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2828acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2829c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2830a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2831a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
28326bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2833a30b2313SHong Zhang     }
2834a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
28350298fd71SBarry Smith       ierr    = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,NULL, y->i,y->j,NULL, &y->xtoy);CHKERRQ(ierr);
2836a30b2313SHong Zhang       y->XtoY = X;
2837407f6b05SHong Zhang       ierr    = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2838c537a176SHong Zhang     }
2839f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2840ba0e910bSBarry 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);
2841ac90fabeSBarry Smith   } else {
28428229c054SShri Abhyankar     Mat      B;
28438229c054SShri Abhyankar     PetscInt *nnz;
284416b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2845ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2846bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
28474aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2848a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr);
2849176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
28508229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2851ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2852ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2853ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
28548229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2855ac90fabeSBarry Smith   }
2856ac90fabeSBarry Smith   PetscFunctionReturn(0);
2857ac90fabeSBarry Smith }
2858ac90fabeSBarry Smith 
2859521d7252SBarry Smith #undef __FUNCT__
2860354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
28617087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2862354c94deSBarry Smith {
2863354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2864354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2865354c94deSBarry Smith   PetscInt    i,nz;
2866354c94deSBarry Smith   PetscScalar *a;
2867354c94deSBarry Smith 
2868354c94deSBarry Smith   PetscFunctionBegin;
2869354c94deSBarry Smith   nz = aij->nz;
2870354c94deSBarry Smith   a  = aij->a;
28712205254eSKarl Rupp   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2872354c94deSBarry Smith #else
2873354c94deSBarry Smith   PetscFunctionBegin;
2874354c94deSBarry Smith #endif
2875354c94deSBarry Smith   PetscFunctionReturn(0);
2876354c94deSBarry Smith }
2877354c94deSBarry Smith 
2878e34fafa9SBarry Smith #undef __FUNCT__
2879985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2880985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2881e34fafa9SBarry Smith {
2882e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2883e34fafa9SBarry Smith   PetscErrorCode ierr;
2884d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2885e34fafa9SBarry Smith   PetscReal      atmp;
2886985db425SBarry Smith   PetscScalar    *x;
2887e34fafa9SBarry Smith   MatScalar      *aa;
2888e34fafa9SBarry Smith 
2889e34fafa9SBarry Smith   PetscFunctionBegin;
2890e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2891e34fafa9SBarry Smith   aa = a->a;
2892e34fafa9SBarry Smith   ai = a->i;
2893e34fafa9SBarry Smith   aj = a->j;
2894e34fafa9SBarry Smith 
2895985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2896e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2897e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2898e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2899e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2900e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
29019189402eSHong Zhang     x[i]  = 0.0;
2902e34fafa9SBarry Smith     for (j=0; j<ncols; j++) {
2903985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2904985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2905985db425SBarry Smith       aa++; aj++;
2906985db425SBarry Smith     }
2907985db425SBarry Smith   }
2908985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2909985db425SBarry Smith   PetscFunctionReturn(0);
2910985db425SBarry Smith }
2911985db425SBarry Smith 
2912985db425SBarry Smith #undef __FUNCT__
2913985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2914985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2915985db425SBarry Smith {
2916985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2917985db425SBarry Smith   PetscErrorCode ierr;
2918d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2919985db425SBarry Smith   PetscScalar    *x;
2920985db425SBarry Smith   MatScalar      *aa;
2921985db425SBarry Smith 
2922985db425SBarry Smith   PetscFunctionBegin;
2923e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2924985db425SBarry Smith   aa = a->a;
2925985db425SBarry Smith   ai = a->i;
2926985db425SBarry Smith   aj = a->j;
2927985db425SBarry Smith 
2928985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2929985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2930985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2931e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2932985db425SBarry Smith   for (i=0; i<m; i++) {
2933985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2934d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2935985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2936985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2937985db425SBarry Smith       x[i] = 0.0;
2938985db425SBarry Smith       if (idx) {
2939985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2940985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2941985db425SBarry Smith           if (aj[j] > j) {
2942985db425SBarry Smith             idx[i] = j;
2943985db425SBarry Smith             break;
2944985db425SBarry Smith           }
2945985db425SBarry Smith         }
2946985db425SBarry Smith       }
2947985db425SBarry Smith     }
2948985db425SBarry Smith     for (j=0; j<ncols; j++) {
2949985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2950985db425SBarry Smith       aa++; aj++;
2951985db425SBarry Smith     }
2952985db425SBarry Smith   }
2953985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2954985db425SBarry Smith   PetscFunctionReturn(0);
2955985db425SBarry Smith }
2956985db425SBarry Smith 
2957985db425SBarry Smith #undef __FUNCT__
2958c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2959c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2960c87e5d42SMatthew Knepley {
2961c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2962c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2963c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2964c87e5d42SMatthew Knepley   PetscReal      atmp;
2965c87e5d42SMatthew Knepley   PetscScalar    *x;
2966c87e5d42SMatthew Knepley   MatScalar      *aa;
2967c87e5d42SMatthew Knepley 
2968c87e5d42SMatthew Knepley   PetscFunctionBegin;
2969e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2970c87e5d42SMatthew Knepley   aa = a->a;
2971c87e5d42SMatthew Knepley   ai = a->i;
2972c87e5d42SMatthew Knepley   aj = a->j;
2973c87e5d42SMatthew Knepley 
2974c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2975c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2976c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
29773bb78c5cSMatthew 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);
2978c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2979c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2980289a08f5SMatthew Knepley     if (ncols) {
2981289a08f5SMatthew Knepley       /* Get first nonzero */
2982289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
2983289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
29842205254eSKarl Rupp         if (atmp > 1.0e-12) {
29852205254eSKarl Rupp           x[i] = atmp;
29862205254eSKarl Rupp           if (idx) idx[i] = aj[j];
29872205254eSKarl Rupp           break;
29882205254eSKarl Rupp         }
2989289a08f5SMatthew Knepley       }
299012431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2991289a08f5SMatthew Knepley     } else {
2992289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2993289a08f5SMatthew Knepley     }
2994c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
2995c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2996289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2997c87e5d42SMatthew Knepley       aa++; aj++;
2998c87e5d42SMatthew Knepley     }
2999c87e5d42SMatthew Knepley   }
3000c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3001c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
3002c87e5d42SMatthew Knepley }
3003c87e5d42SMatthew Knepley 
3004c87e5d42SMatthew Knepley #undef __FUNCT__
3005985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
3006985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3007985db425SBarry Smith {
3008985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3009985db425SBarry Smith   PetscErrorCode ierr;
3010d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3011985db425SBarry Smith   PetscScalar    *x;
3012985db425SBarry Smith   MatScalar      *aa;
3013985db425SBarry Smith 
3014985db425SBarry Smith   PetscFunctionBegin;
3015e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3016985db425SBarry Smith   aa = a->a;
3017985db425SBarry Smith   ai = a->i;
3018985db425SBarry Smith   aj = a->j;
3019985db425SBarry Smith 
3020985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3021985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3022985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
3023e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3024985db425SBarry Smith   for (i=0; i<m; i++) {
3025985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
3026d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
3027985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
3028985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3029985db425SBarry Smith       x[i] = 0.0;
3030985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
3031985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
3032985db425SBarry Smith         for (j=0; j<ncols; j++) {
3033985db425SBarry Smith           if (aj[j] > j) {
3034985db425SBarry Smith             idx[i] = j;
3035985db425SBarry Smith             break;
3036985db425SBarry Smith           }
3037985db425SBarry Smith         }
3038985db425SBarry Smith       }
3039985db425SBarry Smith     }
3040985db425SBarry Smith     for (j=0; j<ncols; j++) {
3041985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3042985db425SBarry Smith       aa++; aj++;
3043e34fafa9SBarry Smith     }
3044e34fafa9SBarry Smith   }
3045e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3046e34fafa9SBarry Smith   PetscFunctionReturn(0);
3047e34fafa9SBarry Smith }
3048bbead8a2SBarry Smith 
3049bbead8a2SBarry Smith #include <petscblaslapack.h>
305006873bf2SBarry Smith #include <petsc-private/kernels/blockinvert.h>
3051bbead8a2SBarry Smith 
3052bbead8a2SBarry Smith #undef __FUNCT__
3053bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
3054713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3055bbead8a2SBarry Smith {
3056bbead8a2SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
3057bbead8a2SBarry Smith   PetscErrorCode ierr;
305834fc4b71SJed 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;
3059bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
3060bbead8a2SBarry Smith   PetscReal      shift = 0.0;
3061bbead8a2SBarry Smith 
3062bbead8a2SBarry Smith   PetscFunctionBegin;
30634a0d0026SBarry Smith   if (a->ibdiagvalid) {
30644a0d0026SBarry Smith     if (values) *values = a->ibdiag;
30654a0d0026SBarry Smith     PetscFunctionReturn(0);
30664a0d0026SBarry Smith   }
3067bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
3068bbead8a2SBarry Smith   if (!a->ibdiag) {
3069bbead8a2SBarry Smith     ierr = PetscMalloc(bs2*mbs*sizeof(PetscScalar),&a->ibdiag);CHKERRQ(ierr);
30703bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
3071bbead8a2SBarry Smith   }
3072bbead8a2SBarry Smith   diag = a->ibdiag;
3073bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
3074bbead8a2SBarry Smith   /* factor and invert each block */
3075bbead8a2SBarry Smith   switch (bs) {
3076bbead8a2SBarry Smith   case 1:
3077bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3078bbead8a2SBarry Smith       ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
3079bbead8a2SBarry Smith       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3080bbead8a2SBarry Smith     }
3081bbead8a2SBarry Smith     break;
3082bbead8a2SBarry Smith   case 2:
3083bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3084bbead8a2SBarry Smith       ij[0] = 2*i; ij[1] = 2*i + 1;
3085bbead8a2SBarry Smith       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
308696b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
308796b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
3088bbead8a2SBarry Smith       diag += 4;
3089bbead8a2SBarry Smith     }
3090bbead8a2SBarry Smith     break;
3091bbead8a2SBarry Smith   case 3:
3092bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3093bbead8a2SBarry Smith       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3094bbead8a2SBarry Smith       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
309596b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
309696b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
3097bbead8a2SBarry Smith       diag += 9;
3098bbead8a2SBarry Smith     }
3099bbead8a2SBarry Smith     break;
3100bbead8a2SBarry Smith   case 4:
3101bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3102bbead8a2SBarry Smith       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3103bbead8a2SBarry Smith       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
310496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
310596b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
3106bbead8a2SBarry Smith       diag += 16;
3107bbead8a2SBarry Smith     }
3108bbead8a2SBarry Smith     break;
3109bbead8a2SBarry Smith   case 5:
3110bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3111bbead8a2SBarry 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;
3112bbead8a2SBarry Smith       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
311396b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
311496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
3115bbead8a2SBarry Smith       diag += 25;
3116bbead8a2SBarry Smith     }
3117bbead8a2SBarry Smith     break;
3118bbead8a2SBarry Smith   case 6:
3119bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3120bbead8a2SBarry 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;
3121bbead8a2SBarry Smith       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
312296b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
312396b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3124bbead8a2SBarry Smith       diag += 36;
3125bbead8a2SBarry Smith     }
3126bbead8a2SBarry Smith     break;
3127bbead8a2SBarry Smith   case 7:
3128bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3129bbead8a2SBarry 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;
3130bbead8a2SBarry Smith       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
313196b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
313296b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3133bbead8a2SBarry Smith       diag += 49;
3134bbead8a2SBarry Smith     }
3135bbead8a2SBarry Smith     break;
3136bbead8a2SBarry Smith   default:
3137bbead8a2SBarry Smith     ierr = PetscMalloc3(bs,MatScalar,&v_work,bs,PetscInt,&v_pivots,bs,PetscInt,&IJ);CHKERRQ(ierr);
3138bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3139bbead8a2SBarry Smith       for (j=0; j<bs; j++) {
3140bbead8a2SBarry Smith         IJ[j] = bs*i + j;
3141bbead8a2SBarry Smith       }
3142bbead8a2SBarry Smith       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
314396b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
314496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3145bbead8a2SBarry Smith       diag += bs2;
3146bbead8a2SBarry Smith     }
3147bbead8a2SBarry Smith     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3148bbead8a2SBarry Smith   }
3149bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3150bbead8a2SBarry Smith   PetscFunctionReturn(0);
3151bbead8a2SBarry Smith }
3152bbead8a2SBarry Smith 
315373a71a0fSBarry Smith #undef __FUNCT__
315473a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_SeqAIJ"
315573a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
315673a71a0fSBarry Smith {
315773a71a0fSBarry Smith   PetscErrorCode ierr;
315873a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
315973a71a0fSBarry Smith   PetscScalar    a;
316073a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
316173a71a0fSBarry Smith 
316273a71a0fSBarry Smith   PetscFunctionBegin;
316373a71a0fSBarry Smith   if (!x->assembled) {
316473a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
316573a71a0fSBarry Smith     for (i=0; i<m; i++) {
316673a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
316773a71a0fSBarry Smith         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
316873a71a0fSBarry Smith         col  = (PetscInt)(n*PetscRealPart(a));
316973a71a0fSBarry Smith         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
317073a71a0fSBarry Smith       }
317173a71a0fSBarry Smith     }
317273a71a0fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
317373a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
317473a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
317573a71a0fSBarry Smith   PetscFunctionReturn(0);
317673a71a0fSBarry Smith }
317773a71a0fSBarry Smith 
31787087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
3179682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
31800a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3181cb5b572fSBarry Smith                                         MatGetRow_SeqAIJ,
3182cb5b572fSBarry Smith                                         MatRestoreRow_SeqAIJ,
3183cb5b572fSBarry Smith                                         MatMult_SeqAIJ,
318497304618SKris Buschelman                                 /*  4*/ MatMultAdd_SeqAIJ,
31857c922b88SBarry Smith                                         MatMultTranspose_SeqAIJ,
31867c922b88SBarry Smith                                         MatMultTransposeAdd_SeqAIJ,
3187db4efbfdSBarry Smith                                         0,
3188db4efbfdSBarry Smith                                         0,
3189db4efbfdSBarry Smith                                         0,
3190db4efbfdSBarry Smith                                 /* 10*/ 0,
3191cb5b572fSBarry Smith                                         MatLUFactor_SeqAIJ,
3192cb5b572fSBarry Smith                                         0,
319341f059aeSBarry Smith                                         MatSOR_SeqAIJ,
319417ab2063SBarry Smith                                         MatTranspose_SeqAIJ,
319597304618SKris Buschelman                                 /*1 5*/ MatGetInfo_SeqAIJ,
3196cb5b572fSBarry Smith                                         MatEqual_SeqAIJ,
3197cb5b572fSBarry Smith                                         MatGetDiagonal_SeqAIJ,
3198cb5b572fSBarry Smith                                         MatDiagonalScale_SeqAIJ,
3199cb5b572fSBarry Smith                                         MatNorm_SeqAIJ,
320097304618SKris Buschelman                                 /* 20*/ 0,
3201cb5b572fSBarry Smith                                         MatAssemblyEnd_SeqAIJ,
3202cb5b572fSBarry Smith                                         MatSetOption_SeqAIJ,
3203cb5b572fSBarry Smith                                         MatZeroEntries_SeqAIJ,
3204d519adbfSMatthew Knepley                                 /* 24*/ MatZeroRows_SeqAIJ,
3205db4efbfdSBarry Smith                                         0,
3206db4efbfdSBarry Smith                                         0,
3207db4efbfdSBarry Smith                                         0,
3208db4efbfdSBarry Smith                                         0,
32094994cf47SJed Brown                                 /* 29*/ MatSetUp_SeqAIJ,
3210db4efbfdSBarry Smith                                         0,
3211db4efbfdSBarry Smith                                         0,
32128c778c55SBarry Smith                                         0,
32138c778c55SBarry Smith                                         0,
3214d519adbfSMatthew Knepley                                 /* 34*/ MatDuplicate_SeqAIJ,
3215cb5b572fSBarry Smith                                         0,
3216cb5b572fSBarry Smith                                         0,
3217cb5b572fSBarry Smith                                         MatILUFactor_SeqAIJ,
3218cb5b572fSBarry Smith                                         0,
3219d519adbfSMatthew Knepley                                 /* 39*/ MatAXPY_SeqAIJ,
3220cb5b572fSBarry Smith                                         MatGetSubMatrices_SeqAIJ,
3221cb5b572fSBarry Smith                                         MatIncreaseOverlap_SeqAIJ,
3222cb5b572fSBarry Smith                                         MatGetValues_SeqAIJ,
3223cb5b572fSBarry Smith                                         MatCopy_SeqAIJ,
3224d519adbfSMatthew Knepley                                 /* 44*/ MatGetRowMax_SeqAIJ,
3225cb5b572fSBarry Smith                                         MatScale_SeqAIJ,
3226cb5b572fSBarry Smith                                         0,
322779299369SBarry Smith                                         MatDiagonalSet_SeqAIJ,
32286e169961SBarry Smith                                         MatZeroRowsColumns_SeqAIJ,
322973a71a0fSBarry Smith                                 /* 49*/ MatSetRandom_SeqAIJ,
32303b2fbd54SBarry Smith                                         MatGetRowIJ_SeqAIJ,
32313b2fbd54SBarry Smith                                         MatRestoreRowIJ_SeqAIJ,
32323b2fbd54SBarry Smith                                         MatGetColumnIJ_SeqAIJ,
3233a93ec695SBarry Smith                                         MatRestoreColumnIJ_SeqAIJ,
3234d519adbfSMatthew Knepley                                 /* 54*/ MatFDColoringCreate_SeqAIJ,
3235b9617806SBarry Smith                                         0,
32360513a670SBarry Smith                                         0,
3237cda55fadSBarry Smith                                         MatPermute_SeqAIJ,
3238cda55fadSBarry Smith                                         0,
3239d519adbfSMatthew Knepley                                 /* 59*/ 0,
3240b9b97703SBarry Smith                                         MatDestroy_SeqAIJ,
3241b9b97703SBarry Smith                                         MatView_SeqAIJ,
3242357abbc8SBarry Smith                                         0,
3243321b30b9SSatish Balay                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3244321b30b9SSatish Balay                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3245321b30b9SSatish Balay                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3246ee4f033dSBarry Smith                                         0,
3247ee4f033dSBarry Smith                                         0,
3248ee4f033dSBarry Smith                                         0,
3249d519adbfSMatthew Knepley                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3250c87e5d42SMatthew Knepley                                         MatGetRowMinAbs_SeqAIJ,
3251ee4f033dSBarry Smith                                         0,
3252ee4f033dSBarry Smith                                         MatSetColoring_SeqAIJ,
3253dcf5cc72SBarry Smith                                         0,
3254d519adbfSMatthew Knepley                                 /* 74*/ MatSetValuesAdifor_SeqAIJ,
32553acb8795SBarry Smith                                         MatFDColoringApply_AIJ,
325697304618SKris Buschelman                                         0,
325797304618SKris Buschelman                                         0,
325897304618SKris Buschelman                                         0,
32596ce1633cSBarry Smith                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
326097304618SKris Buschelman                                         0,
326197304618SKris Buschelman                                         0,
326297304618SKris Buschelman                                         0,
3263bc011b1eSHong Zhang                                         MatLoad_SeqAIJ,
3264d519adbfSMatthew Knepley                                 /* 84*/ MatIsSymmetric_SeqAIJ,
32651cbb95d3SBarry Smith                                         MatIsHermitian_SeqAIJ,
32666284ec50SHong Zhang                                         0,
32676284ec50SHong Zhang                                         0,
3268bc011b1eSHong Zhang                                         0,
3269d519adbfSMatthew Knepley                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
327026be0446SHong Zhang                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
327126be0446SHong Zhang                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
327265e8a0caSHong Zhang                                         MatPtAP_SeqAIJ_SeqAIJ,
32734a1b09b7SHong Zhang                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_DenseAxpy,
327465e8a0caSHong Zhang                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
32756fc122caSHong Zhang                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
32766fc122caSHong Zhang                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
32776fc122caSHong Zhang                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
32782121bac1SHong Zhang                                         0,
32792121bac1SHong Zhang                                 /* 99*/ 0,
3280609c6c4dSKris Buschelman                                         0,
3281609c6c4dSKris Buschelman                                         0,
328287d4246cSBarry Smith                                         MatConjugate_SeqAIJ,
328387d4246cSBarry Smith                                         0,
3284d519adbfSMatthew Knepley                                 /*104*/ MatSetValuesRow_SeqAIJ,
328599cafbc1SBarry Smith                                         MatRealPart_SeqAIJ,
3286f5edf698SHong Zhang                                         MatImaginaryPart_SeqAIJ,
3287f5edf698SHong Zhang                                         0,
32882bebee5dSHong Zhang                                         0,
3289cbd44569SHong Zhang                                 /*109*/ MatMatSolve_SeqAIJ,
3290985db425SBarry Smith                                         0,
32912af78befSBarry Smith                                         MatGetRowMin_SeqAIJ,
32922af78befSBarry Smith                                         0,
3293599ef60dSHong Zhang                                         MatMissingDiagonal_SeqAIJ,
3294d519adbfSMatthew Knepley                                 /*114*/ 0,
3295599ef60dSHong Zhang                                         0,
32963c2a7987SHong Zhang                                         0,
3297fe97e370SBarry Smith                                         0,
3298fbdbba38SShri Abhyankar                                         0,
3299fbdbba38SShri Abhyankar                                 /*119*/ 0,
3300fbdbba38SShri Abhyankar                                         0,
3301fbdbba38SShri Abhyankar                                         0,
330282d44351SHong Zhang                                         0,
3303b3a44c85SBarry Smith                                         MatGetMultiProcBlock_SeqAIJ,
33040716a85fSBarry Smith                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3305bbead8a2SBarry Smith                                         MatGetColumnNorms_SeqAIJ,
330637868618SMatthew G Knepley                                         MatInvertBlockDiagonal_SeqAIJ,
330737868618SMatthew G Knepley                                         0,
330837868618SMatthew G Knepley                                         0,
33095df89d91SHong Zhang                                 /*129*/ 0,
331075648e8dSHong Zhang                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
331175648e8dSHong Zhang                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
331275648e8dSHong Zhang                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3313b9af6bddSHong Zhang                                         MatTransposeColoringCreate_SeqAIJ,
3314b9af6bddSHong Zhang                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
33152b8ad9a3SHong Zhang                                         MatTransColoringApplyDenToSp_SeqAIJ,
33162b8ad9a3SHong Zhang                                         MatRARt_SeqAIJ_SeqAIJ,
33172b8ad9a3SHong Zhang                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
33183964eb88SJed Brown                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
33193964eb88SJed Brown                                  /*139*/0,
33203964eb88SJed Brown                                         0
33219e29f15eSvictorle };
332217ab2063SBarry Smith 
33234a2ae208SSatish Balay #undef __FUNCT__
33244a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
33257087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3326bef8e0ddSBarry Smith {
3327bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
332897f1f81fSBarry Smith   PetscInt   i,nz,n;
3329bef8e0ddSBarry Smith 
3330bef8e0ddSBarry Smith   PetscFunctionBegin;
3331bef8e0ddSBarry Smith   nz = aij->maxnz;
3332d0f46423SBarry Smith   n  = mat->rmap->n;
3333bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3334bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3335bef8e0ddSBarry Smith   }
3336bef8e0ddSBarry Smith   aij->nz = nz;
3337bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3338bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3339bef8e0ddSBarry Smith   }
3340bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3341bef8e0ddSBarry Smith }
3342bef8e0ddSBarry Smith 
33434a2ae208SSatish Balay #undef __FUNCT__
33444a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3345bef8e0ddSBarry Smith /*@
3346bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3347bef8e0ddSBarry Smith        in the matrix.
3348bef8e0ddSBarry Smith 
3349bef8e0ddSBarry Smith   Input Parameters:
3350bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3351bef8e0ddSBarry Smith -  indices - the column indices
3352bef8e0ddSBarry Smith 
335315091d37SBarry Smith   Level: advanced
335415091d37SBarry Smith 
3355bef8e0ddSBarry Smith   Notes:
3356bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3357bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3358bef8e0ddSBarry Smith   of the MatSetValues() operation.
3359bef8e0ddSBarry Smith 
3360bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3361d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3362bef8e0ddSBarry Smith 
3363bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3364bef8e0ddSBarry Smith 
3365b9617806SBarry Smith     The indices should start with zero, not one.
3366b9617806SBarry Smith 
3367bef8e0ddSBarry Smith @*/
33687087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3369bef8e0ddSBarry Smith {
33704ac538c5SBarry Smith   PetscErrorCode ierr;
3371bef8e0ddSBarry Smith 
3372bef8e0ddSBarry Smith   PetscFunctionBegin;
33730700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
33744482741eSBarry Smith   PetscValidPointer(indices,2);
33754ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3376bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3377bef8e0ddSBarry Smith }
3378bef8e0ddSBarry Smith 
3379be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3380be6bf707SBarry Smith 
33814a2ae208SSatish Balay #undef __FUNCT__
33824a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
33837087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3384be6bf707SBarry Smith {
3385be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
33866849ba73SBarry Smith   PetscErrorCode ierr;
3387d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3388be6bf707SBarry Smith 
3389be6bf707SBarry Smith   PetscFunctionBegin;
3390f23aa3ddSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3391be6bf707SBarry Smith 
3392be6bf707SBarry Smith   /* allocate space for values if not already there */
3393be6bf707SBarry Smith   if (!aij->saved_values) {
339487828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
33953bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3396be6bf707SBarry Smith   }
3397be6bf707SBarry Smith 
3398be6bf707SBarry Smith   /* copy values over */
339987828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3400be6bf707SBarry Smith   PetscFunctionReturn(0);
3401be6bf707SBarry Smith }
3402be6bf707SBarry Smith 
34034a2ae208SSatish Balay #undef __FUNCT__
3404b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3405be6bf707SBarry Smith /*@
3406be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3407be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3408be6bf707SBarry Smith        nonlinear portion.
3409be6bf707SBarry Smith 
3410be6bf707SBarry Smith    Collect on Mat
3411be6bf707SBarry Smith 
3412be6bf707SBarry Smith   Input Parameters:
34130e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3414be6bf707SBarry Smith 
341515091d37SBarry Smith   Level: advanced
341615091d37SBarry Smith 
3417be6bf707SBarry Smith   Common Usage, with SNESSolve():
3418be6bf707SBarry Smith $    Create Jacobian matrix
3419be6bf707SBarry Smith $    Set linear terms into matrix
3420be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3421be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3422be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3423512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3424be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3425be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3426be6bf707SBarry Smith $    In your Jacobian routine
3427be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3428be6bf707SBarry Smith $      Set nonlinear terms in matrix
3429be6bf707SBarry Smith 
3430be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3431be6bf707SBarry Smith $    // build linear portion of Jacobian
3432512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3433be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3434be6bf707SBarry Smith $    loop over nonlinear iterations
3435be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3436be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3437be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3438be6bf707SBarry Smith $       Solve linear system with Jacobian
3439be6bf707SBarry Smith $    endloop
3440be6bf707SBarry Smith 
3441be6bf707SBarry Smith   Notes:
3442be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3443512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3444be6bf707SBarry Smith     calling this routine.
3445be6bf707SBarry Smith 
34460c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
34470c468ba9SBarry Smith     and does not allocated additional space.
34480c468ba9SBarry Smith 
3449be6bf707SBarry Smith .seealso: MatRetrieveValues()
3450be6bf707SBarry Smith 
3451be6bf707SBarry Smith @*/
34527087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3453be6bf707SBarry Smith {
34544ac538c5SBarry Smith   PetscErrorCode ierr;
3455be6bf707SBarry Smith 
3456be6bf707SBarry Smith   PetscFunctionBegin;
34570700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3458e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3459e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
34604ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3461be6bf707SBarry Smith   PetscFunctionReturn(0);
3462be6bf707SBarry Smith }
3463be6bf707SBarry Smith 
34644a2ae208SSatish Balay #undef __FUNCT__
34654a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
34667087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3467be6bf707SBarry Smith {
3468be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
34696849ba73SBarry Smith   PetscErrorCode ierr;
3470d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3471be6bf707SBarry Smith 
3472be6bf707SBarry Smith   PetscFunctionBegin;
3473f23aa3ddSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3474f23aa3ddSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3475be6bf707SBarry Smith   /* copy values over */
347687828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3477be6bf707SBarry Smith   PetscFunctionReturn(0);
3478be6bf707SBarry Smith }
3479be6bf707SBarry Smith 
34804a2ae208SSatish Balay #undef __FUNCT__
34814a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3482be6bf707SBarry Smith /*@
3483be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3484be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3485be6bf707SBarry Smith        nonlinear portion.
3486be6bf707SBarry Smith 
3487be6bf707SBarry Smith    Collect on Mat
3488be6bf707SBarry Smith 
3489be6bf707SBarry Smith   Input Parameters:
3490be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3491be6bf707SBarry Smith 
349215091d37SBarry Smith   Level: advanced
349315091d37SBarry Smith 
3494be6bf707SBarry Smith .seealso: MatStoreValues()
3495be6bf707SBarry Smith 
3496be6bf707SBarry Smith @*/
34977087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3498be6bf707SBarry Smith {
34994ac538c5SBarry Smith   PetscErrorCode ierr;
3500be6bf707SBarry Smith 
3501be6bf707SBarry Smith   PetscFunctionBegin;
35020700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3503e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3504e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
35054ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3506be6bf707SBarry Smith   PetscFunctionReturn(0);
3507be6bf707SBarry Smith }
3508be6bf707SBarry Smith 
3509f83d6046SBarry Smith 
3510be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
35114a2ae208SSatish Balay #undef __FUNCT__
35124a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
351317ab2063SBarry Smith /*@C
3514682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
35150d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
35166e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
351751c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
35182bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
351917ab2063SBarry Smith 
3520db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3521db81eaa0SLois Curfman McInnes 
352217ab2063SBarry Smith    Input Parameters:
3523db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
352417ab2063SBarry Smith .  m - number of rows
352517ab2063SBarry Smith .  n - number of columns
352617ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
352751c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
35280298fd71SBarry Smith          (possibly different for each row) or NULL
352917ab2063SBarry Smith 
353017ab2063SBarry Smith    Output Parameter:
3531416022c9SBarry Smith .  A - the matrix
353217ab2063SBarry Smith 
3533175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3534ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3535175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3536175b88e8SBarry Smith 
3537b259b22eSLois Curfman McInnes    Notes:
353849a6f317SBarry Smith    If nnz is given then nz is ignored
353949a6f317SBarry Smith 
354017ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
354117ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
35420002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
354344cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
354417ab2063SBarry Smith 
354517ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
35460298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
35473d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
35486da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
354917ab2063SBarry Smith 
3550682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
35514fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3552682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
35536c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
35546c7ebb05SLois Curfman McInnes 
35556c7ebb05SLois Curfman McInnes    Options Database Keys:
3556698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
35579db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
355817ab2063SBarry Smith 
3559027ccd11SLois Curfman McInnes    Level: intermediate
3560027ccd11SLois Curfman McInnes 
356169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
356236db0b34SBarry Smith 
356317ab2063SBarry Smith @*/
35647087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
356517ab2063SBarry Smith {
3566dfbe8321SBarry Smith   PetscErrorCode ierr;
35676945ee14SBarry Smith 
35683a40ed3dSBarry Smith   PetscFunctionBegin;
3569f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3570117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3571c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3572d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3573273d9f13SBarry Smith   PetscFunctionReturn(0);
3574273d9f13SBarry Smith }
3575273d9f13SBarry Smith 
35764a2ae208SSatish Balay #undef __FUNCT__
35774a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3578273d9f13SBarry Smith /*@C
3579273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3580273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3581273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3582273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3583273d9f13SBarry Smith 
3584273d9f13SBarry Smith    Collective on MPI_Comm
3585273d9f13SBarry Smith 
3586273d9f13SBarry Smith    Input Parameters:
3587117016b1SBarry Smith +  B - The matrix-free
3588273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3589273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
35900298fd71SBarry Smith          (possibly different for each row) or NULL
3591273d9f13SBarry Smith 
3592273d9f13SBarry Smith    Notes:
359349a6f317SBarry Smith      If nnz is given then nz is ignored
359449a6f317SBarry Smith 
3595273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3596273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3597273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3598273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3599273d9f13SBarry Smith 
3600273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
36010298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3602273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3603273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3604273d9f13SBarry Smith 
3605aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3606aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3607aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3608aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3609aa95bbe8SBarry Smith 
3610a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3611a96a251dSBarry Smith    entries or columns indices
3612a96a251dSBarry Smith 
3613273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3614273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3615273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3616273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3617273d9f13SBarry Smith 
3618273d9f13SBarry Smith    Options Database Keys:
3619698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3620698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3621273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3622273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3623273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3624273d9f13SBarry Smith 
3625273d9f13SBarry Smith    Level: intermediate
3626273d9f13SBarry Smith 
362769b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3628273d9f13SBarry Smith 
3629273d9f13SBarry Smith @*/
36307087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3631273d9f13SBarry Smith {
36324ac538c5SBarry Smith   PetscErrorCode ierr;
3633a23d5eceSKris Buschelman 
3634a23d5eceSKris Buschelman   PetscFunctionBegin;
36356ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
36366ba663aaSJed Brown   PetscValidType(B,1);
36374ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3638a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3639a23d5eceSKris Buschelman }
3640a23d5eceSKris Buschelman 
3641a23d5eceSKris Buschelman #undef __FUNCT__
3642a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
36437087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3644a23d5eceSKris Buschelman {
3645273d9f13SBarry Smith   Mat_SeqAIJ     *b;
36462576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
36476849ba73SBarry Smith   PetscErrorCode ierr;
364897f1f81fSBarry Smith   PetscInt       i;
3649273d9f13SBarry Smith 
3650273d9f13SBarry Smith   PetscFunctionBegin;
36512576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3652a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3653c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3654c461c341SBarry Smith     nz             = 0;
3655c461c341SBarry Smith   }
3656c461c341SBarry Smith 
365726283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
365826283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3659899cda47SBarry Smith 
3660435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3661e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3662b73539f3SBarry Smith   if (nnz) {
3663d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3664e32f2f54SBarry 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]);
3665e32f2f54SBarry 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);
3666b73539f3SBarry Smith     }
3667b73539f3SBarry Smith   }
3668b73539f3SBarry Smith 
3669273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
36702205254eSKarl Rupp 
3671273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3672273d9f13SBarry Smith 
3673ab93d7beSBarry Smith   if (!skipallocation) {
36742ee49352SLisandro Dalcin     if (!b->imax) {
3675d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
36763bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
36772ee49352SLisandro Dalcin     }
3678273d9f13SBarry Smith     if (!nnz) {
3679435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3680c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3681d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3682d0f46423SBarry Smith       nz = nz*B->rmap->n;
3683273d9f13SBarry Smith     } else {
3684273d9f13SBarry Smith       nz = 0;
3685d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3686273d9f13SBarry Smith     }
3687ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
36882205254eSKarl Rupp     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;
3689ab93d7beSBarry Smith 
3690273d9f13SBarry Smith     /* allocate the matrix space */
36912ee49352SLisandro Dalcin     ierr    = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3692d0f46423SBarry Smith     ierr    = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
36933bb1ff40SBarry Smith     ierr    = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3694bfeeae90SHong Zhang     b->i[0] = 0;
3695d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
36965da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
36975da197adSKris Buschelman     }
3698273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3699e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3700e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3701b31eba2aSShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
3702b31eba2aSShri Abhyankar     ierr = MatZeroEntries_SeqAIJ(B);CHKERRQ(ierr);
3703b31eba2aSShri Abhyankar #endif
3704c461c341SBarry Smith   } else {
3705e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
3706e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
3707c461c341SBarry Smith   }
3708273d9f13SBarry Smith 
3709273d9f13SBarry Smith   b->nz               = 0;
3710273d9f13SBarry Smith   b->maxnz            = nz;
3711273d9f13SBarry Smith   B->info.nz_unneeded = (double)b->maxnz;
37122205254eSKarl Rupp   if (realalloc) {
37132205254eSKarl Rupp     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
37142205254eSKarl Rupp   }
3715273d9f13SBarry Smith   PetscFunctionReturn(0);
3716273d9f13SBarry Smith }
3717273d9f13SBarry Smith 
3718a1661176SMatthew Knepley #undef  __FUNCT__
3719a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
372058d36128SBarry Smith /*@
3721a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3722a1661176SMatthew Knepley 
3723a1661176SMatthew Knepley    Input Parameters:
3724a1661176SMatthew Knepley +  B - the matrix
3725a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3726a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3727a1661176SMatthew Knepley -  v - optional values in the matrix
3728a1661176SMatthew Knepley 
3729a1661176SMatthew Knepley    Level: developer
3730a1661176SMatthew Knepley 
373158d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
373258d36128SBarry Smith 
3733a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3734a1661176SMatthew Knepley 
3735a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3736a1661176SMatthew Knepley @*/
3737a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3738a1661176SMatthew Knepley {
3739a1661176SMatthew Knepley   PetscErrorCode ierr;
3740a1661176SMatthew Knepley 
3741a1661176SMatthew Knepley   PetscFunctionBegin;
37420700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
37436ba663aaSJed Brown   PetscValidType(B,1);
37444ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3745a1661176SMatthew Knepley   PetscFunctionReturn(0);
3746a1661176SMatthew Knepley }
3747a1661176SMatthew Knepley 
3748a1661176SMatthew Knepley #undef  __FUNCT__
3749a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
37507087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3751a1661176SMatthew Knepley {
3752a1661176SMatthew Knepley   PetscInt       i;
3753a1661176SMatthew Knepley   PetscInt       m,n;
3754a1661176SMatthew Knepley   PetscInt       nz;
3755a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3756a1661176SMatthew Knepley   PetscScalar    *values;
3757a1661176SMatthew Knepley   PetscErrorCode ierr;
3758a1661176SMatthew Knepley 
3759a1661176SMatthew Knepley   PetscFunctionBegin;
376065e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3761779a8d59SSatish Balay 
3762779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3763779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3764779a8d59SSatish Balay 
3765779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3766a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3767a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3768b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3769a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
377065e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3771a1661176SMatthew Knepley     nnz[i] = nz;
3772a1661176SMatthew Knepley   }
3773a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3774a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3775a1661176SMatthew Knepley 
3776a1661176SMatthew Knepley   if (v) {
3777a1661176SMatthew Knepley     values = (PetscScalar*) v;
3778a1661176SMatthew Knepley   } else {
37790e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3780a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3781a1661176SMatthew Knepley   }
3782a1661176SMatthew Knepley 
3783a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3784b7940d39SSatish Balay     nz   = Ii[i+1] - Ii[i];
3785b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3786a1661176SMatthew Knepley   }
3787a1661176SMatthew Knepley 
3788a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3789a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3790a1661176SMatthew Knepley 
3791a1661176SMatthew Knepley   if (!v) {
3792a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3793a1661176SMatthew Knepley   }
37947827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3795a1661176SMatthew Knepley   PetscFunctionReturn(0);
3796a1661176SMatthew Knepley }
3797a1661176SMatthew Knepley 
3798c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
379906873bf2SBarry Smith #include <petsc-private/kernels/petscaxpy.h>
3800170fe5c8SBarry Smith 
3801170fe5c8SBarry Smith #undef __FUNCT__
3802170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3803170fe5c8SBarry Smith /*
3804170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3805170fe5c8SBarry Smith 
3806170fe5c8SBarry Smith                n                       p                          p
3807170fe5c8SBarry Smith         (              )       (              )         (                  )
3808170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3809170fe5c8SBarry Smith         (              )       (              )         (                  )
3810170fe5c8SBarry Smith 
3811170fe5c8SBarry Smith */
3812170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3813170fe5c8SBarry Smith {
3814170fe5c8SBarry Smith   PetscErrorCode    ierr;
3815170fe5c8SBarry Smith   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3816170fe5c8SBarry Smith   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3817170fe5c8SBarry Smith   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
38181de00fd4SBarry Smith   PetscInt          i,n,m,q,p;
3819170fe5c8SBarry Smith   const PetscInt    *ii,*idx;
3820170fe5c8SBarry Smith   const PetscScalar *b,*a,*a_q;
3821170fe5c8SBarry Smith   PetscScalar       *c,*c_q;
3822170fe5c8SBarry Smith 
3823170fe5c8SBarry Smith   PetscFunctionBegin;
3824d0f46423SBarry Smith   m    = A->rmap->n;
3825d0f46423SBarry Smith   n    = A->cmap->n;
3826d0f46423SBarry Smith   p    = B->cmap->n;
3827170fe5c8SBarry Smith   a    = sub_a->v;
3828170fe5c8SBarry Smith   b    = sub_b->a;
3829170fe5c8SBarry Smith   c    = sub_c->v;
3830170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3831170fe5c8SBarry Smith 
3832170fe5c8SBarry Smith   ii  = sub_b->i;
3833170fe5c8SBarry Smith   idx = sub_b->j;
3834170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3835170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3836170fe5c8SBarry Smith     while (q-->0) {
3837170fe5c8SBarry Smith       c_q = c + m*(*idx);
3838170fe5c8SBarry Smith       a_q = a + m*i;
3839854c7f52SBarry Smith       PetscKernelAXPY(c_q,*b,a_q,m);
3840170fe5c8SBarry Smith       idx++;
3841170fe5c8SBarry Smith       b++;
3842170fe5c8SBarry Smith     }
3843170fe5c8SBarry Smith   }
3844170fe5c8SBarry Smith   PetscFunctionReturn(0);
3845170fe5c8SBarry Smith }
3846170fe5c8SBarry Smith 
3847170fe5c8SBarry Smith #undef __FUNCT__
3848170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3849170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3850170fe5c8SBarry Smith {
3851170fe5c8SBarry Smith   PetscErrorCode ierr;
3852d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3853170fe5c8SBarry Smith   Mat            Cmat;
3854170fe5c8SBarry Smith 
3855170fe5c8SBarry Smith   PetscFunctionBegin;
3856e32f2f54SBarry 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);
3857ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
3858170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3859a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
3860170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
38610298fd71SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
3862d73949e8SHong Zhang 
3863d73949e8SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
38642205254eSKarl Rupp 
3865170fe5c8SBarry Smith   *C = Cmat;
3866170fe5c8SBarry Smith   PetscFunctionReturn(0);
3867170fe5c8SBarry Smith }
3868170fe5c8SBarry Smith 
3869170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3870170fe5c8SBarry Smith #undef __FUNCT__
3871170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3872170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3873170fe5c8SBarry Smith {
3874170fe5c8SBarry Smith   PetscErrorCode ierr;
3875170fe5c8SBarry Smith 
3876170fe5c8SBarry Smith   PetscFunctionBegin;
3877170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
38783ff4c91cSHong Zhang     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3879170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
38803ff4c91cSHong Zhang     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3881170fe5c8SBarry Smith   }
38823ff4c91cSHong Zhang   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3883170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
38843ff4c91cSHong Zhang   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3885170fe5c8SBarry Smith   PetscFunctionReturn(0);
3886170fe5c8SBarry Smith }
3887170fe5c8SBarry Smith 
3888170fe5c8SBarry Smith 
38890bad9183SKris Buschelman /*MC
3890fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
38910bad9183SKris Buschelman    based on compressed sparse row format.
38920bad9183SKris Buschelman 
38930bad9183SKris Buschelman    Options Database Keys:
38940bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
38950bad9183SKris Buschelman 
38960bad9183SKris Buschelman   Level: beginner
38970bad9183SKris Buschelman 
3898f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
38990bad9183SKris Buschelman M*/
39000bad9183SKris Buschelman 
3901ccd284c7SBarry Smith /*MC
3902ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
3903ccd284c7SBarry Smith 
3904ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
3905ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
3906ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
3907ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3908ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3909ccd284c7SBarry Smith 
3910ccd284c7SBarry Smith    Options Database Keys:
3911ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
3912ccd284c7SBarry Smith 
3913ccd284c7SBarry Smith   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
3914ccd284c7SBarry Smith    enough exist.
3915ccd284c7SBarry Smith 
3916ccd284c7SBarry Smith   Level: beginner
3917ccd284c7SBarry Smith 
3918ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3919ccd284c7SBarry Smith M*/
3920ccd284c7SBarry Smith 
3921ccd284c7SBarry Smith /*MC
3922ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
3923ccd284c7SBarry Smith 
3924ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3925ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3926ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3927ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3928ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3929ccd284c7SBarry Smith 
3930ccd284c7SBarry Smith    Options Database Keys:
3931ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3932ccd284c7SBarry Smith 
3933ccd284c7SBarry Smith   Level: beginner
3934ccd284c7SBarry Smith 
3935ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3936ccd284c7SBarry Smith M*/
3937ccd284c7SBarry Smith 
3938b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
39398cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3940b5e56a35SBarry Smith #endif
3941ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
39428cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat*);
3943af1023dbSSatish Balay #endif
39448cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
39458cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
39468cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
39477087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool*);
3948611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
39498cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3950611f576cSBarry Smith #endif
3951611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
39528cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3953611f576cSBarry Smith #endif
3954f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
39558cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3956f3c0ef26SHong Zhang #endif
3957eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
39588cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3959eb3b5408SSatish Balay #endif
3960586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
39618cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3962586621ddSJed Brown #endif
3963719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
39648cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3965719d5645SBarry Smith #endif
3966b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
39678cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
39687087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
39697087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3970b3866ffcSBarry Smith #endif
397117f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
39728cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
397317f1a0eaSHong Zhang #endif
397417667f90SBarry Smith 
3975c0c8ee5eSDmitry Karpeev 
39768c778c55SBarry Smith #undef __FUNCT__
39778c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray"
39788c778c55SBarry Smith /*@C
39798c778c55SBarry Smith    MatSeqAIJGetArray - gives access to the array where the data for a SeqSeqAIJ matrix is stored
39808c778c55SBarry Smith 
39818c778c55SBarry Smith    Not Collective
39828c778c55SBarry Smith 
39838c778c55SBarry Smith    Input Parameter:
39848c778c55SBarry Smith .  mat - a MATSEQDENSE matrix
39858c778c55SBarry Smith 
39868c778c55SBarry Smith    Output Parameter:
39878c778c55SBarry Smith .   array - pointer to the data
39888c778c55SBarry Smith 
39898c778c55SBarry Smith    Level: intermediate
39908c778c55SBarry Smith 
3991774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
39928c778c55SBarry Smith @*/
39938c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
39948c778c55SBarry Smith {
39958c778c55SBarry Smith   PetscErrorCode ierr;
39968c778c55SBarry Smith 
39978c778c55SBarry Smith   PetscFunctionBegin;
39988c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
39998c778c55SBarry Smith   PetscFunctionReturn(0);
40008c778c55SBarry Smith }
40018c778c55SBarry Smith 
40028c778c55SBarry Smith #undef __FUNCT__
40038c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray"
40048c778c55SBarry Smith /*@C
40058c778c55SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a SeqSeqAIJ matrix is stored obtained by MatSeqAIJGetArray()
40068c778c55SBarry Smith 
40078c778c55SBarry Smith    Not Collective
40088c778c55SBarry Smith 
40098c778c55SBarry Smith    Input Parameters:
40108c778c55SBarry Smith .  mat - a MATSEQDENSE matrix
40118c778c55SBarry Smith .  array - pointer to the data
40128c778c55SBarry Smith 
40138c778c55SBarry Smith    Level: intermediate
40148c778c55SBarry Smith 
4015774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
40168c778c55SBarry Smith @*/
40178c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
40188c778c55SBarry Smith {
40198c778c55SBarry Smith   PetscErrorCode ierr;
40208c778c55SBarry Smith 
40218c778c55SBarry Smith   PetscFunctionBegin;
40228c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
40238c778c55SBarry Smith   PetscFunctionReturn(0);
40248c778c55SBarry Smith }
40258c778c55SBarry Smith 
40264a2ae208SSatish Balay #undef __FUNCT__
40274a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
40288cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4029273d9f13SBarry Smith {
4030273d9f13SBarry Smith   Mat_SeqAIJ     *b;
4031dfbe8321SBarry Smith   PetscErrorCode ierr;
403238baddfdSBarry Smith   PetscMPIInt    size;
4033273d9f13SBarry Smith 
4034273d9f13SBarry Smith   PetscFunctionBegin;
4035ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
4036e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
4037273d9f13SBarry Smith 
403838f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
40392205254eSKarl Rupp 
4040b0a32e0cSBarry Smith   B->data = (void*)b;
40412205254eSKarl Rupp 
4042549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
40432205254eSKarl Rupp 
4044416022c9SBarry Smith   b->row                = 0;
4045416022c9SBarry Smith   b->col                = 0;
404682bf6240SBarry Smith   b->icol               = 0;
4047b810aeb4SBarry Smith   b->reallocs           = 0;
404836db0b34SBarry Smith   b->ignorezeroentries  = PETSC_FALSE;
4049f1e2ffcdSBarry Smith   b->roworiented        = PETSC_TRUE;
4050416022c9SBarry Smith   b->nonew              = 0;
4051416022c9SBarry Smith   b->diag               = 0;
4052416022c9SBarry Smith   b->solve_work         = 0;
40532a1b7f2aSHong Zhang   B->spptr              = 0;
4054be6bf707SBarry Smith   b->saved_values       = 0;
4055d7f994e1SBarry Smith   b->idiag              = 0;
405671f1c65dSBarry Smith   b->mdiag              = 0;
405771f1c65dSBarry Smith   b->ssor_work          = 0;
405871f1c65dSBarry Smith   b->omega              = 1.0;
405971f1c65dSBarry Smith   b->fshift             = 0.0;
406071f1c65dSBarry Smith   b->idiagvalid         = PETSC_FALSE;
4061bbead8a2SBarry Smith   b->ibdiagvalid        = PETSC_FALSE;
4062a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
4063a30b2313SHong Zhang   b->xtoy               = 0;
4064a30b2313SHong Zhang   b->XtoY               = 0;
406588e51ccdSHong Zhang   B->same_nonzero       = PETSC_FALSE;
406617ab2063SBarry Smith 
406735d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
4068bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
4069bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
40708c778c55SBarry Smith 
4071b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4072bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_matlab_C",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
4073bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
4074bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
4075b3866ffcSBarry Smith #endif
4076b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
4077bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
4078b5e56a35SBarry Smith #endif
4079ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
4080bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_essl_C",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
4081719d5645SBarry Smith #endif
4082611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
4083bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_C",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
4084611f576cSBarry Smith #endif
4085f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
4086bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_dist_C",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
4087f3c0ef26SHong Zhang #endif
4088611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
4089bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_aij_mumps);CHKERRQ(ierr);
4090611f576cSBarry Smith #endif
4091eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
4092bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_umfpack_C",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
4093eb3b5408SSatish Balay #endif
4094586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
4095bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_cholmod_C",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
4096586621ddSJed Brown #endif
4097719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
4098bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_lusol_C",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
4099719d5645SBarry Smith #endif
410017f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
4101bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_clique_C",MatGetFactor_aij_clique);CHKERRQ(ierr);
410217f1a0eaSHong Zhang #endif
410317f1a0eaSHong Zhang 
4104bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_petsc_C",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
4105bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactorAvailable_petsc_C",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
4106bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_bas_C",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
4107bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
4108bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
4109bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
4110bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
4111bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
4112bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
4113bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
4114bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4115bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4116bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
4117bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
4118bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
4119bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
4120bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
4121bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
41224108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
412317667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
41243a40ed3dSBarry Smith   PetscFunctionReturn(0);
412517ab2063SBarry Smith }
412617ab2063SBarry Smith 
41274a2ae208SSatish Balay #undef __FUNCT__
4128b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
4129b24902e0SBarry Smith /*
4130b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4131b24902e0SBarry Smith */
4132ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
413317ab2063SBarry Smith {
4134416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
41356849ba73SBarry Smith   PetscErrorCode ierr;
4136d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
413717ab2063SBarry Smith 
41383a40ed3dSBarry Smith   PetscFunctionBegin;
4139273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4140273d9f13SBarry Smith 
4141d5f3da31SBarry Smith   C->factortype = A->factortype;
4142416022c9SBarry Smith   c->row        = 0;
4143416022c9SBarry Smith   c->col        = 0;
414482bf6240SBarry Smith   c->icol       = 0;
41456ad4291fSHong Zhang   c->reallocs   = 0;
414617ab2063SBarry Smith 
41476ad4291fSHong Zhang   C->assembled = PETSC_TRUE;
414817ab2063SBarry Smith 
4149aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4150aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4151eec197d1SBarry Smith 
415233b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
41533bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
415417ab2063SBarry Smith   for (i=0; i<m; i++) {
4155416022c9SBarry Smith     c->imax[i] = a->imax[i];
4156416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
415717ab2063SBarry Smith   }
415817ab2063SBarry Smith 
415917ab2063SBarry Smith   /* allocate the matrix space */
4160f77e22a1SHong Zhang   if (mallocmatspace) {
4161a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
41623bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
41632205254eSKarl Rupp 
4164f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
41652205254eSKarl Rupp 
416697f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
416717ab2063SBarry Smith     if (m > 0) {
416897f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4169be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4170bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4171be6bf707SBarry Smith       } else {
4172bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
417317ab2063SBarry Smith       }
417408480c60SBarry Smith     }
4175f77e22a1SHong Zhang   }
417617ab2063SBarry Smith 
41776ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4178416022c9SBarry Smith   c->roworiented       = a->roworiented;
4179416022c9SBarry Smith   c->nonew             = a->nonew;
4180416022c9SBarry Smith   if (a->diag) {
418197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
41823bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
418317ab2063SBarry Smith     for (i=0; i<m; i++) {
4184416022c9SBarry Smith       c->diag[i] = a->diag[i];
418517ab2063SBarry Smith     }
41863a40ed3dSBarry Smith   } else c->diag = 0;
41872205254eSKarl Rupp 
41886ad4291fSHong Zhang   c->solve_work         = 0;
41896ad4291fSHong Zhang   c->saved_values       = 0;
41906ad4291fSHong Zhang   c->idiag              = 0;
419171f1c65dSBarry Smith   c->ssor_work          = 0;
4192a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
4193e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
4194e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
41956ad4291fSHong Zhang   c->xtoy               = 0;
41966ad4291fSHong Zhang   c->XtoY               = 0;
41976ad4291fSHong Zhang 
4198893ad86cSHong Zhang   c->rmax         = a->rmax;
4199416022c9SBarry Smith   c->nz           = a->nz;
42008ed568f8SMatthew G Knepley   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4201273d9f13SBarry Smith   C->preallocated = PETSC_TRUE;
4202754ec7b1SSatish Balay 
42036ad4291fSHong Zhang   c->compressedrow.use   = a->compressedrow.use;
42046ad4291fSHong Zhang   c->compressedrow.nrows = a->compressedrow.nrows;
4205cd6b891eSBarry Smith   c->compressedrow.check = a->compressedrow.check;
4206cd6b891eSBarry Smith   if (a->compressedrow.use) {
42076ad4291fSHong Zhang     i    = a->compressedrow.nrows;
42080e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
42096ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
42106ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
421127ea64f8SHong Zhang   } else {
421227ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
42130298fd71SBarry Smith     c->compressedrow.i      = NULL;
42140298fd71SBarry Smith     c->compressedrow.rindex = NULL;
42156ad4291fSHong Zhang   }
421688e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
42174846f1f5SKris Buschelman 
42182205254eSKarl Rupp   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4219140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
42203a40ed3dSBarry Smith   PetscFunctionReturn(0);
422117ab2063SBarry Smith }
422217ab2063SBarry Smith 
42234a2ae208SSatish Balay #undef __FUNCT__
4224b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
4225b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4226b24902e0SBarry Smith {
4227b24902e0SBarry Smith   PetscErrorCode ierr;
4228b24902e0SBarry Smith 
4229b24902e0SBarry Smith   PetscFunctionBegin;
4230ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
42314b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4232a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
4233a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4234f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4235b24902e0SBarry Smith   PetscFunctionReturn(0);
4236b24902e0SBarry Smith }
4237b24902e0SBarry Smith 
4238b24902e0SBarry Smith #undef __FUNCT__
42394a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
4240112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4241fbdbba38SShri Abhyankar {
4242fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4243fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4244fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4245fbdbba38SShri Abhyankar   int            fd;
4246fbdbba38SShri Abhyankar   PetscMPIInt    size;
4247fbdbba38SShri Abhyankar   MPI_Comm       comm;
4248bbead8a2SBarry Smith   PetscInt       bs = 1;
4249fbdbba38SShri Abhyankar 
4250fbdbba38SShri Abhyankar   PetscFunctionBegin;
4251fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4252fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4253fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4254bbead8a2SBarry Smith 
42550298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
42560298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
4257bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
42581814747fSJed Brown   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
4259bbead8a2SBarry Smith 
4260fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4261fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4262fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4263fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4264fbdbba38SShri Abhyankar 
4265bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4266fbdbba38SShri Abhyankar 
4267fbdbba38SShri Abhyankar   /* read in row lengths */
4268fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
4269fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4270fbdbba38SShri Abhyankar 
4271fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4272fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
4273fbdbba38SShri 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);
4274fbdbba38SShri Abhyankar 
4275fbdbba38SShri Abhyankar   /* set global size if not set already*/
4276f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4277fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4278aabbc4fbSShri Abhyankar   } else {
4279fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
4280fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
42814c5b953cSHong Zhang     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
42824c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
42834c5b953cSHong Zhang     }
4284f501eaabSShri 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);
4285aabbc4fbSShri Abhyankar   }
4286fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4287fbdbba38SShri Abhyankar   a    = (Mat_SeqAIJ*)newMat->data;
4288fbdbba38SShri Abhyankar 
4289fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4290fbdbba38SShri Abhyankar 
4291fbdbba38SShri Abhyankar   /* read in nonzero values */
4292fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4293fbdbba38SShri Abhyankar 
4294fbdbba38SShri Abhyankar   /* set matrix "i" values */
4295fbdbba38SShri Abhyankar   a->i[0] = 0;
4296fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4297fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4298fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4299fbdbba38SShri Abhyankar   }
4300fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4301fbdbba38SShri Abhyankar 
4302fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4303fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4304fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4305fbdbba38SShri Abhyankar }
4306fbdbba38SShri Abhyankar 
4307fbdbba38SShri Abhyankar #undef __FUNCT__
4308b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4309ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
43107264ac53SSatish Balay {
43117264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4312dfbe8321SBarry Smith   PetscErrorCode ierr;
4313eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4314eeffb40dSHong Zhang   PetscInt k;
4315eeffb40dSHong Zhang #endif
43167264ac53SSatish Balay 
43173a40ed3dSBarry Smith   PetscFunctionBegin;
4318bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4319d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4320ca44d042SBarry Smith     *flg = PETSC_FALSE;
4321ca44d042SBarry Smith     PetscFunctionReturn(0);
4322bcd2baecSBarry Smith   }
43237264ac53SSatish Balay 
43247264ac53SSatish Balay   /* if the a->i are the same */
4325d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4326abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
43277264ac53SSatish Balay 
43287264ac53SSatish Balay   /* if a->j are the same */
432997f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4330abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4331bcd2baecSBarry Smith 
4332bcd2baecSBarry Smith   /* if a->a are the same */
4333eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4334eeffb40dSHong Zhang   for (k=0; k<a->nz; k++) {
4335eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4336eeffb40dSHong Zhang       *flg = PETSC_FALSE;
43373a40ed3dSBarry Smith       PetscFunctionReturn(0);
4338eeffb40dSHong Zhang     }
4339eeffb40dSHong Zhang   }
4340eeffb40dSHong Zhang #else
4341eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4342eeffb40dSHong Zhang #endif
4343eeffb40dSHong Zhang   PetscFunctionReturn(0);
43447264ac53SSatish Balay }
434536db0b34SBarry Smith 
43464a2ae208SSatish Balay #undef __FUNCT__
43474a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
434805869f15SSatish Balay /*@
434936db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
435036db0b34SBarry Smith               provided by the user.
435136db0b34SBarry Smith 
4352c75a6043SHong Zhang       Collective on MPI_Comm
435336db0b34SBarry Smith 
435436db0b34SBarry Smith    Input Parameters:
435536db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
435636db0b34SBarry Smith .   m - number of rows
435736db0b34SBarry Smith .   n - number of columns
435836db0b34SBarry Smith .   i - row indices
435936db0b34SBarry Smith .   j - column indices
436036db0b34SBarry Smith -   a - matrix values
436136db0b34SBarry Smith 
436236db0b34SBarry Smith    Output Parameter:
436336db0b34SBarry Smith .   mat - the matrix
436436db0b34SBarry Smith 
436536db0b34SBarry Smith    Level: intermediate
436636db0b34SBarry Smith 
436736db0b34SBarry Smith    Notes:
43680551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4369292fb18eSBarry Smith     once the matrix is destroyed and not before
437036db0b34SBarry Smith 
437136db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
437236db0b34SBarry Smith 
4373bfeeae90SHong Zhang        The i and j indices are 0 based
437436db0b34SBarry Smith 
4375a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4376a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
4377a4552177SSatish Balay     as shown:
4378a4552177SSatish Balay 
4379a4552177SSatish Balay         1 0 0
4380a4552177SSatish Balay         2 0 3
4381a4552177SSatish Balay         4 5 6
4382a4552177SSatish Balay 
4383a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
43849985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4385a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
4386a4552177SSatish Balay 
43879985e31cSBarry Smith 
438869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
438936db0b34SBarry Smith 
439036db0b34SBarry Smith @*/
43917087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
439236db0b34SBarry Smith {
4393dfbe8321SBarry Smith   PetscErrorCode ierr;
4394cbcfb4deSHong Zhang   PetscInt       ii;
439536db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4396cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4397cbcfb4deSHong Zhang   PetscInt jj;
4398cbcfb4deSHong Zhang #endif
439936db0b34SBarry Smith 
440036db0b34SBarry Smith   PetscFunctionBegin;
4401f23aa3ddSBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4402f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4403f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4404a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4405ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4406ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4407ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4408ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
4409ab93d7beSBarry Smith 
441036db0b34SBarry Smith   aij->i            = i;
441136db0b34SBarry Smith   aij->j            = j;
441236db0b34SBarry Smith   aij->a            = a;
441336db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
441436db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4415e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4416e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
441736db0b34SBarry Smith 
441836db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
441936db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
44202515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
4421e32f2f54SBarry 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]);
44229985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4423e32f2f54SBarry 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);
4424e32f2f54SBarry 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);
44259985e31cSBarry Smith     }
442636db0b34SBarry Smith #endif
442736db0b34SBarry Smith   }
44282515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
442936db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
4430e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
4431e32f2f54SBarry 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]);
443236db0b34SBarry Smith   }
443336db0b34SBarry Smith #endif
443436db0b34SBarry Smith 
4435b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4436b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
443736db0b34SBarry Smith   PetscFunctionReturn(0);
443836db0b34SBarry Smith }
44398a0b0e6bSVictor Minden #undef __FUNCT__
44408a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
444180ef6e79SMatthew G Knepley /*@C
4442d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
44438a0b0e6bSVictor Minden               provided by the user.
44448a0b0e6bSVictor Minden 
44458a0b0e6bSVictor Minden       Collective on MPI_Comm
44468a0b0e6bSVictor Minden 
44478a0b0e6bSVictor Minden    Input Parameters:
44488a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
44498a0b0e6bSVictor Minden .   m   - number of rows
44508a0b0e6bSVictor Minden .   n   - number of columns
44518a0b0e6bSVictor Minden .   i   - row indices
44528a0b0e6bSVictor Minden .   j   - column indices
44531230e6d1SVictor Minden .   a   - matrix values
44541230e6d1SVictor Minden .   nz  - number of nonzeros
44551230e6d1SVictor Minden -   idx - 0 or 1 based
44568a0b0e6bSVictor Minden 
44578a0b0e6bSVictor Minden    Output Parameter:
44588a0b0e6bSVictor Minden .   mat - the matrix
44598a0b0e6bSVictor Minden 
44608a0b0e6bSVictor Minden    Level: intermediate
44618a0b0e6bSVictor Minden 
44628a0b0e6bSVictor Minden    Notes:
44638a0b0e6bSVictor Minden        The i and j indices are 0 based
44648a0b0e6bSVictor Minden 
44658a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
44668a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
44678a0b0e6bSVictor Minden     as shown:
44688a0b0e6bSVictor Minden 
44698a0b0e6bSVictor Minden         1 0 0
44708a0b0e6bSVictor Minden         2 0 3
44718a0b0e6bSVictor Minden         4 5 6
44728a0b0e6bSVictor Minden 
44738a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
44748a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
44758a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
44768a0b0e6bSVictor Minden 
44778a0b0e6bSVictor Minden 
447869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
44798a0b0e6bSVictor Minden 
44808a0b0e6bSVictor Minden @*/
44811230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
44828a0b0e6bSVictor Minden {
44838a0b0e6bSVictor Minden   PetscErrorCode ierr;
4484d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
44858a0b0e6bSVictor Minden 
44868a0b0e6bSVictor Minden 
44878a0b0e6bSVictor Minden   PetscFunctionBegin;
4488d021a1c5SVictor Minden   ierr = PetscMalloc(m*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
44891230e6d1SVictor Minden   ierr = PetscMemzero(nnz,m*sizeof(PetscInt));CHKERRQ(ierr);
44901230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
44911230e6d1SVictor Minden     nnz[i[ii]] += 1;
44921230e6d1SVictor Minden   }
44938a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
44948a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4495a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
44968a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
44971230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
44981230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
44991230e6d1SVictor Minden     if (idx) {
45001230e6d1SVictor Minden       row = i[ii] - 1;
45011230e6d1SVictor Minden       col = j[ii] - 1;
45021230e6d1SVictor Minden     } else {
45031230e6d1SVictor Minden       row = i[ii];
45041230e6d1SVictor Minden       col = j[ii];
45058a0b0e6bSVictor Minden     }
45061230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
45078a0b0e6bSVictor Minden   }
45088a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
45098a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4510d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
45118a0b0e6bSVictor Minden   PetscFunctionReturn(0);
45128a0b0e6bSVictor Minden }
451336db0b34SBarry Smith 
4514cc8ba8e1SBarry Smith #undef __FUNCT__
4515ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4516dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4517cc8ba8e1SBarry Smith {
4518dfbe8321SBarry Smith   PetscErrorCode ierr;
4519cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
452036db0b34SBarry Smith 
4521cc8ba8e1SBarry Smith   PetscFunctionBegin;
45228ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4523cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4524cc8ba8e1SBarry Smith     a->coloring = coloring;
452512c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
452697f1f81fSBarry Smith     PetscInt        i,*larray;
452712c595b3SBarry Smith     ISColoring      ocoloring;
452808b6dcc0SBarry Smith     ISColoringValue *colors;
452912c595b3SBarry Smith 
453012c595b3SBarry Smith     /* set coloring for diagonal portion */
45310e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
45322205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) larray[i] = i;
45330298fd71SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);CHKERRQ(ierr);
45340e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
45352205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
453612c595b3SBarry Smith     ierr        = PetscFree(larray);CHKERRQ(ierr);
4537d0f46423SBarry Smith     ierr        = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
453812c595b3SBarry Smith     a->coloring = ocoloring;
453912c595b3SBarry Smith   }
4540cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4541cc8ba8e1SBarry Smith }
4542cc8ba8e1SBarry Smith 
4543ee4f033dSBarry Smith #undef __FUNCT__
4544ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
454597f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4546ee4f033dSBarry Smith {
4547ee4f033dSBarry Smith   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
4548d0f46423SBarry Smith   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
454954f21887SBarry Smith   MatScalar       *v      = a->a;
455054f21887SBarry Smith   PetscScalar     *values = (PetscScalar*)advalues;
455108b6dcc0SBarry Smith   ISColoringValue *color;
4552ee4f033dSBarry Smith 
4553ee4f033dSBarry Smith   PetscFunctionBegin;
4554e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4555ee4f033dSBarry Smith   color = a->coloring->colors;
4556ee4f033dSBarry Smith   /* loop over rows */
4557ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4558ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4559ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
45602205254eSKarl Rupp     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
4561ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4562cc8ba8e1SBarry Smith   }
4563cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4564cc8ba8e1SBarry Smith }
456536db0b34SBarry Smith 
4566acf2f550SJed Brown #undef __FUNCT__
4567acf2f550SJed Brown #define __FUNCT__ "MatSeqAIJInvalidateDiagonal"
4568acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4569acf2f550SJed Brown {
4570acf2f550SJed Brown   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4571acf2f550SJed Brown   PetscErrorCode ierr;
4572acf2f550SJed Brown 
4573acf2f550SJed Brown   PetscFunctionBegin;
4574acf2f550SJed Brown   a->idiagvalid  = PETSC_FALSE;
4575acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
45762205254eSKarl Rupp 
4577acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4578acf2f550SJed Brown   PetscFunctionReturn(0);
4579acf2f550SJed Brown }
4580acf2f550SJed Brown 
458181824310SBarry Smith /*
458281824310SBarry Smith     Special version for direct calls from Fortran
458381824310SBarry Smith */
4584b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
458581824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
458681824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
458781824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
458881824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
458981824310SBarry Smith #endif
459081824310SBarry Smith 
459181824310SBarry Smith /* Change these macros so can be used in void function */
459281824310SBarry Smith #undef CHKERRQ
4593ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
459481824310SBarry Smith #undef SETERRQ2
4595e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
45964994cf47SJed Brown #undef SETERRQ3
45974994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
459881824310SBarry Smith 
459981824310SBarry Smith #undef __FUNCT__
460081824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
46018cc058d9SJed Brown PETSC_EXTERN void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
460281824310SBarry Smith {
460381824310SBarry Smith   Mat            A  = *AA;
460481824310SBarry Smith   PetscInt       m  = *mm, n = *nn;
460581824310SBarry Smith   InsertMode     is = *isis;
460681824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
460781824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
460881824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
460981824310SBarry Smith   PetscErrorCode ierr;
461081824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
461154f21887SBarry Smith   MatScalar      *ap,value,*aa;
4612ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4613ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
461481824310SBarry Smith 
461581824310SBarry Smith   PetscFunctionBegin;
46164994cf47SJed Brown   MatCheckPreallocated(A,1);
461781824310SBarry Smith   imax  = a->imax;
461881824310SBarry Smith   ai    = a->i;
461981824310SBarry Smith   ailen = a->ilen;
462081824310SBarry Smith   aj    = a->j;
462181824310SBarry Smith   aa    = a->a;
462281824310SBarry Smith 
462381824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
462481824310SBarry Smith     row = im[k];
462581824310SBarry Smith     if (row < 0) continue;
462681824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4627ce94432eSBarry Smith     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
462881824310SBarry Smith #endif
462981824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
463081824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
463181824310SBarry Smith     low  = 0;
463281824310SBarry Smith     high = nrow;
463381824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
463481824310SBarry Smith       if (in[l] < 0) continue;
463581824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4636ce94432eSBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
463781824310SBarry Smith #endif
463881824310SBarry Smith       col = in[l];
46392205254eSKarl Rupp       if (roworiented) value = v[l + k*n];
46402205254eSKarl Rupp       else value = v[k + l*m];
46412205254eSKarl Rupp 
464281824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
464381824310SBarry Smith 
46442205254eSKarl Rupp       if (col <= lastcol) low = 0;
46452205254eSKarl Rupp       else high = nrow;
464681824310SBarry Smith       lastcol = col;
464781824310SBarry Smith       while (high-low > 5) {
464881824310SBarry Smith         t = (low+high)/2;
464981824310SBarry Smith         if (rp[t] > col) high = t;
465081824310SBarry Smith         else             low  = t;
465181824310SBarry Smith       }
465281824310SBarry Smith       for (i=low; i<high; i++) {
465381824310SBarry Smith         if (rp[i] > col) break;
465481824310SBarry Smith         if (rp[i] == col) {
465581824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
465681824310SBarry Smith           else                  ap[i] = value;
465781824310SBarry Smith           goto noinsert;
465881824310SBarry Smith         }
465981824310SBarry Smith       }
466081824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
466181824310SBarry Smith       if (nonew == 1) goto noinsert;
4662ce94432eSBarry Smith       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4663fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
466481824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
466581824310SBarry Smith       /* shift up all the later entries in this row */
466681824310SBarry Smith       for (ii=N; ii>=i; ii--) {
466781824310SBarry Smith         rp[ii+1] = rp[ii];
466881824310SBarry Smith         ap[ii+1] = ap[ii];
466981824310SBarry Smith       }
467081824310SBarry Smith       rp[i] = col;
467181824310SBarry Smith       ap[i] = value;
467281824310SBarry Smith noinsert:;
467381824310SBarry Smith       low = i + 1;
467481824310SBarry Smith     }
467581824310SBarry Smith     ailen[row] = nrow;
467681824310SBarry Smith   }
467781824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
467881824310SBarry Smith   PetscFunctionReturnVoid();
467981824310SBarry Smith }
46809f7953f8SBarry Smith 
468162298a1eSBarry Smith 
4682