xref: /petsc/src/mat/impls/aij/seq/aij.c (revision ce94432eddcd14845bc7e8083b7f8ea723b9bf7d)
1b377110cSBarry Smith 
2d5d45c9bSBarry Smith /*
33369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
4d5d45c9bSBarry Smith   matrix storage format.
5d5d45c9bSBarry Smith */
63369ce9aSBarry Smith 
77c4f633dSBarry Smith 
8c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
9c6db04a5SJed Brown #include <petscblaslapack.h>
10c6db04a5SJed Brown #include <petscbt.h>
11d441b888SJed Brown #include <../src/mat/blocktranspose.h>
1278b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
1378b84d54SShri Abhyankar #include <petscthreadcomm.h>
1478b84d54SShri Abhyankar #endif
150716a85fSBarry Smith 
160716a85fSBarry Smith #undef __FUNCT__
170716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_SeqAIJ"
180716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
190716a85fSBarry Smith {
200716a85fSBarry Smith   PetscErrorCode ierr;
210716a85fSBarry Smith   PetscInt       i,m,n;
220716a85fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
230716a85fSBarry Smith 
240716a85fSBarry Smith   PetscFunctionBegin;
250716a85fSBarry Smith   ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr);
260716a85fSBarry Smith   ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr);
270716a85fSBarry Smith   if (type == NORM_2) {
280716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
290716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
300716a85fSBarry Smith     }
310716a85fSBarry Smith   } else if (type == NORM_1) {
320716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
330716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
340716a85fSBarry Smith     }
350716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
360716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
370716a85fSBarry Smith       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
380716a85fSBarry Smith     }
390716a85fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");
400716a85fSBarry Smith 
410716a85fSBarry Smith   if (type == NORM_2) {
428f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
430716a85fSBarry Smith   }
440716a85fSBarry Smith   PetscFunctionReturn(0);
450716a85fSBarry Smith }
460716a85fSBarry Smith 
474a2ae208SSatish Balay #undef __FUNCT__
48f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ_Private"
49f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows)
506ce1633cSBarry Smith {
516ce1633cSBarry Smith   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
526ce1633cSBarry Smith   const MatScalar *aa = a->a;
536ce1633cSBarry Smith   PetscInt        i,m=A->rmap->n,cnt = 0;
546ce1633cSBarry Smith   const PetscInt  *jj = a->j,*diag;
556ce1633cSBarry Smith   PetscInt        *rows;
566ce1633cSBarry Smith   PetscErrorCode  ierr;
576ce1633cSBarry Smith 
586ce1633cSBarry Smith   PetscFunctionBegin;
596ce1633cSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
606ce1633cSBarry Smith   diag = a->diag;
616ce1633cSBarry Smith   for (i=0; i<m; i++) {
626ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
636ce1633cSBarry Smith       cnt++;
646ce1633cSBarry Smith     }
656ce1633cSBarry Smith   }
666ce1633cSBarry Smith   ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr);
676ce1633cSBarry Smith   cnt  = 0;
686ce1633cSBarry Smith   for (i=0; i<m; i++) {
696ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
706ce1633cSBarry Smith       rows[cnt++] = i;
716ce1633cSBarry Smith     }
726ce1633cSBarry Smith   }
73f1f41ecbSJed Brown   *nrows = cnt;
74f1f41ecbSJed Brown   *zrows = rows;
75f1f41ecbSJed Brown   PetscFunctionReturn(0);
76f1f41ecbSJed Brown }
77f1f41ecbSJed Brown 
78f1f41ecbSJed Brown #undef __FUNCT__
79f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ"
80f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
81f1f41ecbSJed Brown {
82f1f41ecbSJed Brown   PetscInt       nrows,*rows;
83f1f41ecbSJed Brown   PetscErrorCode ierr;
84f1f41ecbSJed Brown 
85f1f41ecbSJed Brown   PetscFunctionBegin;
860298fd71SBarry Smith   *zrows = NULL;
87f1f41ecbSJed Brown   ierr   = MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);CHKERRQ(ierr);
88*ce94432eSBarry 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;
468d0f46423SBarry Smith     if ((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);
4967566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");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);
5187566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");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);
5787566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");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);
5997566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");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);
6247566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");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 
6974a2ae208SSatish Balay #undef __FUNCT__
6984a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
699dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
700416022c9SBarry Smith {
701480ef9eaSBarry Smith   Mat               A  = (Mat) Aa;
702416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
703dfbe8321SBarry Smith   PetscErrorCode    ierr;
704d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
70536db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
706b0a32e0cSBarry Smith   PetscViewer       viewer;
707f3ef73ceSBarry Smith   PetscViewerFormat format;
708cddf8d76SBarry Smith 
7093a40ed3dSBarry Smith   PetscFunctionBegin;
710480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
711b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
71219bcc07fSBarry Smith 
713b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
714416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
7150513a670SBarry Smith 
716fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
7170513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
718b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
719416022c9SBarry Smith     for (i=0; i<m; i++) {
720cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
721bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
722bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
72336db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
724b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
725cddf8d76SBarry Smith       }
726cddf8d76SBarry Smith     }
727b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
728cddf8d76SBarry Smith     for (i=0; i<m; i++) {
729cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
730bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
731bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
732cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
733b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
734cddf8d76SBarry Smith       }
735cddf8d76SBarry Smith     }
736b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
737cddf8d76SBarry Smith     for (i=0; i<m; i++) {
738cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
739bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
740bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
74136db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
742b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
743416022c9SBarry Smith       }
744416022c9SBarry Smith     }
7450513a670SBarry Smith   } else {
7460513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
7470513a670SBarry Smith     /* first determine max of all nonzero values */
74897f1f81fSBarry Smith     PetscInt  nz = a->nz,count;
749b0a32e0cSBarry Smith     PetscDraw popup;
75036db0b34SBarry Smith     PetscReal scale;
7510513a670SBarry Smith 
7520513a670SBarry Smith     for (i=0; i<nz; i++) {
7530513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
7540513a670SBarry Smith     }
755b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
756b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
7572205254eSKarl Rupp     if (popup) {
7582205254eSKarl Rupp       ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);
7592205254eSKarl Rupp     }
7600513a670SBarry Smith     count = 0;
7610513a670SBarry Smith     for (i=0; i<m; i++) {
7620513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
763bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
764bfeeae90SHong Zhang         x_l   = a->j[j]; x_r = x_l + 1.0;
76597f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
766b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
7670513a670SBarry Smith         count++;
7680513a670SBarry Smith       }
7690513a670SBarry Smith     }
7700513a670SBarry Smith   }
771480ef9eaSBarry Smith   PetscFunctionReturn(0);
772480ef9eaSBarry Smith }
773cddf8d76SBarry Smith 
7744a2ae208SSatish Balay #undef __FUNCT__
7754a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
776dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
777480ef9eaSBarry Smith {
778dfbe8321SBarry Smith   PetscErrorCode ierr;
779b0a32e0cSBarry Smith   PetscDraw      draw;
78036db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
781ace3abfcSBarry Smith   PetscBool      isnull;
782480ef9eaSBarry Smith 
783480ef9eaSBarry Smith   PetscFunctionBegin;
784b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
785b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
786480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
787480ef9eaSBarry Smith 
788480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
789d0f46423SBarry Smith   xr   = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
790480ef9eaSBarry Smith   xr  += w;    yr += h;  xl = -w;     yl = -h;
791b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
792b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
7930298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
7943a40ed3dSBarry Smith   PetscFunctionReturn(0);
795416022c9SBarry Smith }
796416022c9SBarry Smith 
7974a2ae208SSatish Balay #undef __FUNCT__
7984a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
799dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
800416022c9SBarry Smith {
801dfbe8321SBarry Smith   PetscErrorCode ierr;
802ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
803416022c9SBarry Smith 
8043a40ed3dSBarry Smith   PetscFunctionBegin;
805251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
806251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
807251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
808c45a1595SBarry Smith   if (iascii) {
8093a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
8100f5bd95cSBarry Smith   } else if (isbinary) {
8113a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
8120f5bd95cSBarry Smith   } else if (isdraw) {
8133a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
81411aeaf0aSBarry Smith   }
8154108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
8163a40ed3dSBarry Smith   PetscFunctionReturn(0);
81717ab2063SBarry Smith }
81819bcc07fSBarry Smith 
8194a2ae208SSatish Balay #undef __FUNCT__
8204a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
821dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
82217ab2063SBarry Smith {
823416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8246849ba73SBarry Smith   PetscErrorCode ierr;
82597f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
826d0f46423SBarry Smith   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
82754f21887SBarry Smith   MatScalar      *aa    = a->a,*ap;
8283447b6efSHong Zhang   PetscReal      ratio  = 0.6;
82917ab2063SBarry Smith 
8303a40ed3dSBarry Smith   PetscFunctionBegin;
8313a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
83217ab2063SBarry Smith 
83343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
83417ab2063SBarry Smith   for (i=1; i<m; i++) {
835416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
83617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
83794a9d846SBarry Smith     rmax    = PetscMax(rmax,ailen[i]);
83817ab2063SBarry Smith     if (fshift) {
839bfeeae90SHong Zhang       ip = aj + ai[i];
840bfeeae90SHong Zhang       ap = aa + ai[i];
84117ab2063SBarry Smith       N  = ailen[i];
84217ab2063SBarry Smith       for (j=0; j<N; j++) {
84317ab2063SBarry Smith         ip[j-fshift] = ip[j];
84417ab2063SBarry Smith         ap[j-fshift] = ap[j];
84517ab2063SBarry Smith       }
84617ab2063SBarry Smith     }
84717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
84817ab2063SBarry Smith   }
84917ab2063SBarry Smith   if (m) {
85017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
85117ab2063SBarry Smith     ai[m]   = ai[m-1] + ailen[m-1];
85217ab2063SBarry Smith   }
85317ab2063SBarry Smith   /* reset ilen and imax for each row */
85417ab2063SBarry Smith   for (i=0; i<m; i++) {
85517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
85617ab2063SBarry Smith   }
857bfeeae90SHong Zhang   a->nz = ai[m];
85865e19b50SBarry 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);
85917ab2063SBarry Smith 
86009f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
861d0f46423SBarry 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);
862ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
863ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
8642205254eSKarl Rupp 
8658e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
866dd5f02e7SSatish Balay   a->reallocs         = 0;
8674e220ebcSLois Curfman McInnes   A->info.nz_unneeded = (double)fshift;
86836db0b34SBarry Smith   a->rmax             = rmax;
8694e220ebcSLois Curfman McInnes 
870cd6b891eSBarry Smith   ierr = MatCheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
8712205254eSKarl Rupp 
87288e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
87371c2f376SKris Buschelman 
8744108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
87571f1c65dSBarry Smith 
876acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
8773a40ed3dSBarry Smith   PetscFunctionReturn(0);
87817ab2063SBarry Smith }
87917ab2063SBarry Smith 
8804a2ae208SSatish Balay #undef __FUNCT__
88199cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
88299cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
88399cafbc1SBarry Smith {
88499cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
88599cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
88654f21887SBarry Smith   MatScalar      *aa = a->a;
887acf2f550SJed Brown   PetscErrorCode ierr;
88899cafbc1SBarry Smith 
88999cafbc1SBarry Smith   PetscFunctionBegin;
89099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
891acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
89299cafbc1SBarry Smith   PetscFunctionReturn(0);
89399cafbc1SBarry Smith }
89499cafbc1SBarry Smith 
89599cafbc1SBarry Smith #undef __FUNCT__
89699cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
89799cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
89899cafbc1SBarry Smith {
89999cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
90099cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
90154f21887SBarry Smith   MatScalar      *aa = a->a;
902acf2f550SJed Brown   PetscErrorCode ierr;
90399cafbc1SBarry Smith 
90499cafbc1SBarry Smith   PetscFunctionBegin;
90599cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
906acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
90799cafbc1SBarry Smith   PetscFunctionReturn(0);
90899cafbc1SBarry Smith }
90999cafbc1SBarry Smith 
91078b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
91178b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ_Kernel(PetscInt thread_id,Mat A)
91278b84d54SShri Abhyankar {
91378b84d54SShri Abhyankar   PetscErrorCode ierr;
91478b84d54SShri Abhyankar   PetscInt       *trstarts=A->rmap->trstarts;
91578b84d54SShri Abhyankar   PetscInt       n,start,end;
91678b84d54SShri Abhyankar   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
91778b84d54SShri Abhyankar 
91878b84d54SShri Abhyankar   start = trstarts[thread_id];
91978b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
92019baf141SJed Brown   n     = a->i[end] - a->i[start];
92119baf141SJed Brown   ierr  = PetscMemzero(a->a+a->i[start],n*sizeof(PetscScalar));CHKERRQ(ierr);
92278b84d54SShri Abhyankar   return 0;
92378b84d54SShri Abhyankar }
92478b84d54SShri Abhyankar 
92578b84d54SShri Abhyankar #undef __FUNCT__
92678b84d54SShri Abhyankar #define __FUNCT__ "MatZeroEntries_SeqAIJ"
92778b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
92878b84d54SShri Abhyankar {
92978b84d54SShri Abhyankar   PetscErrorCode ierr;
93078b84d54SShri Abhyankar 
93178b84d54SShri Abhyankar   PetscFunctionBegin;
932*ce94432eSBarry Smith   ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatZeroEntries_SeqAIJ_Kernel,1,A);CHKERRQ(ierr);
933acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
93478b84d54SShri Abhyankar   PetscFunctionReturn(0);
93578b84d54SShri Abhyankar }
93678b84d54SShri Abhyankar #else
93799cafbc1SBarry Smith #undef __FUNCT__
9384a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
939dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
94017ab2063SBarry Smith {
941416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
942dfbe8321SBarry Smith   PetscErrorCode ierr;
9433a40ed3dSBarry Smith 
9443a40ed3dSBarry Smith   PetscFunctionBegin;
945d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
946acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
9473a40ed3dSBarry Smith   PetscFunctionReturn(0);
94817ab2063SBarry Smith }
94978b84d54SShri Abhyankar #endif
950416022c9SBarry Smith 
9514a2ae208SSatish Balay #undef __FUNCT__
9524a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
953dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
95417ab2063SBarry Smith {
955416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
956dfbe8321SBarry Smith   PetscErrorCode ierr;
957d5d45c9bSBarry Smith 
9583a40ed3dSBarry Smith   PetscFunctionBegin;
959aa482453SBarry Smith #if defined(PETSC_USE_LOG)
960d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
96117ab2063SBarry Smith #endif
962e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
9636bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
9646bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
96505b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
966d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
96705b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
96871f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
96905b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
9706bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
97105b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
9726bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
97305b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
9746bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
975cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
9760b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
977a30b2313SHong Zhang 
9784108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
979bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
980901853e0SKris Buschelman 
981dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
9820298fd71SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",NULL);CHKERRQ(ierr);
9830298fd71SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",NULL);CHKERRQ(ierr);
9840298fd71SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",NULL);CHKERRQ(ierr);
9850298fd71SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",NULL);CHKERRQ(ierr);
9860298fd71SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",NULL);CHKERRQ(ierr);
9870298fd71SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",NULL);CHKERRQ(ierr);
9880298fd71SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",NULL);CHKERRQ(ierr);
9890298fd71SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",NULL);CHKERRQ(ierr);
9900298fd71SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",NULL);CHKERRQ(ierr);
9910298fd71SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",NULL);CHKERRQ(ierr);
9923a40ed3dSBarry Smith   PetscFunctionReturn(0);
99317ab2063SBarry Smith }
99417ab2063SBarry Smith 
9954a2ae208SSatish Balay #undef __FUNCT__
9964a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
997ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
99817ab2063SBarry Smith {
999416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10004846f1f5SKris Buschelman   PetscErrorCode ierr;
10013a40ed3dSBarry Smith 
10023a40ed3dSBarry Smith   PetscFunctionBegin;
1003a65d3064SKris Buschelman   switch (op) {
1004a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
10054e0d8c25SBarry Smith     a->roworiented = flg;
1006a65d3064SKris Buschelman     break;
1007a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1008a9817697SBarry Smith     a->keepnonzeropattern = flg;
1009a65d3064SKris Buschelman     break;
1010512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1011512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1012a65d3064SKris Buschelman     break;
1013a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
10144e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1015a65d3064SKris Buschelman     break;
1016a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
10174e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1018a65d3064SKris Buschelman     break;
101928b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
102028b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
102128b2fa4aSMatthew Knepley     break;
1022a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
10234e0d8c25SBarry Smith     a->ignorezeroentries = flg;
10240df259c2SBarry Smith     break;
1025cd6b891eSBarry Smith   case MAT_CHECK_COMPRESSED_ROW:
1026cd6b891eSBarry Smith     a->compressedrow.check = flg;
1027d487561eSHong Zhang     break;
10283d472b54SHong Zhang   case MAT_SPD:
1029b1646e73SJed Brown   case MAT_SYMMETRIC:
1030b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1031b1646e73SJed Brown   case MAT_HERMITIAN:
1032b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
10335021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
10345021d80fSJed Brown     break;
10354e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1036a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1037a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1038290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1039a65d3064SKris Buschelman     break;
1040b87ac2d8SJed Brown   case MAT_USE_INODES:
1041b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1042b87ac2d8SJed Brown     break;
1043a65d3064SKris Buschelman   default:
1044e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1045a65d3064SKris Buschelman   }
10464108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
10473a40ed3dSBarry Smith   PetscFunctionReturn(0);
104817ab2063SBarry Smith }
104917ab2063SBarry Smith 
10504a2ae208SSatish Balay #undef __FUNCT__
10514a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1052dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
105317ab2063SBarry Smith {
1054416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10556849ba73SBarry Smith   PetscErrorCode ierr;
1056d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
105735e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
105817ab2063SBarry Smith 
10593a40ed3dSBarry Smith   PetscFunctionBegin;
1060d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1061e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
106235e7444dSHong Zhang 
1063d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1064d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
106535e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
10662c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
106735e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
106835e7444dSHong Zhang     PetscFunctionReturn(0);
106935e7444dSHong Zhang   }
107035e7444dSHong Zhang 
10712dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
10721ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
107335e7444dSHong Zhang   for (i=0; i<n; i++) {
107435e7444dSHong Zhang     nz = ai[i+1] - ai[i];
10752f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
107635e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++) {
107735e7444dSHong Zhang       if (aj[j] == i) {
107835e7444dSHong Zhang         x[i] = aa[j];
107917ab2063SBarry Smith         break;
108017ab2063SBarry Smith       }
108117ab2063SBarry Smith     }
108217ab2063SBarry Smith   }
10831ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
10843a40ed3dSBarry Smith   PetscFunctionReturn(0);
108517ab2063SBarry Smith }
108617ab2063SBarry Smith 
1087c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10884a2ae208SSatish Balay #undef __FUNCT__
10894a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1090dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
109117ab2063SBarry Smith {
1092416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10935c897100SBarry Smith   PetscScalar    *x,*y;
1094dfbe8321SBarry Smith   PetscErrorCode ierr;
1095d0f46423SBarry Smith   PetscInt       m = A->rmap->n;
10965c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1097a77337e4SBarry Smith   MatScalar         *v;
1098a77337e4SBarry Smith   PetscScalar       alpha;
10990298fd71SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=NULL;
11003447b6efSHong Zhang   Mat_CompressedRow cprow    = a->compressedrow;
1101ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
11025c897100SBarry Smith #endif
110317ab2063SBarry Smith 
11043a40ed3dSBarry Smith   PetscFunctionBegin;
11052e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
11061ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
11071ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
11085c897100SBarry Smith 
11095c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1110bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
11115c897100SBarry Smith #else
11123447b6efSHong Zhang   if (usecprow) {
11133447b6efSHong Zhang     m    = cprow.nrows;
11143447b6efSHong Zhang     ii   = cprow.i;
11157b2bb3b9SHong Zhang     ridx = cprow.rindex;
11163447b6efSHong Zhang   } else {
11173447b6efSHong Zhang     ii = a->i;
11183447b6efSHong Zhang   }
111917ab2063SBarry Smith   for (i=0; i<m; i++) {
11203447b6efSHong Zhang     idx = a->j + ii[i];
11213447b6efSHong Zhang     v   = a->a + ii[i];
11223447b6efSHong Zhang     n   = ii[i+1] - ii[i];
11233447b6efSHong Zhang     if (usecprow) {
11247b2bb3b9SHong Zhang       alpha = x[ridx[i]];
11253447b6efSHong Zhang     } else {
112617ab2063SBarry Smith       alpha = x[i];
11273447b6efSHong Zhang     }
112804fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
112917ab2063SBarry Smith   }
11305c897100SBarry Smith #endif
1131dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
11321ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11331ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11343a40ed3dSBarry Smith   PetscFunctionReturn(0);
113517ab2063SBarry Smith }
113617ab2063SBarry Smith 
11374a2ae208SSatish Balay #undef __FUNCT__
11385c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1139dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
11405c897100SBarry Smith {
1141dfbe8321SBarry Smith   PetscErrorCode ierr;
11425c897100SBarry Smith 
11435c897100SBarry Smith   PetscFunctionBegin;
1144170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
11455c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
11465c897100SBarry Smith   PetscFunctionReturn(0);
11475c897100SBarry Smith }
11485c897100SBarry Smith 
1149c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
115078b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
115178b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ_Kernel(PetscInt thread_id,Mat A,Vec xx,Vec yy)
115278b84d54SShri Abhyankar {
115378b84d54SShri Abhyankar   PetscErrorCode    ierr;
115478b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
115578b84d54SShri Abhyankar   PetscScalar       *y;
115678b84d54SShri Abhyankar   const PetscScalar *x;
115778b84d54SShri Abhyankar   const MatScalar   *aa;
115878b84d54SShri Abhyankar   PetscInt          *trstarts=A->rmap->trstarts;
115978b84d54SShri Abhyankar   PetscInt          n,start,end,i;
116078b84d54SShri Abhyankar   const PetscInt    *aj,*ai;
116178b84d54SShri Abhyankar   PetscScalar       sum;
116278b84d54SShri Abhyankar 
116378b84d54SShri Abhyankar   ierr  = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
116478b84d54SShri Abhyankar   ierr  = VecGetArray(yy,&y);CHKERRQ(ierr);
116578b84d54SShri Abhyankar   start = trstarts[thread_id];
116678b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
116778b84d54SShri Abhyankar   aj    = a->j;
116878b84d54SShri Abhyankar   aa    = a->a;
116978b84d54SShri Abhyankar   ai    = a->i;
117078b84d54SShri Abhyankar   for (i=start; i<end; i++) {
117178b84d54SShri Abhyankar     n   = ai[i+1] - ai[i];
117278b84d54SShri Abhyankar     aj  = a->j + ai[i];
117378b84d54SShri Abhyankar     aa  = a->a + ai[i];
117478b84d54SShri Abhyankar     sum = 0.0;
117578b84d54SShri Abhyankar     PetscSparseDensePlusDot(sum,x,aa,aj,n);
117678b84d54SShri Abhyankar     y[i] = sum;
117778b84d54SShri Abhyankar   }
117878b84d54SShri Abhyankar   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
117978b84d54SShri Abhyankar   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
118078b84d54SShri Abhyankar   return 0;
118178b84d54SShri Abhyankar }
118278b84d54SShri Abhyankar 
118378b84d54SShri Abhyankar #undef __FUNCT__
118478b84d54SShri Abhyankar #define __FUNCT__ "MatMult_SeqAIJ"
118578b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
118678b84d54SShri Abhyankar {
118778b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
118878b84d54SShri Abhyankar   PetscScalar       *y;
118978b84d54SShri Abhyankar   const PetscScalar *x;
119078b84d54SShri Abhyankar   const MatScalar   *aa;
119178b84d54SShri Abhyankar   PetscErrorCode    ierr;
119278b84d54SShri Abhyankar   PetscInt          m=A->rmap->n;
11930298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
119478b84d54SShri Abhyankar   PetscInt          n,i,nonzerorow=0;
119578b84d54SShri Abhyankar   PetscScalar       sum;
119678b84d54SShri Abhyankar   PetscBool         usecprow=a->compressedrow.use;
119778b84d54SShri Abhyankar 
119878b84d54SShri Abhyankar #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
119978b84d54SShri Abhyankar #pragma disjoint(*x,*y,*aa)
120078b84d54SShri Abhyankar #endif
120178b84d54SShri Abhyankar 
120278b84d54SShri Abhyankar   PetscFunctionBegin;
120378b84d54SShri Abhyankar   aj = a->j;
120478b84d54SShri Abhyankar   aa = a->a;
120578b84d54SShri Abhyankar   ii = a->i;
120678b84d54SShri Abhyankar   if (usecprow) { /* use compressed row format */
120778b84d54SShri Abhyankar     ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
120878b84d54SShri Abhyankar     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
120978b84d54SShri Abhyankar     m    = a->compressedrow.nrows;
121078b84d54SShri Abhyankar     ii   = a->compressedrow.i;
121178b84d54SShri Abhyankar     ridx = a->compressedrow.rindex;
121278b84d54SShri Abhyankar     for (i=0; i<m; i++) {
121378b84d54SShri Abhyankar       n           = ii[i+1] - ii[i];
121478b84d54SShri Abhyankar       aj          = a->j + ii[i];
121578b84d54SShri Abhyankar       aa          = a->a + ii[i];
121678b84d54SShri Abhyankar       sum         = 0.0;
121778b84d54SShri Abhyankar       nonzerorow += (n>0);
121878b84d54SShri Abhyankar       PetscSparseDensePlusDot(sum,x,aa,aj,n);
121978b84d54SShri Abhyankar       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
122078b84d54SShri Abhyankar       y[*ridx++] = sum;
122178b84d54SShri Abhyankar     }
122278b84d54SShri Abhyankar     ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
122378b84d54SShri Abhyankar     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
122478b84d54SShri Abhyankar   } else { /* do not use compressed row format */
122578b84d54SShri Abhyankar #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
122678b84d54SShri Abhyankar     fortranmultaij_(&m,x,ii,aj,aa,y);
122778b84d54SShri Abhyankar #else
1228*ce94432eSBarry Smith     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
122978b84d54SShri Abhyankar #endif
123078b84d54SShri Abhyankar   }
123178b84d54SShri Abhyankar   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
123278b84d54SShri Abhyankar   PetscFunctionReturn(0);
123378b84d54SShri Abhyankar }
123478b84d54SShri Abhyankar #else
12355c897100SBarry Smith #undef __FUNCT__
12364a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1237dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
123817ab2063SBarry Smith {
1239416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1240d9fead3dSBarry Smith   PetscScalar       *y;
124154f21887SBarry Smith   const PetscScalar *x;
124254f21887SBarry Smith   const MatScalar   *aa;
1243dfbe8321SBarry Smith   PetscErrorCode    ierr;
1244003131ecSBarry Smith   PetscInt          m=A->rmap->n;
12450298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
12468aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1247362ced78SSatish Balay   PetscScalar       sum;
1248ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
124917ab2063SBarry Smith 
1250b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
125197952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1252fee21e36SBarry Smith #endif
1253fee21e36SBarry Smith 
12543a40ed3dSBarry Smith   PetscFunctionBegin;
12553649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12561ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
125797952fefSHong Zhang   aj   = a->j;
125897952fefSHong Zhang   aa   = a->a;
1259416022c9SBarry Smith   ii   = a->i;
12604eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
126197952fefSHong Zhang     m    = a->compressedrow.nrows;
126297952fefSHong Zhang     ii   = a->compressedrow.i;
126397952fefSHong Zhang     ridx = a->compressedrow.rindex;
126497952fefSHong Zhang     for (i=0; i<m; i++) {
126597952fefSHong Zhang       n           = ii[i+1] - ii[i];
126697952fefSHong Zhang       aj          = a->j + ii[i];
126797952fefSHong Zhang       aa          = a->a + ii[i];
126897952fefSHong Zhang       sum         = 0.0;
1269a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1270003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1271003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
127297952fefSHong Zhang       y[*ridx++] = sum;
127397952fefSHong Zhang     }
127497952fefSHong Zhang   } else { /* do not use compressed row format */
1275b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1276b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1277b05257ddSBarry Smith #else
127878b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
1279*ce94432eSBarry Smith     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
128078b84d54SShri Abhyankar #else
128117ab2063SBarry Smith     for (i=0; i<m; i++) {
1282003131ecSBarry Smith       n           = ii[i+1] - ii[i];
1283003131ecSBarry Smith       aj          = a->j + ii[i];
1284003131ecSBarry Smith       aa          = a->a + ii[i];
128517ab2063SBarry Smith       sum         = 0.0;
1286a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1287003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
128817ab2063SBarry Smith       y[i] = sum;
128917ab2063SBarry Smith     }
12908d195f9aSBarry Smith #endif
129178b84d54SShri Abhyankar #endif
1292b05257ddSBarry Smith   }
1293dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
12943649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12951ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12963a40ed3dSBarry Smith   PetscFunctionReturn(0);
129717ab2063SBarry Smith }
129878b84d54SShri Abhyankar #endif
129917ab2063SBarry Smith 
1300c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
13014a2ae208SSatish Balay #undef __FUNCT__
13024a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1303dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
130417ab2063SBarry Smith {
1305416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1306f15663dcSBarry Smith   PetscScalar       *y,*z;
1307f15663dcSBarry Smith   const PetscScalar *x;
130854f21887SBarry Smith   const MatScalar   *aa;
1309dfbe8321SBarry Smith   PetscErrorCode    ierr;
1310d0f46423SBarry Smith   PetscInt          m = A->rmap->n,*aj,*ii;
13110298fd71SBarry Smith   PetscInt          n,i,*ridx=NULL;
1312362ced78SSatish Balay   PetscScalar       sum;
1313ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
13149ea0dfa2SSatish Balay 
13153a40ed3dSBarry Smith   PetscFunctionBegin;
1316f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
13171ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
13182e8a6d31SBarry Smith   if (zz != yy) {
13191ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
13202e8a6d31SBarry Smith   } else {
13212e8a6d31SBarry Smith     z = y;
13222e8a6d31SBarry Smith   }
1323bfeeae90SHong Zhang 
132497952fefSHong Zhang   aj = a->j;
132597952fefSHong Zhang   aa = a->a;
1326cddf8d76SBarry Smith   ii = a->i;
13274eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
13284eb6d288SHong Zhang     if (zz != yy) {
13294eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
13304eb6d288SHong Zhang     }
133197952fefSHong Zhang     m    = a->compressedrow.nrows;
133297952fefSHong Zhang     ii   = a->compressedrow.i;
133397952fefSHong Zhang     ridx = a->compressedrow.rindex;
133497952fefSHong Zhang     for (i=0; i<m; i++) {
133597952fefSHong Zhang       n   = ii[i+1] - ii[i];
133697952fefSHong Zhang       aj  = a->j + ii[i];
133797952fefSHong Zhang       aa  = a->a + ii[i];
133897952fefSHong Zhang       sum = y[*ridx];
1339f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
134097952fefSHong Zhang       z[*ridx++] = sum;
134197952fefSHong Zhang     }
134297952fefSHong Zhang   } else { /* do not use compressed row format */
1343f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1344f15663dcSBarry Smith     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1345f15663dcSBarry Smith #else
134617ab2063SBarry Smith     for (i=0; i<m; i++) {
1347f15663dcSBarry Smith       n   = ii[i+1] - ii[i];
1348f15663dcSBarry Smith       aj  = a->j + ii[i];
1349f15663dcSBarry Smith       aa  = a->a + ii[i];
135017ab2063SBarry Smith       sum = y[i];
1351f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
135217ab2063SBarry Smith       z[i] = sum;
135317ab2063SBarry Smith     }
135402ab625aSSatish Balay #endif
1355f15663dcSBarry Smith   }
1356dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1357f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13581ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13592e8a6d31SBarry Smith   if (zz != yy) {
13601ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
13612e8a6d31SBarry Smith   }
13628154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
13636b375ea7SVictor Minden   /*
1364918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1365918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1366918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
13676b375ea7SVictor Minden   */
1368918e98c3SVictor Minden #endif
13693a40ed3dSBarry Smith   PetscFunctionReturn(0);
137017ab2063SBarry Smith }
137117ab2063SBarry Smith 
137217ab2063SBarry Smith /*
137317ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
137417ab2063SBarry Smith */
13754a2ae208SSatish Balay #undef __FUNCT__
13764a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1377dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
137817ab2063SBarry Smith {
1379416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
13806849ba73SBarry Smith   PetscErrorCode ierr;
1381d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
138217ab2063SBarry Smith 
13833a40ed3dSBarry Smith   PetscFunctionBegin;
138409f38230SBarry Smith   if (!a->diag) {
138509f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
13869518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
138709f38230SBarry Smith   }
1388d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
138909f38230SBarry Smith     a->diag[i] = a->i[i+1];
1390bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1391bfeeae90SHong Zhang       if (a->j[j] == i) {
139209f38230SBarry Smith         a->diag[i] = j;
139317ab2063SBarry Smith         break;
139417ab2063SBarry Smith       }
139517ab2063SBarry Smith     }
139617ab2063SBarry Smith   }
13973a40ed3dSBarry Smith   PetscFunctionReturn(0);
139817ab2063SBarry Smith }
139917ab2063SBarry Smith 
1400be5855fcSBarry Smith /*
1401be5855fcSBarry Smith      Checks for missing diagonals
1402be5855fcSBarry Smith */
14034a2ae208SSatish Balay #undef __FUNCT__
14044a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1405ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1406be5855fcSBarry Smith {
1407be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
140897f1f81fSBarry Smith   PetscInt   *diag,*jj = a->j,i;
1409be5855fcSBarry Smith 
1410be5855fcSBarry Smith   PetscFunctionBegin;
141109f38230SBarry Smith   *missing = PETSC_FALSE;
1412d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
141309f38230SBarry Smith     *missing = PETSC_TRUE;
141409f38230SBarry Smith     if (d) *d = 0;
1415358d2f5dSShri Abhyankar     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
141609f38230SBarry Smith   } else {
1417f1e2ffcdSBarry Smith     diag = a->diag;
1418d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1419bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
142009f38230SBarry Smith         *missing = PETSC_TRUE;
142109f38230SBarry Smith         if (d) *d = i;
142209f38230SBarry Smith         PetscInfo1(A,"Matrix is missing diagonal number %D",i);
1423358d2f5dSShri Abhyankar         break;
142409f38230SBarry Smith       }
1425be5855fcSBarry Smith     }
1426be5855fcSBarry Smith   }
1427be5855fcSBarry Smith   PetscFunctionReturn(0);
1428be5855fcSBarry Smith }
1429be5855fcSBarry Smith 
143071f1c65dSBarry Smith EXTERN_C_BEGIN
143171f1c65dSBarry Smith #undef __FUNCT__
143271f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
14337087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
143471f1c65dSBarry Smith {
143571f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
143671f1c65dSBarry Smith   PetscErrorCode ierr;
1437d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
143854f21887SBarry Smith   MatScalar      *v = a->a;
143954f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
144071f1c65dSBarry Smith 
144171f1c65dSBarry Smith   PetscFunctionBegin;
144271f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
144371f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
144471f1c65dSBarry Smith   diag = a->diag;
144571f1c65dSBarry Smith   if (!a->idiag) {
144671f1c65dSBarry Smith     ierr = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
144771f1c65dSBarry Smith     ierr = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
144871f1c65dSBarry Smith     v    = a->a;
144971f1c65dSBarry Smith   }
145071f1c65dSBarry Smith   mdiag = a->mdiag;
145171f1c65dSBarry Smith   idiag = a->idiag;
145271f1c65dSBarry Smith 
1453028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
145471f1c65dSBarry Smith     for (i=0; i<m; i++) {
145571f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1456e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
145771f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
145871f1c65dSBarry Smith     }
145971f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
146071f1c65dSBarry Smith   } else {
146171f1c65dSBarry Smith     for (i=0; i<m; i++) {
146271f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
146371f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
146471f1c65dSBarry Smith     }
1465dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
146671f1c65dSBarry Smith   }
146771f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
146871f1c65dSBarry Smith   PetscFunctionReturn(0);
146971f1c65dSBarry Smith }
14705a9745a3SMatthew Knepley EXTERN_C_END
147171f1c65dSBarry Smith 
1472c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
14734a2ae208SSatish Balay #undef __FUNCT__
147441f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
147541f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
147617ab2063SBarry Smith {
1477416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1478e6d1f457SBarry Smith   PetscScalar       *x,d,sum,*t,scale;
1479e6d1f457SBarry Smith   const MatScalar   *v = a->a,*idiag=0,*mdiag;
148054f21887SBarry Smith   const PetscScalar *b, *bs,*xb, *ts;
1481dfbe8321SBarry Smith   PetscErrorCode    ierr;
1482d0f46423SBarry Smith   PetscInt          n = A->cmap->n,m = A->rmap->n,i;
148397f1f81fSBarry Smith   const PetscInt    *idx,*diag;
148417ab2063SBarry Smith 
14853a40ed3dSBarry Smith   PetscFunctionBegin;
1486b965ef7fSBarry Smith   its = its*lits;
148791723122SBarry Smith 
148871f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
148971f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
149071f1c65dSBarry Smith   a->fshift = fshift;
149171f1c65dSBarry Smith   a->omega  = omega;
1492ed480e8bSBarry Smith 
149371f1c65dSBarry Smith   diag  = a->diag;
149471f1c65dSBarry Smith   t     = a->ssor_work;
1495ed480e8bSBarry Smith   idiag = a->idiag;
149671f1c65dSBarry Smith   mdiag = a->mdiag;
1497ed480e8bSBarry Smith 
14981ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
14993649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
150071f1c65dSBarry Smith   CHKMEMQ;
1501ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
150217ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
150317ab2063SBarry Smith     /* apply (U + D/omega) to the vector */
1504ed480e8bSBarry Smith     bs = b;
150517ab2063SBarry Smith     for (i=0; i<m; i++) {
150671f1c65dSBarry Smith       d   = fshift + mdiag[i];
1507416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1508ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1509ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
151017ab2063SBarry Smith       sum = b[i]*d/omega;
1511003131ecSBarry Smith       PetscSparseDensePlusDot(sum,bs,v,idx,n);
151217ab2063SBarry Smith       x[i] = sum;
151317ab2063SBarry Smith     }
15141ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15153649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1516efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
15173a40ed3dSBarry Smith     PetscFunctionReturn(0);
151817ab2063SBarry Smith   }
1519c783ea89SBarry Smith 
15202205254eSKarl Rupp   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
15212205254eSKarl Rupp   else if (flag & SOR_EISENSTAT) {
152217ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1523887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
152417ab2063SBarry Smith 
152517ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
152617ab2063SBarry Smith 
1527887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
152817ab2063SBarry Smith     */
152917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
153017ab2063SBarry Smith 
153117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
153217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1533416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1534ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1535ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
153617ab2063SBarry Smith       sum = b[i];
1537e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1538ed480e8bSBarry Smith       x[i] = sum*idiag[i];
153917ab2063SBarry Smith     }
154017ab2063SBarry Smith 
154117ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1542416022c9SBarry Smith     v = a->a;
15432205254eSKarl Rupp     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
154417ab2063SBarry Smith 
154517ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1546ed480e8bSBarry Smith     ts   = t;
1547416022c9SBarry Smith     diag = a->diag;
154817ab2063SBarry Smith     for (i=0; i<m; i++) {
1549416022c9SBarry Smith       n   = diag[i] - a->i[i];
1550ed480e8bSBarry Smith       idx = a->j + a->i[i];
1551ed480e8bSBarry Smith       v   = a->a + a->i[i];
155217ab2063SBarry Smith       sum = t[i];
1553003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1554ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1555733d66baSBarry Smith       /*  x = x + t */
1556733d66baSBarry Smith       x[i] += t[i];
155717ab2063SBarry Smith     }
155817ab2063SBarry Smith 
1559dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
15601ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15613649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
15623a40ed3dSBarry Smith     PetscFunctionReturn(0);
156317ab2063SBarry Smith   }
156417ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
156517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
156617ab2063SBarry Smith       for (i=0; i<m; i++) {
1567416022c9SBarry Smith         n   = diag[i] - a->i[i];
1568ed480e8bSBarry Smith         idx = a->j + a->i[i];
1569ed480e8bSBarry Smith         v   = a->a + a->i[i];
157017ab2063SBarry Smith         sum = b[i];
1571e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
15725c99c7daSBarry Smith         t[i] = sum;
1573ed480e8bSBarry Smith         x[i] = sum*idiag[i];
157417ab2063SBarry Smith       }
15755c99c7daSBarry Smith       xb   = t;
1576efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
15773a40ed3dSBarry Smith     } else xb = b;
157817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
157917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1580416022c9SBarry Smith         n   = a->i[i+1] - diag[i] - 1;
1581ed480e8bSBarry Smith         idx = a->j + diag[i] + 1;
1582ed480e8bSBarry Smith         v   = a->a + diag[i] + 1;
158317ab2063SBarry Smith         sum = xb[i];
1584e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
15855c99c7daSBarry Smith         if (xb == b) {
1586ed480e8bSBarry Smith           x[i] = sum*idiag[i];
15875c99c7daSBarry Smith         } else {
15885c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
158917ab2063SBarry Smith         }
15905c99c7daSBarry Smith       }
1591efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
159217ab2063SBarry Smith     }
159317ab2063SBarry Smith     its--;
159417ab2063SBarry Smith   }
159517ab2063SBarry Smith   while (its--) {
159617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
159717ab2063SBarry Smith       for (i=0; i<m; i++) {
1598416022c9SBarry Smith         n   = a->i[i+1] - a->i[i];
1599ed480e8bSBarry Smith         idx = a->j + a->i[i];
1600ed480e8bSBarry Smith         v   = a->a + a->i[i];
160117ab2063SBarry Smith         sum = b[i];
1602e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1603ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
160417ab2063SBarry Smith       }
16059f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
160617ab2063SBarry Smith     }
160717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
160817ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1609416022c9SBarry Smith         n   = a->i[i+1] - a->i[i];
1610ed480e8bSBarry Smith         idx = a->j + a->i[i];
1611ed480e8bSBarry Smith         v   = a->a + a->i[i];
161217ab2063SBarry Smith         sum = b[i];
1613e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1614ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
161517ab2063SBarry Smith       }
16169f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
161717ab2063SBarry Smith     }
161817ab2063SBarry Smith   }
16191ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16203649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
162171f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
162217ab2063SBarry Smith }
162317ab2063SBarry Smith 
16242af78befSBarry Smith 
16254a2ae208SSatish Balay #undef __FUNCT__
16264a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1627dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
162817ab2063SBarry Smith {
1629416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
16304e220ebcSLois Curfman McInnes 
16313a40ed3dSBarry Smith   PetscFunctionBegin;
16324e220ebcSLois Curfman McInnes   info->block_size   = 1.0;
16334e220ebcSLois Curfman McInnes   info->nz_allocated = (double)a->maxnz;
16344e220ebcSLois Curfman McInnes   info->nz_used      = (double)a->nz;
16354e220ebcSLois Curfman McInnes   info->nz_unneeded  = (double)(a->maxnz - a->nz);
16364e220ebcSLois Curfman McInnes   info->assemblies   = (double)A->num_ass;
16378e58a170SBarry Smith   info->mallocs      = (double)A->info.mallocs;
16387adad957SLisandro Dalcin   info->memory       = ((PetscObject)A)->mem;
1639d5f3da31SBarry Smith   if (A->factortype) {
16404e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
16414e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
16424e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
16434e220ebcSLois Curfman McInnes   } else {
16444e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
16454e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
16464e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
16474e220ebcSLois Curfman McInnes   }
16483a40ed3dSBarry Smith   PetscFunctionReturn(0);
164917ab2063SBarry Smith }
165017ab2063SBarry Smith 
16514a2ae208SSatish Balay #undef __FUNCT__
16524a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
16532b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
165417ab2063SBarry Smith {
1655416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
16563b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
16576849ba73SBarry Smith   PetscErrorCode    ierr;
165897b48c8fSBarry Smith   const PetscScalar *xx;
165997b48c8fSBarry Smith   PetscScalar       *bb;
1660ace3abfcSBarry Smith   PetscBool         missing;
166117ab2063SBarry Smith 
16623a40ed3dSBarry Smith   PetscFunctionBegin;
166397b48c8fSBarry Smith   if (x && b) {
166497b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
166597b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
166697b48c8fSBarry Smith     for (i=0; i<N; i++) {
166797b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
166897b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
166997b48c8fSBarry Smith     }
167097b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
167197b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
167297b48c8fSBarry Smith   }
167397b48c8fSBarry Smith 
1674a9817697SBarry Smith   if (a->keepnonzeropattern) {
1675f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1676e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1677bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1678f1e2ffcdSBarry Smith     }
1679f4df32b1SMatthew Knepley     if (diag != 0.0) {
168009f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1681e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1682f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1683f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1684f1e2ffcdSBarry Smith       }
1685f1e2ffcdSBarry Smith     }
168688e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1687f1e2ffcdSBarry Smith   } else {
1688f4df32b1SMatthew Knepley     if (diag != 0.0) {
168917ab2063SBarry Smith       for (i=0; i<N; i++) {
1690e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
16917ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1692416022c9SBarry Smith           a->ilen[rows[i]]    = 1;
1693f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1694bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
16957ae801bdSBarry Smith         } else { /* in case row was completely empty */
1696f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
169717ab2063SBarry Smith         }
169817ab2063SBarry Smith       }
16993a40ed3dSBarry Smith     } else {
170017ab2063SBarry Smith       for (i=0; i<N; i++) {
1701e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1702416022c9SBarry Smith         a->ilen[rows[i]] = 0;
170317ab2063SBarry Smith       }
170417ab2063SBarry Smith     }
170588e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1706f1e2ffcdSBarry Smith   }
170743a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17083a40ed3dSBarry Smith   PetscFunctionReturn(0);
170917ab2063SBarry Smith }
171017ab2063SBarry Smith 
17114a2ae208SSatish Balay #undef __FUNCT__
17126e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
17136e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
17146e169961SBarry Smith {
17156e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
17166e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
17176e169961SBarry Smith   PetscErrorCode    ierr;
17182b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
17196e169961SBarry Smith   const PetscScalar *xx;
17206e169961SBarry Smith   PetscScalar       *bb;
17216e169961SBarry Smith 
17226e169961SBarry Smith   PetscFunctionBegin;
17236e169961SBarry Smith   if (x && b) {
17246e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
17256e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
17262b40b63fSBarry Smith     vecs = PETSC_TRUE;
17276e169961SBarry Smith   }
17286e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
17296e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
17306e169961SBarry Smith   for (i=0; i<N; i++) {
17316e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
17326e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
17332205254eSKarl Rupp 
17346e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
17356e169961SBarry Smith   }
17366e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
17376e169961SBarry Smith     if (!zeroed[i]) {
17386e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
17396e169961SBarry Smith         if (zeroed[a->j[j]]) {
17402b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
17416e169961SBarry Smith           a->a[j] = 0.0;
17426e169961SBarry Smith         }
17436e169961SBarry Smith       }
17442b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
17456e169961SBarry Smith   }
17466e169961SBarry Smith   if (x && b) {
17476e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
17486e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
17496e169961SBarry Smith   }
17506e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
17516e169961SBarry Smith   if (diag != 0.0) {
17526e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
17536e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
17546e169961SBarry Smith     for (i=0; i<N; i++) {
17556e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
17566e169961SBarry Smith     }
17576e169961SBarry Smith   }
17586e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
17596e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17606e169961SBarry Smith   PetscFunctionReturn(0);
17616e169961SBarry Smith }
17626e169961SBarry Smith 
17636e169961SBarry Smith #undef __FUNCT__
17644a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1765a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
176617ab2063SBarry Smith {
1767416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
176897f1f81fSBarry Smith   PetscInt   *itmp;
176917ab2063SBarry Smith 
17703a40ed3dSBarry Smith   PetscFunctionBegin;
1771e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
177217ab2063SBarry Smith 
1773416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1774bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
177517ab2063SBarry Smith   if (idx) {
1776bfeeae90SHong Zhang     itmp = a->j + a->i[row];
177726fbe8dcSKarl Rupp     if (*nz) *idx = itmp;
177817ab2063SBarry Smith     else *idx = 0;
177917ab2063SBarry Smith   }
17803a40ed3dSBarry Smith   PetscFunctionReturn(0);
178117ab2063SBarry Smith }
178217ab2063SBarry Smith 
1783bfeeae90SHong Zhang /* remove this function? */
17844a2ae208SSatish Balay #undef __FUNCT__
17854a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1786a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
178717ab2063SBarry Smith {
17883a40ed3dSBarry Smith   PetscFunctionBegin;
17893a40ed3dSBarry Smith   PetscFunctionReturn(0);
179017ab2063SBarry Smith }
179117ab2063SBarry Smith 
17924a2ae208SSatish Balay #undef __FUNCT__
17934a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1794dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
179517ab2063SBarry Smith {
1796416022c9SBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
179754f21887SBarry Smith   MatScalar      *v  = a->a;
179836db0b34SBarry Smith   PetscReal      sum = 0.0;
17996849ba73SBarry Smith   PetscErrorCode ierr;
180097f1f81fSBarry Smith   PetscInt       i,j;
180117ab2063SBarry Smith 
18023a40ed3dSBarry Smith   PetscFunctionBegin;
180317ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1804416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
180536db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
180617ab2063SBarry Smith     }
18078f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
18083a40ed3dSBarry Smith   } else if (type == NORM_1) {
180936db0b34SBarry Smith     PetscReal *tmp;
181097f1f81fSBarry Smith     PetscInt  *jj = a->j;
1811d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1812d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1813064f8208SBarry Smith     *nrm = 0.0;
1814416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1815bfeeae90SHong Zhang       tmp[*jj++] += PetscAbsScalar(*v);  v++;
181617ab2063SBarry Smith     }
1817d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1818064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
181917ab2063SBarry Smith     }
1820606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
18213a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1822064f8208SBarry Smith     *nrm = 0.0;
1823d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1824bfeeae90SHong Zhang       v   = a->a + a->i[j];
182517ab2063SBarry Smith       sum = 0.0;
1826416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1827cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
182817ab2063SBarry Smith       }
1829064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
183017ab2063SBarry Smith     }
1831f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
18323a40ed3dSBarry Smith   PetscFunctionReturn(0);
183317ab2063SBarry Smith }
183417ab2063SBarry Smith 
18354e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
18364e938277SHong Zhang #undef __FUNCT__
18374e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
18384e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
18394e938277SHong Zhang {
18404e938277SHong Zhang   PetscErrorCode ierr;
18414e938277SHong Zhang   PetscInt       i,j,anzj;
18424e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
18434e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
18444e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
18454e938277SHong Zhang 
18464e938277SHong Zhang   PetscFunctionBegin;
18474e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
18484e938277SHong Zhang   ierr = PetscMalloc((an+1)*sizeof(PetscInt),&ati);CHKERRQ(ierr);
18494e938277SHong Zhang   ierr = PetscMalloc(ai[am]*sizeof(PetscInt),&atj);CHKERRQ(ierr);
18504e938277SHong Zhang   ierr = PetscMalloc(an*sizeof(PetscInt),&atfill);CHKERRQ(ierr);
18514e938277SHong Zhang   ierr = PetscMemzero(ati,(an+1)*sizeof(PetscInt));CHKERRQ(ierr);
18524e938277SHong Zhang 
18534e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
18544e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
185526fbe8dcSKarl Rupp   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
18564e938277SHong Zhang   /* Form ati for csr format of A^T. */
185726fbe8dcSKarl Rupp   for (i=0;i<an;i++) ati[i+1] += ati[i];
18584e938277SHong Zhang 
18594e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
18604e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
18614e938277SHong Zhang 
18624e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
18634e938277SHong Zhang   for (i=0;i<am;i++) {
18644e938277SHong Zhang     anzj = ai[i+1] - ai[i];
18654e938277SHong Zhang     for (j=0;j<anzj;j++) {
18664e938277SHong Zhang       atj[atfill[*aj]] = i;
18674e938277SHong Zhang       atfill[*aj++]   += 1;
18684e938277SHong Zhang     }
18694e938277SHong Zhang   }
18704e938277SHong Zhang 
18714e938277SHong Zhang   /* Clean up temporary space and complete requests. */
18724e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
1873*ce94432eSBarry Smith   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
18742205254eSKarl Rupp 
1875a2f3521dSMark F. Adams   (*B)->rmap->bs = A->cmap->bs;
1876a2f3521dSMark F. Adams   (*B)->cmap->bs = A->rmap->bs;
1877a2f3521dSMark F. Adams 
18784e938277SHong Zhang   b          = (Mat_SeqAIJ*)((*B)->data);
18794e938277SHong Zhang   b->free_a  = PETSC_FALSE;
18804e938277SHong Zhang   b->free_ij = PETSC_TRUE;
18814e938277SHong Zhang   b->nonew   = 0;
18824e938277SHong Zhang   PetscFunctionReturn(0);
18834e938277SHong Zhang }
18844e938277SHong Zhang 
18854a2ae208SSatish Balay #undef __FUNCT__
18864a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1887fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
188817ab2063SBarry Smith {
1889416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1890416022c9SBarry Smith   Mat            C;
18916849ba73SBarry Smith   PetscErrorCode ierr;
1892d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
189354f21887SBarry Smith   MatScalar      *array = a->a;
189417ab2063SBarry Smith 
18953a40ed3dSBarry Smith   PetscFunctionBegin;
1896e32f2f54SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1897fc4dec0aSBarry Smith 
1898fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1899d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1900d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1901bfeeae90SHong Zhang 
1902bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
1903*ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
1904d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
1905a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(C,A->cmap->bs,A->rmap->bs);CHKERRQ(ierr);
19067adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1907ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1908606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1909a541d17aSBarry Smith   } else {
1910a541d17aSBarry Smith     C = *B;
1911a541d17aSBarry Smith   }
1912a541d17aSBarry Smith 
191317ab2063SBarry Smith   for (i=0; i<m; i++) {
191417ab2063SBarry Smith     len    = ai[i+1]-ai[i];
191587d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1916b9b97703SBarry Smith     array += len;
1917b9b97703SBarry Smith     aj    += len;
191817ab2063SBarry Smith   }
19196d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19206d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
192117ab2063SBarry Smith 
1922815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1923416022c9SBarry Smith     *B = C;
192417ab2063SBarry Smith   } else {
1925eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
192617ab2063SBarry Smith   }
19273a40ed3dSBarry Smith   PetscFunctionReturn(0);
192817ab2063SBarry Smith }
192917ab2063SBarry Smith 
1930cd0d46ebSvictorle EXTERN_C_BEGIN
1931cd0d46ebSvictorle #undef __FUNCT__
19325fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
19337087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1934cd0d46ebSvictorle {
1935cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
193654f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
193754f21887SBarry Smith   MatScalar      *va,*vb;
19386849ba73SBarry Smith   PetscErrorCode ierr;
193997f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1940cd0d46ebSvictorle 
1941cd0d46ebSvictorle   PetscFunctionBegin;
1942cd0d46ebSvictorle   bij = (Mat_SeqAIJ*) B->data;
1943cd0d46ebSvictorle 
1944cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1945cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
19465485867bSBarry Smith   if (ma!=nb || na!=mb) {
19475485867bSBarry Smith     *f = PETSC_FALSE;
19485485867bSBarry Smith     PetscFunctionReturn(0);
19495485867bSBarry Smith   }
1950cd0d46ebSvictorle   aii  = aij->i; bii = bij->i;
1951cd0d46ebSvictorle   adx  = aij->j; bdx = bij->j;
1952cd0d46ebSvictorle   va   = aij->a; vb = bij->a;
195397f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
195497f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1955cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1956cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1957cd0d46ebSvictorle 
1958cd0d46ebSvictorle   *f = PETSC_TRUE;
1959cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1960cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
196197f1f81fSBarry Smith       PetscInt    idc,idr;
19625485867bSBarry Smith       PetscScalar vc,vr;
1963cd0d46ebSvictorle       /* column/row index/value */
19645485867bSBarry Smith       idc = adx[aptr[i]];
19655485867bSBarry Smith       idr = bdx[bptr[idc]];
19665485867bSBarry Smith       vc  = va[aptr[i]];
19675485867bSBarry Smith       vr  = vb[bptr[idc]];
19685485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
19695485867bSBarry Smith         *f = PETSC_FALSE;
19705485867bSBarry Smith         goto done;
1971cd0d46ebSvictorle       } else {
19725485867bSBarry Smith         aptr[i]++;
19735485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1974cd0d46ebSvictorle       }
1975cd0d46ebSvictorle     }
1976cd0d46ebSvictorle   }
1977cd0d46ebSvictorle done:
1978cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
19793aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
1980cd0d46ebSvictorle   PetscFunctionReturn(0);
1981cd0d46ebSvictorle }
1982cd0d46ebSvictorle EXTERN_C_END
1983cd0d46ebSvictorle 
19841cbb95d3SBarry Smith EXTERN_C_BEGIN
19851cbb95d3SBarry Smith #undef __FUNCT__
19861cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
19877087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
19881cbb95d3SBarry Smith {
19891cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
199054f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
199154f21887SBarry Smith   MatScalar      *va,*vb;
19921cbb95d3SBarry Smith   PetscErrorCode ierr;
19931cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
19941cbb95d3SBarry Smith 
19951cbb95d3SBarry Smith   PetscFunctionBegin;
19961cbb95d3SBarry Smith   bij = (Mat_SeqAIJ*) B->data;
19971cbb95d3SBarry Smith 
19981cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
19991cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
20001cbb95d3SBarry Smith   if (ma!=nb || na!=mb) {
20011cbb95d3SBarry Smith     *f = PETSC_FALSE;
20021cbb95d3SBarry Smith     PetscFunctionReturn(0);
20031cbb95d3SBarry Smith   }
20041cbb95d3SBarry Smith   aii  = aij->i; bii = bij->i;
20051cbb95d3SBarry Smith   adx  = aij->j; bdx = bij->j;
20061cbb95d3SBarry Smith   va   = aij->a; vb = bij->a;
20071cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
20081cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
20091cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
20101cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
20111cbb95d3SBarry Smith 
20121cbb95d3SBarry Smith   *f = PETSC_TRUE;
20131cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
20141cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
20151cbb95d3SBarry Smith       PetscInt    idc,idr;
20161cbb95d3SBarry Smith       PetscScalar vc,vr;
20171cbb95d3SBarry Smith       /* column/row index/value */
20181cbb95d3SBarry Smith       idc = adx[aptr[i]];
20191cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
20201cbb95d3SBarry Smith       vc  = va[aptr[i]];
20211cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
20221cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
20231cbb95d3SBarry Smith         *f = PETSC_FALSE;
20241cbb95d3SBarry Smith         goto done;
20251cbb95d3SBarry Smith       } else {
20261cbb95d3SBarry Smith         aptr[i]++;
20271cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
20281cbb95d3SBarry Smith       }
20291cbb95d3SBarry Smith     }
20301cbb95d3SBarry Smith   }
20311cbb95d3SBarry Smith done:
20321cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
20331cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
20341cbb95d3SBarry Smith   PetscFunctionReturn(0);
20351cbb95d3SBarry Smith }
20361cbb95d3SBarry Smith EXTERN_C_END
20371cbb95d3SBarry Smith 
20389e29f15eSvictorle #undef __FUNCT__
20399e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2040ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20419e29f15eSvictorle {
2042dfbe8321SBarry Smith   PetscErrorCode ierr;
20436e111a19SKarl Rupp 
20449e29f15eSvictorle   PetscFunctionBegin;
20455485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20469e29f15eSvictorle   PetscFunctionReturn(0);
20479e29f15eSvictorle }
20489e29f15eSvictorle 
20494a2ae208SSatish Balay #undef __FUNCT__
20501cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2051ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20521cbb95d3SBarry Smith {
20531cbb95d3SBarry Smith   PetscErrorCode ierr;
20546e111a19SKarl Rupp 
20551cbb95d3SBarry Smith   PetscFunctionBegin;
20561cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20571cbb95d3SBarry Smith   PetscFunctionReturn(0);
20581cbb95d3SBarry Smith }
20591cbb95d3SBarry Smith 
20601cbb95d3SBarry Smith #undef __FUNCT__
20614a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2062dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
206317ab2063SBarry Smith {
2064416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
206554f21887SBarry Smith   PetscScalar    *l,*r,x;
206654f21887SBarry Smith   MatScalar      *v;
2067dfbe8321SBarry Smith   PetscErrorCode ierr;
2068d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
206917ab2063SBarry Smith 
20703a40ed3dSBarry Smith   PetscFunctionBegin;
207117ab2063SBarry Smith   if (ll) {
20723ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
20733ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2074e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2075e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
20761ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2077416022c9SBarry Smith     v    = a->a;
207817ab2063SBarry Smith     for (i=0; i<m; i++) {
207917ab2063SBarry Smith       x = l[i];
2080416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
20812205254eSKarl Rupp       for (j=0; j<M; j++) (*v++) *= x;
208217ab2063SBarry Smith     }
20831ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2084efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
208517ab2063SBarry Smith   }
208617ab2063SBarry Smith   if (rr) {
2087e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2088e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
20891ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2090416022c9SBarry Smith     v    = a->a; jj = a->j;
20912205254eSKarl Rupp     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
20921ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2093efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
209417ab2063SBarry Smith   }
2095acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
20963a40ed3dSBarry Smith   PetscFunctionReturn(0);
209717ab2063SBarry Smith }
209817ab2063SBarry Smith 
20994a2ae208SSatish Balay #undef __FUNCT__
21004a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
210197f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
210217ab2063SBarry Smith {
2103db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
21046849ba73SBarry Smith   PetscErrorCode ierr;
2105d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
210697f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
21075d0c19d7SBarry Smith   const PetscInt *irow,*icol;
21085d0c19d7SBarry Smith   PetscInt       nrows,ncols;
210997f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
211054f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2111416022c9SBarry Smith   Mat            C;
2112ace3abfcSBarry Smith   PetscBool      stride,sorted;
211317ab2063SBarry Smith 
21143a40ed3dSBarry Smith   PetscFunctionBegin;
211514ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
2116e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
211714ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
2118e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
211999141d43SSatish Balay 
212017ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2121b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2122b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
212317ab2063SBarry Smith 
2124fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2125251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2126fee21e36SBarry Smith   if (stride && step == 1) {
212702834360SBarry Smith     /* special case of contiguous rows */
21280e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
212902834360SBarry Smith     /* loop over new rows determining lens and starting points */
213002834360SBarry Smith     for (i=0; i<nrows; i++) {
2131bfeeae90SHong Zhang       kstart = ai[irow[i]];
2132a2744918SBarry Smith       kend   = kstart + ailen[irow[i]];
213302834360SBarry Smith       for (k=kstart; k<kend; k++) {
2134bfeeae90SHong Zhang         if (aj[k] >= first) {
213502834360SBarry Smith           starts[i] = k;
213602834360SBarry Smith           break;
213702834360SBarry Smith         }
213802834360SBarry Smith       }
2139a2744918SBarry Smith       sum = 0;
214002834360SBarry Smith       while (k < kend) {
2141bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2142a2744918SBarry Smith         sum++;
214302834360SBarry Smith       }
2144a2744918SBarry Smith       lens[i] = sum;
214502834360SBarry Smith     }
214602834360SBarry Smith     /* create submatrix */
2147cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
214897f1f81fSBarry Smith       PetscInt n_cols,n_rows;
214908480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2150e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2151d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
215208480c60SBarry Smith       C    = *B;
21533a40ed3dSBarry Smith     } else {
21543bef6203SJed Brown       PetscInt rbs,cbs;
2155*ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2156f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
21573bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
21583bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
21593bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
21607adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2161ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
216208480c60SBarry Smith     }
2163db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2164db02288aSLois Curfman McInnes 
216502834360SBarry Smith     /* loop over rows inserting into submatrix */
2166db02288aSLois Curfman McInnes     a_new = c->a;
2167db02288aSLois Curfman McInnes     j_new = c->j;
2168db02288aSLois Curfman McInnes     i_new = c->i;
2169bfeeae90SHong Zhang 
217002834360SBarry Smith     for (i=0; i<nrows; i++) {
2171a2744918SBarry Smith       ii    = starts[i];
2172a2744918SBarry Smith       lensi = lens[i];
2173a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2174a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
217502834360SBarry Smith       }
217687828ca2SBarry Smith       ierr       = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2177a2744918SBarry Smith       a_new     += lensi;
2178a2744918SBarry Smith       i_new[i+1] = i_new[i] + lensi;
2179a2744918SBarry Smith       c->ilen[i] = lensi;
218002834360SBarry Smith     }
21810e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
21823a40ed3dSBarry Smith   } else {
218302834360SBarry Smith     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
21840e83c824SBarry Smith     ierr = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
218597f1f81fSBarry Smith     ierr = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
21860e83c824SBarry Smith     ierr = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
21874dcab191SBarry Smith     for (i=0; i<ncols; i++) {
21884dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
21894dcab191SBarry 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);
21904dcab191SBarry Smith #endif
21914dcab191SBarry Smith       smap[icol[i]] = i+1;
21924dcab191SBarry Smith     }
21934dcab191SBarry Smith 
219402834360SBarry Smith     /* determine lens of each row */
219502834360SBarry Smith     for (i=0; i<nrows; i++) {
2196bfeeae90SHong Zhang       kstart  = ai[irow[i]];
219702834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
219802834360SBarry Smith       lens[i] = 0;
219902834360SBarry Smith       for (k=kstart; k<kend; k++) {
2200bfeeae90SHong Zhang         if (smap[aj[k]]) {
220102834360SBarry Smith           lens[i]++;
220202834360SBarry Smith         }
220302834360SBarry Smith       }
220402834360SBarry Smith     }
220517ab2063SBarry Smith     /* Create and fill new matrix */
2206a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2207ace3abfcSBarry Smith       PetscBool equal;
22080f5bd95cSBarry Smith 
220999141d43SSatish Balay       c = (Mat_SeqAIJ*)((*B)->data);
2210e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2211d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
2212f23aa3ddSBarry Smith       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2213d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
221408480c60SBarry Smith       C    = *B;
22153a40ed3dSBarry Smith     } else {
22163bef6203SJed Brown       PetscInt rbs,cbs;
2217*ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2218f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
22193bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
22203bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
22213bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
22227adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2223ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
222408480c60SBarry Smith     }
222599141d43SSatish Balay     c = (Mat_SeqAIJ*)(C->data);
222617ab2063SBarry Smith     for (i=0; i<nrows; i++) {
222799141d43SSatish Balay       row      = irow[i];
2228bfeeae90SHong Zhang       kstart   = ai[row];
222999141d43SSatish Balay       kend     = kstart + a->ilen[row];
2230bfeeae90SHong Zhang       mat_i    = c->i[i];
223199141d43SSatish Balay       mat_j    = c->j + mat_i;
223299141d43SSatish Balay       mat_a    = c->a + mat_i;
223399141d43SSatish Balay       mat_ilen = c->ilen + i;
223417ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2235bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2236ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
223799141d43SSatish Balay           *mat_a++ = a->a[k];
223899141d43SSatish Balay           (*mat_ilen)++;
223999141d43SSatish Balay 
224017ab2063SBarry Smith         }
224117ab2063SBarry Smith       }
224217ab2063SBarry Smith     }
224302834360SBarry Smith     /* Free work space */
224402834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2245606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2246606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
224702834360SBarry Smith   }
22486d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22496d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
225017ab2063SBarry Smith 
225117ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2252416022c9SBarry Smith   *B   = C;
22533a40ed3dSBarry Smith   PetscFunctionReturn(0);
225417ab2063SBarry Smith }
225517ab2063SBarry Smith 
22561df811f5SHong Zhang #undef __FUNCT__
225782d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2258fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
225982d44351SHong Zhang {
226082d44351SHong Zhang   PetscErrorCode ierr;
226182d44351SHong Zhang   Mat            B;
226282d44351SHong Zhang 
226382d44351SHong Zhang   PetscFunctionBegin;
226482d44351SHong Zhang   ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
226582d44351SHong Zhang   ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
2266a2f3521dSMark F. Adams   ierr    = MatSetBlockSizes(B,mat->rmap->bs,mat->cmap->bs);CHKERRQ(ierr);
226782d44351SHong Zhang   ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
226882d44351SHong Zhang   ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
226982d44351SHong Zhang   *subMat = B;
227082d44351SHong Zhang   PetscFunctionReturn(0);
227182d44351SHong Zhang }
227282d44351SHong Zhang 
227382d44351SHong Zhang #undef __FUNCT__
22744a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
22750481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2276a871dcd8SBarry Smith {
227763b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2278dfbe8321SBarry Smith   PetscErrorCode ierr;
227963b91edcSBarry Smith   Mat            outA;
2280ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
228163b91edcSBarry Smith 
22823a40ed3dSBarry Smith   PetscFunctionBegin;
2283e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
22841df811f5SHong Zhang 
2285b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2286b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2287a871dcd8SBarry Smith 
228863b91edcSBarry Smith   outA             = inA;
2289d5f3da31SBarry Smith   outA->factortype = MAT_FACTOR_LU;
22902205254eSKarl Rupp 
2291c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
22926bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
22932205254eSKarl Rupp 
2294c3122656SLisandro Dalcin   a->row = row;
22952205254eSKarl Rupp 
2296c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
22976bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
22982205254eSKarl Rupp 
2299c3122656SLisandro Dalcin   a->col = col;
230063b91edcSBarry Smith 
230136db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
23026bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
23034c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
230452e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2305f0ec6fceSSatish Balay 
230694a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2307d0f46423SBarry Smith     ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2308d0f46423SBarry Smith     ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
230994a9d846SBarry Smith   }
231063b91edcSBarry Smith 
2311f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2312137fb511SHong Zhang   if (row_identity && col_identity) {
2313ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2314137fb511SHong Zhang   } else {
2315719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2316137fb511SHong Zhang   }
23173a40ed3dSBarry Smith   PetscFunctionReturn(0);
2318a871dcd8SBarry Smith }
2319a871dcd8SBarry Smith 
23204a2ae208SSatish Balay #undef __FUNCT__
23214a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2322f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2323f0b747eeSBarry Smith {
2324f0b747eeSBarry Smith   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2325f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2326efee365bSSatish Balay   PetscErrorCode ierr;
2327c5df96a5SBarry Smith   PetscBLASInt   one = 1,bnz;
23283a40ed3dSBarry Smith 
23293a40ed3dSBarry Smith   PetscFunctionBegin;
2330c5df96a5SBarry Smith   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
2331a83cb05cSBarry Smith   PetscStackCall("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2332efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2333acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
23343a40ed3dSBarry Smith   PetscFunctionReturn(0);
2335f0b747eeSBarry Smith }
2336f0b747eeSBarry Smith 
23374a2ae208SSatish Balay #undef __FUNCT__
23384a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
233997f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2340cddf8d76SBarry Smith {
2341dfbe8321SBarry Smith   PetscErrorCode ierr;
234297f1f81fSBarry Smith   PetscInt       i;
2343cddf8d76SBarry Smith 
23443a40ed3dSBarry Smith   PetscFunctionBegin;
2345cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2346b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2347cddf8d76SBarry Smith   }
2348cddf8d76SBarry Smith 
2349cddf8d76SBarry Smith   for (i=0; i<n; i++) {
23506a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2351cddf8d76SBarry Smith   }
23523a40ed3dSBarry Smith   PetscFunctionReturn(0);
2353cddf8d76SBarry Smith }
2354cddf8d76SBarry Smith 
23554a2ae208SSatish Balay #undef __FUNCT__
23564a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
235797f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
23584dcbc457SBarry Smith {
2359e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
23606849ba73SBarry Smith   PetscErrorCode ierr;
23615d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
23625d0c19d7SBarry Smith   const PetscInt *idx;
236397f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2364f1af5d2fSBarry Smith   PetscBT        table;
2365bbd702dbSSatish Balay 
23663a40ed3dSBarry Smith   PetscFunctionBegin;
2367d0f46423SBarry Smith   m  = A->rmap->n;
2368e4d965acSSatish Balay   ai = a->i;
2369bfeeae90SHong Zhang   aj = a->j;
23708a047759SSatish Balay 
2371e32f2f54SBarry Smith   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
237206763907SSatish Balay 
237397f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
237453b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
237506763907SSatish Balay 
2376e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2377b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2378e4d965acSSatish Balay     isz  = 0;
23796831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2380e4d965acSSatish Balay 
2381e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
23824dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2383b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2384e4d965acSSatish Balay 
2385dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2386e4d965acSSatish Balay     for (j=0; j<n; ++j) {
23872205254eSKarl Rupp       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
23884dcbc457SBarry Smith     }
238906763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
23906bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2391e4d965acSSatish Balay 
239204a348a9SBarry Smith     k = 0;
239304a348a9SBarry Smith     for (j=0; j<ov; j++) { /* for each overlap */
239404a348a9SBarry Smith       n = isz;
239506763907SSatish Balay       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2396e4d965acSSatish Balay         row   = nidx[k];
2397e4d965acSSatish Balay         start = ai[row];
2398e4d965acSSatish Balay         end   = ai[row+1];
239904a348a9SBarry Smith         for (l = start; l<end; l++) {
2400efb16452SHong Zhang           val = aj[l];
24012205254eSKarl Rupp           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2402e4d965acSSatish Balay         }
2403e4d965acSSatish Balay       }
2404e4d965acSSatish Balay     }
240570b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2406e4d965acSSatish Balay   }
240794bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2408606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
24093a40ed3dSBarry Smith   PetscFunctionReturn(0);
24104dcbc457SBarry Smith }
241117ab2063SBarry Smith 
24120513a670SBarry Smith /* -------------------------------------------------------------- */
24134a2ae208SSatish Balay #undef __FUNCT__
24144a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2415dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
24160513a670SBarry Smith {
24170513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
24186849ba73SBarry Smith   PetscErrorCode ierr;
24193b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
24205d0c19d7SBarry Smith   const PetscInt *row,*col;
24215d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
242256cd22aeSBarry Smith   IS             icolp,irowp;
24230298fd71SBarry Smith   PetscInt       *cwork = NULL;
24240298fd71SBarry Smith   PetscScalar    *vwork = NULL;
24250513a670SBarry Smith 
24263a40ed3dSBarry Smith   PetscFunctionBegin;
24274c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
242856cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
24294c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
243056cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
24310513a670SBarry Smith 
24320513a670SBarry Smith   /* determine lengths of permuted rows */
243397f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
24342205254eSKarl Rupp   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2435*ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2436f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
2437a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
24387adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2439ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2440606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
24410513a670SBarry Smith 
244297f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
24430513a670SBarry Smith   for (i=0; i<m; i++) {
244432ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24452205254eSKarl Rupp     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2446cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
244732ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24480513a670SBarry Smith   }
2449606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
24502205254eSKarl Rupp 
24513c7d62e4SBarry Smith   (*B)->assembled = PETSC_FALSE;
24522205254eSKarl Rupp 
24530513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24540513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
245556cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
245656cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
24576bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
24586bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
24593a40ed3dSBarry Smith   PetscFunctionReturn(0);
24600513a670SBarry Smith }
24610513a670SBarry Smith 
24624a2ae208SSatish Balay #undef __FUNCT__
24634a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2464dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2465cb5b572fSBarry Smith {
2466dfbe8321SBarry Smith   PetscErrorCode ierr;
2467cb5b572fSBarry Smith 
2468cb5b572fSBarry Smith   PetscFunctionBegin;
246933f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
247033f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2471be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2472be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2473be6bf707SBarry Smith 
2474700c5bfcSBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2475d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2476cb5b572fSBarry Smith   } else {
2477cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2478cb5b572fSBarry Smith   }
2479cb5b572fSBarry Smith   PetscFunctionReturn(0);
2480cb5b572fSBarry Smith }
2481cb5b572fSBarry Smith 
24824a2ae208SSatish Balay #undef __FUNCT__
24834994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
24844994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2485273d9f13SBarry Smith {
2486dfbe8321SBarry Smith   PetscErrorCode ierr;
2487273d9f13SBarry Smith 
2488273d9f13SBarry Smith   PetscFunctionBegin;
2489ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2490273d9f13SBarry Smith   PetscFunctionReturn(0);
2491273d9f13SBarry Smith }
2492273d9f13SBarry Smith 
24934a2ae208SSatish Balay #undef __FUNCT__
24948c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray_SeqAIJ"
24958c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
24966c0721eeSBarry Smith {
24976c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
24986e111a19SKarl Rupp 
24996c0721eeSBarry Smith   PetscFunctionBegin;
25006c0721eeSBarry Smith   *array = a->a;
25016c0721eeSBarry Smith   PetscFunctionReturn(0);
25026c0721eeSBarry Smith }
25036c0721eeSBarry Smith 
25044a2ae208SSatish Balay #undef __FUNCT__
25058c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray_SeqAIJ"
25068c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
25076c0721eeSBarry Smith {
25086c0721eeSBarry Smith   PetscFunctionBegin;
25096c0721eeSBarry Smith   PetscFunctionReturn(0);
25106c0721eeSBarry Smith }
2511273d9f13SBarry Smith 
2512ee4f033dSBarry Smith #undef __FUNCT__
2513ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2514dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2515ee4f033dSBarry Smith {
25166849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void*))coloring->f;
25176849ba73SBarry Smith   PetscErrorCode ierr;
25184e269d77SPeter Brune   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow;
2519efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
252087828ca2SBarry Smith   PetscScalar    *vscale_array;
2521ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2522ee4f033dSBarry Smith   Vec            w1,w2,w3;
2523ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2524ace3abfcSBarry Smith   PetscBool      flg   = PETSC_FALSE;
2525ee4f033dSBarry Smith 
2526ee4f033dSBarry Smith   PetscFunctionBegin;
2527ee4f033dSBarry Smith   if (!coloring->w1) {
2528ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
252952e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2530ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
253152e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2532ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
253352e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2534ee4f033dSBarry Smith   }
2535ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2536ee4f033dSBarry Smith 
2537ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
25380298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,NULL);CHKERRQ(ierr);
2539ee4f033dSBarry Smith   if (flg) {
2540ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2541ee4f033dSBarry Smith   } else {
2542ace3abfcSBarry Smith     PetscBool assembled;
25430b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
25440b9b6f31SBarry Smith     if (assembled) {
2545ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2546ee4f033dSBarry Smith     }
25470b9b6f31SBarry Smith   }
2548ee4f033dSBarry Smith 
2549ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2550ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2551ee4f033dSBarry Smith 
25524e269d77SPeter Brune   if (!coloring->fset) {
255366f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2554ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
255566f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
25564e269d77SPeter Brune   } else {
25574e269d77SPeter Brune     coloring->fset = PETSC_FALSE;
2558ee4f033dSBarry Smith   }
2559ee4f033dSBarry Smith 
2560ee4f033dSBarry Smith   /*
2561ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2562ee4f033dSBarry Smith   */
25631ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
25641ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2565ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2566ee4f033dSBarry Smith     /*
2567ee4f033dSBarry Smith        Loop over each column associated with color adding the
2568ee4f033dSBarry Smith        perturbation to the vector w3.
2569ee4f033dSBarry Smith     */
2570ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2571ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2572ee4f033dSBarry Smith       dx  = xx[col];
2573ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2574ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2575ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2576ee4f033dSBarry Smith       dx               *= epsilon;
2577ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2578ee4f033dSBarry Smith     }
2579ee4f033dSBarry Smith   }
25802205254eSKarl Rupp   vscale_array = vscale_array + start;
25812205254eSKarl Rupp 
25822205254eSKarl Rupp   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2583ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2584ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2585ee4f033dSBarry Smith 
2586ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2587ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2588ee4f033dSBarry Smith 
2589ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2590ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2591ee4f033dSBarry Smith 
25921ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2593ee4f033dSBarry Smith   /*
2594ee4f033dSBarry Smith       Loop over each color
2595ee4f033dSBarry Smith   */
2596ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
259749b058dcSBarry Smith     coloring->currentcolor = k;
25982205254eSKarl Rupp 
2599ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
26001ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2601ee4f033dSBarry Smith     /*
2602ee4f033dSBarry Smith        Loop over each column associated with color adding the
2603ee4f033dSBarry Smith        perturbation to the vector w3.
2604ee4f033dSBarry Smith     */
2605ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2606ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2607ee4f033dSBarry Smith       dx  = xx[col];
26085b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2609ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2610ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2611ee4f033dSBarry Smith       dx *= epsilon;
2612e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2613ee4f033dSBarry Smith       w3_array[col] += dx;
2614ee4f033dSBarry Smith     }
26151ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2616ee4f033dSBarry Smith 
2617ee4f033dSBarry Smith     /*
2618ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2619ee4f033dSBarry Smith     */
2620ee4f033dSBarry Smith 
262166f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2622ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
262366f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2624efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2625ee4f033dSBarry Smith 
2626ee4f033dSBarry Smith     /*
2627ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2628ee4f033dSBarry Smith     */
26291ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2630ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2631ee4f033dSBarry Smith       row     = coloring->rows[k][l];
2632ee4f033dSBarry Smith       col     = coloring->columnsforrow[k][l];
2633ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2634ee4f033dSBarry Smith       srow    = row + start;
2635ee4f033dSBarry Smith       ierr    = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2636ee4f033dSBarry Smith     }
26371ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2638ee4f033dSBarry Smith   }
263949b058dcSBarry Smith   coloring->currentcolor = k;
26402205254eSKarl Rupp 
26411ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
26422205254eSKarl Rupp   xx   = xx + start;
26432205254eSKarl Rupp   ierr = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2644ee4f033dSBarry Smith   ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2645ee4f033dSBarry Smith   ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2646ee4f033dSBarry Smith   PetscFunctionReturn(0);
2647ee4f033dSBarry Smith }
2648ee4f033dSBarry Smith 
26498229c054SShri Abhyankar /*
26508229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
26518229c054SShri Abhyankar    have different nonzero structure.
26528229c054SShri Abhyankar */
2653ac90fabeSBarry Smith #undef __FUNCT__
26548229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
26558229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2656ec7775f6SShri Abhyankar {
26578229c054SShri Abhyankar   PetscInt       i,m=Y->rmap->N;
2658ec7775f6SShri Abhyankar   Mat_SeqAIJ     *x  = (Mat_SeqAIJ*)X->data;
2659ec7775f6SShri Abhyankar   Mat_SeqAIJ     *y  = (Mat_SeqAIJ*)Y->data;
2660ec7775f6SShri Abhyankar   const PetscInt *xi = x->i,*yi = y->i;
2661ec7775f6SShri Abhyankar 
2662ec7775f6SShri Abhyankar   PetscFunctionBegin;
2663ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2664ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
26658af7cee1SJed Brown     PetscInt       j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
26668af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
26678af7cee1SJed Brown     nnz[i] = 0;
26688af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
26698af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
26708af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
26718af7cee1SJed Brown       nnz[i]++;
26728af7cee1SJed Brown     }
26738af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2674ec7775f6SShri Abhyankar   }
2675ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2676ec7775f6SShri Abhyankar }
2677ec7775f6SShri Abhyankar 
2678ec7775f6SShri Abhyankar #undef __FUNCT__
2679ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2680f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2681ac90fabeSBarry Smith {
2682dfbe8321SBarry Smith   PetscErrorCode ierr;
268397f1f81fSBarry Smith   PetscInt       i;
2684ac90fabeSBarry Smith   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2685c5df96a5SBarry Smith   PetscBLASInt   one=1,bnz;
2686ac90fabeSBarry Smith 
2687ac90fabeSBarry Smith   PetscFunctionBegin;
2688c5df96a5SBarry Smith   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2689ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2690f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2691a83cb05cSBarry Smith     PetscStackCall("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2692acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2693c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2694a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2695a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
26966bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2697a30b2313SHong Zhang     }
2698a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
26990298fd71SBarry Smith       ierr    = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,NULL, y->i,y->j,NULL, &y->xtoy);CHKERRQ(ierr);
2700a30b2313SHong Zhang       y->XtoY = X;
2701407f6b05SHong Zhang       ierr    = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2702c537a176SHong Zhang     }
2703f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2704ba0e910bSBarry 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);
2705ac90fabeSBarry Smith   } else {
27068229c054SShri Abhyankar     Mat      B;
27078229c054SShri Abhyankar     PetscInt *nnz;
270816b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2709*ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2710bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
27114aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2712a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr);
2713176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
27148229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2715ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2716ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2717ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
27188229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2719ac90fabeSBarry Smith   }
2720ac90fabeSBarry Smith   PetscFunctionReturn(0);
2721ac90fabeSBarry Smith }
2722ac90fabeSBarry Smith 
2723521d7252SBarry Smith #undef __FUNCT__
2724354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
27257087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2726354c94deSBarry Smith {
2727354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2728354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2729354c94deSBarry Smith   PetscInt    i,nz;
2730354c94deSBarry Smith   PetscScalar *a;
2731354c94deSBarry Smith 
2732354c94deSBarry Smith   PetscFunctionBegin;
2733354c94deSBarry Smith   nz = aij->nz;
2734354c94deSBarry Smith   a  = aij->a;
27352205254eSKarl Rupp   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2736354c94deSBarry Smith #else
2737354c94deSBarry Smith   PetscFunctionBegin;
2738354c94deSBarry Smith #endif
2739354c94deSBarry Smith   PetscFunctionReturn(0);
2740354c94deSBarry Smith }
2741354c94deSBarry Smith 
2742e34fafa9SBarry Smith #undef __FUNCT__
2743985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2744985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2745e34fafa9SBarry Smith {
2746e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2747e34fafa9SBarry Smith   PetscErrorCode ierr;
2748d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2749e34fafa9SBarry Smith   PetscReal      atmp;
2750985db425SBarry Smith   PetscScalar    *x;
2751e34fafa9SBarry Smith   MatScalar      *aa;
2752e34fafa9SBarry Smith 
2753e34fafa9SBarry Smith   PetscFunctionBegin;
2754e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2755e34fafa9SBarry Smith   aa = a->a;
2756e34fafa9SBarry Smith   ai = a->i;
2757e34fafa9SBarry Smith   aj = a->j;
2758e34fafa9SBarry Smith 
2759985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2760e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2761e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2762e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2763e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2764e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
27659189402eSHong Zhang     x[i]  = 0.0;
2766e34fafa9SBarry Smith     for (j=0; j<ncols; j++) {
2767985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2768985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2769985db425SBarry Smith       aa++; aj++;
2770985db425SBarry Smith     }
2771985db425SBarry Smith   }
2772985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2773985db425SBarry Smith   PetscFunctionReturn(0);
2774985db425SBarry Smith }
2775985db425SBarry Smith 
2776985db425SBarry Smith #undef __FUNCT__
2777985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2778985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2779985db425SBarry Smith {
2780985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2781985db425SBarry Smith   PetscErrorCode ierr;
2782d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2783985db425SBarry Smith   PetscScalar    *x;
2784985db425SBarry Smith   MatScalar      *aa;
2785985db425SBarry Smith 
2786985db425SBarry Smith   PetscFunctionBegin;
2787e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2788985db425SBarry Smith   aa = a->a;
2789985db425SBarry Smith   ai = a->i;
2790985db425SBarry Smith   aj = a->j;
2791985db425SBarry Smith 
2792985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2793985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2794985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2795e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2796985db425SBarry Smith   for (i=0; i<m; i++) {
2797985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2798d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2799985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2800985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2801985db425SBarry Smith       x[i] = 0.0;
2802985db425SBarry Smith       if (idx) {
2803985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2804985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2805985db425SBarry Smith           if (aj[j] > j) {
2806985db425SBarry Smith             idx[i] = j;
2807985db425SBarry Smith             break;
2808985db425SBarry Smith           }
2809985db425SBarry Smith         }
2810985db425SBarry Smith       }
2811985db425SBarry Smith     }
2812985db425SBarry Smith     for (j=0; j<ncols; j++) {
2813985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2814985db425SBarry Smith       aa++; aj++;
2815985db425SBarry Smith     }
2816985db425SBarry Smith   }
2817985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2818985db425SBarry Smith   PetscFunctionReturn(0);
2819985db425SBarry Smith }
2820985db425SBarry Smith 
2821985db425SBarry Smith #undef __FUNCT__
2822c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2823c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2824c87e5d42SMatthew Knepley {
2825c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2826c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2827c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2828c87e5d42SMatthew Knepley   PetscReal      atmp;
2829c87e5d42SMatthew Knepley   PetscScalar    *x;
2830c87e5d42SMatthew Knepley   MatScalar      *aa;
2831c87e5d42SMatthew Knepley 
2832c87e5d42SMatthew Knepley   PetscFunctionBegin;
2833e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2834c87e5d42SMatthew Knepley   aa = a->a;
2835c87e5d42SMatthew Knepley   ai = a->i;
2836c87e5d42SMatthew Knepley   aj = a->j;
2837c87e5d42SMatthew Knepley 
2838c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2839c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2840c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
28413bb78c5cSMatthew 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);
2842c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2843c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2844289a08f5SMatthew Knepley     if (ncols) {
2845289a08f5SMatthew Knepley       /* Get first nonzero */
2846289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
2847289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
28482205254eSKarl Rupp         if (atmp > 1.0e-12) {
28492205254eSKarl Rupp           x[i] = atmp;
28502205254eSKarl Rupp           if (idx) idx[i] = aj[j];
28512205254eSKarl Rupp           break;
28522205254eSKarl Rupp         }
2853289a08f5SMatthew Knepley       }
285412431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2855289a08f5SMatthew Knepley     } else {
2856289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2857289a08f5SMatthew Knepley     }
2858c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
2859c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2860289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2861c87e5d42SMatthew Knepley       aa++; aj++;
2862c87e5d42SMatthew Knepley     }
2863c87e5d42SMatthew Knepley   }
2864c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2865c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2866c87e5d42SMatthew Knepley }
2867c87e5d42SMatthew Knepley 
2868c87e5d42SMatthew Knepley #undef __FUNCT__
2869985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2870985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2871985db425SBarry Smith {
2872985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2873985db425SBarry Smith   PetscErrorCode ierr;
2874d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2875985db425SBarry Smith   PetscScalar    *x;
2876985db425SBarry Smith   MatScalar      *aa;
2877985db425SBarry Smith 
2878985db425SBarry Smith   PetscFunctionBegin;
2879e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2880985db425SBarry Smith   aa = a->a;
2881985db425SBarry Smith   ai = a->i;
2882985db425SBarry Smith   aj = a->j;
2883985db425SBarry Smith 
2884985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2885985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2886985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2887e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2888985db425SBarry Smith   for (i=0; i<m; i++) {
2889985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2890d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2891985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2892985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2893985db425SBarry Smith       x[i] = 0.0;
2894985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2895985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2896985db425SBarry Smith         for (j=0; j<ncols; j++) {
2897985db425SBarry Smith           if (aj[j] > j) {
2898985db425SBarry Smith             idx[i] = j;
2899985db425SBarry Smith             break;
2900985db425SBarry Smith           }
2901985db425SBarry Smith         }
2902985db425SBarry Smith       }
2903985db425SBarry Smith     }
2904985db425SBarry Smith     for (j=0; j<ncols; j++) {
2905985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2906985db425SBarry Smith       aa++; aj++;
2907e34fafa9SBarry Smith     }
2908e34fafa9SBarry Smith   }
2909e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2910e34fafa9SBarry Smith   PetscFunctionReturn(0);
2911e34fafa9SBarry Smith }
2912bbead8a2SBarry Smith 
2913bbead8a2SBarry Smith #include <petscblaslapack.h>
2914bbead8a2SBarry Smith #include <../src/mat/blockinvert.h>
2915bbead8a2SBarry Smith 
2916bbead8a2SBarry Smith #undef __FUNCT__
2917bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
2918713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
2919bbead8a2SBarry Smith {
2920bbead8a2SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
2921bbead8a2SBarry Smith   PetscErrorCode ierr;
292234fc4b71SJed 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;
2923bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
2924bbead8a2SBarry Smith   PetscReal      shift = 0.0;
2925bbead8a2SBarry Smith 
2926bbead8a2SBarry Smith   PetscFunctionBegin;
29274a0d0026SBarry Smith   if (a->ibdiagvalid) {
29284a0d0026SBarry Smith     if (values) *values = a->ibdiag;
29294a0d0026SBarry Smith     PetscFunctionReturn(0);
29304a0d0026SBarry Smith   }
2931bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
2932bbead8a2SBarry Smith   if (!a->ibdiag) {
2933bbead8a2SBarry Smith     ierr = PetscMalloc(bs2*mbs*sizeof(PetscScalar),&a->ibdiag);CHKERRQ(ierr);
2934bbead8a2SBarry Smith     ierr = PetscLogObjectMemory(A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
2935bbead8a2SBarry Smith   }
2936bbead8a2SBarry Smith   diag = a->ibdiag;
2937bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
2938bbead8a2SBarry Smith   /* factor and invert each block */
2939bbead8a2SBarry Smith   switch (bs) {
2940bbead8a2SBarry Smith   case 1:
2941bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2942bbead8a2SBarry Smith       ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
2943bbead8a2SBarry Smith       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
2944bbead8a2SBarry Smith     }
2945bbead8a2SBarry Smith     break;
2946bbead8a2SBarry Smith   case 2:
2947bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2948bbead8a2SBarry Smith       ij[0] = 2*i; ij[1] = 2*i + 1;
2949bbead8a2SBarry Smith       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
295096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
295196b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
2952bbead8a2SBarry Smith       diag += 4;
2953bbead8a2SBarry Smith     }
2954bbead8a2SBarry Smith     break;
2955bbead8a2SBarry Smith   case 3:
2956bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2957bbead8a2SBarry Smith       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
2958bbead8a2SBarry Smith       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
295996b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
296096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
2961bbead8a2SBarry Smith       diag += 9;
2962bbead8a2SBarry Smith     }
2963bbead8a2SBarry Smith     break;
2964bbead8a2SBarry Smith   case 4:
2965bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2966bbead8a2SBarry Smith       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
2967bbead8a2SBarry Smith       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
296896b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
296996b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
2970bbead8a2SBarry Smith       diag += 16;
2971bbead8a2SBarry Smith     }
2972bbead8a2SBarry Smith     break;
2973bbead8a2SBarry Smith   case 5:
2974bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2975bbead8a2SBarry 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;
2976bbead8a2SBarry Smith       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
297796b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
297896b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
2979bbead8a2SBarry Smith       diag += 25;
2980bbead8a2SBarry Smith     }
2981bbead8a2SBarry Smith     break;
2982bbead8a2SBarry Smith   case 6:
2983bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2984bbead8a2SBarry 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;
2985bbead8a2SBarry Smith       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
298696b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
298796b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
2988bbead8a2SBarry Smith       diag += 36;
2989bbead8a2SBarry Smith     }
2990bbead8a2SBarry Smith     break;
2991bbead8a2SBarry Smith   case 7:
2992bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2993bbead8a2SBarry 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;
2994bbead8a2SBarry Smith       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
299596b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
299696b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
2997bbead8a2SBarry Smith       diag += 49;
2998bbead8a2SBarry Smith     }
2999bbead8a2SBarry Smith     break;
3000bbead8a2SBarry Smith   default:
3001bbead8a2SBarry Smith     ierr = PetscMalloc3(bs,MatScalar,&v_work,bs,PetscInt,&v_pivots,bs,PetscInt,&IJ);CHKERRQ(ierr);
3002bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3003bbead8a2SBarry Smith       for (j=0; j<bs; j++) {
3004bbead8a2SBarry Smith         IJ[j] = bs*i + j;
3005bbead8a2SBarry Smith       }
3006bbead8a2SBarry Smith       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
300796b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
300896b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3009bbead8a2SBarry Smith       diag += bs2;
3010bbead8a2SBarry Smith     }
3011bbead8a2SBarry Smith     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3012bbead8a2SBarry Smith   }
3013bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3014bbead8a2SBarry Smith   PetscFunctionReturn(0);
3015bbead8a2SBarry Smith }
3016bbead8a2SBarry Smith 
301773a71a0fSBarry Smith #undef __FUNCT__
301873a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_SeqAIJ"
301973a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
302073a71a0fSBarry Smith {
302173a71a0fSBarry Smith   PetscErrorCode ierr;
302273a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
302373a71a0fSBarry Smith   PetscScalar    a;
302473a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
302573a71a0fSBarry Smith 
302673a71a0fSBarry Smith   PetscFunctionBegin;
302773a71a0fSBarry Smith   if (!x->assembled) {
302873a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
302973a71a0fSBarry Smith     for (i=0; i<m; i++) {
303073a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
303173a71a0fSBarry Smith         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
303273a71a0fSBarry Smith         col  = (PetscInt)(n*PetscRealPart(a));
303373a71a0fSBarry Smith         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
303473a71a0fSBarry Smith       }
303573a71a0fSBarry Smith     }
303673a71a0fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
303773a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
303873a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
303973a71a0fSBarry Smith   PetscFunctionReturn(0);
304073a71a0fSBarry Smith }
304173a71a0fSBarry Smith 
30427087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
3043682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
30440a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3045cb5b572fSBarry Smith                                         MatGetRow_SeqAIJ,
3046cb5b572fSBarry Smith                                         MatRestoreRow_SeqAIJ,
3047cb5b572fSBarry Smith                                         MatMult_SeqAIJ,
304897304618SKris Buschelman                                 /*  4*/ MatMultAdd_SeqAIJ,
30497c922b88SBarry Smith                                         MatMultTranspose_SeqAIJ,
30507c922b88SBarry Smith                                         MatMultTransposeAdd_SeqAIJ,
3051db4efbfdSBarry Smith                                         0,
3052db4efbfdSBarry Smith                                         0,
3053db4efbfdSBarry Smith                                         0,
3054db4efbfdSBarry Smith                                 /* 10*/ 0,
3055cb5b572fSBarry Smith                                         MatLUFactor_SeqAIJ,
3056cb5b572fSBarry Smith                                         0,
305741f059aeSBarry Smith                                         MatSOR_SeqAIJ,
305817ab2063SBarry Smith                                         MatTranspose_SeqAIJ,
305997304618SKris Buschelman                                 /*1 5*/ MatGetInfo_SeqAIJ,
3060cb5b572fSBarry Smith                                         MatEqual_SeqAIJ,
3061cb5b572fSBarry Smith                                         MatGetDiagonal_SeqAIJ,
3062cb5b572fSBarry Smith                                         MatDiagonalScale_SeqAIJ,
3063cb5b572fSBarry Smith                                         MatNorm_SeqAIJ,
306497304618SKris Buschelman                                 /* 20*/ 0,
3065cb5b572fSBarry Smith                                         MatAssemblyEnd_SeqAIJ,
3066cb5b572fSBarry Smith                                         MatSetOption_SeqAIJ,
3067cb5b572fSBarry Smith                                         MatZeroEntries_SeqAIJ,
3068d519adbfSMatthew Knepley                                 /* 24*/ MatZeroRows_SeqAIJ,
3069db4efbfdSBarry Smith                                         0,
3070db4efbfdSBarry Smith                                         0,
3071db4efbfdSBarry Smith                                         0,
3072db4efbfdSBarry Smith                                         0,
30734994cf47SJed Brown                                 /* 29*/ MatSetUp_SeqAIJ,
3074db4efbfdSBarry Smith                                         0,
3075db4efbfdSBarry Smith                                         0,
30768c778c55SBarry Smith                                         0,
30778c778c55SBarry Smith                                         0,
3078d519adbfSMatthew Knepley                                 /* 34*/ MatDuplicate_SeqAIJ,
3079cb5b572fSBarry Smith                                         0,
3080cb5b572fSBarry Smith                                         0,
3081cb5b572fSBarry Smith                                         MatILUFactor_SeqAIJ,
3082cb5b572fSBarry Smith                                         0,
3083d519adbfSMatthew Knepley                                 /* 39*/ MatAXPY_SeqAIJ,
3084cb5b572fSBarry Smith                                         MatGetSubMatrices_SeqAIJ,
3085cb5b572fSBarry Smith                                         MatIncreaseOverlap_SeqAIJ,
3086cb5b572fSBarry Smith                                         MatGetValues_SeqAIJ,
3087cb5b572fSBarry Smith                                         MatCopy_SeqAIJ,
3088d519adbfSMatthew Knepley                                 /* 44*/ MatGetRowMax_SeqAIJ,
3089cb5b572fSBarry Smith                                         MatScale_SeqAIJ,
3090cb5b572fSBarry Smith                                         0,
309179299369SBarry Smith                                         MatDiagonalSet_SeqAIJ,
30926e169961SBarry Smith                                         MatZeroRowsColumns_SeqAIJ,
309373a71a0fSBarry Smith                                 /* 49*/ MatSetRandom_SeqAIJ,
30943b2fbd54SBarry Smith                                         MatGetRowIJ_SeqAIJ,
30953b2fbd54SBarry Smith                                         MatRestoreRowIJ_SeqAIJ,
30963b2fbd54SBarry Smith                                         MatGetColumnIJ_SeqAIJ,
3097a93ec695SBarry Smith                                         MatRestoreColumnIJ_SeqAIJ,
3098d519adbfSMatthew Knepley                                 /* 54*/ MatFDColoringCreate_SeqAIJ,
3099b9617806SBarry Smith                                         0,
31000513a670SBarry Smith                                         0,
3101cda55fadSBarry Smith                                         MatPermute_SeqAIJ,
3102cda55fadSBarry Smith                                         0,
3103d519adbfSMatthew Knepley                                 /* 59*/ 0,
3104b9b97703SBarry Smith                                         MatDestroy_SeqAIJ,
3105b9b97703SBarry Smith                                         MatView_SeqAIJ,
3106357abbc8SBarry Smith                                         0,
3107321b30b9SSatish Balay                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3108321b30b9SSatish Balay                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3109321b30b9SSatish Balay                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3110ee4f033dSBarry Smith                                         0,
3111ee4f033dSBarry Smith                                         0,
3112ee4f033dSBarry Smith                                         0,
3113d519adbfSMatthew Knepley                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3114c87e5d42SMatthew Knepley                                         MatGetRowMinAbs_SeqAIJ,
3115ee4f033dSBarry Smith                                         0,
3116ee4f033dSBarry Smith                                         MatSetColoring_SeqAIJ,
3117dcf5cc72SBarry Smith                                         0,
3118d519adbfSMatthew Knepley                                 /* 74*/ MatSetValuesAdifor_SeqAIJ,
31193acb8795SBarry Smith                                         MatFDColoringApply_AIJ,
312097304618SKris Buschelman                                         0,
312197304618SKris Buschelman                                         0,
312297304618SKris Buschelman                                         0,
31236ce1633cSBarry Smith                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
312497304618SKris Buschelman                                         0,
312597304618SKris Buschelman                                         0,
312697304618SKris Buschelman                                         0,
3127bc011b1eSHong Zhang                                         MatLoad_SeqAIJ,
3128d519adbfSMatthew Knepley                                 /* 84*/ MatIsSymmetric_SeqAIJ,
31291cbb95d3SBarry Smith                                         MatIsHermitian_SeqAIJ,
31306284ec50SHong Zhang                                         0,
31316284ec50SHong Zhang                                         0,
3132bc011b1eSHong Zhang                                         0,
3133d519adbfSMatthew Knepley                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
313426be0446SHong Zhang                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
313526be0446SHong Zhang                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
313665e8a0caSHong Zhang                                         MatPtAP_SeqAIJ_SeqAIJ,
313765e8a0caSHong Zhang                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ,
313865e8a0caSHong Zhang                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
31396fc122caSHong Zhang                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
31406fc122caSHong Zhang                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
31416fc122caSHong Zhang                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
31422121bac1SHong Zhang                                         0,
31432121bac1SHong Zhang                                 /* 99*/ 0,
3144609c6c4dSKris Buschelman                                         0,
3145609c6c4dSKris Buschelman                                         0,
314687d4246cSBarry Smith                                         MatConjugate_SeqAIJ,
314787d4246cSBarry Smith                                         0,
3148d519adbfSMatthew Knepley                                 /*104*/ MatSetValuesRow_SeqAIJ,
314999cafbc1SBarry Smith                                         MatRealPart_SeqAIJ,
3150f5edf698SHong Zhang                                         MatImaginaryPart_SeqAIJ,
3151f5edf698SHong Zhang                                         0,
31522bebee5dSHong Zhang                                         0,
3153cbd44569SHong Zhang                                 /*109*/ MatMatSolve_SeqAIJ,
3154985db425SBarry Smith                                         0,
31552af78befSBarry Smith                                         MatGetRowMin_SeqAIJ,
31562af78befSBarry Smith                                         0,
3157599ef60dSHong Zhang                                         MatMissingDiagonal_SeqAIJ,
3158d519adbfSMatthew Knepley                                 /*114*/ 0,
3159599ef60dSHong Zhang                                         0,
31603c2a7987SHong Zhang                                         0,
3161fe97e370SBarry Smith                                         0,
3162fbdbba38SShri Abhyankar                                         0,
3163fbdbba38SShri Abhyankar                                 /*119*/ 0,
3164fbdbba38SShri Abhyankar                                         0,
3165fbdbba38SShri Abhyankar                                         0,
316682d44351SHong Zhang                                         0,
3167b3a44c85SBarry Smith                                         MatGetMultiProcBlock_SeqAIJ,
31680716a85fSBarry Smith                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3169bbead8a2SBarry Smith                                         MatGetColumnNorms_SeqAIJ,
317037868618SMatthew G Knepley                                         MatInvertBlockDiagonal_SeqAIJ,
317137868618SMatthew G Knepley                                         0,
317237868618SMatthew G Knepley                                         0,
31735df89d91SHong Zhang                                 /*129*/ 0,
317475648e8dSHong Zhang                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
317575648e8dSHong Zhang                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
317675648e8dSHong Zhang                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3177b9af6bddSHong Zhang                                         MatTransposeColoringCreate_SeqAIJ,
3178b9af6bddSHong Zhang                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
31792b8ad9a3SHong Zhang                                         MatTransColoringApplyDenToSp_SeqAIJ,
31802b8ad9a3SHong Zhang                                         MatRARt_SeqAIJ_SeqAIJ,
31812b8ad9a3SHong Zhang                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
31822b8ad9a3SHong Zhang                                         MatRARtNumeric_SeqAIJ_SeqAIJ
31839e29f15eSvictorle };
318417ab2063SBarry Smith 
3185fb2e594dSBarry Smith EXTERN_C_BEGIN
31864a2ae208SSatish Balay #undef __FUNCT__
31874a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
31887087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3189bef8e0ddSBarry Smith {
3190bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
319197f1f81fSBarry Smith   PetscInt   i,nz,n;
3192bef8e0ddSBarry Smith 
3193bef8e0ddSBarry Smith   PetscFunctionBegin;
3194bef8e0ddSBarry Smith   nz = aij->maxnz;
3195d0f46423SBarry Smith   n  = mat->rmap->n;
3196bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3197bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3198bef8e0ddSBarry Smith   }
3199bef8e0ddSBarry Smith   aij->nz = nz;
3200bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3201bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3202bef8e0ddSBarry Smith   }
3203bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3204bef8e0ddSBarry Smith }
3205fb2e594dSBarry Smith EXTERN_C_END
3206bef8e0ddSBarry Smith 
32074a2ae208SSatish Balay #undef __FUNCT__
32084a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3209bef8e0ddSBarry Smith /*@
3210bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3211bef8e0ddSBarry Smith        in the matrix.
3212bef8e0ddSBarry Smith 
3213bef8e0ddSBarry Smith   Input Parameters:
3214bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3215bef8e0ddSBarry Smith -  indices - the column indices
3216bef8e0ddSBarry Smith 
321715091d37SBarry Smith   Level: advanced
321815091d37SBarry Smith 
3219bef8e0ddSBarry Smith   Notes:
3220bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3221bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3222bef8e0ddSBarry Smith   of the MatSetValues() operation.
3223bef8e0ddSBarry Smith 
3224bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3225d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3226bef8e0ddSBarry Smith 
3227bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3228bef8e0ddSBarry Smith 
3229b9617806SBarry Smith     The indices should start with zero, not one.
3230b9617806SBarry Smith 
3231bef8e0ddSBarry Smith @*/
32327087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3233bef8e0ddSBarry Smith {
32344ac538c5SBarry Smith   PetscErrorCode ierr;
3235bef8e0ddSBarry Smith 
3236bef8e0ddSBarry Smith   PetscFunctionBegin;
32370700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
32384482741eSBarry Smith   PetscValidPointer(indices,2);
32394ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3240bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3241bef8e0ddSBarry Smith }
3242bef8e0ddSBarry Smith 
3243be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3244be6bf707SBarry Smith 
3245fb2e594dSBarry Smith EXTERN_C_BEGIN
32464a2ae208SSatish Balay #undef __FUNCT__
32474a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
32487087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3249be6bf707SBarry Smith {
3250be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
32516849ba73SBarry Smith   PetscErrorCode ierr;
3252d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3253be6bf707SBarry Smith 
3254be6bf707SBarry Smith   PetscFunctionBegin;
3255f23aa3ddSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3256be6bf707SBarry Smith 
3257be6bf707SBarry Smith   /* allocate space for values if not already there */
3258be6bf707SBarry Smith   if (!aij->saved_values) {
325987828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
32609518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3261be6bf707SBarry Smith   }
3262be6bf707SBarry Smith 
3263be6bf707SBarry Smith   /* copy values over */
326487828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3265be6bf707SBarry Smith   PetscFunctionReturn(0);
3266be6bf707SBarry Smith }
3267fb2e594dSBarry Smith EXTERN_C_END
3268be6bf707SBarry Smith 
32694a2ae208SSatish Balay #undef __FUNCT__
3270b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3271be6bf707SBarry Smith /*@
3272be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3273be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3274be6bf707SBarry Smith        nonlinear portion.
3275be6bf707SBarry Smith 
3276be6bf707SBarry Smith    Collect on Mat
3277be6bf707SBarry Smith 
3278be6bf707SBarry Smith   Input Parameters:
32790e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3280be6bf707SBarry Smith 
328115091d37SBarry Smith   Level: advanced
328215091d37SBarry Smith 
3283be6bf707SBarry Smith   Common Usage, with SNESSolve():
3284be6bf707SBarry Smith $    Create Jacobian matrix
3285be6bf707SBarry Smith $    Set linear terms into matrix
3286be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3287be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3288be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3289512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3290be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3291be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3292be6bf707SBarry Smith $    In your Jacobian routine
3293be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3294be6bf707SBarry Smith $      Set nonlinear terms in matrix
3295be6bf707SBarry Smith 
3296be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3297be6bf707SBarry Smith $    // build linear portion of Jacobian
3298512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3299be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3300be6bf707SBarry Smith $    loop over nonlinear iterations
3301be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3302be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3303be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3304be6bf707SBarry Smith $       Solve linear system with Jacobian
3305be6bf707SBarry Smith $    endloop
3306be6bf707SBarry Smith 
3307be6bf707SBarry Smith   Notes:
3308be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3309512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3310be6bf707SBarry Smith     calling this routine.
3311be6bf707SBarry Smith 
33120c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
33130c468ba9SBarry Smith     and does not allocated additional space.
33140c468ba9SBarry Smith 
3315be6bf707SBarry Smith .seealso: MatRetrieveValues()
3316be6bf707SBarry Smith 
3317be6bf707SBarry Smith @*/
33187087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3319be6bf707SBarry Smith {
33204ac538c5SBarry Smith   PetscErrorCode ierr;
3321be6bf707SBarry Smith 
3322be6bf707SBarry Smith   PetscFunctionBegin;
33230700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3324e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3325e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
33264ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3327be6bf707SBarry Smith   PetscFunctionReturn(0);
3328be6bf707SBarry Smith }
3329be6bf707SBarry Smith 
3330fb2e594dSBarry Smith EXTERN_C_BEGIN
33314a2ae208SSatish Balay #undef __FUNCT__
33324a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
33337087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3334be6bf707SBarry Smith {
3335be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
33366849ba73SBarry Smith   PetscErrorCode ierr;
3337d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3338be6bf707SBarry Smith 
3339be6bf707SBarry Smith   PetscFunctionBegin;
3340f23aa3ddSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3341f23aa3ddSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3342be6bf707SBarry Smith   /* copy values over */
334387828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3344be6bf707SBarry Smith   PetscFunctionReturn(0);
3345be6bf707SBarry Smith }
3346fb2e594dSBarry Smith EXTERN_C_END
3347be6bf707SBarry Smith 
33484a2ae208SSatish Balay #undef __FUNCT__
33494a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3350be6bf707SBarry Smith /*@
3351be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3352be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3353be6bf707SBarry Smith        nonlinear portion.
3354be6bf707SBarry Smith 
3355be6bf707SBarry Smith    Collect on Mat
3356be6bf707SBarry Smith 
3357be6bf707SBarry Smith   Input Parameters:
3358be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3359be6bf707SBarry Smith 
336015091d37SBarry Smith   Level: advanced
336115091d37SBarry Smith 
3362be6bf707SBarry Smith .seealso: MatStoreValues()
3363be6bf707SBarry Smith 
3364be6bf707SBarry Smith @*/
33657087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3366be6bf707SBarry Smith {
33674ac538c5SBarry Smith   PetscErrorCode ierr;
3368be6bf707SBarry Smith 
3369be6bf707SBarry Smith   PetscFunctionBegin;
33700700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3371e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3372e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
33734ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3374be6bf707SBarry Smith   PetscFunctionReturn(0);
3375be6bf707SBarry Smith }
3376be6bf707SBarry Smith 
3377f83d6046SBarry Smith 
3378be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
33794a2ae208SSatish Balay #undef __FUNCT__
33804a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
338117ab2063SBarry Smith /*@C
3382682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
33830d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
33846e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
338551c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
33862bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
338717ab2063SBarry Smith 
3388db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3389db81eaa0SLois Curfman McInnes 
339017ab2063SBarry Smith    Input Parameters:
3391db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
339217ab2063SBarry Smith .  m - number of rows
339317ab2063SBarry Smith .  n - number of columns
339417ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
339551c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
33960298fd71SBarry Smith          (possibly different for each row) or NULL
339717ab2063SBarry Smith 
339817ab2063SBarry Smith    Output Parameter:
3399416022c9SBarry Smith .  A - the matrix
340017ab2063SBarry Smith 
3401175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3402ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3403175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3404175b88e8SBarry Smith 
3405b259b22eSLois Curfman McInnes    Notes:
340649a6f317SBarry Smith    If nnz is given then nz is ignored
340749a6f317SBarry Smith 
340817ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
340917ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
34100002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
341144cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
341217ab2063SBarry Smith 
341317ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
34140298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
34153d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
34166da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
341717ab2063SBarry Smith 
3418682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
34194fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3420682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
34216c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
34226c7ebb05SLois Curfman McInnes 
34236c7ebb05SLois Curfman McInnes    Options Database Keys:
3424698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
34259db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
342617ab2063SBarry Smith 
3427027ccd11SLois Curfman McInnes    Level: intermediate
3428027ccd11SLois Curfman McInnes 
342969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
343036db0b34SBarry Smith 
343117ab2063SBarry Smith @*/
34327087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
343317ab2063SBarry Smith {
3434dfbe8321SBarry Smith   PetscErrorCode ierr;
34356945ee14SBarry Smith 
34363a40ed3dSBarry Smith   PetscFunctionBegin;
3437f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3438117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3439c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3440d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3441273d9f13SBarry Smith   PetscFunctionReturn(0);
3442273d9f13SBarry Smith }
3443273d9f13SBarry Smith 
34444a2ae208SSatish Balay #undef __FUNCT__
34454a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3446273d9f13SBarry Smith /*@C
3447273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3448273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3449273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3450273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3451273d9f13SBarry Smith 
3452273d9f13SBarry Smith    Collective on MPI_Comm
3453273d9f13SBarry Smith 
3454273d9f13SBarry Smith    Input Parameters:
3455117016b1SBarry Smith +  B - The matrix-free
3456273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3457273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
34580298fd71SBarry Smith          (possibly different for each row) or NULL
3459273d9f13SBarry Smith 
3460273d9f13SBarry Smith    Notes:
346149a6f317SBarry Smith      If nnz is given then nz is ignored
346249a6f317SBarry Smith 
3463273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3464273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3465273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3466273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3467273d9f13SBarry Smith 
3468273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
34690298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3470273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3471273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3472273d9f13SBarry Smith 
3473aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3474aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3475aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3476aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3477aa95bbe8SBarry Smith 
3478a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3479a96a251dSBarry Smith    entries or columns indices
3480a96a251dSBarry Smith 
3481273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3482273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3483273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3484273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3485273d9f13SBarry Smith 
3486273d9f13SBarry Smith    Options Database Keys:
3487698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3488698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3489273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3490273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3491273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3492273d9f13SBarry Smith 
3493273d9f13SBarry Smith    Level: intermediate
3494273d9f13SBarry Smith 
349569b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3496273d9f13SBarry Smith 
3497273d9f13SBarry Smith @*/
34987087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3499273d9f13SBarry Smith {
35004ac538c5SBarry Smith   PetscErrorCode ierr;
3501a23d5eceSKris Buschelman 
3502a23d5eceSKris Buschelman   PetscFunctionBegin;
35036ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
35046ba663aaSJed Brown   PetscValidType(B,1);
35054ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3506a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3507a23d5eceSKris Buschelman }
3508a23d5eceSKris Buschelman 
3509a23d5eceSKris Buschelman EXTERN_C_BEGIN
3510a23d5eceSKris Buschelman #undef __FUNCT__
3511a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
35127087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3513a23d5eceSKris Buschelman {
3514273d9f13SBarry Smith   Mat_SeqAIJ     *b;
35152576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
35166849ba73SBarry Smith   PetscErrorCode ierr;
351797f1f81fSBarry Smith   PetscInt       i;
3518273d9f13SBarry Smith 
3519273d9f13SBarry Smith   PetscFunctionBegin;
35202576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3521a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3522c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3523c461c341SBarry Smith     nz             = 0;
3524c461c341SBarry Smith   }
3525c461c341SBarry Smith 
352626283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
352726283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3528899cda47SBarry Smith 
3529435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3530e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3531b73539f3SBarry Smith   if (nnz) {
3532d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3533e32f2f54SBarry 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]);
3534e32f2f54SBarry 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);
3535b73539f3SBarry Smith     }
3536b73539f3SBarry Smith   }
3537b73539f3SBarry Smith 
3538273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
35392205254eSKarl Rupp 
3540273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3541273d9f13SBarry Smith 
3542ab93d7beSBarry Smith   if (!skipallocation) {
35432ee49352SLisandro Dalcin     if (!b->imax) {
3544d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3545d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
35462ee49352SLisandro Dalcin     }
3547273d9f13SBarry Smith     if (!nnz) {
3548435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3549c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3550d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3551d0f46423SBarry Smith       nz = nz*B->rmap->n;
3552273d9f13SBarry Smith     } else {
3553273d9f13SBarry Smith       nz = 0;
3554d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3555273d9f13SBarry Smith     }
3556ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
35572205254eSKarl Rupp     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;
3558ab93d7beSBarry Smith 
3559273d9f13SBarry Smith     /* allocate the matrix space */
35602ee49352SLisandro Dalcin     ierr    = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3561d0f46423SBarry Smith     ierr    = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3562d0f46423SBarry Smith     ierr    = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3563bfeeae90SHong Zhang     b->i[0] = 0;
3564d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
35655da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
35665da197adSKris Buschelman     }
3567273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3568e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3569e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3570b31eba2aSShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
3571b31eba2aSShri Abhyankar     ierr = MatZeroEntries_SeqAIJ(B);CHKERRQ(ierr);
3572b31eba2aSShri Abhyankar #endif
3573c461c341SBarry Smith   } else {
3574e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
3575e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
3576c461c341SBarry Smith   }
3577273d9f13SBarry Smith 
3578273d9f13SBarry Smith   b->nz               = 0;
3579273d9f13SBarry Smith   b->maxnz            = nz;
3580273d9f13SBarry Smith   B->info.nz_unneeded = (double)b->maxnz;
35812205254eSKarl Rupp   if (realalloc) {
35822205254eSKarl Rupp     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
35832205254eSKarl Rupp   }
3584273d9f13SBarry Smith   PetscFunctionReturn(0);
3585273d9f13SBarry Smith }
3586a23d5eceSKris Buschelman EXTERN_C_END
3587273d9f13SBarry Smith 
3588a1661176SMatthew Knepley #undef  __FUNCT__
3589a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
359058d36128SBarry Smith /*@
3591a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3592a1661176SMatthew Knepley 
3593a1661176SMatthew Knepley    Input Parameters:
3594a1661176SMatthew Knepley +  B - the matrix
3595a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3596a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3597a1661176SMatthew Knepley -  v - optional values in the matrix
3598a1661176SMatthew Knepley 
3599a1661176SMatthew Knepley    Level: developer
3600a1661176SMatthew Knepley 
360158d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
360258d36128SBarry Smith 
3603a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3604a1661176SMatthew Knepley 
3605a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3606a1661176SMatthew Knepley @*/
3607a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3608a1661176SMatthew Knepley {
3609a1661176SMatthew Knepley   PetscErrorCode ierr;
3610a1661176SMatthew Knepley 
3611a1661176SMatthew Knepley   PetscFunctionBegin;
36120700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
36136ba663aaSJed Brown   PetscValidType(B,1);
36144ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3615a1661176SMatthew Knepley   PetscFunctionReturn(0);
3616a1661176SMatthew Knepley }
3617a1661176SMatthew Knepley 
3618a1661176SMatthew Knepley EXTERN_C_BEGIN
3619a1661176SMatthew Knepley #undef  __FUNCT__
3620a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
36217087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3622a1661176SMatthew Knepley {
3623a1661176SMatthew Knepley   PetscInt       i;
3624a1661176SMatthew Knepley   PetscInt       m,n;
3625a1661176SMatthew Knepley   PetscInt       nz;
3626a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3627a1661176SMatthew Knepley   PetscScalar    *values;
3628a1661176SMatthew Knepley   PetscErrorCode ierr;
3629a1661176SMatthew Knepley 
3630a1661176SMatthew Knepley   PetscFunctionBegin;
363165e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3632779a8d59SSatish Balay 
3633779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3634779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3635779a8d59SSatish Balay 
3636779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3637a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3638a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3639b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3640a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
364165e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3642a1661176SMatthew Knepley     nnz[i] = nz;
3643a1661176SMatthew Knepley   }
3644a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3645a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3646a1661176SMatthew Knepley 
3647a1661176SMatthew Knepley   if (v) {
3648a1661176SMatthew Knepley     values = (PetscScalar*) v;
3649a1661176SMatthew Knepley   } else {
36500e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3651a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3652a1661176SMatthew Knepley   }
3653a1661176SMatthew Knepley 
3654a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3655b7940d39SSatish Balay     nz   = Ii[i+1] - Ii[i];
3656b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3657a1661176SMatthew Knepley   }
3658a1661176SMatthew Knepley 
3659a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3660a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3661a1661176SMatthew Knepley 
3662a1661176SMatthew Knepley   if (!v) {
3663a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3664a1661176SMatthew Knepley   }
36657827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3666a1661176SMatthew Knepley   PetscFunctionReturn(0);
3667a1661176SMatthew Knepley }
3668a1661176SMatthew Knepley EXTERN_C_END
3669a1661176SMatthew Knepley 
3670c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3671b45d2f2cSJed Brown #include <petsc-private/petscaxpy.h>
3672170fe5c8SBarry Smith 
3673170fe5c8SBarry Smith #undef __FUNCT__
3674170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3675170fe5c8SBarry Smith /*
3676170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3677170fe5c8SBarry Smith 
3678170fe5c8SBarry Smith                n                       p                          p
3679170fe5c8SBarry Smith         (              )       (              )         (                  )
3680170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3681170fe5c8SBarry Smith         (              )       (              )         (                  )
3682170fe5c8SBarry Smith 
3683170fe5c8SBarry Smith */
3684170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3685170fe5c8SBarry Smith {
3686170fe5c8SBarry Smith   PetscErrorCode    ierr;
3687170fe5c8SBarry Smith   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3688170fe5c8SBarry Smith   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3689170fe5c8SBarry Smith   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
36901de00fd4SBarry Smith   PetscInt          i,n,m,q,p;
3691170fe5c8SBarry Smith   const PetscInt    *ii,*idx;
3692170fe5c8SBarry Smith   const PetscScalar *b,*a,*a_q;
3693170fe5c8SBarry Smith   PetscScalar       *c,*c_q;
3694170fe5c8SBarry Smith 
3695170fe5c8SBarry Smith   PetscFunctionBegin;
3696d0f46423SBarry Smith   m    = A->rmap->n;
3697d0f46423SBarry Smith   n    = A->cmap->n;
3698d0f46423SBarry Smith   p    = B->cmap->n;
3699170fe5c8SBarry Smith   a    = sub_a->v;
3700170fe5c8SBarry Smith   b    = sub_b->a;
3701170fe5c8SBarry Smith   c    = sub_c->v;
3702170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3703170fe5c8SBarry Smith 
3704170fe5c8SBarry Smith   ii  = sub_b->i;
3705170fe5c8SBarry Smith   idx = sub_b->j;
3706170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3707170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3708170fe5c8SBarry Smith     while (q-->0) {
3709170fe5c8SBarry Smith       c_q = c + m*(*idx);
3710170fe5c8SBarry Smith       a_q = a + m*i;
3711be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3712170fe5c8SBarry Smith       idx++;
3713170fe5c8SBarry Smith       b++;
3714170fe5c8SBarry Smith     }
3715170fe5c8SBarry Smith   }
3716170fe5c8SBarry Smith   PetscFunctionReturn(0);
3717170fe5c8SBarry Smith }
3718170fe5c8SBarry Smith 
3719170fe5c8SBarry Smith #undef __FUNCT__
3720170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3721170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3722170fe5c8SBarry Smith {
3723170fe5c8SBarry Smith   PetscErrorCode ierr;
3724d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3725170fe5c8SBarry Smith   Mat            Cmat;
3726170fe5c8SBarry Smith 
3727170fe5c8SBarry Smith   PetscFunctionBegin;
3728e32f2f54SBarry 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);
3729*ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
3730170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3731a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
3732170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
37330298fd71SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
3734d73949e8SHong Zhang 
3735d73949e8SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
37362205254eSKarl Rupp 
3737170fe5c8SBarry Smith   *C = Cmat;
3738170fe5c8SBarry Smith   PetscFunctionReturn(0);
3739170fe5c8SBarry Smith }
3740170fe5c8SBarry Smith 
3741170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3742170fe5c8SBarry Smith #undef __FUNCT__
3743170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3744170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3745170fe5c8SBarry Smith {
3746170fe5c8SBarry Smith   PetscErrorCode ierr;
3747170fe5c8SBarry Smith 
3748170fe5c8SBarry Smith   PetscFunctionBegin;
3749170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
3750170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3751170fe5c8SBarry Smith   }
3752170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3753170fe5c8SBarry Smith   PetscFunctionReturn(0);
3754170fe5c8SBarry Smith }
3755170fe5c8SBarry Smith 
3756170fe5c8SBarry Smith 
37570bad9183SKris Buschelman /*MC
3758fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
37590bad9183SKris Buschelman    based on compressed sparse row format.
37600bad9183SKris Buschelman 
37610bad9183SKris Buschelman    Options Database Keys:
37620bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
37630bad9183SKris Buschelman 
37640bad9183SKris Buschelman   Level: beginner
37650bad9183SKris Buschelman 
3766f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
37670bad9183SKris Buschelman M*/
37680bad9183SKris Buschelman 
3769ccd284c7SBarry Smith /*MC
3770ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
3771ccd284c7SBarry Smith 
3772ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
3773ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
3774ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
3775ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3776ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3777ccd284c7SBarry Smith 
3778ccd284c7SBarry Smith    Options Database Keys:
3779ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
3780ccd284c7SBarry Smith 
3781ccd284c7SBarry Smith   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
3782ccd284c7SBarry Smith    enough exist.
3783ccd284c7SBarry Smith 
3784ccd284c7SBarry Smith   Level: beginner
3785ccd284c7SBarry Smith 
3786ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3787ccd284c7SBarry Smith M*/
3788ccd284c7SBarry Smith 
3789ccd284c7SBarry Smith /*MC
3790ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
3791ccd284c7SBarry Smith 
3792ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3793ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3794ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3795ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3796ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3797ccd284c7SBarry Smith 
3798ccd284c7SBarry Smith    Options Database Keys:
3799ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3800ccd284c7SBarry Smith 
3801ccd284c7SBarry Smith   Level: beginner
3802ccd284c7SBarry Smith 
3803ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3804ccd284c7SBarry Smith M*/
3805ccd284c7SBarry Smith 
3806a6175056SHong Zhang EXTERN_C_BEGIN
3807b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3808b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3809b5e56a35SBarry Smith #endif
3810ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3811af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat*);
3812af1023dbSSatish Balay #endif
38137087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
38147087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
38157087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
38167087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool*);
3817611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
38187087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3819611f576cSBarry Smith #endif
3820611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
38217087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3822611f576cSBarry Smith #endif
3823f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3824f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3825f3c0ef26SHong Zhang #endif
3826eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
38277087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3828eb3b5408SSatish Balay #endif
3829586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
38307087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3831586621ddSJed Brown #endif
3832719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
38337087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3834719d5645SBarry Smith #endif
3835b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
38367087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
38377087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
38387087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3839b3866ffcSBarry Smith #endif
384017f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
384117f1a0eaSHong Zhang extern PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
384217f1a0eaSHong Zhang #endif
384317667f90SBarry Smith EXTERN_C_END
384417667f90SBarry Smith 
3845c0c8ee5eSDmitry Karpeev 
38468c778c55SBarry Smith #undef __FUNCT__
38478c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray"
38488c778c55SBarry Smith /*@C
38498c778c55SBarry Smith    MatSeqAIJGetArray - gives access to the array where the data for a SeqSeqAIJ matrix is stored
38508c778c55SBarry Smith 
38518c778c55SBarry Smith    Not Collective
38528c778c55SBarry Smith 
38538c778c55SBarry Smith    Input Parameter:
38548c778c55SBarry Smith .  mat - a MATSEQDENSE matrix
38558c778c55SBarry Smith 
38568c778c55SBarry Smith    Output Parameter:
38578c778c55SBarry Smith .   array - pointer to the data
38588c778c55SBarry Smith 
38598c778c55SBarry Smith    Level: intermediate
38608c778c55SBarry Smith 
38618c778c55SBarry Smith .seealso: MatSeqAIJRestoreArray()
38628c778c55SBarry Smith @*/
38638c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
38648c778c55SBarry Smith {
38658c778c55SBarry Smith   PetscErrorCode ierr;
38668c778c55SBarry Smith 
38678c778c55SBarry Smith   PetscFunctionBegin;
38688c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
38698c778c55SBarry Smith   PetscFunctionReturn(0);
38708c778c55SBarry Smith }
38718c778c55SBarry Smith 
38728c778c55SBarry Smith #undef __FUNCT__
38738c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray"
38748c778c55SBarry Smith /*@C
38758c778c55SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a SeqSeqAIJ matrix is stored obtained by MatSeqAIJGetArray()
38768c778c55SBarry Smith 
38778c778c55SBarry Smith    Not Collective
38788c778c55SBarry Smith 
38798c778c55SBarry Smith    Input Parameters:
38808c778c55SBarry Smith .  mat - a MATSEQDENSE matrix
38818c778c55SBarry Smith .  array - pointer to the data
38828c778c55SBarry Smith 
38838c778c55SBarry Smith    Level: intermediate
38848c778c55SBarry Smith 
38858c778c55SBarry Smith .seealso: MatSeqAIJGetArray()
38868c778c55SBarry Smith @*/
38878c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
38888c778c55SBarry Smith {
38898c778c55SBarry Smith   PetscErrorCode ierr;
38908c778c55SBarry Smith 
38918c778c55SBarry Smith   PetscFunctionBegin;
38928c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
38938c778c55SBarry Smith   PetscFunctionReturn(0);
38948c778c55SBarry Smith }
38958c778c55SBarry Smith 
389617667f90SBarry Smith EXTERN_C_BEGIN
38974a2ae208SSatish Balay #undef __FUNCT__
38984a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
38997087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqAIJ(Mat B)
3900273d9f13SBarry Smith {
3901273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3902dfbe8321SBarry Smith   PetscErrorCode ierr;
390338baddfdSBarry Smith   PetscMPIInt    size;
3904273d9f13SBarry Smith 
3905273d9f13SBarry Smith   PetscFunctionBegin;
3906*ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
3907e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3908273d9f13SBarry Smith 
390938f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
39102205254eSKarl Rupp 
3911b0a32e0cSBarry Smith   B->data = (void*)b;
39122205254eSKarl Rupp 
3913549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
39142205254eSKarl Rupp 
3915416022c9SBarry Smith   b->row                = 0;
3916416022c9SBarry Smith   b->col                = 0;
391782bf6240SBarry Smith   b->icol               = 0;
3918b810aeb4SBarry Smith   b->reallocs           = 0;
391936db0b34SBarry Smith   b->ignorezeroentries  = PETSC_FALSE;
3920f1e2ffcdSBarry Smith   b->roworiented        = PETSC_TRUE;
3921416022c9SBarry Smith   b->nonew              = 0;
3922416022c9SBarry Smith   b->diag               = 0;
3923416022c9SBarry Smith   b->solve_work         = 0;
39242a1b7f2aSHong Zhang   B->spptr              = 0;
3925be6bf707SBarry Smith   b->saved_values       = 0;
3926d7f994e1SBarry Smith   b->idiag              = 0;
392771f1c65dSBarry Smith   b->mdiag              = 0;
392871f1c65dSBarry Smith   b->ssor_work          = 0;
392971f1c65dSBarry Smith   b->omega              = 1.0;
393071f1c65dSBarry Smith   b->fshift             = 0.0;
393171f1c65dSBarry Smith   b->idiagvalid         = PETSC_FALSE;
3932bbead8a2SBarry Smith   b->ibdiagvalid        = PETSC_FALSE;
3933a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
3934a30b2313SHong Zhang   b->xtoy               = 0;
3935a30b2313SHong Zhang   b->XtoY               = 0;
393688e51ccdSHong Zhang   B->same_nonzero       = PETSC_FALSE;
393717ab2063SBarry Smith 
393835d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
39398c778c55SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJGetArray_C","MatSeqAIJGetArray_SeqAIJ",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
39408c778c55SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJRestoreArray_C","MatSeqAIJRestoreArray_SeqAIJ",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
39418c778c55SBarry Smith 
3942b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3943700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3944b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3945b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3946b3866ffcSBarry Smith #endif
3947b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3948700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3949b5e56a35SBarry Smith #endif
3950ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3951700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3952719d5645SBarry Smith #endif
3953611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3954700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3955611f576cSBarry Smith #endif
3956f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3957700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3958f3c0ef26SHong Zhang #endif
3959611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3960700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr);
3961611f576cSBarry Smith #endif
3962eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3963700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3964eb3b5408SSatish Balay #endif
3965586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3966700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3967586621ddSJed Brown #endif
3968719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
396917f1a0eaSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_aij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3970719d5645SBarry Smith #endif
397117f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
397217f1a0eaSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_clique_C","MatGetFactor_aij_clique",MatGetFactor_aij_clique);CHKERRQ(ierr);
397317f1a0eaSHong Zhang #endif
397417f1a0eaSHong Zhang 
3975700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3976700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3977700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
3978700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3979700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3980700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3981700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3982700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3983700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3984700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3985700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3986700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3987700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3988700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
3989700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3990700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3991700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3992700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
39934108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
399417667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
39953a40ed3dSBarry Smith   PetscFunctionReturn(0);
399617ab2063SBarry Smith }
3997273d9f13SBarry Smith EXTERN_C_END
399817ab2063SBarry Smith 
39994a2ae208SSatish Balay #undef __FUNCT__
4000b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
4001b24902e0SBarry Smith /*
4002b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4003b24902e0SBarry Smith */
4004ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
400517ab2063SBarry Smith {
4006416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
40076849ba73SBarry Smith   PetscErrorCode ierr;
4008d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
400917ab2063SBarry Smith 
40103a40ed3dSBarry Smith   PetscFunctionBegin;
4011273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4012273d9f13SBarry Smith 
4013d5f3da31SBarry Smith   C->factortype = A->factortype;
4014416022c9SBarry Smith   c->row        = 0;
4015416022c9SBarry Smith   c->col        = 0;
401682bf6240SBarry Smith   c->icol       = 0;
40176ad4291fSHong Zhang   c->reallocs   = 0;
401817ab2063SBarry Smith 
40196ad4291fSHong Zhang   C->assembled = PETSC_TRUE;
402017ab2063SBarry Smith 
4021aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4022aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4023eec197d1SBarry Smith 
402433b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
40259518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
402617ab2063SBarry Smith   for (i=0; i<m; i++) {
4027416022c9SBarry Smith     c->imax[i] = a->imax[i];
4028416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
402917ab2063SBarry Smith   }
403017ab2063SBarry Smith 
403117ab2063SBarry Smith   /* allocate the matrix space */
4032f77e22a1SHong Zhang   if (mallocmatspace) {
4033a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
40349518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
40352205254eSKarl Rupp 
4036f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
40372205254eSKarl Rupp 
403897f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
403917ab2063SBarry Smith     if (m > 0) {
404097f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4041be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4042bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4043be6bf707SBarry Smith       } else {
4044bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
404517ab2063SBarry Smith       }
404608480c60SBarry Smith     }
4047f77e22a1SHong Zhang   }
404817ab2063SBarry Smith 
40496ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4050416022c9SBarry Smith   c->roworiented       = a->roworiented;
4051416022c9SBarry Smith   c->nonew             = a->nonew;
4052416022c9SBarry Smith   if (a->diag) {
405397f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
405452e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
405517ab2063SBarry Smith     for (i=0; i<m; i++) {
4056416022c9SBarry Smith       c->diag[i] = a->diag[i];
405717ab2063SBarry Smith     }
40583a40ed3dSBarry Smith   } else c->diag = 0;
40592205254eSKarl Rupp 
40606ad4291fSHong Zhang   c->solve_work         = 0;
40616ad4291fSHong Zhang   c->saved_values       = 0;
40626ad4291fSHong Zhang   c->idiag              = 0;
406371f1c65dSBarry Smith   c->ssor_work          = 0;
4064a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
4065e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
4066e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
40676ad4291fSHong Zhang   c->xtoy               = 0;
40686ad4291fSHong Zhang   c->XtoY               = 0;
40696ad4291fSHong Zhang 
4070893ad86cSHong Zhang   c->rmax         = a->rmax;
4071416022c9SBarry Smith   c->nz           = a->nz;
40728ed568f8SMatthew G Knepley   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4073273d9f13SBarry Smith   C->preallocated = PETSC_TRUE;
4074754ec7b1SSatish Balay 
40756ad4291fSHong Zhang   c->compressedrow.use   = a->compressedrow.use;
40766ad4291fSHong Zhang   c->compressedrow.nrows = a->compressedrow.nrows;
4077cd6b891eSBarry Smith   c->compressedrow.check = a->compressedrow.check;
4078cd6b891eSBarry Smith   if (a->compressedrow.use) {
40796ad4291fSHong Zhang     i    = a->compressedrow.nrows;
40800e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
40816ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
40826ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
408327ea64f8SHong Zhang   } else {
408427ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
40850298fd71SBarry Smith     c->compressedrow.i      = NULL;
40860298fd71SBarry Smith     c->compressedrow.rindex = NULL;
40876ad4291fSHong Zhang   }
408888e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
40894846f1f5SKris Buschelman 
40902205254eSKarl Rupp   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4091140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
40923a40ed3dSBarry Smith   PetscFunctionReturn(0);
409317ab2063SBarry Smith }
409417ab2063SBarry Smith 
40954a2ae208SSatish Balay #undef __FUNCT__
4096b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
4097b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4098b24902e0SBarry Smith {
4099b24902e0SBarry Smith   PetscErrorCode ierr;
4100b24902e0SBarry Smith 
4101b24902e0SBarry Smith   PetscFunctionBegin;
4102*ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
41034b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4104a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
4105a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4106f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4107b24902e0SBarry Smith   PetscFunctionReturn(0);
4108b24902e0SBarry Smith }
4109b24902e0SBarry Smith 
4110b24902e0SBarry Smith #undef __FUNCT__
41114a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
4112112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4113fbdbba38SShri Abhyankar {
4114fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4115fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4116fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4117fbdbba38SShri Abhyankar   int            fd;
4118fbdbba38SShri Abhyankar   PetscMPIInt    size;
4119fbdbba38SShri Abhyankar   MPI_Comm       comm;
4120bbead8a2SBarry Smith   PetscInt       bs = 1;
4121fbdbba38SShri Abhyankar 
4122fbdbba38SShri Abhyankar   PetscFunctionBegin;
4123fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4124fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4125fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4126bbead8a2SBarry Smith 
41270298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
41280298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
4129bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4130bbead8a2SBarry Smith 
4131fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4132fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4133fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4134fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4135fbdbba38SShri Abhyankar 
4136bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4137fbdbba38SShri Abhyankar 
4138fbdbba38SShri Abhyankar   /* read in row lengths */
4139fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
4140fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4141fbdbba38SShri Abhyankar 
4142fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4143fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
4144fbdbba38SShri 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);
4145fbdbba38SShri Abhyankar 
4146fbdbba38SShri Abhyankar   /* set global size if not set already*/
4147f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4148fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4149aabbc4fbSShri Abhyankar   } else {
4150fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
4151fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
41524c5b953cSHong Zhang     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
41534c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
41544c5b953cSHong Zhang     }
4155f501eaabSShri 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);
4156aabbc4fbSShri Abhyankar   }
4157fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4158fbdbba38SShri Abhyankar   a    = (Mat_SeqAIJ*)newMat->data;
4159fbdbba38SShri Abhyankar 
4160fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4161fbdbba38SShri Abhyankar 
4162fbdbba38SShri Abhyankar   /* read in nonzero values */
4163fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4164fbdbba38SShri Abhyankar 
4165fbdbba38SShri Abhyankar   /* set matrix "i" values */
4166fbdbba38SShri Abhyankar   a->i[0] = 0;
4167fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4168fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4169fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4170fbdbba38SShri Abhyankar   }
4171fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4172fbdbba38SShri Abhyankar 
4173fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4174fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4175bbead8a2SBarry Smith   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
4176fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4177fbdbba38SShri Abhyankar }
4178fbdbba38SShri Abhyankar 
4179fbdbba38SShri Abhyankar #undef __FUNCT__
4180b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4181ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
41827264ac53SSatish Balay {
41837264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4184dfbe8321SBarry Smith   PetscErrorCode ierr;
4185eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4186eeffb40dSHong Zhang   PetscInt k;
4187eeffb40dSHong Zhang #endif
41887264ac53SSatish Balay 
41893a40ed3dSBarry Smith   PetscFunctionBegin;
4190bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4191d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4192ca44d042SBarry Smith     *flg = PETSC_FALSE;
4193ca44d042SBarry Smith     PetscFunctionReturn(0);
4194bcd2baecSBarry Smith   }
41957264ac53SSatish Balay 
41967264ac53SSatish Balay   /* if the a->i are the same */
4197d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4198abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
41997264ac53SSatish Balay 
42007264ac53SSatish Balay   /* if a->j are the same */
420197f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4202abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4203bcd2baecSBarry Smith 
4204bcd2baecSBarry Smith   /* if a->a are the same */
4205eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4206eeffb40dSHong Zhang   for (k=0; k<a->nz; k++) {
4207eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4208eeffb40dSHong Zhang       *flg = PETSC_FALSE;
42093a40ed3dSBarry Smith       PetscFunctionReturn(0);
4210eeffb40dSHong Zhang     }
4211eeffb40dSHong Zhang   }
4212eeffb40dSHong Zhang #else
4213eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4214eeffb40dSHong Zhang #endif
4215eeffb40dSHong Zhang   PetscFunctionReturn(0);
42167264ac53SSatish Balay }
421736db0b34SBarry Smith 
42184a2ae208SSatish Balay #undef __FUNCT__
42194a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
422005869f15SSatish Balay /*@
422136db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
422236db0b34SBarry Smith               provided by the user.
422336db0b34SBarry Smith 
4224c75a6043SHong Zhang       Collective on MPI_Comm
422536db0b34SBarry Smith 
422636db0b34SBarry Smith    Input Parameters:
422736db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
422836db0b34SBarry Smith .   m - number of rows
422936db0b34SBarry Smith .   n - number of columns
423036db0b34SBarry Smith .   i - row indices
423136db0b34SBarry Smith .   j - column indices
423236db0b34SBarry Smith -   a - matrix values
423336db0b34SBarry Smith 
423436db0b34SBarry Smith    Output Parameter:
423536db0b34SBarry Smith .   mat - the matrix
423636db0b34SBarry Smith 
423736db0b34SBarry Smith    Level: intermediate
423836db0b34SBarry Smith 
423936db0b34SBarry Smith    Notes:
42400551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4241292fb18eSBarry Smith     once the matrix is destroyed and not before
424236db0b34SBarry Smith 
424336db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
424436db0b34SBarry Smith 
4245bfeeae90SHong Zhang        The i and j indices are 0 based
424636db0b34SBarry Smith 
4247a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4248a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
4249a4552177SSatish Balay     as shown:
4250a4552177SSatish Balay 
4251a4552177SSatish Balay         1 0 0
4252a4552177SSatish Balay         2 0 3
4253a4552177SSatish Balay         4 5 6
4254a4552177SSatish Balay 
4255a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
42569985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4257a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
4258a4552177SSatish Balay 
42599985e31cSBarry Smith 
426069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
426136db0b34SBarry Smith 
426236db0b34SBarry Smith @*/
42637087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
426436db0b34SBarry Smith {
4265dfbe8321SBarry Smith   PetscErrorCode ierr;
4266cbcfb4deSHong Zhang   PetscInt       ii;
426736db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4268cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4269cbcfb4deSHong Zhang   PetscInt jj;
4270cbcfb4deSHong Zhang #endif
427136db0b34SBarry Smith 
427236db0b34SBarry Smith   PetscFunctionBegin;
4273f23aa3ddSBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4274f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4275f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4276a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4277ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4278ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4279ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4280ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
4281ab93d7beSBarry Smith 
428236db0b34SBarry Smith   aij->i            = i;
428336db0b34SBarry Smith   aij->j            = j;
428436db0b34SBarry Smith   aij->a            = a;
428536db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
428636db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4287e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4288e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
428936db0b34SBarry Smith 
429036db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
429136db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
42922515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
4293e32f2f54SBarry 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]);
42949985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4295e32f2f54SBarry 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);
4296e32f2f54SBarry 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);
42979985e31cSBarry Smith     }
429836db0b34SBarry Smith #endif
429936db0b34SBarry Smith   }
43002515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
430136db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
4302e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
4303e32f2f54SBarry 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]);
430436db0b34SBarry Smith   }
430536db0b34SBarry Smith #endif
430636db0b34SBarry Smith 
4307b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4308b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
430936db0b34SBarry Smith   PetscFunctionReturn(0);
431036db0b34SBarry Smith }
43118a0b0e6bSVictor Minden #undef __FUNCT__
43128a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
431380ef6e79SMatthew G Knepley /*@C
4314d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
43158a0b0e6bSVictor Minden               provided by the user.
43168a0b0e6bSVictor Minden 
43178a0b0e6bSVictor Minden       Collective on MPI_Comm
43188a0b0e6bSVictor Minden 
43198a0b0e6bSVictor Minden    Input Parameters:
43208a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
43218a0b0e6bSVictor Minden .   m   - number of rows
43228a0b0e6bSVictor Minden .   n   - number of columns
43238a0b0e6bSVictor Minden .   i   - row indices
43248a0b0e6bSVictor Minden .   j   - column indices
43251230e6d1SVictor Minden .   a   - matrix values
43261230e6d1SVictor Minden .   nz  - number of nonzeros
43271230e6d1SVictor Minden -   idx - 0 or 1 based
43288a0b0e6bSVictor Minden 
43298a0b0e6bSVictor Minden    Output Parameter:
43308a0b0e6bSVictor Minden .   mat - the matrix
43318a0b0e6bSVictor Minden 
43328a0b0e6bSVictor Minden    Level: intermediate
43338a0b0e6bSVictor Minden 
43348a0b0e6bSVictor Minden    Notes:
43358a0b0e6bSVictor Minden        The i and j indices are 0 based
43368a0b0e6bSVictor Minden 
43378a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
43388a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
43398a0b0e6bSVictor Minden     as shown:
43408a0b0e6bSVictor Minden 
43418a0b0e6bSVictor Minden         1 0 0
43428a0b0e6bSVictor Minden         2 0 3
43438a0b0e6bSVictor Minden         4 5 6
43448a0b0e6bSVictor Minden 
43458a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
43468a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
43478a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
43488a0b0e6bSVictor Minden 
43498a0b0e6bSVictor Minden 
435069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
43518a0b0e6bSVictor Minden 
43528a0b0e6bSVictor Minden @*/
43531230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
43548a0b0e6bSVictor Minden {
43558a0b0e6bSVictor Minden   PetscErrorCode ierr;
4356d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
43578a0b0e6bSVictor Minden 
43588a0b0e6bSVictor Minden 
43598a0b0e6bSVictor Minden   PetscFunctionBegin;
4360d021a1c5SVictor Minden   ierr = PetscMalloc(m*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
43611230e6d1SVictor Minden   ierr = PetscMemzero(nnz,m*sizeof(PetscInt));CHKERRQ(ierr);
43621230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
43631230e6d1SVictor Minden     nnz[i[ii]] += 1;
43641230e6d1SVictor Minden   }
43658a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
43668a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4367a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
43688a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
43691230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
43701230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
43711230e6d1SVictor Minden     if (idx) {
43721230e6d1SVictor Minden       row = i[ii] - 1;
43731230e6d1SVictor Minden       col = j[ii] - 1;
43741230e6d1SVictor Minden     } else {
43751230e6d1SVictor Minden       row = i[ii];
43761230e6d1SVictor Minden       col = j[ii];
43778a0b0e6bSVictor Minden     }
43781230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
43798a0b0e6bSVictor Minden   }
43808a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
43818a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4382d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
43838a0b0e6bSVictor Minden   PetscFunctionReturn(0);
43848a0b0e6bSVictor Minden }
438536db0b34SBarry Smith 
4386cc8ba8e1SBarry Smith #undef __FUNCT__
4387ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4388dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4389cc8ba8e1SBarry Smith {
4390dfbe8321SBarry Smith   PetscErrorCode ierr;
4391cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
439236db0b34SBarry Smith 
4393cc8ba8e1SBarry Smith   PetscFunctionBegin;
43948ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4395cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4396cc8ba8e1SBarry Smith     a->coloring = coloring;
439712c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
439897f1f81fSBarry Smith     PetscInt        i,*larray;
439912c595b3SBarry Smith     ISColoring      ocoloring;
440008b6dcc0SBarry Smith     ISColoringValue *colors;
440112c595b3SBarry Smith 
440212c595b3SBarry Smith     /* set coloring for diagonal portion */
44030e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
44042205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) larray[i] = i;
44050298fd71SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);CHKERRQ(ierr);
44060e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
44072205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
440812c595b3SBarry Smith     ierr        = PetscFree(larray);CHKERRQ(ierr);
4409d0f46423SBarry Smith     ierr        = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
441012c595b3SBarry Smith     a->coloring = ocoloring;
441112c595b3SBarry Smith   }
4412cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4413cc8ba8e1SBarry Smith }
4414cc8ba8e1SBarry Smith 
4415ee4f033dSBarry Smith #undef __FUNCT__
4416ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
441797f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4418ee4f033dSBarry Smith {
4419ee4f033dSBarry Smith   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
4420d0f46423SBarry Smith   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
442154f21887SBarry Smith   MatScalar       *v      = a->a;
442254f21887SBarry Smith   PetscScalar     *values = (PetscScalar*)advalues;
442308b6dcc0SBarry Smith   ISColoringValue *color;
4424ee4f033dSBarry Smith 
4425ee4f033dSBarry Smith   PetscFunctionBegin;
4426e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4427ee4f033dSBarry Smith   color = a->coloring->colors;
4428ee4f033dSBarry Smith   /* loop over rows */
4429ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4430ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4431ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
44322205254eSKarl Rupp     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
4433ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4434cc8ba8e1SBarry Smith   }
4435cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4436cc8ba8e1SBarry Smith }
443736db0b34SBarry Smith 
4438acf2f550SJed Brown #undef __FUNCT__
4439acf2f550SJed Brown #define __FUNCT__ "MatSeqAIJInvalidateDiagonal"
4440acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4441acf2f550SJed Brown {
4442acf2f550SJed Brown   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4443acf2f550SJed Brown   PetscErrorCode ierr;
4444acf2f550SJed Brown 
4445acf2f550SJed Brown   PetscFunctionBegin;
4446acf2f550SJed Brown   a->idiagvalid  = PETSC_FALSE;
4447acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
44482205254eSKarl Rupp 
4449acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4450acf2f550SJed Brown   PetscFunctionReturn(0);
4451acf2f550SJed Brown }
4452acf2f550SJed Brown 
445381824310SBarry Smith /*
445481824310SBarry Smith     Special version for direct calls from Fortran
445581824310SBarry Smith */
4456b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
445781824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
445881824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
445981824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
446081824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
446181824310SBarry Smith #endif
446281824310SBarry Smith 
446381824310SBarry Smith /* Change these macros so can be used in void function */
446481824310SBarry Smith #undef CHKERRQ
4465*ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
446681824310SBarry Smith #undef SETERRQ2
4467e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
44684994cf47SJed Brown #undef SETERRQ3
44694994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
447081824310SBarry Smith 
447181824310SBarry Smith EXTERN_C_BEGIN
447281824310SBarry Smith #undef __FUNCT__
447381824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
44741f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
447581824310SBarry Smith {
447681824310SBarry Smith   Mat            A  = *AA;
447781824310SBarry Smith   PetscInt       m  = *mm, n = *nn;
447881824310SBarry Smith   InsertMode     is = *isis;
447981824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
448081824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
448181824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
448281824310SBarry Smith   PetscErrorCode ierr;
448381824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
448454f21887SBarry Smith   MatScalar      *ap,value,*aa;
4485ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4486ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
448781824310SBarry Smith 
448881824310SBarry Smith   PetscFunctionBegin;
44894994cf47SJed Brown   MatCheckPreallocated(A,1);
449081824310SBarry Smith   imax  = a->imax;
449181824310SBarry Smith   ai    = a->i;
449281824310SBarry Smith   ailen = a->ilen;
449381824310SBarry Smith   aj    = a->j;
449481824310SBarry Smith   aa    = a->a;
449581824310SBarry Smith 
449681824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
449781824310SBarry Smith     row = im[k];
449881824310SBarry Smith     if (row < 0) continue;
449981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4500*ce94432eSBarry Smith     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
450181824310SBarry Smith #endif
450281824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
450381824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
450481824310SBarry Smith     low  = 0;
450581824310SBarry Smith     high = nrow;
450681824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
450781824310SBarry Smith       if (in[l] < 0) continue;
450881824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4509*ce94432eSBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
451081824310SBarry Smith #endif
451181824310SBarry Smith       col = in[l];
45122205254eSKarl Rupp       if (roworiented) value = v[l + k*n];
45132205254eSKarl Rupp       else value = v[k + l*m];
45142205254eSKarl Rupp 
451581824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
451681824310SBarry Smith 
45172205254eSKarl Rupp       if (col <= lastcol) low = 0;
45182205254eSKarl Rupp       else high = nrow;
451981824310SBarry Smith       lastcol = col;
452081824310SBarry Smith       while (high-low > 5) {
452181824310SBarry Smith         t = (low+high)/2;
452281824310SBarry Smith         if (rp[t] > col) high = t;
452381824310SBarry Smith         else             low  = t;
452481824310SBarry Smith       }
452581824310SBarry Smith       for (i=low; i<high; i++) {
452681824310SBarry Smith         if (rp[i] > col) break;
452781824310SBarry Smith         if (rp[i] == col) {
452881824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
452981824310SBarry Smith           else                  ap[i] = value;
453081824310SBarry Smith           goto noinsert;
453181824310SBarry Smith         }
453281824310SBarry Smith       }
453381824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
453481824310SBarry Smith       if (nonew == 1) goto noinsert;
4535*ce94432eSBarry Smith       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4536fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
453781824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
453881824310SBarry Smith       /* shift up all the later entries in this row */
453981824310SBarry Smith       for (ii=N; ii>=i; ii--) {
454081824310SBarry Smith         rp[ii+1] = rp[ii];
454181824310SBarry Smith         ap[ii+1] = ap[ii];
454281824310SBarry Smith       }
454381824310SBarry Smith       rp[i] = col;
454481824310SBarry Smith       ap[i] = value;
454581824310SBarry Smith noinsert:;
454681824310SBarry Smith       low = i + 1;
454781824310SBarry Smith     }
454881824310SBarry Smith     ailen[row] = nrow;
454981824310SBarry Smith   }
455081824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
455181824310SBarry Smith   PetscFunctionReturnVoid();
455281824310SBarry Smith }
455381824310SBarry Smith EXTERN_C_END
455462298a1eSBarry Smith 
4555