xref: /petsc/src/mat/impls/aij/seq/aij.c (revision cf37664f2dbf3effe674ec5ac2718028c1621ae0)
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>
11af0996ceSBarry Smith #include <petsc/private/kernels/blocktranspose.h>
120716a85fSBarry Smith 
130716a85fSBarry Smith #undef __FUNCT__
140716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_SeqAIJ"
150716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
160716a85fSBarry Smith {
170716a85fSBarry Smith   PetscErrorCode ierr;
180716a85fSBarry Smith   PetscInt       i,m,n;
190716a85fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
200716a85fSBarry Smith 
210716a85fSBarry Smith   PetscFunctionBegin;
220716a85fSBarry Smith   ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr);
230716a85fSBarry Smith   ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr);
240716a85fSBarry Smith   if (type == NORM_2) {
250716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
260716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
270716a85fSBarry Smith     }
280716a85fSBarry Smith   } else if (type == NORM_1) {
290716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
300716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
310716a85fSBarry Smith     }
320716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
330716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
340716a85fSBarry Smith       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
350716a85fSBarry Smith     }
360716a85fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");
370716a85fSBarry Smith 
380716a85fSBarry Smith   if (type == NORM_2) {
398f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
400716a85fSBarry Smith   }
410716a85fSBarry Smith   PetscFunctionReturn(0);
420716a85fSBarry Smith }
430716a85fSBarry Smith 
444a2ae208SSatish Balay #undef __FUNCT__
453a062f41SBarry Smith #define __FUNCT__ "MatFindOffBlockDiagonalEntries_SeqAIJ"
463a062f41SBarry Smith PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A,IS *is)
473a062f41SBarry Smith {
483a062f41SBarry Smith   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
493a062f41SBarry Smith   PetscInt        i,m=A->rmap->n,cnt = 0, bs = A->rmap->bs;
503a062f41SBarry Smith   const PetscInt  *jj = a->j,*ii = a->i;
513a062f41SBarry Smith   PetscInt        *rows;
523a062f41SBarry Smith   PetscErrorCode  ierr;
533a062f41SBarry Smith 
543a062f41SBarry Smith   PetscFunctionBegin;
553a062f41SBarry Smith   for (i=0; i<m; i++) {
563a062f41SBarry Smith     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
573a062f41SBarry Smith       cnt++;
583a062f41SBarry Smith     }
593a062f41SBarry Smith   }
603a062f41SBarry Smith   ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr);
613a062f41SBarry Smith   cnt  = 0;
623a062f41SBarry Smith   for (i=0; i<m; i++) {
633a062f41SBarry Smith     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
643a062f41SBarry Smith       rows[cnt] = i;
653a062f41SBarry Smith       cnt++;
663a062f41SBarry Smith     }
673a062f41SBarry Smith   }
683a062f41SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,is);CHKERRQ(ierr);
693a062f41SBarry Smith   PetscFunctionReturn(0);
703a062f41SBarry Smith }
713a062f41SBarry Smith 
723a062f41SBarry Smith #undef __FUNCT__
73f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ_Private"
74f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows)
756ce1633cSBarry Smith {
766ce1633cSBarry Smith   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
776ce1633cSBarry Smith   const MatScalar *aa = a->a;
786ce1633cSBarry Smith   PetscInt        i,m=A->rmap->n,cnt = 0;
79b2db7409Sstefano_zampini   const PetscInt  *ii = a->i,*jj = a->j,*diag;
806ce1633cSBarry Smith   PetscInt        *rows;
816ce1633cSBarry Smith   PetscErrorCode  ierr;
826ce1633cSBarry Smith 
836ce1633cSBarry Smith   PetscFunctionBegin;
846ce1633cSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
856ce1633cSBarry Smith   diag = a->diag;
866ce1633cSBarry Smith   for (i=0; i<m; i++) {
87b2db7409Sstefano_zampini     if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
886ce1633cSBarry Smith       cnt++;
896ce1633cSBarry Smith     }
906ce1633cSBarry Smith   }
91785e854fSJed Brown   ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr);
926ce1633cSBarry Smith   cnt  = 0;
936ce1633cSBarry Smith   for (i=0; i<m; i++) {
94b2db7409Sstefano_zampini     if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
956ce1633cSBarry Smith       rows[cnt++] = i;
966ce1633cSBarry Smith     }
976ce1633cSBarry Smith   }
98f1f41ecbSJed Brown   *nrows = cnt;
99f1f41ecbSJed Brown   *zrows = rows;
100f1f41ecbSJed Brown   PetscFunctionReturn(0);
101f1f41ecbSJed Brown }
102f1f41ecbSJed Brown 
103f1f41ecbSJed Brown #undef __FUNCT__
104f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ"
105f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
106f1f41ecbSJed Brown {
107f1f41ecbSJed Brown   PetscInt       nrows,*rows;
108f1f41ecbSJed Brown   PetscErrorCode ierr;
109f1f41ecbSJed Brown 
110f1f41ecbSJed Brown   PetscFunctionBegin;
1110298fd71SBarry Smith   *zrows = NULL;
112f1f41ecbSJed Brown   ierr   = MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);CHKERRQ(ierr);
113ce94432eSBarry Smith   ierr   = ISCreateGeneral(PetscObjectComm((PetscObject)A),nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
1146ce1633cSBarry Smith   PetscFunctionReturn(0);
1156ce1633cSBarry Smith }
1166ce1633cSBarry Smith 
1176ce1633cSBarry Smith #undef __FUNCT__
118b3a44c85SBarry Smith #define __FUNCT__ "MatFindNonzeroRows_SeqAIJ"
119b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
120b3a44c85SBarry Smith {
121b3a44c85SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
122b3a44c85SBarry Smith   const MatScalar *aa;
123b3a44c85SBarry Smith   PetscInt        m=A->rmap->n,cnt = 0;
124b3a44c85SBarry Smith   const PetscInt  *ii;
125b3a44c85SBarry Smith   PetscInt        n,i,j,*rows;
126b3a44c85SBarry Smith   PetscErrorCode  ierr;
127b3a44c85SBarry Smith 
128b3a44c85SBarry Smith   PetscFunctionBegin;
129b3a44c85SBarry Smith   *keptrows = 0;
130b3a44c85SBarry Smith   ii        = a->i;
131b3a44c85SBarry Smith   for (i=0; i<m; i++) {
132b3a44c85SBarry Smith     n = ii[i+1] - ii[i];
133b3a44c85SBarry Smith     if (!n) {
134b3a44c85SBarry Smith       cnt++;
135b3a44c85SBarry Smith       goto ok1;
136b3a44c85SBarry Smith     }
137b3a44c85SBarry Smith     aa = a->a + ii[i];
138b3a44c85SBarry Smith     for (j=0; j<n; j++) {
139b3a44c85SBarry Smith       if (aa[j] != 0.0) goto ok1;
140b3a44c85SBarry Smith     }
141b3a44c85SBarry Smith     cnt++;
142b3a44c85SBarry Smith ok1:;
143b3a44c85SBarry Smith   }
144b3a44c85SBarry Smith   if (!cnt) PetscFunctionReturn(0);
145854ce69bSBarry Smith   ierr = PetscMalloc1(A->rmap->n-cnt,&rows);CHKERRQ(ierr);
146b3a44c85SBarry Smith   cnt  = 0;
147b3a44c85SBarry Smith   for (i=0; i<m; i++) {
148b3a44c85SBarry Smith     n = ii[i+1] - ii[i];
149b3a44c85SBarry Smith     if (!n) continue;
150b3a44c85SBarry Smith     aa = a->a + ii[i];
151b3a44c85SBarry Smith     for (j=0; j<n; j++) {
152b3a44c85SBarry Smith       if (aa[j] != 0.0) {
153b3a44c85SBarry Smith         rows[cnt++] = i;
154b3a44c85SBarry Smith         break;
155b3a44c85SBarry Smith       }
156b3a44c85SBarry Smith     }
157b3a44c85SBarry Smith   }
158b3a44c85SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
159b3a44c85SBarry Smith   PetscFunctionReturn(0);
160b3a44c85SBarry Smith }
161b3a44c85SBarry Smith 
162b3a44c85SBarry Smith #undef __FUNCT__
16379299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1647087cfbeSBarry Smith PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
16579299369SBarry Smith {
16679299369SBarry Smith   PetscErrorCode    ierr;
16779299369SBarry Smith   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*) Y->data;
16899e65526SBarry Smith   PetscInt          i,m = Y->rmap->n;
16999e65526SBarry Smith   const PetscInt    *diag;
17054f21887SBarry Smith   MatScalar         *aa = aij->a;
17199e65526SBarry Smith   const PetscScalar *v;
172ace3abfcSBarry Smith   PetscBool         missing;
17379299369SBarry Smith 
17479299369SBarry Smith   PetscFunctionBegin;
17509f38230SBarry Smith   if (Y->assembled) {
1760298fd71SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);CHKERRQ(ierr);
17709f38230SBarry Smith     if (!missing) {
17879299369SBarry Smith       diag = aij->diag;
17999e65526SBarry Smith       ierr = VecGetArrayRead(D,&v);CHKERRQ(ierr);
18079299369SBarry Smith       if (is == INSERT_VALUES) {
18179299369SBarry Smith         for (i=0; i<m; i++) {
18279299369SBarry Smith           aa[diag[i]] = v[i];
18379299369SBarry Smith         }
18479299369SBarry Smith       } else {
18579299369SBarry Smith         for (i=0; i<m; i++) {
18679299369SBarry Smith           aa[diag[i]] += v[i];
18779299369SBarry Smith         }
18879299369SBarry Smith       }
18999e65526SBarry Smith       ierr = VecRestoreArrayRead(D,&v);CHKERRQ(ierr);
19079299369SBarry Smith       PetscFunctionReturn(0);
19179299369SBarry Smith     }
192acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
19309f38230SBarry Smith   }
19409f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
19509f38230SBarry Smith   PetscFunctionReturn(0);
19609f38230SBarry Smith }
19779299369SBarry Smith 
19879299369SBarry Smith #undef __FUNCT__
1994a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
2001a83f524SJed Brown PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
20117ab2063SBarry Smith {
202416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
203dfbe8321SBarry Smith   PetscErrorCode ierr;
20497f1f81fSBarry Smith   PetscInt       i,ishift;
20517ab2063SBarry Smith 
2063a40ed3dSBarry Smith   PetscFunctionBegin;
207d0f46423SBarry Smith   *m = A->rmap->n;
2083a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
209bfeeae90SHong Zhang   ishift = 0;
21053e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
2112462f5fdSStefano Zampini     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
212bfeeae90SHong Zhang   } else if (oshift == 1) {
2131a83f524SJed Brown     PetscInt *tia;
214d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
2153b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
216854ce69bSBarry Smith     ierr = PetscMalloc1(A->rmap->n+1,&tia);CHKERRQ(ierr);
2171a83f524SJed Brown     for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1;
2181a83f524SJed Brown     *ia = tia;
219ecc77c7aSBarry Smith     if (ja) {
2201a83f524SJed Brown       PetscInt *tja;
221854ce69bSBarry Smith       ierr = PetscMalloc1(nz+1,&tja);CHKERRQ(ierr);
2221a83f524SJed Brown       for (i=0; i<nz; i++) tja[i] = a->j[i] + 1;
2231a83f524SJed Brown       *ja = tja;
224ecc77c7aSBarry Smith     }
2256945ee14SBarry Smith   } else {
226ecc77c7aSBarry Smith     *ia = a->i;
227ecc77c7aSBarry Smith     if (ja) *ja = a->j;
228a2ce50c7SBarry Smith   }
2293a40ed3dSBarry Smith   PetscFunctionReturn(0);
230a2744918SBarry Smith }
231a2744918SBarry Smith 
2324a2ae208SSatish Balay #undef __FUNCT__
2334a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
2341a83f524SJed Brown PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2356945ee14SBarry Smith {
236dfbe8321SBarry Smith   PetscErrorCode ierr;
2376945ee14SBarry Smith 
2383a40ed3dSBarry Smith   PetscFunctionBegin;
2393a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
240bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
241606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
242ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
243bcd2baecSBarry Smith   }
2443a40ed3dSBarry Smith   PetscFunctionReturn(0);
24517ab2063SBarry Smith }
24617ab2063SBarry Smith 
2474a2ae208SSatish Balay #undef __FUNCT__
2484a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
2491a83f524SJed Brown PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2503b2fbd54SBarry Smith {
2513b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
252dfbe8321SBarry Smith   PetscErrorCode ierr;
253d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
25497f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
2553b2fbd54SBarry Smith 
2563a40ed3dSBarry Smith   PetscFunctionBegin;
257899cda47SBarry Smith   *nn = n;
2583a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2593b2fbd54SBarry Smith   if (symmetric) {
2602462f5fdSStefano Zampini     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,0,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
2613b2fbd54SBarry Smith   } else {
2621795a4d1SJed Brown     ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
263854ce69bSBarry Smith     ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr);
264854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&cja);CHKERRQ(ierr);
2653b2fbd54SBarry Smith     jj   = a->j;
2663b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
267bfeeae90SHong Zhang       collengths[jj[i]]++;
2683b2fbd54SBarry Smith     }
2693b2fbd54SBarry Smith     cia[0] = oshift;
2703b2fbd54SBarry Smith     for (i=0; i<n; i++) {
2713b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
2723b2fbd54SBarry Smith     }
27397f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
2743b2fbd54SBarry Smith     jj   = a->j;
275a93ec695SBarry Smith     for (row=0; row<m; row++) {
276a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
277a93ec695SBarry Smith       for (i=0; i<mr; i++) {
278bfeeae90SHong Zhang         col = *jj++;
2792205254eSKarl Rupp 
2803b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2813b2fbd54SBarry Smith       }
2823b2fbd54SBarry Smith     }
283606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2843b2fbd54SBarry Smith     *ia  = cia; *ja = cja;
2853b2fbd54SBarry Smith   }
2863a40ed3dSBarry Smith   PetscFunctionReturn(0);
2873b2fbd54SBarry Smith }
2883b2fbd54SBarry Smith 
2894a2ae208SSatish Balay #undef __FUNCT__
2904a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
2911a83f524SJed Brown PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2923b2fbd54SBarry Smith {
293dfbe8321SBarry Smith   PetscErrorCode ierr;
294606d414cSSatish Balay 
2953a40ed3dSBarry Smith   PetscFunctionBegin;
2963a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2973b2fbd54SBarry Smith 
298606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
299606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
3003a40ed3dSBarry Smith   PetscFunctionReturn(0);
3013b2fbd54SBarry Smith }
3023b2fbd54SBarry Smith 
3037cee066cSHong Zhang /*
3047cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
3057cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
306040ebd07SHong Zhang  spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
3077cee066cSHong Zhang */
3087cee066cSHong Zhang #undef __FUNCT__
3097cee066cSHong Zhang #define __FUNCT__ "MatGetColumnIJ_SeqAIJ_Color"
3107cee066cSHong Zhang PetscErrorCode MatGetColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
3117cee066cSHong Zhang {
3127cee066cSHong Zhang   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3137cee066cSHong Zhang   PetscErrorCode ierr;
3147cee066cSHong Zhang   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
3157cee066cSHong Zhang   PetscInt       nz = a->i[m],row,*jj,mr,col;
3167cee066cSHong Zhang   PetscInt       *cspidx;
3177cee066cSHong Zhang 
3187cee066cSHong Zhang   PetscFunctionBegin;
3197cee066cSHong Zhang   *nn = n;
3207cee066cSHong Zhang   if (!ia) PetscFunctionReturn(0);
321625f6d37SHong Zhang 
3221795a4d1SJed Brown   ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
323854ce69bSBarry Smith   ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr);
324854ce69bSBarry Smith   ierr = PetscMalloc1(nz+1,&cja);CHKERRQ(ierr);
325854ce69bSBarry Smith   ierr = PetscMalloc1(nz+1,&cspidx);CHKERRQ(ierr);
3267cee066cSHong Zhang   jj   = a->j;
3277cee066cSHong Zhang   for (i=0; i<nz; i++) {
3287cee066cSHong Zhang     collengths[jj[i]]++;
3297cee066cSHong Zhang   }
3307cee066cSHong Zhang   cia[0] = oshift;
3317cee066cSHong Zhang   for (i=0; i<n; i++) {
3327cee066cSHong Zhang     cia[i+1] = cia[i] + collengths[i];
3337cee066cSHong Zhang   }
3347cee066cSHong Zhang   ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
3357cee066cSHong Zhang   jj   = a->j;
3367cee066cSHong Zhang   for (row=0; row<m; row++) {
3377cee066cSHong Zhang     mr = a->i[row+1] - a->i[row];
3387cee066cSHong Zhang     for (i=0; i<mr; i++) {
3397cee066cSHong Zhang       col = *jj++;
3407cee066cSHong Zhang       cspidx[cia[col] + collengths[col] - oshift] = a->i[row] + i; /* index of a->j */
3417cee066cSHong Zhang       cja[cia[col] + collengths[col]++ - oshift]  = row + oshift;
3427cee066cSHong Zhang     }
3437cee066cSHong Zhang   }
3447cee066cSHong Zhang   ierr   = PetscFree(collengths);CHKERRQ(ierr);
3457cee066cSHong Zhang   *ia    = cia; *ja = cja;
3467cee066cSHong Zhang   *spidx = cspidx;
3477cee066cSHong Zhang   PetscFunctionReturn(0);
3487cee066cSHong Zhang }
3497cee066cSHong Zhang 
3507cee066cSHong Zhang #undef __FUNCT__
3517cee066cSHong Zhang #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ_Color"
3527cee066cSHong Zhang PetscErrorCode MatRestoreColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
3537cee066cSHong Zhang {
3547cee066cSHong Zhang   PetscErrorCode ierr;
3557cee066cSHong Zhang 
3567cee066cSHong Zhang   PetscFunctionBegin;
3575243ef75SHong Zhang   ierr = MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
3587cee066cSHong Zhang   ierr = PetscFree(*spidx);CHKERRQ(ierr);
3597cee066cSHong Zhang   PetscFunctionReturn(0);
3607cee066cSHong Zhang }
3617cee066cSHong Zhang 
36287d4246cSBarry Smith #undef __FUNCT__
36387d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
36487d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
36587d4246cSBarry Smith {
36687d4246cSBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
36787d4246cSBarry Smith   PetscInt       *ai = a->i;
36887d4246cSBarry Smith   PetscErrorCode ierr;
36987d4246cSBarry Smith 
37087d4246cSBarry Smith   PetscFunctionBegin;
37187d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
37287d4246cSBarry Smith   PetscFunctionReturn(0);
37387d4246cSBarry Smith }
37487d4246cSBarry Smith 
375bd04181cSBarry Smith /*
376bd04181cSBarry Smith     MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions
377bd04181cSBarry Smith 
378bd04181cSBarry Smith       -   a single row of values is set with each call
379bd04181cSBarry Smith       -   no row or column indices are negative or (in error) larger than the number of rows or columns
380bd04181cSBarry Smith       -   the values are always added to the matrix, not set
381bd04181cSBarry Smith       -   no new locations are introduced in the nonzero structure of the matrix
382bd04181cSBarry Smith 
3831f763a69SBarry Smith      This does NOT assume the global column indices are sorted
384bd04181cSBarry Smith 
3851f763a69SBarry Smith */
386bd04181cSBarry Smith 
387af0996ceSBarry Smith #include <petsc/private/isimpl.h>
388189e4007SBarry Smith #undef __FUNCT__
389189e4007SBarry Smith #define __FUNCT__ "MatSeqAIJSetValuesLocalFast"
390189e4007SBarry Smith PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
391189e4007SBarry Smith {
392189e4007SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3931f763a69SBarry Smith   PetscInt       low,high,t,row,nrow,i,col,l;
3941f763a69SBarry Smith   const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j;
3951f763a69SBarry Smith   PetscInt       lastcol = -1;
396189e4007SBarry Smith   MatScalar      *ap,value,*aa = a->a;
397189e4007SBarry Smith   const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices;
398189e4007SBarry Smith 
399f38dd0b8SBarry Smith   row = ridx[im[0]];
4001f763a69SBarry Smith   rp   = aj + ai[row];
4011f763a69SBarry Smith   ap = aa + ai[row];
4021f763a69SBarry Smith   nrow = ailen[row];
403189e4007SBarry Smith   low  = 0;
404189e4007SBarry Smith   high = nrow;
405189e4007SBarry Smith   for (l=0; l<n; l++) { /* loop over added columns */
406189e4007SBarry Smith     col = cidx[in[l]];
407f38dd0b8SBarry Smith     value = v[l];
408189e4007SBarry Smith 
409189e4007SBarry Smith     if (col <= lastcol) low = 0;
410189e4007SBarry Smith     else high = nrow;
411189e4007SBarry Smith     lastcol = col;
412189e4007SBarry Smith     while (high-low > 5) {
413189e4007SBarry Smith       t = (low+high)/2;
414189e4007SBarry Smith       if (rp[t] > col) high = t;
415189e4007SBarry Smith       else low = t;
416189e4007SBarry Smith     }
417189e4007SBarry Smith     for (i=low; i<high; i++) {
418189e4007SBarry Smith       if (rp[i] == col) {
4191f763a69SBarry Smith         ap[i] += value;
420189e4007SBarry Smith         low = i + 1;
4211f763a69SBarry Smith         break;
422189e4007SBarry Smith       }
423189e4007SBarry Smith     }
424189e4007SBarry Smith   }
425f38dd0b8SBarry Smith   return 0;
426189e4007SBarry Smith }
427189e4007SBarry Smith 
428bd04181cSBarry Smith #undef __FUNCT__
4294a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
43097f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
43117ab2063SBarry Smith {
432416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
433e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
43497f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
4356849ba73SBarry Smith   PetscErrorCode ierr;
436e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
43754f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
438ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
439ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
44017ab2063SBarry Smith 
4413a40ed3dSBarry Smith   PetscFunctionBegin;
44217ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
443416022c9SBarry Smith     row = im[k];
4445ef9f2a5SBarry Smith     if (row < 0) continue;
4452515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
446e32f2f54SBarry 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);
4473b2fbd54SBarry Smith #endif
448bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
44917ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
450416022c9SBarry Smith     low  = 0;
451c71e6ed7SBarry Smith     high = nrow;
45217ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
4535ef9f2a5SBarry Smith       if (in[l] < 0) continue;
4542515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
455e32f2f54SBarry 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);
4563b2fbd54SBarry Smith #endif
457bfeeae90SHong Zhang       col = in[l];
4584b0e389bSBarry Smith       if (roworiented) {
4595ef9f2a5SBarry Smith         value = v[l + k*n];
460bef8e0ddSBarry Smith       } else {
4614b0e389bSBarry Smith         value = v[k + l*m];
4624b0e389bSBarry Smith       }
46333b2b78bSBarry Smith       if ((value == 0.0 && ignorezeroentries) && (is == ADD_VALUES)) continue;
46436db0b34SBarry Smith 
4652205254eSKarl Rupp       if (col <= lastcol) low = 0;
4662205254eSKarl Rupp       else high = nrow;
467e2ee6c50SBarry Smith       lastcol = col;
468416022c9SBarry Smith       while (high-low > 5) {
469416022c9SBarry Smith         t = (low+high)/2;
470416022c9SBarry Smith         if (rp[t] > col) high = t;
471416022c9SBarry Smith         else low = t;
47217ab2063SBarry Smith       }
473416022c9SBarry Smith       for (i=low; i<high; i++) {
47417ab2063SBarry Smith         if (rp[i] > col) break;
47517ab2063SBarry Smith         if (rp[i] == col) {
476416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
47717ab2063SBarry Smith           else ap[i] = value;
478e44c0bd4SBarry Smith           low = i + 1;
47917ab2063SBarry Smith           goto noinsert;
48017ab2063SBarry Smith         }
48117ab2063SBarry Smith       }
482abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
483c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
484e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
485fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
486c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
487416022c9SBarry Smith       /* shift up all the later entries in this row */
488416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
48917ab2063SBarry Smith         rp[ii+1] = rp[ii];
49017ab2063SBarry Smith         ap[ii+1] = ap[ii];
49117ab2063SBarry Smith       }
49217ab2063SBarry Smith       rp[i] = col;
49317ab2063SBarry Smith       ap[i] = value;
494416022c9SBarry Smith       low   = i + 1;
495e56f5c9eSBarry Smith       A->nonzerostate++;
496e44c0bd4SBarry Smith noinsert:;
49717ab2063SBarry Smith     }
49817ab2063SBarry Smith     ailen[row] = nrow;
49917ab2063SBarry Smith   }
5003a40ed3dSBarry Smith   PetscFunctionReturn(0);
50117ab2063SBarry Smith }
50217ab2063SBarry Smith 
50381824310SBarry Smith 
5044a2ae208SSatish Balay #undef __FUNCT__
5054a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
506a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
5077eb43aa7SLois Curfman McInnes {
5087eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
50997f1f81fSBarry Smith   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
51097f1f81fSBarry Smith   PetscInt   *ai = a->i,*ailen = a->ilen;
51154f21887SBarry Smith   MatScalar  *ap,*aa = a->a;
5127eb43aa7SLois Curfman McInnes 
5133a40ed3dSBarry Smith   PetscFunctionBegin;
5147eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
5157eb43aa7SLois Curfman McInnes     row = im[k];
516e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
517e32f2f54SBarry 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);
518bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
5197eb43aa7SLois Curfman McInnes     nrow = ailen[row];
5207eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
521e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
522e32f2f54SBarry 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);
523bfeeae90SHong Zhang       col  = in[l];
5247eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
5257eb43aa7SLois Curfman McInnes       while (high-low > 5) {
5267eb43aa7SLois Curfman McInnes         t = (low+high)/2;
5277eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
5287eb43aa7SLois Curfman McInnes         else low = t;
5297eb43aa7SLois Curfman McInnes       }
5307eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
5317eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
5327eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
533b49de8d1SLois Curfman McInnes           *v++ = ap[i];
5347eb43aa7SLois Curfman McInnes           goto finished;
5357eb43aa7SLois Curfman McInnes         }
5367eb43aa7SLois Curfman McInnes       }
53797e567efSBarry Smith       *v++ = 0.0;
5387eb43aa7SLois Curfman McInnes finished:;
5397eb43aa7SLois Curfman McInnes     }
5407eb43aa7SLois Curfman McInnes   }
5413a40ed3dSBarry Smith   PetscFunctionReturn(0);
5427eb43aa7SLois Curfman McInnes }
5437eb43aa7SLois Curfman McInnes 
54417ab2063SBarry Smith 
5454a2ae208SSatish Balay #undef __FUNCT__
5464a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
547dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
54817ab2063SBarry Smith {
549416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5506849ba73SBarry Smith   PetscErrorCode ierr;
5516f69ff64SBarry Smith   PetscInt       i,*col_lens;
5526f69ff64SBarry Smith   int            fd;
553b37d52dbSMark F. Adams   FILE           *file;
55417ab2063SBarry Smith 
5553a40ed3dSBarry Smith   PetscFunctionBegin;
556b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
557854ce69bSBarry Smith   ierr = PetscMalloc1(4+A->rmap->n,&col_lens);CHKERRQ(ierr);
5582205254eSKarl Rupp 
5590700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
560d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
561d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
562416022c9SBarry Smith   col_lens[3] = a->nz;
563416022c9SBarry Smith 
564416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
565d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
566416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
56717ab2063SBarry Smith   }
568d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
569606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
570416022c9SBarry Smith 
571416022c9SBarry Smith   /* store column indices (zero start index) */
5726f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
573416022c9SBarry Smith 
574416022c9SBarry Smith   /* store nonzero values */
5756f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
576b37d52dbSMark F. Adams 
577b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
578b37d52dbSMark F. Adams   if (file) {
57933d57670SJed Brown     fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs));
580b37d52dbSMark F. Adams   }
5813a40ed3dSBarry Smith   PetscFunctionReturn(0);
58217ab2063SBarry Smith }
583416022c9SBarry Smith 
58409573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
585cd155464SBarry Smith 
5864a2ae208SSatish Balay #undef __FUNCT__
5874a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
588dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
589416022c9SBarry Smith {
590416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
591dfbe8321SBarry Smith   PetscErrorCode    ierr;
59260e0710aSBarry Smith   PetscInt          i,j,m = A->rmap->n;
593e060cb09SBarry Smith   const char        *name;
594f3ef73ceSBarry Smith   PetscViewerFormat format;
59517ab2063SBarry Smith 
5963a40ed3dSBarry Smith   PetscFunctionBegin;
597b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
59871c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
59997f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
60060e0710aSBarry Smith     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
601c337ccceSJed Brown       /* Need a dummy value to ensure the dimension of the matrix. */
602d00d2cf4SBarry Smith       nofinalvalue = 1;
603d00d2cf4SBarry Smith     }
604d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
605d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
60677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
607fbfe6fa7SJed Brown #if defined(PETSC_USE_COMPLEX)
608fbfe6fa7SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
609fbfe6fa7SJed Brown #else
61077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
611fbfe6fa7SJed Brown #endif
612b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
61317ab2063SBarry Smith 
61417ab2063SBarry Smith     for (i=0; i<m; i++) {
61560e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
616aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
617a9bf72d8SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",i+1,a->j[j]+1,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
61817ab2063SBarry Smith #else
61960e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr);
62017ab2063SBarry Smith #endif
62117ab2063SBarry Smith       }
62217ab2063SBarry Smith     }
623d00d2cf4SBarry Smith     if (nofinalvalue) {
624c337ccceSJed Brown #if defined(PETSC_USE_COMPLEX)
625c337ccceSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr);
626c337ccceSJed Brown #else
627d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
628c337ccceSJed Brown #endif
629d00d2cf4SBarry Smith     }
630317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
631fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
632d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
63368369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
634cd155464SBarry Smith     PetscFunctionReturn(0);
635fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
636d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
63744cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
63877431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
63960e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
640aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
64136db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
64260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
64336db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
64460e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
64536db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
64660e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
6476831982aSBarry Smith         }
64844cd7ae7SLois Curfman McInnes #else
64960e0710aSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);}
65044cd7ae7SLois Curfman McInnes #endif
65144cd7ae7SLois Curfman McInnes       }
652b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
65344cd7ae7SLois Curfman McInnes     }
654d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
655fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
65697f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
657d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
658854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&sptr);CHKERRQ(ierr);
659496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
660496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
66160e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
662496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
663aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
66436db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
665496be53dSLois Curfman McInnes #else
666496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
667496be53dSLois Curfman McInnes #endif
668496be53dSLois Curfman McInnes         }
669496be53dSLois Curfman McInnes       }
670496be53dSLois Curfman McInnes     }
6712e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
67277431f27SBarry Smith     ierr    = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
6732e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
6742205254eSKarl Rupp       if (i+4<m) {
6752205254eSKarl 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);
6762205254eSKarl Rupp       } else if (i+3<m) {
6772205254eSKarl 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);
6782205254eSKarl Rupp       } else if (i+2<m) {
6792205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);
6802205254eSKarl Rupp       } else if (i+1<m) {
6812205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);
6822205254eSKarl Rupp       } else if (i<m) {
6832205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);
6842205254eSKarl Rupp       } else {
6852205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);
6862205254eSKarl Rupp       }
687496be53dSLois Curfman McInnes     }
688b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
689606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
690496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
69160e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
69277431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
693496be53dSLois Curfman McInnes       }
694b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
695496be53dSLois Curfman McInnes     }
696b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
697496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
69860e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
699496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
700aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
70136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
70260e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
7036831982aSBarry Smith           }
704496be53dSLois Curfman McInnes #else
70560e0710aSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);}
706496be53dSLois Curfman McInnes #endif
707496be53dSLois Curfman McInnes         }
708496be53dSLois Curfman McInnes       }
709b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
710496be53dSLois Curfman McInnes     }
711d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
712fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
71397f1f81fSBarry Smith     PetscInt    cnt = 0,jcnt;
71487828ca2SBarry Smith     PetscScalar value;
71568f1ed48SBarry Smith #if defined(PETSC_USE_COMPLEX)
71668f1ed48SBarry Smith     PetscBool   realonly = PETSC_TRUE;
71768f1ed48SBarry Smith 
71868f1ed48SBarry Smith     for (i=0; i<a->i[m]; i++) {
71968f1ed48SBarry Smith       if (PetscImaginaryPart(a->a[i]) != 0.0) {
72068f1ed48SBarry Smith         realonly = PETSC_FALSE;
72168f1ed48SBarry Smith         break;
72268f1ed48SBarry Smith       }
72368f1ed48SBarry Smith     }
72468f1ed48SBarry Smith #endif
72502594712SBarry Smith 
726d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
72702594712SBarry Smith     for (i=0; i<m; i++) {
72802594712SBarry Smith       jcnt = 0;
729d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
730e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
73102594712SBarry Smith           value = a->a[cnt++];
732e24b481bSBarry Smith           jcnt++;
73302594712SBarry Smith         } else {
73402594712SBarry Smith           value = 0.0;
73502594712SBarry Smith         }
736aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
73768f1ed48SBarry Smith         if (realonly) {
73860e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr);
73968f1ed48SBarry Smith         } else {
74060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr);
74168f1ed48SBarry Smith         }
74202594712SBarry Smith #else
74360e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr);
74402594712SBarry Smith #endif
74502594712SBarry Smith       }
746b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
74702594712SBarry Smith     }
748d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7493c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
750150b93efSMatthew G. Knepley     PetscInt fshift=1;
751d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
7523c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
75319303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");CHKERRQ(ierr);
7543c215bfdSMatthew Knepley #else
75519303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");CHKERRQ(ierr);
7563c215bfdSMatthew Knepley #endif
757d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
7583c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
75960e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
7603c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
761a9a0e077SKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
7623c215bfdSMatthew Knepley #else
763150b93efSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr);
7643c215bfdSMatthew Knepley #endif
7653c215bfdSMatthew Knepley       }
7663c215bfdSMatthew Knepley     }
767d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7683a40ed3dSBarry Smith   } else {
769d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
770d5f3da31SBarry Smith     if (A->factortype) {
77116cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
77216cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
77316cd7e1dSShri Abhyankar         /* L part */
77460e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
77516cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
77616cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
77760e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
77816cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
7796712e2f1SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
78016cd7e1dSShri Abhyankar           } else {
78160e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
78216cd7e1dSShri Abhyankar           }
78316cd7e1dSShri Abhyankar #else
78460e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
78516cd7e1dSShri Abhyankar #endif
78616cd7e1dSShri Abhyankar         }
78716cd7e1dSShri Abhyankar         /* diagonal */
78816cd7e1dSShri Abhyankar         j = a->diag[i];
78916cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
79016cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
79160e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr);
79216cd7e1dSShri Abhyankar         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
7936712e2f1SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)(-PetscImaginaryPart(1.0/a->a[j])));CHKERRQ(ierr);
79416cd7e1dSShri Abhyankar         } else {
79560e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
79616cd7e1dSShri Abhyankar         }
79716cd7e1dSShri Abhyankar #else
79860e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr);
79916cd7e1dSShri Abhyankar #endif
80016cd7e1dSShri Abhyankar 
80116cd7e1dSShri Abhyankar         /* U part */
80260e0710aSBarry Smith         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
80316cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
80416cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
80560e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
80616cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
80722ab088eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
80816cd7e1dSShri Abhyankar           } else {
80960e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
81016cd7e1dSShri Abhyankar           }
81116cd7e1dSShri Abhyankar #else
81260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
81316cd7e1dSShri Abhyankar #endif
81416cd7e1dSShri Abhyankar         }
81516cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
81616cd7e1dSShri Abhyankar       }
81716cd7e1dSShri Abhyankar     } else {
81817ab2063SBarry Smith       for (i=0; i<m; i++) {
81977431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
82060e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
821aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
82236db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
82360e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
82436db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
82560e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
8263a40ed3dSBarry Smith           } else {
82760e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
82817ab2063SBarry Smith           }
82917ab2063SBarry Smith #else
83060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
83117ab2063SBarry Smith #endif
83217ab2063SBarry Smith         }
833b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
83417ab2063SBarry Smith       }
83516cd7e1dSShri Abhyankar     }
836d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
83717ab2063SBarry Smith   }
838b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8393a40ed3dSBarry Smith   PetscFunctionReturn(0);
840416022c9SBarry Smith }
841416022c9SBarry Smith 
8429804daf3SBarry Smith #include <petscdraw.h>
8434a2ae208SSatish Balay #undef __FUNCT__
8444a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
845dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
846416022c9SBarry Smith {
847480ef9eaSBarry Smith   Mat               A  = (Mat) Aa;
848416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
849dfbe8321SBarry Smith   PetscErrorCode    ierr;
850383922c3SLisandro Dalcin   PetscInt          i,j,m = A->rmap->n;
851383922c3SLisandro Dalcin   int               color;
852b05fc000SLisandro Dalcin   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
853b0a32e0cSBarry Smith   PetscViewer       viewer;
854f3ef73ceSBarry Smith   PetscViewerFormat format;
855cddf8d76SBarry Smith 
8563a40ed3dSBarry Smith   PetscFunctionBegin;
857480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
858b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
859b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
860383922c3SLisandro Dalcin 
861416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
8620513a670SBarry Smith 
863fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
864383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
8650513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
866b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
867416022c9SBarry Smith     for (i=0; i<m; i++) {
868cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
869bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
870bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
87136db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
872b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
873cddf8d76SBarry Smith       }
874cddf8d76SBarry Smith     }
875b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
876cddf8d76SBarry Smith     for (i=0; i<m; i++) {
877cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
878bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
879bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
880cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
881b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
882cddf8d76SBarry Smith       }
883cddf8d76SBarry Smith     }
884b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
885cddf8d76SBarry Smith     for (i=0; i<m; i++) {
886cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
887bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
888bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
88936db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
890b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
891416022c9SBarry Smith       }
892416022c9SBarry Smith     }
893383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
8940513a670SBarry Smith   } else {
8950513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
8960513a670SBarry Smith     /* first determine max of all nonzero values */
897b05fc000SLisandro Dalcin     PetscReal minv = 0.0, maxv = 0.0;
898383922c3SLisandro Dalcin     PetscInt  nz = a->nz, count = 0;
899b0a32e0cSBarry Smith     PetscDraw popup;
9000513a670SBarry Smith 
9010513a670SBarry Smith     for (i=0; i<nz; i++) {
9020513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
9030513a670SBarry Smith     }
904383922c3SLisandro Dalcin     if (minv >= maxv) maxv = minv + PETSC_SMALL;
905b0a32e0cSBarry Smith     ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
90645f3bb6eSLisandro Dalcin     ierr = PetscDrawScalePopup(popup,minv,maxv);CHKERRQ(ierr);
907383922c3SLisandro Dalcin 
908383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
9090513a670SBarry Smith     for (i=0; i<m; i++) {
910383922c3SLisandro Dalcin       y_l = m - i - 1.0;
911383922c3SLisandro Dalcin       y_r = y_l + 1.0;
912bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
913383922c3SLisandro Dalcin         x_l = a->j[j];
914383922c3SLisandro Dalcin         x_r = x_l + 1.0;
915b05fc000SLisandro Dalcin         color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
916b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
9170513a670SBarry Smith         count++;
9180513a670SBarry Smith       }
9190513a670SBarry Smith     }
920383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
9210513a670SBarry Smith   }
922480ef9eaSBarry Smith   PetscFunctionReturn(0);
923480ef9eaSBarry Smith }
924cddf8d76SBarry Smith 
9259804daf3SBarry Smith #include <petscdraw.h>
9264a2ae208SSatish Balay #undef __FUNCT__
9274a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
928dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
929480ef9eaSBarry Smith {
930dfbe8321SBarry Smith   PetscErrorCode ierr;
931b0a32e0cSBarry Smith   PetscDraw      draw;
93236db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
933ace3abfcSBarry Smith   PetscBool      isnull;
934480ef9eaSBarry Smith 
935480ef9eaSBarry Smith   PetscFunctionBegin;
936b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
937b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
938480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
939480ef9eaSBarry Smith 
940d0f46423SBarry Smith   xr   = A->cmap->n; yr  = A->rmap->n; h = yr/10.0; w = xr/10.0;
941480ef9eaSBarry Smith   xr  += w;          yr += h;         xl = -w;     yl = -h;
942b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
943832b7cebSLisandro Dalcin   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
944b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
9450298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
946832b7cebSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
9473a40ed3dSBarry Smith   PetscFunctionReturn(0);
948416022c9SBarry Smith }
949416022c9SBarry Smith 
9504a2ae208SSatish Balay #undef __FUNCT__
9514a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
952dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
953416022c9SBarry Smith {
954dfbe8321SBarry Smith   PetscErrorCode ierr;
955ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
956416022c9SBarry Smith 
9573a40ed3dSBarry Smith   PetscFunctionBegin;
958251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
959251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
960251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
961c45a1595SBarry Smith   if (iascii) {
9623a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
9630f5bd95cSBarry Smith   } else if (isbinary) {
9643a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
9650f5bd95cSBarry Smith   } else if (isdraw) {
9663a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
96711aeaf0aSBarry Smith   }
9684108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
9693a40ed3dSBarry Smith   PetscFunctionReturn(0);
97017ab2063SBarry Smith }
97119bcc07fSBarry Smith 
9724a2ae208SSatish Balay #undef __FUNCT__
9734a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
974dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
97517ab2063SBarry Smith {
976416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9776849ba73SBarry Smith   PetscErrorCode ierr;
97897f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
979d0f46423SBarry Smith   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
98054f21887SBarry Smith   MatScalar      *aa    = a->a,*ap;
9813447b6efSHong Zhang   PetscReal      ratio  = 0.6;
98217ab2063SBarry Smith 
9833a40ed3dSBarry Smith   PetscFunctionBegin;
9843a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
98517ab2063SBarry Smith 
98643ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
98717ab2063SBarry Smith   for (i=1; i<m; i++) {
988416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
98917ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
99094a9d846SBarry Smith     rmax    = PetscMax(rmax,ailen[i]);
99117ab2063SBarry Smith     if (fshift) {
992bfeeae90SHong Zhang       ip = aj + ai[i];
993bfeeae90SHong Zhang       ap = aa + ai[i];
99417ab2063SBarry Smith       N  = ailen[i];
99517ab2063SBarry Smith       for (j=0; j<N; j++) {
99617ab2063SBarry Smith         ip[j-fshift] = ip[j];
99717ab2063SBarry Smith         ap[j-fshift] = ap[j];
99817ab2063SBarry Smith       }
99917ab2063SBarry Smith     }
100017ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
100117ab2063SBarry Smith   }
100217ab2063SBarry Smith   if (m) {
100317ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
100417ab2063SBarry Smith     ai[m]   = ai[m-1] + ailen[m-1];
100517ab2063SBarry Smith   }
10067b083b7cSBarry Smith 
100717ab2063SBarry Smith   /* reset ilen and imax for each row */
10087b083b7cSBarry Smith   a->nonzerorowcnt = 0;
100917ab2063SBarry Smith   for (i=0; i<m; i++) {
101017ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
10117b083b7cSBarry Smith     a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
101217ab2063SBarry Smith   }
1013bfeeae90SHong Zhang   a->nz = ai[m];
101465e19b50SBarry 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);
101517ab2063SBarry Smith 
101609f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1017d0f46423SBarry 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);
1018ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
1019ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
10202205254eSKarl Rupp 
10218e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
1022dd5f02e7SSatish Balay   a->reallocs         = 0;
10236712e2f1SBarry Smith   A->info.nz_unneeded = (PetscReal)fshift;
102436db0b34SBarry Smith   a->rmax             = rmax;
10254e220ebcSLois Curfman McInnes 
102611e456e1SBarry Smith   ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
10274108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
1028acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10293a40ed3dSBarry Smith   PetscFunctionReturn(0);
103017ab2063SBarry Smith }
103117ab2063SBarry Smith 
10324a2ae208SSatish Balay #undef __FUNCT__
103399cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
103499cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
103599cafbc1SBarry Smith {
103699cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
103799cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
103854f21887SBarry Smith   MatScalar      *aa = a->a;
1039acf2f550SJed Brown   PetscErrorCode ierr;
104099cafbc1SBarry Smith 
104199cafbc1SBarry Smith   PetscFunctionBegin;
104299cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1043acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
104499cafbc1SBarry Smith   PetscFunctionReturn(0);
104599cafbc1SBarry Smith }
104699cafbc1SBarry Smith 
104799cafbc1SBarry Smith #undef __FUNCT__
104899cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
104999cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
105099cafbc1SBarry Smith {
105199cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
105299cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
105354f21887SBarry Smith   MatScalar      *aa = a->a;
1054acf2f550SJed Brown   PetscErrorCode ierr;
105599cafbc1SBarry Smith 
105699cafbc1SBarry Smith   PetscFunctionBegin;
105799cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1058acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
105999cafbc1SBarry Smith   PetscFunctionReturn(0);
106099cafbc1SBarry Smith }
106199cafbc1SBarry Smith 
106299cafbc1SBarry Smith #undef __FUNCT__
10634a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
1064dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
106517ab2063SBarry Smith {
1066416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1067dfbe8321SBarry Smith   PetscErrorCode ierr;
10683a40ed3dSBarry Smith 
10693a40ed3dSBarry Smith   PetscFunctionBegin;
1070d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
1071acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10723a40ed3dSBarry Smith   PetscFunctionReturn(0);
107317ab2063SBarry Smith }
1074416022c9SBarry Smith 
10754a2ae208SSatish Balay #undef __FUNCT__
10764a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
1077dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
107817ab2063SBarry Smith {
1079416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1080dfbe8321SBarry Smith   PetscErrorCode ierr;
1081d5d45c9bSBarry Smith 
10823a40ed3dSBarry Smith   PetscFunctionBegin;
1083aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1084d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
108517ab2063SBarry Smith #endif
1086e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
10876bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
10886bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
108905b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
1090d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
109105b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
109271f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
109305b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
10946bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
109505b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
10966bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
1097cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
10980b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
1099a30b2313SHong Zhang 
11004108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
1101bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
1102901853e0SKris Buschelman 
1103dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
1104bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
1105bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1106bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1107bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr);
1108bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr);
1109bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr);
1110af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
1111af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);CHKERRQ(ierr);
1112af8000cdSHong Zhang #endif
111363c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE)
111463c07aadSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);CHKERRQ(ierr);
11153dad0653Sstefano_zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMatMatMult_transpose_seqaij_seqaij_C",NULL);CHKERRQ(ierr);
111663c07aadSStefano Zampini #endif
1117b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);CHKERRQ(ierr);
1118bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1119bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1120bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1121bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr);
11223a40ed3dSBarry Smith   PetscFunctionReturn(0);
112317ab2063SBarry Smith }
112417ab2063SBarry Smith 
11254a2ae208SSatish Balay #undef __FUNCT__
11264a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
1127ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
112817ab2063SBarry Smith {
1129416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11304846f1f5SKris Buschelman   PetscErrorCode ierr;
11313a40ed3dSBarry Smith 
11323a40ed3dSBarry Smith   PetscFunctionBegin;
1133a65d3064SKris Buschelman   switch (op) {
1134a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
11354e0d8c25SBarry Smith     a->roworiented = flg;
1136a65d3064SKris Buschelman     break;
1137a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1138a9817697SBarry Smith     a->keepnonzeropattern = flg;
1139a65d3064SKris Buschelman     break;
1140512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1141512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1142a65d3064SKris Buschelman     break;
1143a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
11444e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1145a65d3064SKris Buschelman     break;
1146a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
11474e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1148a65d3064SKris Buschelman     break;
114928b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
115028b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
115128b2fa4aSMatthew Knepley     break;
1152a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11534e0d8c25SBarry Smith     a->ignorezeroentries = flg;
11540df259c2SBarry Smith     break;
11553d472b54SHong Zhang   case MAT_SPD:
1156b1646e73SJed Brown   case MAT_SYMMETRIC:
1157b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1158b1646e73SJed Brown   case MAT_HERMITIAN:
1159b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
11605021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
11615021d80fSJed Brown     break;
11624e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1163a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1164a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1165290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1166a65d3064SKris Buschelman     break;
1167b87ac2d8SJed Brown   case MAT_USE_INODES:
1168b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1169b87ac2d8SJed Brown     break;
1170c10200c1SHong Zhang   case MAT_SUBMAT_SINGLEIS:
1171c10200c1SHong Zhang     A->submat_singleis = flg;
1172c10200c1SHong Zhang     break;
1173a65d3064SKris Buschelman   default:
1174e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1175a65d3064SKris Buschelman   }
11764108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
11773a40ed3dSBarry Smith   PetscFunctionReturn(0);
117817ab2063SBarry Smith }
117917ab2063SBarry Smith 
11804a2ae208SSatish Balay #undef __FUNCT__
11814a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1182dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
118317ab2063SBarry Smith {
1184416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11856849ba73SBarry Smith   PetscErrorCode ierr;
1186d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
118735e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
118817ab2063SBarry Smith 
11893a40ed3dSBarry Smith   PetscFunctionBegin;
1190d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1191e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
119235e7444dSHong Zhang 
1193d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1194d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
119535e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
11962c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
119735e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
119835e7444dSHong Zhang     PetscFunctionReturn(0);
119935e7444dSHong Zhang   }
120035e7444dSHong Zhang 
12012dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
12021ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
120335e7444dSHong Zhang   for (i=0; i<n; i++) {
120435e7444dSHong Zhang     nz = ai[i+1] - ai[i];
12052f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
120635e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++) {
120735e7444dSHong Zhang       if (aj[j] == i) {
120835e7444dSHong Zhang         x[i] = aa[j];
120917ab2063SBarry Smith         break;
121017ab2063SBarry Smith       }
121117ab2063SBarry Smith     }
121217ab2063SBarry Smith   }
12131ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
12143a40ed3dSBarry Smith   PetscFunctionReturn(0);
121517ab2063SBarry Smith }
121617ab2063SBarry Smith 
1217c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
12184a2ae208SSatish Balay #undef __FUNCT__
12194a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1220dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
122117ab2063SBarry Smith {
1222416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1223d9ca1df4SBarry Smith   PetscScalar       *y;
1224d9ca1df4SBarry Smith   const PetscScalar *x;
1225dfbe8321SBarry Smith   PetscErrorCode    ierr;
1226d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
12275c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1228d9ca1df4SBarry Smith   const MatScalar   *v;
1229a77337e4SBarry Smith   PetscScalar       alpha;
1230d9ca1df4SBarry Smith   PetscInt          n,i,j;
1231d9ca1df4SBarry Smith   const PetscInt    *idx,*ii,*ridx=NULL;
12323447b6efSHong Zhang   Mat_CompressedRow cprow    = a->compressedrow;
1233ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
12345c897100SBarry Smith #endif
123517ab2063SBarry Smith 
12363a40ed3dSBarry Smith   PetscFunctionBegin;
12372e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
1238d9ca1df4SBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12391ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
12405c897100SBarry Smith 
12415c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1242bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
12435c897100SBarry Smith #else
12443447b6efSHong Zhang   if (usecprow) {
12453447b6efSHong Zhang     m    = cprow.nrows;
12463447b6efSHong Zhang     ii   = cprow.i;
12477b2bb3b9SHong Zhang     ridx = cprow.rindex;
12483447b6efSHong Zhang   } else {
12493447b6efSHong Zhang     ii = a->i;
12503447b6efSHong Zhang   }
125117ab2063SBarry Smith   for (i=0; i<m; i++) {
12523447b6efSHong Zhang     idx = a->j + ii[i];
12533447b6efSHong Zhang     v   = a->a + ii[i];
12543447b6efSHong Zhang     n   = ii[i+1] - ii[i];
12553447b6efSHong Zhang     if (usecprow) {
12567b2bb3b9SHong Zhang       alpha = x[ridx[i]];
12573447b6efSHong Zhang     } else {
125817ab2063SBarry Smith       alpha = x[i];
12593447b6efSHong Zhang     }
126004fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
126117ab2063SBarry Smith   }
12625c897100SBarry Smith #endif
1263dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1264d9ca1df4SBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12651ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12663a40ed3dSBarry Smith   PetscFunctionReturn(0);
126717ab2063SBarry Smith }
126817ab2063SBarry Smith 
12694a2ae208SSatish Balay #undef __FUNCT__
12705c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1271dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
12725c897100SBarry Smith {
1273dfbe8321SBarry Smith   PetscErrorCode ierr;
12745c897100SBarry Smith 
12755c897100SBarry Smith   PetscFunctionBegin;
1276170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
12775c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
12785c897100SBarry Smith   PetscFunctionReturn(0);
12795c897100SBarry Smith }
12805c897100SBarry Smith 
1281c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
128278b84d54SShri Abhyankar 
12835c897100SBarry Smith #undef __FUNCT__
12844a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1285dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
128617ab2063SBarry Smith {
1287416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1288d9fead3dSBarry Smith   PetscScalar       *y;
128954f21887SBarry Smith   const PetscScalar *x;
129054f21887SBarry Smith   const MatScalar   *aa;
1291dfbe8321SBarry Smith   PetscErrorCode    ierr;
1292003131ecSBarry Smith   PetscInt          m=A->rmap->n;
12930298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
12947b083b7cSBarry Smith   PetscInt          n,i;
1295362ced78SSatish Balay   PetscScalar       sum;
1296ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
129717ab2063SBarry Smith 
1298b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
129997952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1300fee21e36SBarry Smith #endif
1301fee21e36SBarry Smith 
13023a40ed3dSBarry Smith   PetscFunctionBegin;
13033649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
13041ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1305416022c9SBarry Smith   ii   = a->i;
13064eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
13074f390cb1SBarry Smith     ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr);
130897952fefSHong Zhang     m    = a->compressedrow.nrows;
130997952fefSHong Zhang     ii   = a->compressedrow.i;
131097952fefSHong Zhang     ridx = a->compressedrow.rindex;
131197952fefSHong Zhang     for (i=0; i<m; i++) {
131297952fefSHong Zhang       n           = ii[i+1] - ii[i];
131397952fefSHong Zhang       aj          = a->j + ii[i];
131497952fefSHong Zhang       aa          = a->a + ii[i];
131597952fefSHong Zhang       sum         = 0.0;
1316003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1317003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
131897952fefSHong Zhang       y[*ridx++] = sum;
131997952fefSHong Zhang     }
132097952fefSHong Zhang   } else { /* do not use compressed row format */
1321b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
13223d3eaba7SBarry Smith     aj   = a->j;
13233d3eaba7SBarry Smith     aa   = a->a;
1324b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1325b05257ddSBarry Smith #else
132617ab2063SBarry Smith     for (i=0; i<m; i++) {
1327003131ecSBarry Smith       n           = ii[i+1] - ii[i];
1328003131ecSBarry Smith       aj          = a->j + ii[i];
1329003131ecSBarry Smith       aa          = a->a + ii[i];
133017ab2063SBarry Smith       sum         = 0.0;
1331003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
133217ab2063SBarry Smith       y[i] = sum;
133317ab2063SBarry Smith     }
13348d195f9aSBarry Smith #endif
1335b05257ddSBarry Smith   }
13367b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
13373649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13381ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13393a40ed3dSBarry Smith   PetscFunctionReturn(0);
134017ab2063SBarry Smith }
134117ab2063SBarry Smith 
1342b434eb95SMatthew G. Knepley #undef __FUNCT__
1343b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultMax_SeqAIJ"
1344b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1345b434eb95SMatthew G. Knepley {
1346b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1347b434eb95SMatthew G. Knepley   PetscScalar       *y;
1348b434eb95SMatthew G. Knepley   const PetscScalar *x;
1349b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1350b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1351b434eb95SMatthew G. Knepley   PetscInt          m=A->rmap->n;
1352b434eb95SMatthew G. Knepley   const PetscInt    *aj,*ii,*ridx=NULL;
1353b434eb95SMatthew G. Knepley   PetscInt          n,i,nonzerorow=0;
1354b434eb95SMatthew G. Knepley   PetscScalar       sum;
1355b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1356b434eb95SMatthew G. Knepley 
1357b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1358b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa)
1359b434eb95SMatthew G. Knepley #endif
1360b434eb95SMatthew G. Knepley 
1361b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1362b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1363b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1364b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1365b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1366b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1367b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1368b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1369b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1370b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1371b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1372b434eb95SMatthew G. Knepley       sum         = 0.0;
1373b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1374b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1375b434eb95SMatthew G. Knepley       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1376b434eb95SMatthew G. Knepley       y[*ridx++] = sum;
1377b434eb95SMatthew G. Knepley     }
1378b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
13793d3eaba7SBarry Smith     ii = a->i;
1380b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1381b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1382b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1383b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1384b434eb95SMatthew G. Knepley       sum         = 0.0;
1385b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1386b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1387b434eb95SMatthew G. Knepley       y[i] = sum;
1388b434eb95SMatthew G. Knepley     }
1389b434eb95SMatthew G. Knepley   }
1390b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1391b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1392b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1393b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1394b434eb95SMatthew G. Knepley }
1395b434eb95SMatthew G. Knepley 
1396b434eb95SMatthew G. Knepley #undef __FUNCT__
1397b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultAddMax_SeqAIJ"
1398b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1399b434eb95SMatthew G. Knepley {
1400b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1401b434eb95SMatthew G. Knepley   PetscScalar       *y,*z;
1402b434eb95SMatthew G. Knepley   const PetscScalar *x;
1403b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1404b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1405b434eb95SMatthew G. Knepley   PetscInt          m = A->rmap->n,*aj,*ii;
1406b434eb95SMatthew G. Knepley   PetscInt          n,i,*ridx=NULL;
1407b434eb95SMatthew G. Knepley   PetscScalar       sum;
1408b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1409b434eb95SMatthew G. Knepley 
1410b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1411b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1412d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1413b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1414b434eb95SMatthew G. Knepley     if (zz != yy) {
1415b434eb95SMatthew G. Knepley       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1416b434eb95SMatthew G. Knepley     }
1417b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1418b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1419b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1420b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1421b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1422b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1423b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1424b434eb95SMatthew G. Knepley       sum = y[*ridx];
1425b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1426b434eb95SMatthew G. Knepley       z[*ridx++] = sum;
1427b434eb95SMatthew G. Knepley     }
1428b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
14293d3eaba7SBarry Smith     ii = a->i;
1430b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1431b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1432b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1433b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1434b434eb95SMatthew G. Knepley       sum = y[i];
1435b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1436b434eb95SMatthew G. Knepley       z[i] = sum;
1437b434eb95SMatthew G. Knepley     }
1438b434eb95SMatthew G. Knepley   }
1439b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1440b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1441d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1442b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1443b434eb95SMatthew G. Knepley }
1444b434eb95SMatthew G. Knepley 
1445c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
14464a2ae208SSatish Balay #undef __FUNCT__
14474a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1448dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
144917ab2063SBarry Smith {
1450416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1451f15663dcSBarry Smith   PetscScalar       *y,*z;
1452f15663dcSBarry Smith   const PetscScalar *x;
145354f21887SBarry Smith   const MatScalar   *aa;
1454dfbe8321SBarry Smith   PetscErrorCode    ierr;
1455d9ca1df4SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
1456d9ca1df4SBarry Smith   PetscInt          m = A->rmap->n,n,i;
1457362ced78SSatish Balay   PetscScalar       sum;
1458ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
14599ea0dfa2SSatish Balay 
14603a40ed3dSBarry Smith   PetscFunctionBegin;
1461f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1462d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
14634eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
14644eb6d288SHong Zhang     if (zz != yy) {
14654eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
14664eb6d288SHong Zhang     }
146797952fefSHong Zhang     m    = a->compressedrow.nrows;
146897952fefSHong Zhang     ii   = a->compressedrow.i;
146997952fefSHong Zhang     ridx = a->compressedrow.rindex;
147097952fefSHong Zhang     for (i=0; i<m; i++) {
147197952fefSHong Zhang       n   = ii[i+1] - ii[i];
147297952fefSHong Zhang       aj  = a->j + ii[i];
147397952fefSHong Zhang       aa  = a->a + ii[i];
147497952fefSHong Zhang       sum = y[*ridx];
1475f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
147697952fefSHong Zhang       z[*ridx++] = sum;
147797952fefSHong Zhang     }
147897952fefSHong Zhang   } else { /* do not use compressed row format */
14793d3eaba7SBarry Smith     ii = a->i;
1480f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
14813d3eaba7SBarry Smith     aj = a->j;
14823d3eaba7SBarry Smith     aa = a->a;
1483f15663dcSBarry Smith     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1484f15663dcSBarry Smith #else
148517ab2063SBarry Smith     for (i=0; i<m; i++) {
1486f15663dcSBarry Smith       n   = ii[i+1] - ii[i];
1487f15663dcSBarry Smith       aj  = a->j + ii[i];
1488f15663dcSBarry Smith       aa  = a->a + ii[i];
148917ab2063SBarry Smith       sum = y[i];
1490f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
149117ab2063SBarry Smith       z[i] = sum;
149217ab2063SBarry Smith     }
149302ab625aSSatish Balay #endif
1494f15663dcSBarry Smith   }
1495dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1496f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1497d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
14988154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
14996b375ea7SVictor Minden   /*
1500918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1501918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1502918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
15036b375ea7SVictor Minden   */
1504918e98c3SVictor Minden #endif
15053a40ed3dSBarry Smith   PetscFunctionReturn(0);
150617ab2063SBarry Smith }
150717ab2063SBarry Smith 
150817ab2063SBarry Smith /*
150917ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
151017ab2063SBarry Smith */
15114a2ae208SSatish Balay #undef __FUNCT__
15124a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1513dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
151417ab2063SBarry Smith {
1515416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15166849ba73SBarry Smith   PetscErrorCode ierr;
1517d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
151817ab2063SBarry Smith 
15193a40ed3dSBarry Smith   PetscFunctionBegin;
152009f38230SBarry Smith   if (!a->diag) {
1521785e854fSJed Brown     ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr);
15223bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr);
152309f38230SBarry Smith   }
1524d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
152509f38230SBarry Smith     a->diag[i] = a->i[i+1];
1526bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1527bfeeae90SHong Zhang       if (a->j[j] == i) {
152809f38230SBarry Smith         a->diag[i] = j;
152917ab2063SBarry Smith         break;
153017ab2063SBarry Smith       }
153117ab2063SBarry Smith     }
153217ab2063SBarry Smith   }
15333a40ed3dSBarry Smith   PetscFunctionReturn(0);
153417ab2063SBarry Smith }
153517ab2063SBarry Smith 
1536be5855fcSBarry Smith /*
1537be5855fcSBarry Smith      Checks for missing diagonals
1538be5855fcSBarry Smith */
15394a2ae208SSatish Balay #undef __FUNCT__
15404a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1541ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1542be5855fcSBarry Smith {
1543be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
15447734d3b5SMatthew G. Knepley   PetscInt   *diag,*ii = a->i,i;
1545be5855fcSBarry Smith 
1546be5855fcSBarry Smith   PetscFunctionBegin;
154709f38230SBarry Smith   *missing = PETSC_FALSE;
15487734d3b5SMatthew G. Knepley   if (A->rmap->n > 0 && !ii) {
154909f38230SBarry Smith     *missing = PETSC_TRUE;
155009f38230SBarry Smith     if (d) *d = 0;
1551955c1f14SBarry Smith     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
155209f38230SBarry Smith   } else {
1553f1e2ffcdSBarry Smith     diag = a->diag;
1554d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
15557734d3b5SMatthew G. Knepley       if (diag[i] >= ii[i+1]) {
155609f38230SBarry Smith         *missing = PETSC_TRUE;
155709f38230SBarry Smith         if (d) *d = i;
1558955c1f14SBarry Smith         PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
1559358d2f5dSShri Abhyankar         break;
156009f38230SBarry Smith       }
1561be5855fcSBarry Smith     }
1562be5855fcSBarry Smith   }
1563be5855fcSBarry Smith   PetscFunctionReturn(0);
1564be5855fcSBarry Smith }
1565be5855fcSBarry Smith 
156671f1c65dSBarry Smith #undef __FUNCT__
156771f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
1568422a814eSBarry Smith /*
1569422a814eSBarry Smith    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1570422a814eSBarry Smith */
15717087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
157271f1c65dSBarry Smith {
157371f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
157471f1c65dSBarry Smith   PetscErrorCode ierr;
1575d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
157654f21887SBarry Smith   MatScalar      *v = a->a;
157754f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
157871f1c65dSBarry Smith 
157971f1c65dSBarry Smith   PetscFunctionBegin;
158071f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
158171f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
158271f1c65dSBarry Smith   diag = a->diag;
158371f1c65dSBarry Smith   if (!a->idiag) {
1584dcca6d9dSJed Brown     ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr);
15853bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
158671f1c65dSBarry Smith     v    = a->a;
158771f1c65dSBarry Smith   }
158871f1c65dSBarry Smith   mdiag = a->mdiag;
158971f1c65dSBarry Smith   idiag = a->idiag;
159071f1c65dSBarry Smith 
1591422a814eSBarry Smith   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
159271f1c65dSBarry Smith     for (i=0; i<m; i++) {
159371f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1594899639b0SHong Zhang       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1595899639b0SHong Zhang         if (PetscRealPart(fshift)) {
1596899639b0SHong Zhang           ierr = PetscInfo1(A,"Zero diagonal on row %D\n",i);CHKERRQ(ierr);
15977b6c816cSBarry Smith           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
15987b6c816cSBarry Smith           A->factorerror_zeropivot_value = 0.0;
15997b6c816cSBarry Smith           A->factorerror_zeropivot_row   = i;
16007b6c816cSBarry Smith         } SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1601899639b0SHong Zhang       }
160271f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
160371f1c65dSBarry Smith     }
160471f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
160571f1c65dSBarry Smith   } else {
160671f1c65dSBarry Smith     for (i=0; i<m; i++) {
160771f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
160871f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
160971f1c65dSBarry Smith     }
1610dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
161171f1c65dSBarry Smith   }
161271f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
161371f1c65dSBarry Smith   PetscFunctionReturn(0);
161471f1c65dSBarry Smith }
161571f1c65dSBarry Smith 
1616c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
16174a2ae208SSatish Balay #undef __FUNCT__
161841f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
161941f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
162017ab2063SBarry Smith {
1621416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1622e6d1f457SBarry Smith   PetscScalar       *x,d,sum,*t,scale;
16233d3eaba7SBarry Smith   const MatScalar   *v,*idiag=0,*mdiag;
162454f21887SBarry Smith   const PetscScalar *b, *bs,*xb, *ts;
1625dfbe8321SBarry Smith   PetscErrorCode    ierr;
16263d3eaba7SBarry Smith   PetscInt          n,m = A->rmap->n,i;
162797f1f81fSBarry Smith   const PetscInt    *idx,*diag;
162817ab2063SBarry Smith 
16293a40ed3dSBarry Smith   PetscFunctionBegin;
1630b965ef7fSBarry Smith   its = its*lits;
163191723122SBarry Smith 
163271f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
163371f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
163471f1c65dSBarry Smith   a->fshift = fshift;
163571f1c65dSBarry Smith   a->omega  = omega;
1636ed480e8bSBarry Smith 
163771f1c65dSBarry Smith   diag  = a->diag;
163871f1c65dSBarry Smith   t     = a->ssor_work;
1639ed480e8bSBarry Smith   idiag = a->idiag;
164071f1c65dSBarry Smith   mdiag = a->mdiag;
1641ed480e8bSBarry Smith 
16421ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
16433649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1644ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
164517ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
164617ab2063SBarry Smith     /* apply (U + D/omega) to the vector */
1647ed480e8bSBarry Smith     bs = b;
164817ab2063SBarry Smith     for (i=0; i<m; i++) {
164971f1c65dSBarry Smith       d   = fshift + mdiag[i];
1650416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1651ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1652ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
165317ab2063SBarry Smith       sum = b[i]*d/omega;
1654003131ecSBarry Smith       PetscSparseDensePlusDot(sum,bs,v,idx,n);
165517ab2063SBarry Smith       x[i] = sum;
165617ab2063SBarry Smith     }
16571ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16583649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1659efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
16603a40ed3dSBarry Smith     PetscFunctionReturn(0);
166117ab2063SBarry Smith   }
1662c783ea89SBarry Smith 
16632205254eSKarl Rupp   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
16642205254eSKarl Rupp   else if (flag & SOR_EISENSTAT) {
166517ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1666887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
166717ab2063SBarry Smith 
166817ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
166917ab2063SBarry Smith 
1670887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
167117ab2063SBarry Smith     */
167217ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
167317ab2063SBarry Smith 
167417ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
167517ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1676416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1677ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1678ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
167917ab2063SBarry Smith       sum = b[i];
1680e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1681ed480e8bSBarry Smith       x[i] = sum*idiag[i];
168217ab2063SBarry Smith     }
168317ab2063SBarry Smith 
168417ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1685416022c9SBarry Smith     v = a->a;
16862205254eSKarl Rupp     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
168717ab2063SBarry Smith 
168817ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1689ed480e8bSBarry Smith     ts   = t;
1690416022c9SBarry Smith     diag = a->diag;
169117ab2063SBarry Smith     for (i=0; i<m; i++) {
1692416022c9SBarry Smith       n   = diag[i] - a->i[i];
1693ed480e8bSBarry Smith       idx = a->j + a->i[i];
1694ed480e8bSBarry Smith       v   = a->a + a->i[i];
169517ab2063SBarry Smith       sum = t[i];
1696003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1697ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1698733d66baSBarry Smith       /*  x = x + t */
1699733d66baSBarry Smith       x[i] += t[i];
170017ab2063SBarry Smith     }
170117ab2063SBarry Smith 
1702dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
17031ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
17043649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
17053a40ed3dSBarry Smith     PetscFunctionReturn(0);
170617ab2063SBarry Smith   }
170717ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
170817ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
170917ab2063SBarry Smith       for (i=0; i<m; i++) {
1710416022c9SBarry Smith         n   = diag[i] - a->i[i];
1711ed480e8bSBarry Smith         idx = a->j + a->i[i];
1712ed480e8bSBarry Smith         v   = a->a + a->i[i];
171317ab2063SBarry Smith         sum = b[i];
1714e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
17155c99c7daSBarry Smith         t[i] = sum;
1716ed480e8bSBarry Smith         x[i] = sum*idiag[i];
171717ab2063SBarry Smith       }
17185c99c7daSBarry Smith       xb   = t;
1719efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17203a40ed3dSBarry Smith     } else xb = b;
172117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
172217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1723416022c9SBarry Smith         n   = a->i[i+1] - diag[i] - 1;
1724ed480e8bSBarry Smith         idx = a->j + diag[i] + 1;
1725ed480e8bSBarry Smith         v   = a->a + diag[i] + 1;
172617ab2063SBarry Smith         sum = xb[i];
1727e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
17285c99c7daSBarry Smith         if (xb == b) {
1729ed480e8bSBarry Smith           x[i] = sum*idiag[i];
17305c99c7daSBarry Smith         } else {
1731b19a5dc2SMark Adams           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
173217ab2063SBarry Smith         }
17335c99c7daSBarry Smith       }
1734b19a5dc2SMark Adams       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
173517ab2063SBarry Smith     }
173617ab2063SBarry Smith     its--;
173717ab2063SBarry Smith   }
173817ab2063SBarry Smith   while (its--) {
173917ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
174017ab2063SBarry Smith       for (i=0; i<m; i++) {
1741b19a5dc2SMark Adams         /* lower */
1742b19a5dc2SMark Adams         n   = diag[i] - a->i[i];
1743ed480e8bSBarry Smith         idx = a->j + a->i[i];
1744ed480e8bSBarry Smith         v   = a->a + a->i[i];
174517ab2063SBarry Smith         sum = b[i];
1746e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1747b19a5dc2SMark Adams         t[i] = sum;             /* save application of the lower-triangular part */
1748b19a5dc2SMark Adams         /* upper */
1749b19a5dc2SMark Adams         n   = a->i[i+1] - diag[i] - 1;
1750b19a5dc2SMark Adams         idx = a->j + diag[i] + 1;
1751b19a5dc2SMark Adams         v   = a->a + diag[i] + 1;
1752b19a5dc2SMark Adams         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1753b19a5dc2SMark Adams         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
175417ab2063SBarry Smith       }
1755b19a5dc2SMark Adams       xb   = t;
17569f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1757b19a5dc2SMark Adams     } else xb = b;
175817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
175917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1760b19a5dc2SMark Adams         sum = xb[i];
1761b19a5dc2SMark Adams         if (xb == b) {
1762b19a5dc2SMark Adams           /* whole matrix (no checkpointing available) */
1763416022c9SBarry Smith           n   = a->i[i+1] - a->i[i];
1764ed480e8bSBarry Smith           idx = a->j + a->i[i];
1765ed480e8bSBarry Smith           v   = a->a + a->i[i];
1766e6d1f457SBarry Smith           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1767ed480e8bSBarry Smith           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1768b19a5dc2SMark Adams         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1769b19a5dc2SMark Adams           n   = a->i[i+1] - diag[i] - 1;
1770b19a5dc2SMark Adams           idx = a->j + diag[i] + 1;
1771b19a5dc2SMark Adams           v   = a->a + diag[i] + 1;
1772b19a5dc2SMark Adams           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1773b19a5dc2SMark Adams           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
177417ab2063SBarry Smith         }
1775b19a5dc2SMark Adams       }
1776b19a5dc2SMark Adams       if (xb == b) {
17779f863219SBarry Smith         ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1778b19a5dc2SMark Adams       } else {
1779b19a5dc2SMark Adams         ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1780b19a5dc2SMark Adams       }
178117ab2063SBarry Smith     }
178217ab2063SBarry Smith   }
17831ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
17843649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1785365a8a9eSBarry Smith   PetscFunctionReturn(0);
178617ab2063SBarry Smith }
178717ab2063SBarry Smith 
17882af78befSBarry Smith 
17894a2ae208SSatish Balay #undef __FUNCT__
17904a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1791dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
179217ab2063SBarry Smith {
1793416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
17944e220ebcSLois Curfman McInnes 
17953a40ed3dSBarry Smith   PetscFunctionBegin;
17964e220ebcSLois Curfman McInnes   info->block_size   = 1.0;
17974e220ebcSLois Curfman McInnes   info->nz_allocated = (double)a->maxnz;
17984e220ebcSLois Curfman McInnes   info->nz_used      = (double)a->nz;
17994e220ebcSLois Curfman McInnes   info->nz_unneeded  = (double)(a->maxnz - a->nz);
18004e220ebcSLois Curfman McInnes   info->assemblies   = (double)A->num_ass;
18018e58a170SBarry Smith   info->mallocs      = (double)A->info.mallocs;
18027adad957SLisandro Dalcin   info->memory       = ((PetscObject)A)->mem;
1803d5f3da31SBarry Smith   if (A->factortype) {
18044e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
18054e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
18064e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
18074e220ebcSLois Curfman McInnes   } else {
18084e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
18094e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
18104e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
18114e220ebcSLois Curfman McInnes   }
18123a40ed3dSBarry Smith   PetscFunctionReturn(0);
181317ab2063SBarry Smith }
181417ab2063SBarry Smith 
18154a2ae208SSatish Balay #undef __FUNCT__
18164a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
18172b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
181817ab2063SBarry Smith {
1819416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
18203b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
18216849ba73SBarry Smith   PetscErrorCode    ierr;
182297b48c8fSBarry Smith   const PetscScalar *xx;
182397b48c8fSBarry Smith   PetscScalar       *bb;
1824ace3abfcSBarry Smith   PetscBool         missing;
182517ab2063SBarry Smith 
18263a40ed3dSBarry Smith   PetscFunctionBegin;
182797b48c8fSBarry Smith   if (x && b) {
182897b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
182997b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
183097b48c8fSBarry Smith     for (i=0; i<N; i++) {
183197b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
183297b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
183397b48c8fSBarry Smith     }
183497b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
183597b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
183697b48c8fSBarry Smith   }
183797b48c8fSBarry Smith 
18381d5a398dSstefano_zampini   ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1839a9817697SBarry Smith   if (a->keepnonzeropattern) {
1840f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1841e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1842bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1843f1e2ffcdSBarry Smith     }
1844f4df32b1SMatthew Knepley     if (diag != 0.0) {
1845e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1846f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1847f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1848f1e2ffcdSBarry Smith       }
1849f1e2ffcdSBarry Smith     }
1850f1e2ffcdSBarry Smith   } else {
1851f4df32b1SMatthew Knepley     if (diag != 0.0) {
185217ab2063SBarry Smith       for (i=0; i<N; i++) {
1853e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
18547ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1855416022c9SBarry Smith           a->ilen[rows[i]]    = 1;
1856f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1857bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
18587ae801bdSBarry Smith         } else { /* in case row was completely empty */
1859f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
186017ab2063SBarry Smith         }
186117ab2063SBarry Smith       }
18623a40ed3dSBarry Smith     } else {
186317ab2063SBarry Smith       for (i=0; i<N; i++) {
1864e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1865416022c9SBarry Smith         a->ilen[rows[i]] = 0;
186617ab2063SBarry Smith       }
186717ab2063SBarry Smith     }
1868e56f5c9eSBarry Smith     A->nonzerostate++;
1869f1e2ffcdSBarry Smith   }
187043a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18713a40ed3dSBarry Smith   PetscFunctionReturn(0);
187217ab2063SBarry Smith }
187317ab2063SBarry Smith 
18744a2ae208SSatish Balay #undef __FUNCT__
18756e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
18766e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
18776e169961SBarry Smith {
18786e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
18796e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
18806e169961SBarry Smith   PetscErrorCode    ierr;
18812b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
18826e169961SBarry Smith   const PetscScalar *xx;
18836e169961SBarry Smith   PetscScalar       *bb;
18846e169961SBarry Smith 
18856e169961SBarry Smith   PetscFunctionBegin;
18866e169961SBarry Smith   if (x && b) {
18876e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
18886e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
18892b40b63fSBarry Smith     vecs = PETSC_TRUE;
18906e169961SBarry Smith   }
18911795a4d1SJed Brown   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
18926e169961SBarry Smith   for (i=0; i<N; i++) {
18936e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
18946e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
18952205254eSKarl Rupp 
18966e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
18976e169961SBarry Smith   }
18986e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
18996e169961SBarry Smith     if (!zeroed[i]) {
19006e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
19016e169961SBarry Smith         if (zeroed[a->j[j]]) {
19022b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
19036e169961SBarry Smith           a->a[j] = 0.0;
19046e169961SBarry Smith         }
19056e169961SBarry Smith       }
19062b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
19076e169961SBarry Smith   }
19086e169961SBarry Smith   if (x && b) {
19096e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
19106e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
19116e169961SBarry Smith   }
19126e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
19136e169961SBarry Smith   if (diag != 0.0) {
19146e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
19151d5a398dSstefano_zampini     if (missing) {
19161d5a398dSstefano_zampini       if (a->nonew) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
19171d5a398dSstefano_zampini       else {
19181d5a398dSstefano_zampini         for (i=0; i<N; i++) {
19191d5a398dSstefano_zampini           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
19201d5a398dSstefano_zampini         }
19211d5a398dSstefano_zampini       }
19221d5a398dSstefano_zampini     } else {
19236e169961SBarry Smith       for (i=0; i<N; i++) {
19246e169961SBarry Smith         a->a[a->diag[rows[i]]] = diag;
19256e169961SBarry Smith       }
19266e169961SBarry Smith     }
19271d5a398dSstefano_zampini   }
19286e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19296e169961SBarry Smith   PetscFunctionReturn(0);
19306e169961SBarry Smith }
19316e169961SBarry Smith 
19326e169961SBarry Smith #undef __FUNCT__
19334a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1934a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
193517ab2063SBarry Smith {
1936416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
193797f1f81fSBarry Smith   PetscInt   *itmp;
193817ab2063SBarry Smith 
19393a40ed3dSBarry Smith   PetscFunctionBegin;
1940e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
194117ab2063SBarry Smith 
1942416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1943bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
194417ab2063SBarry Smith   if (idx) {
1945bfeeae90SHong Zhang     itmp = a->j + a->i[row];
194626fbe8dcSKarl Rupp     if (*nz) *idx = itmp;
194717ab2063SBarry Smith     else *idx = 0;
194817ab2063SBarry Smith   }
19493a40ed3dSBarry Smith   PetscFunctionReturn(0);
195017ab2063SBarry Smith }
195117ab2063SBarry Smith 
1952bfeeae90SHong Zhang /* remove this function? */
19534a2ae208SSatish Balay #undef __FUNCT__
19544a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1955a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
195617ab2063SBarry Smith {
19573a40ed3dSBarry Smith   PetscFunctionBegin;
19583a40ed3dSBarry Smith   PetscFunctionReturn(0);
195917ab2063SBarry Smith }
196017ab2063SBarry Smith 
19614a2ae208SSatish Balay #undef __FUNCT__
19624a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1963dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
196417ab2063SBarry Smith {
1965416022c9SBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
196654f21887SBarry Smith   MatScalar      *v  = a->a;
196736db0b34SBarry Smith   PetscReal      sum = 0.0;
19686849ba73SBarry Smith   PetscErrorCode ierr;
196997f1f81fSBarry Smith   PetscInt       i,j;
197017ab2063SBarry Smith 
19713a40ed3dSBarry Smith   PetscFunctionBegin;
197217ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1973570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FP16)
1974570b7f6dSBarry Smith     PetscBLASInt one = 1,nz = a->nz;
1975570b7f6dSBarry Smith     *nrm = BLASnrm2_(&nz,v,&one);
1976570b7f6dSBarry Smith #else
1977416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
197836db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
197917ab2063SBarry Smith     }
19808f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
1981570b7f6dSBarry Smith #endif
198251f70360SJed Brown     ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
19833a40ed3dSBarry Smith   } else if (type == NORM_1) {
198436db0b34SBarry Smith     PetscReal *tmp;
198597f1f81fSBarry Smith     PetscInt  *jj = a->j;
19861795a4d1SJed Brown     ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr);
1987064f8208SBarry Smith     *nrm = 0.0;
1988416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1989bfeeae90SHong Zhang       tmp[*jj++] += PetscAbsScalar(*v);  v++;
199017ab2063SBarry Smith     }
1991d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1992064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
199317ab2063SBarry Smith     }
1994606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
199551f70360SJed Brown     ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr);
19963a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1997064f8208SBarry Smith     *nrm = 0.0;
1998d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1999bfeeae90SHong Zhang       v   = a->a + a->i[j];
200017ab2063SBarry Smith       sum = 0.0;
2001416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2002cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
200317ab2063SBarry Smith       }
2004064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
200517ab2063SBarry Smith     }
200651f70360SJed Brown     ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr);
2007f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
20083a40ed3dSBarry Smith   PetscFunctionReturn(0);
200917ab2063SBarry Smith }
201017ab2063SBarry Smith 
20114e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
20124e938277SHong Zhang #undef __FUNCT__
20134e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
20144e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
20154e938277SHong Zhang {
20164e938277SHong Zhang   PetscErrorCode ierr;
20174e938277SHong Zhang   PetscInt       i,j,anzj;
20184e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
20194e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
20204e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
20214e938277SHong Zhang 
20224e938277SHong Zhang   PetscFunctionBegin;
20234e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
2024854ce69bSBarry Smith   ierr = PetscCalloc1(an+1,&ati);CHKERRQ(ierr);
2025785e854fSJed Brown   ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr);
2026785e854fSJed Brown   ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr);
20274e938277SHong Zhang 
20284e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
20294e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
203026fbe8dcSKarl Rupp   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
20314e938277SHong Zhang   /* Form ati for csr format of A^T. */
203226fbe8dcSKarl Rupp   for (i=0;i<an;i++) ati[i+1] += ati[i];
20334e938277SHong Zhang 
20344e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
20354e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
20364e938277SHong Zhang 
20374e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
20384e938277SHong Zhang   for (i=0;i<am;i++) {
20394e938277SHong Zhang     anzj = ai[i+1] - ai[i];
20404e938277SHong Zhang     for (j=0;j<anzj;j++) {
20414e938277SHong Zhang       atj[atfill[*aj]] = i;
20424e938277SHong Zhang       atfill[*aj++]   += 1;
20434e938277SHong Zhang     }
20444e938277SHong Zhang   }
20454e938277SHong Zhang 
20464e938277SHong Zhang   /* Clean up temporary space and complete requests. */
20474e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
2048ce94432eSBarry Smith   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
204933d57670SJed Brown   ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
2050a2f3521dSMark F. Adams 
20514e938277SHong Zhang   b          = (Mat_SeqAIJ*)((*B)->data);
20524e938277SHong Zhang   b->free_a  = PETSC_FALSE;
20534e938277SHong Zhang   b->free_ij = PETSC_TRUE;
20544e938277SHong Zhang   b->nonew   = 0;
20554e938277SHong Zhang   PetscFunctionReturn(0);
20564e938277SHong Zhang }
20574e938277SHong Zhang 
20584a2ae208SSatish Balay #undef __FUNCT__
20594a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
2060fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
206117ab2063SBarry Smith {
2062416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2063416022c9SBarry Smith   Mat            C;
20646849ba73SBarry Smith   PetscErrorCode ierr;
2065d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
206654f21887SBarry Smith   MatScalar      *array = a->a;
206717ab2063SBarry Smith 
20683a40ed3dSBarry Smith   PetscFunctionBegin;
2069*cf37664fSBarry Smith   if (reuse == MAT_INPLACE_MATRIX && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
2070fc4dec0aSBarry Smith 
2071*cf37664fSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
2072854ce69bSBarry Smith     ierr = PetscCalloc1(1+A->cmap->n,&col);CHKERRQ(ierr);
2073bfeeae90SHong Zhang 
2074bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
2075ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2076d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
207733d57670SJed Brown     ierr = MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
20787adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2079ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
2080606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
2081a541d17aSBarry Smith   } else {
2082a541d17aSBarry Smith     C = *B;
2083a541d17aSBarry Smith   }
2084a541d17aSBarry Smith 
208517ab2063SBarry Smith   for (i=0; i<m; i++) {
208617ab2063SBarry Smith     len    = ai[i+1]-ai[i];
208787d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
2088b9b97703SBarry Smith     array += len;
2089b9b97703SBarry Smith     aj    += len;
209017ab2063SBarry Smith   }
20916d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20926d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
209317ab2063SBarry Smith 
2094*cf37664fSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
2095416022c9SBarry Smith     *B = C;
209617ab2063SBarry Smith   } else {
209728be2f97SBarry Smith     ierr = MatHeaderMerge(A,&C);CHKERRQ(ierr);
209817ab2063SBarry Smith   }
20993a40ed3dSBarry Smith   PetscFunctionReturn(0);
210017ab2063SBarry Smith }
210117ab2063SBarry Smith 
2102cd0d46ebSvictorle #undef __FUNCT__
21035fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
21047087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2105cd0d46ebSvictorle {
21063d3eaba7SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
210754f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
210854f21887SBarry Smith   MatScalar      *va,*vb;
21096849ba73SBarry Smith   PetscErrorCode ierr;
211097f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
2111cd0d46ebSvictorle 
2112cd0d46ebSvictorle   PetscFunctionBegin;
2113cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2114cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
21155485867bSBarry Smith   if (ma!=nb || na!=mb) {
21165485867bSBarry Smith     *f = PETSC_FALSE;
21175485867bSBarry Smith     PetscFunctionReturn(0);
21185485867bSBarry Smith   }
2119cd0d46ebSvictorle   aii  = aij->i; bii = bij->i;
2120cd0d46ebSvictorle   adx  = aij->j; bdx = bij->j;
2121cd0d46ebSvictorle   va   = aij->a; vb = bij->a;
2122785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2123785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
2124cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
2125cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
2126cd0d46ebSvictorle 
2127cd0d46ebSvictorle   *f = PETSC_TRUE;
2128cd0d46ebSvictorle   for (i=0; i<ma; i++) {
2129cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
213097f1f81fSBarry Smith       PetscInt    idc,idr;
21315485867bSBarry Smith       PetscScalar vc,vr;
2132cd0d46ebSvictorle       /* column/row index/value */
21335485867bSBarry Smith       idc = adx[aptr[i]];
21345485867bSBarry Smith       idr = bdx[bptr[idc]];
21355485867bSBarry Smith       vc  = va[aptr[i]];
21365485867bSBarry Smith       vr  = vb[bptr[idc]];
21375485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
21385485867bSBarry Smith         *f = PETSC_FALSE;
21395485867bSBarry Smith         goto done;
2140cd0d46ebSvictorle       } else {
21415485867bSBarry Smith         aptr[i]++;
21425485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
2143cd0d46ebSvictorle       }
2144cd0d46ebSvictorle     }
2145cd0d46ebSvictorle   }
2146cd0d46ebSvictorle done:
2147cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
21483aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
2149cd0d46ebSvictorle   PetscFunctionReturn(0);
2150cd0d46ebSvictorle }
2151cd0d46ebSvictorle 
21521cbb95d3SBarry Smith #undef __FUNCT__
21531cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
21547087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
21551cbb95d3SBarry Smith {
21563d3eaba7SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
215754f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
215854f21887SBarry Smith   MatScalar      *va,*vb;
21591cbb95d3SBarry Smith   PetscErrorCode ierr;
21601cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
21611cbb95d3SBarry Smith 
21621cbb95d3SBarry Smith   PetscFunctionBegin;
21631cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
21641cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
21651cbb95d3SBarry Smith   if (ma!=nb || na!=mb) {
21661cbb95d3SBarry Smith     *f = PETSC_FALSE;
21671cbb95d3SBarry Smith     PetscFunctionReturn(0);
21681cbb95d3SBarry Smith   }
21691cbb95d3SBarry Smith   aii  = aij->i; bii = bij->i;
21701cbb95d3SBarry Smith   adx  = aij->j; bdx = bij->j;
21711cbb95d3SBarry Smith   va   = aij->a; vb = bij->a;
2172785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2173785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
21741cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
21751cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
21761cbb95d3SBarry Smith 
21771cbb95d3SBarry Smith   *f = PETSC_TRUE;
21781cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
21791cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
21801cbb95d3SBarry Smith       PetscInt    idc,idr;
21811cbb95d3SBarry Smith       PetscScalar vc,vr;
21821cbb95d3SBarry Smith       /* column/row index/value */
21831cbb95d3SBarry Smith       idc = adx[aptr[i]];
21841cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
21851cbb95d3SBarry Smith       vc  = va[aptr[i]];
21861cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
21871cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
21881cbb95d3SBarry Smith         *f = PETSC_FALSE;
21891cbb95d3SBarry Smith         goto done;
21901cbb95d3SBarry Smith       } else {
21911cbb95d3SBarry Smith         aptr[i]++;
21921cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
21931cbb95d3SBarry Smith       }
21941cbb95d3SBarry Smith     }
21951cbb95d3SBarry Smith   }
21961cbb95d3SBarry Smith done:
21971cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
21981cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
21991cbb95d3SBarry Smith   PetscFunctionReturn(0);
22001cbb95d3SBarry Smith }
22011cbb95d3SBarry Smith 
22029e29f15eSvictorle #undef __FUNCT__
22039e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2204ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
22059e29f15eSvictorle {
2206dfbe8321SBarry Smith   PetscErrorCode ierr;
22076e111a19SKarl Rupp 
22089e29f15eSvictorle   PetscFunctionBegin;
22095485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
22109e29f15eSvictorle   PetscFunctionReturn(0);
22119e29f15eSvictorle }
22129e29f15eSvictorle 
22134a2ae208SSatish Balay #undef __FUNCT__
22141cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2215ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
22161cbb95d3SBarry Smith {
22171cbb95d3SBarry Smith   PetscErrorCode ierr;
22186e111a19SKarl Rupp 
22191cbb95d3SBarry Smith   PetscFunctionBegin;
22201cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
22211cbb95d3SBarry Smith   PetscFunctionReturn(0);
22221cbb95d3SBarry Smith }
22231cbb95d3SBarry Smith 
22241cbb95d3SBarry Smith #undef __FUNCT__
22254a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2226dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
222717ab2063SBarry Smith {
2228416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
222954f21887SBarry Smith   PetscScalar    *l,*r,x;
223054f21887SBarry Smith   MatScalar      *v;
2231dfbe8321SBarry Smith   PetscErrorCode ierr;
2232d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
223317ab2063SBarry Smith 
22343a40ed3dSBarry Smith   PetscFunctionBegin;
223517ab2063SBarry Smith   if (ll) {
22363ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
22373ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2238e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2239e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
22401ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2241416022c9SBarry Smith     v    = a->a;
224217ab2063SBarry Smith     for (i=0; i<m; i++) {
224317ab2063SBarry Smith       x = l[i];
2244416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
22452205254eSKarl Rupp       for (j=0; j<M; j++) (*v++) *= x;
224617ab2063SBarry Smith     }
22471ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2248efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
224917ab2063SBarry Smith   }
225017ab2063SBarry Smith   if (rr) {
2251e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2252e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
22531ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2254416022c9SBarry Smith     v    = a->a; jj = a->j;
22552205254eSKarl Rupp     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
22561ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2257efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
225817ab2063SBarry Smith   }
2259acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
22603a40ed3dSBarry Smith   PetscFunctionReturn(0);
226117ab2063SBarry Smith }
226217ab2063SBarry Smith 
22634a2ae208SSatish Balay #undef __FUNCT__
22644a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
226597f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
226617ab2063SBarry Smith {
2267db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
22686849ba73SBarry Smith   PetscErrorCode ierr;
2269d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
227097f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
22715d0c19d7SBarry Smith   const PetscInt *irow,*icol;
22725d0c19d7SBarry Smith   PetscInt       nrows,ncols;
227397f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
227454f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2275416022c9SBarry Smith   Mat            C;
2276cdc6f3adSToby Isaac   PetscBool      stride;
227717ab2063SBarry Smith 
22783a40ed3dSBarry Smith   PetscFunctionBegin;
227999141d43SSatish Balay 
228017ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2281b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2282b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
228317ab2063SBarry Smith 
2284251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2285ff718158SBarry Smith   if (stride) {
2286ff718158SBarry Smith     ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2287ff718158SBarry Smith   } else {
2288ff718158SBarry Smith     first = 0;
2289ff718158SBarry Smith     step  = 0;
2290ff718158SBarry Smith   }
2291fee21e36SBarry Smith   if (stride && step == 1) {
229202834360SBarry Smith     /* special case of contiguous rows */
2293dcca6d9dSJed Brown     ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr);
229402834360SBarry Smith     /* loop over new rows determining lens and starting points */
229502834360SBarry Smith     for (i=0; i<nrows; i++) {
2296bfeeae90SHong Zhang       kstart = ai[irow[i]];
2297a2744918SBarry Smith       kend   = kstart + ailen[irow[i]];
2298a91a9bebSLisandro Dalcin       starts[i] = kstart;
229902834360SBarry Smith       for (k=kstart; k<kend; k++) {
2300bfeeae90SHong Zhang         if (aj[k] >= first) {
230102834360SBarry Smith           starts[i] = k;
230202834360SBarry Smith           break;
230302834360SBarry Smith         }
230402834360SBarry Smith       }
2305a2744918SBarry Smith       sum = 0;
230602834360SBarry Smith       while (k < kend) {
2307bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2308a2744918SBarry Smith         sum++;
230902834360SBarry Smith       }
2310a2744918SBarry Smith       lens[i] = sum;
231102834360SBarry Smith     }
231202834360SBarry Smith     /* create submatrix */
2313cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
231497f1f81fSBarry Smith       PetscInt n_cols,n_rows;
231508480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2316e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2317d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
231808480c60SBarry Smith       C    = *B;
23193a40ed3dSBarry Smith     } else {
23203bef6203SJed Brown       PetscInt rbs,cbs;
2321ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2322f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
23233bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
23243bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
23253bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
23267adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2327ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
232808480c60SBarry Smith     }
2329db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2330db02288aSLois Curfman McInnes 
233102834360SBarry Smith     /* loop over rows inserting into submatrix */
2332db02288aSLois Curfman McInnes     a_new = c->a;
2333db02288aSLois Curfman McInnes     j_new = c->j;
2334db02288aSLois Curfman McInnes     i_new = c->i;
2335bfeeae90SHong Zhang 
233602834360SBarry Smith     for (i=0; i<nrows; i++) {
2337a2744918SBarry Smith       ii    = starts[i];
2338a2744918SBarry Smith       lensi = lens[i];
2339a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2340a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
234102834360SBarry Smith       }
234287828ca2SBarry Smith       ierr       = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2343a2744918SBarry Smith       a_new     += lensi;
2344a2744918SBarry Smith       i_new[i+1] = i_new[i] + lensi;
2345a2744918SBarry Smith       c->ilen[i] = lensi;
234602834360SBarry Smith     }
23470e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
23483a40ed3dSBarry Smith   } else {
234902834360SBarry Smith     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
23501795a4d1SJed Brown     ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr);
2351854ce69bSBarry Smith     ierr = PetscMalloc1(1+nrows,&lens);CHKERRQ(ierr);
23524dcab191SBarry Smith     for (i=0; i<ncols; i++) {
23534dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
23544dcab191SBarry 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);
23554dcab191SBarry Smith #endif
23564dcab191SBarry Smith       smap[icol[i]] = i+1;
23574dcab191SBarry Smith     }
23584dcab191SBarry Smith 
235902834360SBarry Smith     /* determine lens of each row */
236002834360SBarry Smith     for (i=0; i<nrows; i++) {
2361bfeeae90SHong Zhang       kstart  = ai[irow[i]];
236202834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
236302834360SBarry Smith       lens[i] = 0;
236402834360SBarry Smith       for (k=kstart; k<kend; k++) {
2365bfeeae90SHong Zhang         if (smap[aj[k]]) {
236602834360SBarry Smith           lens[i]++;
236702834360SBarry Smith         }
236802834360SBarry Smith       }
236902834360SBarry Smith     }
237017ab2063SBarry Smith     /* Create and fill new matrix */
2371a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2372ace3abfcSBarry Smith       PetscBool equal;
23730f5bd95cSBarry Smith 
237499141d43SSatish Balay       c = (Mat_SeqAIJ*)((*B)->data);
2375e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2376d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
2377f23aa3ddSBarry Smith       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2378d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
237908480c60SBarry Smith       C    = *B;
23803a40ed3dSBarry Smith     } else {
23813bef6203SJed Brown       PetscInt rbs,cbs;
2382ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2383f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
23843bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
23853bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
23863bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
23877adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2388ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
238908480c60SBarry Smith     }
239099141d43SSatish Balay     c = (Mat_SeqAIJ*)(C->data);
239117ab2063SBarry Smith     for (i=0; i<nrows; i++) {
239299141d43SSatish Balay       row      = irow[i];
2393bfeeae90SHong Zhang       kstart   = ai[row];
239499141d43SSatish Balay       kend     = kstart + a->ilen[row];
2395bfeeae90SHong Zhang       mat_i    = c->i[i];
239699141d43SSatish Balay       mat_j    = c->j + mat_i;
239799141d43SSatish Balay       mat_a    = c->a + mat_i;
239899141d43SSatish Balay       mat_ilen = c->ilen + i;
239917ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2400bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2401ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
240299141d43SSatish Balay           *mat_a++ = a->a[k];
240399141d43SSatish Balay           (*mat_ilen)++;
240499141d43SSatish Balay 
240517ab2063SBarry Smith         }
240617ab2063SBarry Smith       }
240717ab2063SBarry Smith     }
240802834360SBarry Smith     /* Free work space */
240902834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2410606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2411606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
2412cdc6f3adSToby Isaac     /* sort */
2413cdc6f3adSToby Isaac     for (i = 0; i < nrows; i++) {
2414cdc6f3adSToby Isaac       PetscInt ilen;
2415cdc6f3adSToby Isaac 
2416cdc6f3adSToby Isaac       mat_i = c->i[i];
2417cdc6f3adSToby Isaac       mat_j = c->j + mat_i;
2418cdc6f3adSToby Isaac       mat_a = c->a + mat_i;
2419cdc6f3adSToby Isaac       ilen  = c->ilen[i];
2420cdc6f3adSToby Isaac       ierr  = PetscSortIntWithMatScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr);
2421cdc6f3adSToby Isaac     }
242202834360SBarry Smith   }
24236d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24246d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
242517ab2063SBarry Smith 
242617ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2427416022c9SBarry Smith   *B   = C;
24283a40ed3dSBarry Smith   PetscFunctionReturn(0);
242917ab2063SBarry Smith }
243017ab2063SBarry Smith 
24311df811f5SHong Zhang #undef __FUNCT__
243282d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2433fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
243482d44351SHong Zhang {
243582d44351SHong Zhang   PetscErrorCode ierr;
243682d44351SHong Zhang   Mat            B;
243782d44351SHong Zhang 
243882d44351SHong Zhang   PetscFunctionBegin;
2439c2d650bdSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
244082d44351SHong Zhang     ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
244182d44351SHong Zhang     ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
244233d57670SJed Brown     ierr    = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr);
244382d44351SHong Zhang     ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
244482d44351SHong Zhang     ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
244582d44351SHong Zhang     *subMat = B;
2446c2d650bdSHong Zhang   } else {
2447c2d650bdSHong Zhang     ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
2448c2d650bdSHong Zhang   }
244982d44351SHong Zhang   PetscFunctionReturn(0);
245082d44351SHong Zhang }
245182d44351SHong Zhang 
245282d44351SHong Zhang #undef __FUNCT__
24534a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
24549a625307SHong Zhang PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2455a871dcd8SBarry Smith {
245663b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2457dfbe8321SBarry Smith   PetscErrorCode ierr;
245863b91edcSBarry Smith   Mat            outA;
2459ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
246063b91edcSBarry Smith 
24613a40ed3dSBarry Smith   PetscFunctionBegin;
2462e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
24631df811f5SHong Zhang 
2464b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2465b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2466a871dcd8SBarry Smith 
246763b91edcSBarry Smith   outA             = inA;
2468d5f3da31SBarry Smith   outA->factortype = MAT_FACTOR_LU;
2469f6224b95SHong Zhang   ierr = PetscFree(inA->solvertype);CHKERRQ(ierr);
2470f6224b95SHong Zhang   ierr = PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);CHKERRQ(ierr);
24712205254eSKarl Rupp 
2472c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
24736bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
24742205254eSKarl Rupp 
2475c3122656SLisandro Dalcin   a->row = row;
24762205254eSKarl Rupp 
2477c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
24786bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
24792205254eSKarl Rupp 
2480c3122656SLisandro Dalcin   a->col = col;
248163b91edcSBarry Smith 
248236db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
24836bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
24844c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
24853bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
2486f0ec6fceSSatish Balay 
248794a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2488854ce69bSBarry Smith     ierr = PetscMalloc1(inA->rmap->n+1,&a->solve_work);CHKERRQ(ierr);
24893bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
249094a9d846SBarry Smith   }
249163b91edcSBarry Smith 
2492f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2493137fb511SHong Zhang   if (row_identity && col_identity) {
2494ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2495137fb511SHong Zhang   } else {
2496719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2497137fb511SHong Zhang   }
24983a40ed3dSBarry Smith   PetscFunctionReturn(0);
2499a871dcd8SBarry Smith }
2500a871dcd8SBarry Smith 
25014a2ae208SSatish Balay #undef __FUNCT__
25024a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2503f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2504f0b747eeSBarry Smith {
2505f0b747eeSBarry Smith   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2506f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2507efee365bSSatish Balay   PetscErrorCode ierr;
2508c5df96a5SBarry Smith   PetscBLASInt   one = 1,bnz;
25093a40ed3dSBarry Smith 
25103a40ed3dSBarry Smith   PetscFunctionBegin;
2511c5df96a5SBarry Smith   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
25128b83055fSJed Brown   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2513efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2514acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
25153a40ed3dSBarry Smith   PetscFunctionReturn(0);
2516f0b747eeSBarry Smith }
2517f0b747eeSBarry Smith 
25184a2ae208SSatish Balay #undef __FUNCT__
25194a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
252097f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2521cddf8d76SBarry Smith {
2522dfbe8321SBarry Smith   PetscErrorCode ierr;
252397f1f81fSBarry Smith   PetscInt       i;
2524cddf8d76SBarry Smith 
25253a40ed3dSBarry Smith   PetscFunctionBegin;
2526cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2527854ce69bSBarry Smith     ierr = PetscMalloc1(n+1,B);CHKERRQ(ierr);
2528cddf8d76SBarry Smith   }
2529cddf8d76SBarry Smith 
2530cddf8d76SBarry Smith   for (i=0; i<n; i++) {
25316a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2532cddf8d76SBarry Smith   }
25333a40ed3dSBarry Smith   PetscFunctionReturn(0);
2534cddf8d76SBarry Smith }
2535cddf8d76SBarry Smith 
25364a2ae208SSatish Balay #undef __FUNCT__
25374a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
253897f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
25394dcbc457SBarry Smith {
2540e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
25416849ba73SBarry Smith   PetscErrorCode ierr;
25425d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
25435d0c19d7SBarry Smith   const PetscInt *idx;
254497f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2545f1af5d2fSBarry Smith   PetscBT        table;
2546bbd702dbSSatish Balay 
25473a40ed3dSBarry Smith   PetscFunctionBegin;
2548d0f46423SBarry Smith   m  = A->rmap->n;
2549e4d965acSSatish Balay   ai = a->i;
2550bfeeae90SHong Zhang   aj = a->j;
25518a047759SSatish Balay 
2552e32f2f54SBarry Smith   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
255306763907SSatish Balay 
2554854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&nidx);CHKERRQ(ierr);
255553b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
255606763907SSatish Balay 
2557e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2558b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2559e4d965acSSatish Balay     isz  = 0;
25606831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2561e4d965acSSatish Balay 
2562e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
25634dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2564b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2565e4d965acSSatish Balay 
2566dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2567e4d965acSSatish Balay     for (j=0; j<n; ++j) {
25682205254eSKarl Rupp       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
25694dcbc457SBarry Smith     }
257006763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
25716bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2572e4d965acSSatish Balay 
257304a348a9SBarry Smith     k = 0;
257404a348a9SBarry Smith     for (j=0; j<ov; j++) { /* for each overlap */
257504a348a9SBarry Smith       n = isz;
257606763907SSatish Balay       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2577e4d965acSSatish Balay         row   = nidx[k];
2578e4d965acSSatish Balay         start = ai[row];
2579e4d965acSSatish Balay         end   = ai[row+1];
258004a348a9SBarry Smith         for (l = start; l<end; l++) {
2581efb16452SHong Zhang           val = aj[l];
25822205254eSKarl Rupp           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2583e4d965acSSatish Balay         }
2584e4d965acSSatish Balay       }
2585e4d965acSSatish Balay     }
258670b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2587e4d965acSSatish Balay   }
258894bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2589606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
25903a40ed3dSBarry Smith   PetscFunctionReturn(0);
25914dcbc457SBarry Smith }
259217ab2063SBarry Smith 
25930513a670SBarry Smith /* -------------------------------------------------------------- */
25944a2ae208SSatish Balay #undef __FUNCT__
25954a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2596dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
25970513a670SBarry Smith {
25980513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
25996849ba73SBarry Smith   PetscErrorCode ierr;
26003b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
26015d0c19d7SBarry Smith   const PetscInt *row,*col;
26025d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
260356cd22aeSBarry Smith   IS             icolp,irowp;
26040298fd71SBarry Smith   PetscInt       *cwork = NULL;
26050298fd71SBarry Smith   PetscScalar    *vwork = NULL;
26060513a670SBarry Smith 
26073a40ed3dSBarry Smith   PetscFunctionBegin;
26084c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
260956cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
26104c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
261156cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
26120513a670SBarry Smith 
26130513a670SBarry Smith   /* determine lengths of permuted rows */
2614854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&lens);CHKERRQ(ierr);
26152205254eSKarl Rupp   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2616ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2617f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
261833d57670SJed Brown   ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
26197adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2620ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2621606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
26220513a670SBarry Smith 
2623785e854fSJed Brown   ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr);
26240513a670SBarry Smith   for (i=0; i<m; i++) {
262532ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
26262205254eSKarl Rupp     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2627cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
262832ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
26290513a670SBarry Smith   }
2630606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
26312205254eSKarl Rupp 
26323c7d62e4SBarry Smith   (*B)->assembled = PETSC_FALSE;
26332205254eSKarl Rupp 
26340513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
26350513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
263656cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
263756cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
26386bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
26396bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
26403a40ed3dSBarry Smith   PetscFunctionReturn(0);
26410513a670SBarry Smith }
26420513a670SBarry Smith 
26434a2ae208SSatish Balay #undef __FUNCT__
26444a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2645dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2646cb5b572fSBarry Smith {
2647dfbe8321SBarry Smith   PetscErrorCode ierr;
2648cb5b572fSBarry Smith 
2649cb5b572fSBarry Smith   PetscFunctionBegin;
265033f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
265133f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2652be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2653be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2654be6bf707SBarry Smith 
2655700c5bfcSBarry 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");
2656d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2657cb5b572fSBarry Smith   } else {
2658cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2659cb5b572fSBarry Smith   }
2660cb5b572fSBarry Smith   PetscFunctionReturn(0);
2661cb5b572fSBarry Smith }
2662cb5b572fSBarry Smith 
26634a2ae208SSatish Balay #undef __FUNCT__
26644994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
26654994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2666273d9f13SBarry Smith {
2667dfbe8321SBarry Smith   PetscErrorCode ierr;
2668273d9f13SBarry Smith 
2669273d9f13SBarry Smith   PetscFunctionBegin;
2670ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2671273d9f13SBarry Smith   PetscFunctionReturn(0);
2672273d9f13SBarry Smith }
2673273d9f13SBarry Smith 
26744a2ae208SSatish Balay #undef __FUNCT__
26758c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray_SeqAIJ"
26768c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
26776c0721eeSBarry Smith {
26786c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
26796e111a19SKarl Rupp 
26806c0721eeSBarry Smith   PetscFunctionBegin;
26816c0721eeSBarry Smith   *array = a->a;
26826c0721eeSBarry Smith   PetscFunctionReturn(0);
26836c0721eeSBarry Smith }
26846c0721eeSBarry Smith 
26854a2ae208SSatish Balay #undef __FUNCT__
26868c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray_SeqAIJ"
26878c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
26886c0721eeSBarry Smith {
26896c0721eeSBarry Smith   PetscFunctionBegin;
26906c0721eeSBarry Smith   PetscFunctionReturn(0);
26916c0721eeSBarry Smith }
2692273d9f13SBarry Smith 
26938229c054SShri Abhyankar /*
26948229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
26958229c054SShri Abhyankar    have different nonzero structure.
26968229c054SShri Abhyankar */
2697ac90fabeSBarry Smith #undef __FUNCT__
2698b264fe52SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqX_private"
2699b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2700ec7775f6SShri Abhyankar {
2701b264fe52SHong Zhang   PetscInt       i,j,k,nzx,nzy;
2702ec7775f6SShri Abhyankar 
2703ec7775f6SShri Abhyankar   PetscFunctionBegin;
2704ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2705ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
2706b264fe52SHong Zhang     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2707b264fe52SHong Zhang     nzx = xi[i+1] - xi[i];
2708b264fe52SHong Zhang     nzy = yi[i+1] - yi[i];
27098af7cee1SJed Brown     nnz[i] = 0;
27108af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2711b264fe52SHong Zhang       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2712b264fe52SHong Zhang       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
27138af7cee1SJed Brown       nnz[i]++;
27148af7cee1SJed Brown     }
27158af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2716ec7775f6SShri Abhyankar   }
2717ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2718ec7775f6SShri Abhyankar }
2719ec7775f6SShri Abhyankar 
2720ec7775f6SShri Abhyankar #undef __FUNCT__
2721b264fe52SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
2722b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2723b264fe52SHong Zhang {
2724b264fe52SHong Zhang   PetscInt       m = Y->rmap->N;
2725b264fe52SHong Zhang   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2726b264fe52SHong Zhang   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;
2727b264fe52SHong Zhang   PetscErrorCode ierr;
2728b264fe52SHong Zhang 
2729b264fe52SHong Zhang   PetscFunctionBegin;
2730b264fe52SHong Zhang   /* Set the number of nonzeros in the new matrix */
2731b264fe52SHong Zhang   ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr);
2732b264fe52SHong Zhang   PetscFunctionReturn(0);
2733b264fe52SHong Zhang }
2734b264fe52SHong Zhang 
2735b264fe52SHong Zhang #undef __FUNCT__
2736ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2737f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2738ac90fabeSBarry Smith {
2739dfbe8321SBarry Smith   PetscErrorCode ierr;
2740ac90fabeSBarry Smith   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2741c5df96a5SBarry Smith   PetscBLASInt   one=1,bnz;
2742ac90fabeSBarry Smith 
2743ac90fabeSBarry Smith   PetscFunctionBegin;
2744c5df96a5SBarry Smith   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2745ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2746f4df32b1SMatthew Knepley     PetscScalar alpha = a;
27478b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2748acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2749a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2750ab784542SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2751ab784542SHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2752ac90fabeSBarry Smith   } else {
27538229c054SShri Abhyankar     Mat      B;
27548229c054SShri Abhyankar     PetscInt *nnz;
2755785e854fSJed Brown     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
2756ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2757bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
27584aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
275933d57670SJed Brown     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
2760176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
27618229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2762ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2763ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
276428be2f97SBarry Smith     ierr = MatHeaderReplace(Y,&B);CHKERRQ(ierr);
27658229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2766ac90fabeSBarry Smith   }
2767ac90fabeSBarry Smith   PetscFunctionReturn(0);
2768ac90fabeSBarry Smith }
2769ac90fabeSBarry Smith 
2770521d7252SBarry Smith #undef __FUNCT__
2771354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
27727087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2773354c94deSBarry Smith {
2774354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2775354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2776354c94deSBarry Smith   PetscInt    i,nz;
2777354c94deSBarry Smith   PetscScalar *a;
2778354c94deSBarry Smith 
2779354c94deSBarry Smith   PetscFunctionBegin;
2780354c94deSBarry Smith   nz = aij->nz;
2781354c94deSBarry Smith   a  = aij->a;
27822205254eSKarl Rupp   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2783354c94deSBarry Smith #else
2784354c94deSBarry Smith   PetscFunctionBegin;
2785354c94deSBarry Smith #endif
2786354c94deSBarry Smith   PetscFunctionReturn(0);
2787354c94deSBarry Smith }
2788354c94deSBarry Smith 
2789e34fafa9SBarry Smith #undef __FUNCT__
2790985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2791985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2792e34fafa9SBarry Smith {
2793e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2794e34fafa9SBarry Smith   PetscErrorCode ierr;
2795d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2796e34fafa9SBarry Smith   PetscReal      atmp;
2797985db425SBarry Smith   PetscScalar    *x;
2798e34fafa9SBarry Smith   MatScalar      *aa;
2799e34fafa9SBarry Smith 
2800e34fafa9SBarry Smith   PetscFunctionBegin;
2801e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2802e34fafa9SBarry Smith   aa = a->a;
2803e34fafa9SBarry Smith   ai = a->i;
2804e34fafa9SBarry Smith   aj = a->j;
2805e34fafa9SBarry Smith 
2806985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2807e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2808e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2809e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2810e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2811e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
28129189402eSHong Zhang     x[i]  = 0.0;
2813e34fafa9SBarry Smith     for (j=0; j<ncols; j++) {
2814985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2815985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2816985db425SBarry Smith       aa++; aj++;
2817985db425SBarry Smith     }
2818985db425SBarry Smith   }
2819985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2820985db425SBarry Smith   PetscFunctionReturn(0);
2821985db425SBarry Smith }
2822985db425SBarry Smith 
2823985db425SBarry Smith #undef __FUNCT__
2824985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2825985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2826985db425SBarry Smith {
2827985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2828985db425SBarry Smith   PetscErrorCode ierr;
2829d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2830985db425SBarry Smith   PetscScalar    *x;
2831985db425SBarry Smith   MatScalar      *aa;
2832985db425SBarry Smith 
2833985db425SBarry Smith   PetscFunctionBegin;
2834e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2835985db425SBarry Smith   aa = a->a;
2836985db425SBarry Smith   ai = a->i;
2837985db425SBarry Smith   aj = a->j;
2838985db425SBarry Smith 
2839985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2840985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2841985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2842e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2843985db425SBarry Smith   for (i=0; i<m; i++) {
2844985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2845d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2846985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2847985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2848985db425SBarry Smith       x[i] = 0.0;
2849985db425SBarry Smith       if (idx) {
2850985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2851985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2852985db425SBarry Smith           if (aj[j] > j) {
2853985db425SBarry Smith             idx[i] = j;
2854985db425SBarry Smith             break;
2855985db425SBarry Smith           }
2856985db425SBarry Smith         }
2857985db425SBarry Smith       }
2858985db425SBarry Smith     }
2859985db425SBarry Smith     for (j=0; j<ncols; j++) {
2860985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2861985db425SBarry Smith       aa++; aj++;
2862985db425SBarry Smith     }
2863985db425SBarry Smith   }
2864985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2865985db425SBarry Smith   PetscFunctionReturn(0);
2866985db425SBarry Smith }
2867985db425SBarry Smith 
2868985db425SBarry Smith #undef __FUNCT__
2869c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2870c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2871c87e5d42SMatthew Knepley {
2872c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2873c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2874c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2875c87e5d42SMatthew Knepley   PetscReal      atmp;
2876c87e5d42SMatthew Knepley   PetscScalar    *x;
2877c87e5d42SMatthew Knepley   MatScalar      *aa;
2878c87e5d42SMatthew Knepley 
2879c87e5d42SMatthew Knepley   PetscFunctionBegin;
2880e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2881c87e5d42SMatthew Knepley   aa = a->a;
2882c87e5d42SMatthew Knepley   ai = a->i;
2883c87e5d42SMatthew Knepley   aj = a->j;
2884c87e5d42SMatthew Knepley 
2885c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2886c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2887c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
288860e0710aSBarry Smith   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);
2889c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2890c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2891289a08f5SMatthew Knepley     if (ncols) {
2892289a08f5SMatthew Knepley       /* Get first nonzero */
2893289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
2894289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
28952205254eSKarl Rupp         if (atmp > 1.0e-12) {
28962205254eSKarl Rupp           x[i] = atmp;
28972205254eSKarl Rupp           if (idx) idx[i] = aj[j];
28982205254eSKarl Rupp           break;
28992205254eSKarl Rupp         }
2900289a08f5SMatthew Knepley       }
290112431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2902289a08f5SMatthew Knepley     } else {
2903289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2904289a08f5SMatthew Knepley     }
2905c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
2906c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2907289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2908c87e5d42SMatthew Knepley       aa++; aj++;
2909c87e5d42SMatthew Knepley     }
2910c87e5d42SMatthew Knepley   }
2911c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2912c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2913c87e5d42SMatthew Knepley }
2914c87e5d42SMatthew Knepley 
2915c87e5d42SMatthew Knepley #undef __FUNCT__
2916985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2917985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2918985db425SBarry Smith {
2919985db425SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
2920985db425SBarry Smith   PetscErrorCode  ierr;
2921d9ca1df4SBarry Smith   PetscInt        i,j,m = A->rmap->n,ncols,n;
2922d9ca1df4SBarry Smith   const PetscInt  *ai,*aj;
2923985db425SBarry Smith   PetscScalar     *x;
2924d9ca1df4SBarry Smith   const MatScalar *aa;
2925985db425SBarry Smith 
2926985db425SBarry Smith   PetscFunctionBegin;
2927e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2928985db425SBarry Smith   aa = a->a;
2929985db425SBarry Smith   ai = a->i;
2930985db425SBarry Smith   aj = a->j;
2931985db425SBarry Smith 
2932985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2933985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2934985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2935e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2936985db425SBarry Smith   for (i=0; i<m; i++) {
2937985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2938d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2939985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2940985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2941985db425SBarry Smith       x[i] = 0.0;
2942985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2943985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2944985db425SBarry Smith         for (j=0; j<ncols; j++) {
2945985db425SBarry Smith           if (aj[j] > j) {
2946985db425SBarry Smith             idx[i] = j;
2947985db425SBarry Smith             break;
2948985db425SBarry Smith           }
2949985db425SBarry Smith         }
2950985db425SBarry Smith       }
2951985db425SBarry Smith     }
2952985db425SBarry Smith     for (j=0; j<ncols; j++) {
2953985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2954985db425SBarry Smith       aa++; aj++;
2955e34fafa9SBarry Smith     }
2956e34fafa9SBarry Smith   }
2957e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2958e34fafa9SBarry Smith   PetscFunctionReturn(0);
2959e34fafa9SBarry Smith }
2960bbead8a2SBarry Smith 
2961bbead8a2SBarry Smith #include <petscblaslapack.h>
2962af0996ceSBarry Smith #include <petsc/private/kernels/blockinvert.h>
2963bbead8a2SBarry Smith 
2964bbead8a2SBarry Smith #undef __FUNCT__
2965bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
2966713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
2967bbead8a2SBarry Smith {
2968bbead8a2SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
2969bbead8a2SBarry Smith   PetscErrorCode ierr;
297033d57670SJed Brown   PetscInt       i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
2971bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
2972bbead8a2SBarry Smith   PetscReal      shift = 0.0;
29731a9391e3SHong Zhang   PetscBool      allowzeropivot,zeropivotdetected=PETSC_FALSE;
2974bbead8a2SBarry Smith 
2975bbead8a2SBarry Smith   PetscFunctionBegin;
2976a455e926SHong Zhang   allowzeropivot = PetscNot(A->erroriffailure);
29774a0d0026SBarry Smith   if (a->ibdiagvalid) {
29784a0d0026SBarry Smith     if (values) *values = a->ibdiag;
29794a0d0026SBarry Smith     PetscFunctionReturn(0);
29804a0d0026SBarry Smith   }
2981bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
2982bbead8a2SBarry Smith   if (!a->ibdiag) {
2983785e854fSJed Brown     ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr);
29843bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
2985bbead8a2SBarry Smith   }
2986bbead8a2SBarry Smith   diag = a->ibdiag;
2987bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
2988bbead8a2SBarry Smith   /* factor and invert each block */
2989bbead8a2SBarry Smith   switch (bs) {
2990bbead8a2SBarry Smith   case 1:
2991bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2992bbead8a2SBarry Smith       ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
2993ec1892c8SHong Zhang       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
2994ec1892c8SHong Zhang         if (allowzeropivot) {
29957b6c816cSBarry Smith           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
29967b6c816cSBarry Smith           A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
29977b6c816cSBarry Smith           A->factorerror_zeropivot_row   = i;
29987b6c816cSBarry Smith           ierr = PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);CHKERRQ(ierr);
29997b6c816cSBarry Smith         } else SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D pivot %g tolerance %g",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);
3000ec1892c8SHong Zhang       }
3001bbead8a2SBarry Smith       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3002bbead8a2SBarry Smith     }
3003bbead8a2SBarry Smith     break;
3004bbead8a2SBarry Smith   case 2:
3005bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3006bbead8a2SBarry Smith       ij[0] = 2*i; ij[1] = 2*i + 1;
3007bbead8a2SBarry Smith       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
3008a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
30097b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
301096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
3011bbead8a2SBarry Smith       diag += 4;
3012bbead8a2SBarry Smith     }
3013bbead8a2SBarry Smith     break;
3014bbead8a2SBarry Smith   case 3:
3015bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3016bbead8a2SBarry Smith       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3017bbead8a2SBarry Smith       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
3018a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
30197b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
302096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
3021bbead8a2SBarry Smith       diag += 9;
3022bbead8a2SBarry Smith     }
3023bbead8a2SBarry Smith     break;
3024bbead8a2SBarry Smith   case 4:
3025bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3026bbead8a2SBarry Smith       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3027bbead8a2SBarry Smith       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
3028a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
30297b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
303096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
3031bbead8a2SBarry Smith       diag += 16;
3032bbead8a2SBarry Smith     }
3033bbead8a2SBarry Smith     break;
3034bbead8a2SBarry Smith   case 5:
3035bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3036bbead8a2SBarry 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;
3037bbead8a2SBarry Smith       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
3038a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
30397b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
304096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
3041bbead8a2SBarry Smith       diag += 25;
3042bbead8a2SBarry Smith     }
3043bbead8a2SBarry Smith     break;
3044bbead8a2SBarry Smith   case 6:
3045bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3046bbead8a2SBarry 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;
3047bbead8a2SBarry Smith       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
3048a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
30497b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
305096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3051bbead8a2SBarry Smith       diag += 36;
3052bbead8a2SBarry Smith     }
3053bbead8a2SBarry Smith     break;
3054bbead8a2SBarry Smith   case 7:
3055bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3056bbead8a2SBarry 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;
3057bbead8a2SBarry Smith       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
3058a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
30597b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
306096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3061bbead8a2SBarry Smith       diag += 49;
3062bbead8a2SBarry Smith     }
3063bbead8a2SBarry Smith     break;
3064bbead8a2SBarry Smith   default:
3065dcca6d9dSJed Brown     ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr);
3066bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3067bbead8a2SBarry Smith       for (j=0; j<bs; j++) {
3068bbead8a2SBarry Smith         IJ[j] = bs*i + j;
3069bbead8a2SBarry Smith       }
3070bbead8a2SBarry Smith       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
30715f8bbccaSHong Zhang       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
30727b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
307396b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3074bbead8a2SBarry Smith       diag += bs2;
3075bbead8a2SBarry Smith     }
3076bbead8a2SBarry Smith     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3077bbead8a2SBarry Smith   }
3078bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3079bbead8a2SBarry Smith   PetscFunctionReturn(0);
3080bbead8a2SBarry Smith }
3081bbead8a2SBarry Smith 
308273a71a0fSBarry Smith #undef __FUNCT__
308373a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_SeqAIJ"
308473a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
308573a71a0fSBarry Smith {
308673a71a0fSBarry Smith   PetscErrorCode ierr;
308773a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
308873a71a0fSBarry Smith   PetscScalar    a;
308973a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
309073a71a0fSBarry Smith 
309173a71a0fSBarry Smith   PetscFunctionBegin;
309273a71a0fSBarry Smith   if (!x->assembled) {
309373a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
309473a71a0fSBarry Smith     for (i=0; i<m; i++) {
309573a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
309673a71a0fSBarry Smith         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
309773a71a0fSBarry Smith         col  = (PetscInt)(n*PetscRealPart(a));
309873a71a0fSBarry Smith         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
309973a71a0fSBarry Smith       }
310073a71a0fSBarry Smith     }
310173a71a0fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
310273a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
310373a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
310473a71a0fSBarry Smith   PetscFunctionReturn(0);
310573a71a0fSBarry Smith }
310673a71a0fSBarry Smith 
31077d68702bSBarry Smith #undef __FUNCT__
31087d68702bSBarry Smith #define __FUNCT__ "MatShift_SeqAIJ"
31097d68702bSBarry Smith PetscErrorCode MatShift_SeqAIJ(Mat Y,PetscScalar a)
31107d68702bSBarry Smith {
31117d68702bSBarry Smith   PetscErrorCode ierr;
31127d68702bSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)Y->data;
31137d68702bSBarry Smith 
31147d68702bSBarry Smith   PetscFunctionBegin;
31156f33a894SBarry Smith   if (!Y->preallocated || !aij->nz) {
31167d68702bSBarry Smith     ierr = MatSeqAIJSetPreallocation(Y,1,NULL);CHKERRQ(ierr);
31177d68702bSBarry Smith   }
31187d68702bSBarry Smith   ierr = MatShift_Basic(Y,a);CHKERRQ(ierr);
31197d68702bSBarry Smith   PetscFunctionReturn(0);
31207d68702bSBarry Smith }
31217d68702bSBarry Smith 
3122682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
31230a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3124cb5b572fSBarry Smith                                         MatGetRow_SeqAIJ,
3125cb5b572fSBarry Smith                                         MatRestoreRow_SeqAIJ,
3126cb5b572fSBarry Smith                                         MatMult_SeqAIJ,
312797304618SKris Buschelman                                 /*  4*/ MatMultAdd_SeqAIJ,
31287c922b88SBarry Smith                                         MatMultTranspose_SeqAIJ,
31297c922b88SBarry Smith                                         MatMultTransposeAdd_SeqAIJ,
3130db4efbfdSBarry Smith                                         0,
3131db4efbfdSBarry Smith                                         0,
3132db4efbfdSBarry Smith                                         0,
3133db4efbfdSBarry Smith                                 /* 10*/ 0,
3134cb5b572fSBarry Smith                                         MatLUFactor_SeqAIJ,
3135cb5b572fSBarry Smith                                         0,
313641f059aeSBarry Smith                                         MatSOR_SeqAIJ,
313717ab2063SBarry Smith                                         MatTranspose_SeqAIJ,
313897304618SKris Buschelman                                 /*1 5*/ MatGetInfo_SeqAIJ,
3139cb5b572fSBarry Smith                                         MatEqual_SeqAIJ,
3140cb5b572fSBarry Smith                                         MatGetDiagonal_SeqAIJ,
3141cb5b572fSBarry Smith                                         MatDiagonalScale_SeqAIJ,
3142cb5b572fSBarry Smith                                         MatNorm_SeqAIJ,
314397304618SKris Buschelman                                 /* 20*/ 0,
3144cb5b572fSBarry Smith                                         MatAssemblyEnd_SeqAIJ,
3145cb5b572fSBarry Smith                                         MatSetOption_SeqAIJ,
3146cb5b572fSBarry Smith                                         MatZeroEntries_SeqAIJ,
3147d519adbfSMatthew Knepley                                 /* 24*/ MatZeroRows_SeqAIJ,
3148db4efbfdSBarry Smith                                         0,
3149db4efbfdSBarry Smith                                         0,
3150db4efbfdSBarry Smith                                         0,
3151db4efbfdSBarry Smith                                         0,
31524994cf47SJed Brown                                 /* 29*/ MatSetUp_SeqAIJ,
3153db4efbfdSBarry Smith                                         0,
3154db4efbfdSBarry Smith                                         0,
31558c778c55SBarry Smith                                         0,
31568c778c55SBarry Smith                                         0,
3157d519adbfSMatthew Knepley                                 /* 34*/ MatDuplicate_SeqAIJ,
3158cb5b572fSBarry Smith                                         0,
3159cb5b572fSBarry Smith                                         0,
3160cb5b572fSBarry Smith                                         MatILUFactor_SeqAIJ,
3161cb5b572fSBarry Smith                                         0,
3162d519adbfSMatthew Knepley                                 /* 39*/ MatAXPY_SeqAIJ,
3163cb5b572fSBarry Smith                                         MatGetSubMatrices_SeqAIJ,
3164cb5b572fSBarry Smith                                         MatIncreaseOverlap_SeqAIJ,
3165cb5b572fSBarry Smith                                         MatGetValues_SeqAIJ,
3166cb5b572fSBarry Smith                                         MatCopy_SeqAIJ,
3167d519adbfSMatthew Knepley                                 /* 44*/ MatGetRowMax_SeqAIJ,
3168cb5b572fSBarry Smith                                         MatScale_SeqAIJ,
31697d68702bSBarry Smith                                         MatShift_SeqAIJ,
317079299369SBarry Smith                                         MatDiagonalSet_SeqAIJ,
31716e169961SBarry Smith                                         MatZeroRowsColumns_SeqAIJ,
317273a71a0fSBarry Smith                                 /* 49*/ MatSetRandom_SeqAIJ,
31733b2fbd54SBarry Smith                                         MatGetRowIJ_SeqAIJ,
31743b2fbd54SBarry Smith                                         MatRestoreRowIJ_SeqAIJ,
31753b2fbd54SBarry Smith                                         MatGetColumnIJ_SeqAIJ,
3176a93ec695SBarry Smith                                         MatRestoreColumnIJ_SeqAIJ,
317793dfae19SHong Zhang                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3178b9617806SBarry Smith                                         0,
31790513a670SBarry Smith                                         0,
3180cda55fadSBarry Smith                                         MatPermute_SeqAIJ,
3181cda55fadSBarry Smith                                         0,
3182d519adbfSMatthew Knepley                                 /* 59*/ 0,
3183b9b97703SBarry Smith                                         MatDestroy_SeqAIJ,
3184b9b97703SBarry Smith                                         MatView_SeqAIJ,
3185357abbc8SBarry Smith                                         0,
3186321b30b9SSatish Balay                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3187321b30b9SSatish Balay                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3188321b30b9SSatish Balay                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3189ee4f033dSBarry Smith                                         0,
3190ee4f033dSBarry Smith                                         0,
3191ee4f033dSBarry Smith                                         0,
3192d519adbfSMatthew Knepley                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3193c87e5d42SMatthew Knepley                                         MatGetRowMinAbs_SeqAIJ,
3194ee4f033dSBarry Smith                                         0,
3195dcf5cc72SBarry Smith                                         0,
31962c93a97aSBarry Smith                                         0,
31972c93a97aSBarry Smith                                 /* 74*/ 0,
31983acb8795SBarry Smith                                         MatFDColoringApply_AIJ,
319997304618SKris Buschelman                                         0,
320097304618SKris Buschelman                                         0,
320197304618SKris Buschelman                                         0,
32026ce1633cSBarry Smith                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
320397304618SKris Buschelman                                         0,
320497304618SKris Buschelman                                         0,
320597304618SKris Buschelman                                         0,
3206bc011b1eSHong Zhang                                         MatLoad_SeqAIJ,
3207d519adbfSMatthew Knepley                                 /* 84*/ MatIsSymmetric_SeqAIJ,
32081cbb95d3SBarry Smith                                         MatIsHermitian_SeqAIJ,
32096284ec50SHong Zhang                                         0,
32106284ec50SHong Zhang                                         0,
3211bc011b1eSHong Zhang                                         0,
3212d519adbfSMatthew Knepley                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
321326be0446SHong Zhang                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
321426be0446SHong Zhang                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
321565e8a0caSHong Zhang                                         MatPtAP_SeqAIJ_SeqAIJ,
32164a1b09b7SHong Zhang                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_DenseAxpy,
321765e8a0caSHong Zhang                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
32186fc122caSHong Zhang                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
32196fc122caSHong Zhang                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
32206fc122caSHong Zhang                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
32212121bac1SHong Zhang                                         0,
32222121bac1SHong Zhang                                 /* 99*/ 0,
3223609c6c4dSKris Buschelman                                         0,
3224609c6c4dSKris Buschelman                                         0,
322587d4246cSBarry Smith                                         MatConjugate_SeqAIJ,
322687d4246cSBarry Smith                                         0,
3227d519adbfSMatthew Knepley                                 /*104*/ MatSetValuesRow_SeqAIJ,
322899cafbc1SBarry Smith                                         MatRealPart_SeqAIJ,
3229f5edf698SHong Zhang                                         MatImaginaryPart_SeqAIJ,
3230f5edf698SHong Zhang                                         0,
32312bebee5dSHong Zhang                                         0,
3232cbd44569SHong Zhang                                 /*109*/ MatMatSolve_SeqAIJ,
3233985db425SBarry Smith                                         0,
32342af78befSBarry Smith                                         MatGetRowMin_SeqAIJ,
32352af78befSBarry Smith                                         0,
3236599ef60dSHong Zhang                                         MatMissingDiagonal_SeqAIJ,
3237d519adbfSMatthew Knepley                                 /*114*/ 0,
3238599ef60dSHong Zhang                                         0,
32393c2a7987SHong Zhang                                         0,
3240fe97e370SBarry Smith                                         0,
3241fbdbba38SShri Abhyankar                                         0,
3242fbdbba38SShri Abhyankar                                 /*119*/ 0,
3243fbdbba38SShri Abhyankar                                         0,
3244fbdbba38SShri Abhyankar                                         0,
324582d44351SHong Zhang                                         0,
3246b3a44c85SBarry Smith                                         MatGetMultiProcBlock_SeqAIJ,
32470716a85fSBarry Smith                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3248bbead8a2SBarry Smith                                         MatGetColumnNorms_SeqAIJ,
324937868618SMatthew G Knepley                                         MatInvertBlockDiagonal_SeqAIJ,
325037868618SMatthew G Knepley                                         0,
325137868618SMatthew G Knepley                                         0,
32525df89d91SHong Zhang                                 /*129*/ 0,
325375648e8dSHong Zhang                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
325475648e8dSHong Zhang                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
325575648e8dSHong Zhang                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3256b9af6bddSHong Zhang                                         MatTransposeColoringCreate_SeqAIJ,
3257b9af6bddSHong Zhang                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
32582b8ad9a3SHong Zhang                                         MatTransColoringApplyDenToSp_SeqAIJ,
32592b8ad9a3SHong Zhang                                         MatRARt_SeqAIJ_SeqAIJ,
32602b8ad9a3SHong Zhang                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
32613964eb88SJed Brown                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
32623964eb88SJed Brown                                  /*139*/0,
3263f9426fe0SMark Adams                                         0,
32641919a2e2SJed Brown                                         0,
32653a062f41SBarry Smith                                         MatFDColoringSetUp_SeqXAIJ,
32669c8f2541SHong Zhang                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
32679c8f2541SHong Zhang                                  /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ
32689e29f15eSvictorle };
326917ab2063SBarry Smith 
32704a2ae208SSatish Balay #undef __FUNCT__
32714a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
32727087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3273bef8e0ddSBarry Smith {
3274bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
327597f1f81fSBarry Smith   PetscInt   i,nz,n;
3276bef8e0ddSBarry Smith 
3277bef8e0ddSBarry Smith   PetscFunctionBegin;
3278bef8e0ddSBarry Smith   nz = aij->maxnz;
3279d0f46423SBarry Smith   n  = mat->rmap->n;
3280bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3281bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3282bef8e0ddSBarry Smith   }
3283bef8e0ddSBarry Smith   aij->nz = nz;
3284bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3285bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3286bef8e0ddSBarry Smith   }
3287bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3288bef8e0ddSBarry Smith }
3289bef8e0ddSBarry Smith 
32904a2ae208SSatish Balay #undef __FUNCT__
32914a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3292bef8e0ddSBarry Smith /*@
3293bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3294bef8e0ddSBarry Smith        in the matrix.
3295bef8e0ddSBarry Smith 
3296bef8e0ddSBarry Smith   Input Parameters:
3297bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3298bef8e0ddSBarry Smith -  indices - the column indices
3299bef8e0ddSBarry Smith 
330015091d37SBarry Smith   Level: advanced
330115091d37SBarry Smith 
3302bef8e0ddSBarry Smith   Notes:
3303bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3304bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3305bef8e0ddSBarry Smith   of the MatSetValues() operation.
3306bef8e0ddSBarry Smith 
3307bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3308d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3309bef8e0ddSBarry Smith 
3310bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3311bef8e0ddSBarry Smith 
3312b9617806SBarry Smith     The indices should start with zero, not one.
3313b9617806SBarry Smith 
3314bef8e0ddSBarry Smith @*/
33157087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3316bef8e0ddSBarry Smith {
33174ac538c5SBarry Smith   PetscErrorCode ierr;
3318bef8e0ddSBarry Smith 
3319bef8e0ddSBarry Smith   PetscFunctionBegin;
33200700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
33214482741eSBarry Smith   PetscValidPointer(indices,2);
33224ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3323bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3324bef8e0ddSBarry Smith }
3325bef8e0ddSBarry Smith 
3326be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3327be6bf707SBarry Smith 
33284a2ae208SSatish Balay #undef __FUNCT__
33294a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
33307087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3331be6bf707SBarry Smith {
3332be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
33336849ba73SBarry Smith   PetscErrorCode ierr;
3334d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3335be6bf707SBarry Smith 
3336be6bf707SBarry Smith   PetscFunctionBegin;
3337169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3338be6bf707SBarry Smith 
3339be6bf707SBarry Smith   /* allocate space for values if not already there */
3340be6bf707SBarry Smith   if (!aij->saved_values) {
3341854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr);
33423bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3343be6bf707SBarry Smith   }
3344be6bf707SBarry Smith 
3345be6bf707SBarry Smith   /* copy values over */
334687828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3347be6bf707SBarry Smith   PetscFunctionReturn(0);
3348be6bf707SBarry Smith }
3349be6bf707SBarry Smith 
33504a2ae208SSatish Balay #undef __FUNCT__
3351b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3352be6bf707SBarry Smith /*@
3353be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3354be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3355be6bf707SBarry Smith        nonlinear portion.
3356be6bf707SBarry Smith 
3357be6bf707SBarry Smith    Collect on Mat
3358be6bf707SBarry Smith 
3359be6bf707SBarry Smith   Input Parameters:
33600e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3361be6bf707SBarry Smith 
336215091d37SBarry Smith   Level: advanced
336315091d37SBarry Smith 
3364be6bf707SBarry Smith   Common Usage, with SNESSolve():
3365be6bf707SBarry Smith $    Create Jacobian matrix
3366be6bf707SBarry Smith $    Set linear terms into matrix
3367be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3368be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3369be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3370512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3371be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3372be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3373be6bf707SBarry Smith $    In your Jacobian routine
3374be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3375be6bf707SBarry Smith $      Set nonlinear terms in matrix
3376be6bf707SBarry Smith 
3377be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3378be6bf707SBarry Smith $    // build linear portion of Jacobian
3379512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3380be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3381be6bf707SBarry Smith $    loop over nonlinear iterations
3382be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3383be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3384be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3385be6bf707SBarry Smith $       Solve linear system with Jacobian
3386be6bf707SBarry Smith $    endloop
3387be6bf707SBarry Smith 
3388be6bf707SBarry Smith   Notes:
3389be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3390512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3391be6bf707SBarry Smith     calling this routine.
3392be6bf707SBarry Smith 
33930c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
33940c468ba9SBarry Smith     and does not allocated additional space.
33950c468ba9SBarry Smith 
3396be6bf707SBarry Smith .seealso: MatRetrieveValues()
3397be6bf707SBarry Smith 
3398be6bf707SBarry Smith @*/
33997087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3400be6bf707SBarry Smith {
34014ac538c5SBarry Smith   PetscErrorCode ierr;
3402be6bf707SBarry Smith 
3403be6bf707SBarry Smith   PetscFunctionBegin;
34040700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3405e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3406e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
34074ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3408be6bf707SBarry Smith   PetscFunctionReturn(0);
3409be6bf707SBarry Smith }
3410be6bf707SBarry Smith 
34114a2ae208SSatish Balay #undef __FUNCT__
34124a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
34137087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3414be6bf707SBarry Smith {
3415be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
34166849ba73SBarry Smith   PetscErrorCode ierr;
3417d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3418be6bf707SBarry Smith 
3419be6bf707SBarry Smith   PetscFunctionBegin;
3420169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3421f23aa3ddSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3422be6bf707SBarry Smith   /* copy values over */
342387828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3424be6bf707SBarry Smith   PetscFunctionReturn(0);
3425be6bf707SBarry Smith }
3426be6bf707SBarry Smith 
34274a2ae208SSatish Balay #undef __FUNCT__
34284a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3429be6bf707SBarry Smith /*@
3430be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3431be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3432be6bf707SBarry Smith        nonlinear portion.
3433be6bf707SBarry Smith 
3434be6bf707SBarry Smith    Collect on Mat
3435be6bf707SBarry Smith 
3436be6bf707SBarry Smith   Input Parameters:
3437386f7cf9SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3438be6bf707SBarry Smith 
343915091d37SBarry Smith   Level: advanced
344015091d37SBarry Smith 
3441be6bf707SBarry Smith .seealso: MatStoreValues()
3442be6bf707SBarry Smith 
3443be6bf707SBarry Smith @*/
34447087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3445be6bf707SBarry Smith {
34464ac538c5SBarry Smith   PetscErrorCode ierr;
3447be6bf707SBarry Smith 
3448be6bf707SBarry Smith   PetscFunctionBegin;
34490700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3450e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3451e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
34524ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3453be6bf707SBarry Smith   PetscFunctionReturn(0);
3454be6bf707SBarry Smith }
3455be6bf707SBarry Smith 
3456f83d6046SBarry Smith 
3457be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
34584a2ae208SSatish Balay #undef __FUNCT__
34594a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
346017ab2063SBarry Smith /*@C
3461682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
34620d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
34636e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
346451c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
34652bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
346617ab2063SBarry Smith 
3467db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3468db81eaa0SLois Curfman McInnes 
346917ab2063SBarry Smith    Input Parameters:
3470db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
347117ab2063SBarry Smith .  m - number of rows
347217ab2063SBarry Smith .  n - number of columns
347317ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
347451c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
34750298fd71SBarry Smith          (possibly different for each row) or NULL
347617ab2063SBarry Smith 
347717ab2063SBarry Smith    Output Parameter:
3478416022c9SBarry Smith .  A - the matrix
347917ab2063SBarry Smith 
3480175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3481ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3482175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3483175b88e8SBarry Smith 
3484b259b22eSLois Curfman McInnes    Notes:
348549a6f317SBarry Smith    If nnz is given then nz is ignored
348649a6f317SBarry Smith 
348717ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
348817ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
34890002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
349044cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
349117ab2063SBarry Smith 
349217ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
34930298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
34943d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
34956da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
349617ab2063SBarry Smith 
3497682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
34984fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3499682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
35006c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
35016c7ebb05SLois Curfman McInnes 
35026c7ebb05SLois Curfman McInnes    Options Database Keys:
3503698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
35049db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
350517ab2063SBarry Smith 
3506027ccd11SLois Curfman McInnes    Level: intermediate
3507027ccd11SLois Curfman McInnes 
350869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
350936db0b34SBarry Smith 
351017ab2063SBarry Smith @*/
35117087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
351217ab2063SBarry Smith {
3513dfbe8321SBarry Smith   PetscErrorCode ierr;
35146945ee14SBarry Smith 
35153a40ed3dSBarry Smith   PetscFunctionBegin;
3516f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3517117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3518c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3519d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3520273d9f13SBarry Smith   PetscFunctionReturn(0);
3521273d9f13SBarry Smith }
3522273d9f13SBarry Smith 
35234a2ae208SSatish Balay #undef __FUNCT__
35244a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3525273d9f13SBarry Smith /*@C
3526273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3527273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3528273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3529273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3530273d9f13SBarry Smith 
3531273d9f13SBarry Smith    Collective on MPI_Comm
3532273d9f13SBarry Smith 
3533273d9f13SBarry Smith    Input Parameters:
35341c4f3114SJed Brown +  B - The matrix
3535273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3536273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
35370298fd71SBarry Smith          (possibly different for each row) or NULL
3538273d9f13SBarry Smith 
3539273d9f13SBarry Smith    Notes:
354049a6f317SBarry Smith      If nnz is given then nz is ignored
354149a6f317SBarry Smith 
3542273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3543273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3544273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3545273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3546273d9f13SBarry Smith 
3547273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
35480298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3549273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3550273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3551273d9f13SBarry Smith 
3552aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3553aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3554aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3555aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3556aa95bbe8SBarry Smith 
3557a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3558a96a251dSBarry Smith    entries or columns indices
3559a96a251dSBarry Smith 
3560273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3561273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3562273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3563273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3564273d9f13SBarry Smith 
3565273d9f13SBarry Smith    Options Database Keys:
3566698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3567698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3568273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3569273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3570273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3571273d9f13SBarry Smith 
3572273d9f13SBarry Smith    Level: intermediate
3573273d9f13SBarry Smith 
357469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3575273d9f13SBarry Smith 
3576273d9f13SBarry Smith @*/
35777087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3578273d9f13SBarry Smith {
35794ac538c5SBarry Smith   PetscErrorCode ierr;
3580a23d5eceSKris Buschelman 
3581a23d5eceSKris Buschelman   PetscFunctionBegin;
35826ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
35836ba663aaSJed Brown   PetscValidType(B,1);
35844ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3585a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3586a23d5eceSKris Buschelman }
3587a23d5eceSKris Buschelman 
3588a23d5eceSKris Buschelman #undef __FUNCT__
3589a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
35907087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3591a23d5eceSKris Buschelman {
3592273d9f13SBarry Smith   Mat_SeqAIJ     *b;
35932576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
35946849ba73SBarry Smith   PetscErrorCode ierr;
359597f1f81fSBarry Smith   PetscInt       i;
3596273d9f13SBarry Smith 
3597273d9f13SBarry Smith   PetscFunctionBegin;
35982576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3599a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3600c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3601c461c341SBarry Smith     nz             = 0;
3602c461c341SBarry Smith   }
3603c461c341SBarry Smith 
360426283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
360526283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3606899cda47SBarry Smith 
3607435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
360860e0710aSBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3609b73539f3SBarry Smith   if (nnz) {
3610d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
361160e0710aSBarry 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]);
361260e0710aSBarry 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);
3613b73539f3SBarry Smith     }
3614b73539f3SBarry Smith   }
3615b73539f3SBarry Smith 
3616273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
36172205254eSKarl Rupp 
3618273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3619273d9f13SBarry Smith 
3620ab93d7beSBarry Smith   if (!skipallocation) {
36212ee49352SLisandro Dalcin     if (!b->imax) {
3622dcca6d9dSJed Brown       ierr = PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);CHKERRQ(ierr);
36233bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
36242ee49352SLisandro Dalcin     }
3625273d9f13SBarry Smith     if (!nnz) {
3626435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3627c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3628d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3629d0f46423SBarry Smith       nz = nz*B->rmap->n;
3630273d9f13SBarry Smith     } else {
3631273d9f13SBarry Smith       nz = 0;
3632d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3633273d9f13SBarry Smith     }
3634ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
36352205254eSKarl Rupp     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;
3636ab93d7beSBarry Smith 
3637273d9f13SBarry Smith     /* allocate the matrix space */
363853dd7562SDmitry Karpeev     /* FIXME: should B's old memory be unlogged? */
36392ee49352SLisandro Dalcin     ierr    = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3640dcca6d9dSJed Brown     ierr    = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr);
36413bb1ff40SBarry Smith     ierr    = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3642bfeeae90SHong Zhang     b->i[0] = 0;
3643d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
36445da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
36455da197adSKris Buschelman     }
3646273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3647e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3648e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3649c461c341SBarry Smith   } else {
3650e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
3651e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
3652c461c341SBarry Smith   }
3653273d9f13SBarry Smith 
3654273d9f13SBarry Smith   b->nz               = 0;
3655273d9f13SBarry Smith   b->maxnz            = nz;
3656273d9f13SBarry Smith   B->info.nz_unneeded = (double)b->maxnz;
36572205254eSKarl Rupp   if (realalloc) {
36582205254eSKarl Rupp     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
36592205254eSKarl Rupp   }
3660cb7b82ddSBarry Smith   B->was_assembled = PETSC_FALSE;
3661cb7b82ddSBarry Smith   B->assembled     = PETSC_FALSE;
3662273d9f13SBarry Smith   PetscFunctionReturn(0);
3663273d9f13SBarry Smith }
3664273d9f13SBarry Smith 
3665a1661176SMatthew Knepley #undef  __FUNCT__
3666a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
366758d36128SBarry Smith /*@
3668a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3669a1661176SMatthew Knepley 
3670a1661176SMatthew Knepley    Input Parameters:
3671a1661176SMatthew Knepley +  B - the matrix
3672a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3673a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3674a1661176SMatthew Knepley -  v - optional values in the matrix
3675a1661176SMatthew Knepley 
3676a1661176SMatthew Knepley    Level: developer
3677a1661176SMatthew Knepley 
367858d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
367958d36128SBarry Smith 
3680a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3681a1661176SMatthew Knepley 
3682a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3683a1661176SMatthew Knepley @*/
3684a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3685a1661176SMatthew Knepley {
3686a1661176SMatthew Knepley   PetscErrorCode ierr;
3687a1661176SMatthew Knepley 
3688a1661176SMatthew Knepley   PetscFunctionBegin;
36890700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
36906ba663aaSJed Brown   PetscValidType(B,1);
36914ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3692a1661176SMatthew Knepley   PetscFunctionReturn(0);
3693a1661176SMatthew Knepley }
3694a1661176SMatthew Knepley 
3695a1661176SMatthew Knepley #undef  __FUNCT__
3696a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
36977087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3698a1661176SMatthew Knepley {
3699a1661176SMatthew Knepley   PetscInt       i;
3700a1661176SMatthew Knepley   PetscInt       m,n;
3701a1661176SMatthew Knepley   PetscInt       nz;
3702a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3703a1661176SMatthew Knepley   PetscScalar    *values;
3704a1661176SMatthew Knepley   PetscErrorCode ierr;
3705a1661176SMatthew Knepley 
3706a1661176SMatthew Knepley   PetscFunctionBegin;
370765e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3708779a8d59SSatish Balay 
3709779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3710779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3711779a8d59SSatish Balay 
3712779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3713854ce69bSBarry Smith   ierr = PetscMalloc1(m+1, &nnz);CHKERRQ(ierr);
3714a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3715b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3716a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
371765e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3718a1661176SMatthew Knepley     nnz[i] = nz;
3719a1661176SMatthew Knepley   }
3720a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3721a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3722a1661176SMatthew Knepley 
3723a1661176SMatthew Knepley   if (v) {
3724a1661176SMatthew Knepley     values = (PetscScalar*) v;
3725a1661176SMatthew Knepley   } else {
37261795a4d1SJed Brown     ierr = PetscCalloc1(nz_max, &values);CHKERRQ(ierr);
3727a1661176SMatthew Knepley   }
3728a1661176SMatthew Knepley 
3729a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3730b7940d39SSatish Balay     nz   = Ii[i+1] - Ii[i];
3731b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3732a1661176SMatthew Knepley   }
3733a1661176SMatthew Knepley 
3734a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3735a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3736a1661176SMatthew Knepley 
3737a1661176SMatthew Knepley   if (!v) {
3738a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3739a1661176SMatthew Knepley   }
37407827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3741a1661176SMatthew Knepley   PetscFunctionReturn(0);
3742a1661176SMatthew Knepley }
3743a1661176SMatthew Knepley 
3744c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3745af0996ceSBarry Smith #include <petsc/private/kernels/petscaxpy.h>
3746170fe5c8SBarry Smith 
3747170fe5c8SBarry Smith #undef __FUNCT__
3748170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3749170fe5c8SBarry Smith /*
3750170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3751170fe5c8SBarry Smith 
3752170fe5c8SBarry Smith                n                       p                          p
3753170fe5c8SBarry Smith         (              )       (              )         (                  )
3754170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3755170fe5c8SBarry Smith         (              )       (              )         (                  )
3756170fe5c8SBarry Smith 
3757170fe5c8SBarry Smith */
3758170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3759170fe5c8SBarry Smith {
3760170fe5c8SBarry Smith   PetscErrorCode    ierr;
3761170fe5c8SBarry Smith   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3762170fe5c8SBarry Smith   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3763170fe5c8SBarry Smith   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
37641de00fd4SBarry Smith   PetscInt          i,n,m,q,p;
3765170fe5c8SBarry Smith   const PetscInt    *ii,*idx;
3766170fe5c8SBarry Smith   const PetscScalar *b,*a,*a_q;
3767170fe5c8SBarry Smith   PetscScalar       *c,*c_q;
3768170fe5c8SBarry Smith 
3769170fe5c8SBarry Smith   PetscFunctionBegin;
3770d0f46423SBarry Smith   m    = A->rmap->n;
3771d0f46423SBarry Smith   n    = A->cmap->n;
3772d0f46423SBarry Smith   p    = B->cmap->n;
3773170fe5c8SBarry Smith   a    = sub_a->v;
3774170fe5c8SBarry Smith   b    = sub_b->a;
3775170fe5c8SBarry Smith   c    = sub_c->v;
3776170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3777170fe5c8SBarry Smith 
3778170fe5c8SBarry Smith   ii  = sub_b->i;
3779170fe5c8SBarry Smith   idx = sub_b->j;
3780170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3781170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3782170fe5c8SBarry Smith     while (q-->0) {
3783170fe5c8SBarry Smith       c_q = c + m*(*idx);
3784170fe5c8SBarry Smith       a_q = a + m*i;
3785854c7f52SBarry Smith       PetscKernelAXPY(c_q,*b,a_q,m);
3786170fe5c8SBarry Smith       idx++;
3787170fe5c8SBarry Smith       b++;
3788170fe5c8SBarry Smith     }
3789170fe5c8SBarry Smith   }
3790170fe5c8SBarry Smith   PetscFunctionReturn(0);
3791170fe5c8SBarry Smith }
3792170fe5c8SBarry Smith 
3793170fe5c8SBarry Smith #undef __FUNCT__
3794170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3795170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3796170fe5c8SBarry Smith {
3797170fe5c8SBarry Smith   PetscErrorCode ierr;
3798d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3799170fe5c8SBarry Smith   Mat            Cmat;
3800170fe5c8SBarry Smith 
3801170fe5c8SBarry Smith   PetscFunctionBegin;
380260e0710aSBarry 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);
3803ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
3804170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
380533d57670SJed Brown   ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr);
3806170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
38070298fd71SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
3808d73949e8SHong Zhang 
3809d73949e8SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
38102205254eSKarl Rupp 
3811170fe5c8SBarry Smith   *C = Cmat;
3812170fe5c8SBarry Smith   PetscFunctionReturn(0);
3813170fe5c8SBarry Smith }
3814170fe5c8SBarry Smith 
3815170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3816170fe5c8SBarry Smith #undef __FUNCT__
3817170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3818150d2497SBarry Smith PETSC_INTERN PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3819170fe5c8SBarry Smith {
3820170fe5c8SBarry Smith   PetscErrorCode ierr;
3821170fe5c8SBarry Smith 
3822170fe5c8SBarry Smith   PetscFunctionBegin;
3823170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
38243ff4c91cSHong Zhang     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3825170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
38263ff4c91cSHong Zhang     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3827170fe5c8SBarry Smith   }
38283ff4c91cSHong Zhang   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3829170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
38303ff4c91cSHong Zhang   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3831170fe5c8SBarry Smith   PetscFunctionReturn(0);
3832170fe5c8SBarry Smith }
3833170fe5c8SBarry Smith 
3834170fe5c8SBarry Smith 
38350bad9183SKris Buschelman /*MC
3836fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
38370bad9183SKris Buschelman    based on compressed sparse row format.
38380bad9183SKris Buschelman 
38390bad9183SKris Buschelman    Options Database Keys:
38400bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
38410bad9183SKris Buschelman 
38420bad9183SKris Buschelman   Level: beginner
38430bad9183SKris Buschelman 
3844f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
38450bad9183SKris Buschelman M*/
38460bad9183SKris Buschelman 
3847ccd284c7SBarry Smith /*MC
3848ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
3849ccd284c7SBarry Smith 
3850ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
3851ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
3852ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
3853ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3854ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3855ccd284c7SBarry Smith 
3856ccd284c7SBarry Smith    Options Database Keys:
3857ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
3858ccd284c7SBarry Smith 
3859ccd284c7SBarry Smith   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
3860ccd284c7SBarry Smith    enough exist.
3861ccd284c7SBarry Smith 
3862ccd284c7SBarry Smith   Level: beginner
3863ccd284c7SBarry Smith 
3864ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3865ccd284c7SBarry Smith M*/
3866ccd284c7SBarry Smith 
3867ccd284c7SBarry Smith /*MC
3868ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
3869ccd284c7SBarry Smith 
3870ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3871ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3872ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3873ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3874ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3875ccd284c7SBarry Smith 
3876ccd284c7SBarry Smith    Options Database Keys:
3877ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3878ccd284c7SBarry Smith 
3879ccd284c7SBarry Smith   Level: beginner
3880ccd284c7SBarry Smith 
3881ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3882ccd284c7SBarry Smith M*/
3883ccd284c7SBarry Smith 
3884cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3885af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
3886cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
3887af8000cdSHong Zhang #endif
388863c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE)
388963c07aadSStefano Zampini PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*);
38903dad0653Sstefano_zampini PETSC_INTERN PetscErrorCode MatMatMatMult_Transpose_AIJ_AIJ(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
389163c07aadSStefano Zampini #endif
3892cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);
389342c9c57cSBarry Smith 
3894b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
389529b38603SBarry Smith PETSC_EXTERN PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
389629b38603SBarry Smith PETSC_EXTERN PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3897b3866ffcSBarry Smith #endif
389817667f90SBarry Smith 
3899c0c8ee5eSDmitry Karpeev 
39008c778c55SBarry Smith #undef __FUNCT__
39018c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray"
39028c778c55SBarry Smith /*@C
39038c778c55SBarry Smith    MatSeqAIJGetArray - gives access to the array where the data for a SeqSeqAIJ matrix is stored
39048c778c55SBarry Smith 
39058c778c55SBarry Smith    Not Collective
39068c778c55SBarry Smith 
39078c778c55SBarry Smith    Input Parameter:
3908579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
39098c778c55SBarry Smith 
39108c778c55SBarry Smith    Output Parameter:
39118c778c55SBarry Smith .   array - pointer to the data
39128c778c55SBarry Smith 
39138c778c55SBarry Smith    Level: intermediate
39148c778c55SBarry Smith 
3915774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
39168c778c55SBarry Smith @*/
39178c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
39188c778c55SBarry Smith {
39198c778c55SBarry Smith   PetscErrorCode ierr;
39208c778c55SBarry Smith 
39218c778c55SBarry Smith   PetscFunctionBegin;
39228c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
39238c778c55SBarry Smith   PetscFunctionReturn(0);
39248c778c55SBarry Smith }
39258c778c55SBarry Smith 
39268c778c55SBarry Smith #undef __FUNCT__
392721e72a00SBarry Smith #define __FUNCT__ "MatSeqAIJGetMaxRowNonzeros"
392821e72a00SBarry Smith /*@C
392921e72a00SBarry Smith    MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
393021e72a00SBarry Smith 
393121e72a00SBarry Smith    Not Collective
393221e72a00SBarry Smith 
393321e72a00SBarry Smith    Input Parameter:
3934579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
393521e72a00SBarry Smith 
393621e72a00SBarry Smith    Output Parameter:
393721e72a00SBarry Smith .   nz - the maximum number of nonzeros in any row
393821e72a00SBarry Smith 
393921e72a00SBarry Smith    Level: intermediate
394021e72a00SBarry Smith 
394121e72a00SBarry Smith .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
394221e72a00SBarry Smith @*/
394321e72a00SBarry Smith PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
394421e72a00SBarry Smith {
394521e72a00SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
394621e72a00SBarry Smith 
394721e72a00SBarry Smith   PetscFunctionBegin;
394821e72a00SBarry Smith   *nz = aij->rmax;
394921e72a00SBarry Smith   PetscFunctionReturn(0);
395021e72a00SBarry Smith }
395121e72a00SBarry Smith 
395221e72a00SBarry Smith #undef __FUNCT__
39538c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray"
39548c778c55SBarry Smith /*@C
3955579dbff0SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray()
39568c778c55SBarry Smith 
39578c778c55SBarry Smith    Not Collective
39588c778c55SBarry Smith 
39598c778c55SBarry Smith    Input Parameters:
3960579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
39618c778c55SBarry Smith .  array - pointer to the data
39628c778c55SBarry Smith 
39638c778c55SBarry Smith    Level: intermediate
39648c778c55SBarry Smith 
3965774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
39668c778c55SBarry Smith @*/
39678c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
39688c778c55SBarry Smith {
39698c778c55SBarry Smith   PetscErrorCode ierr;
39708c778c55SBarry Smith 
39718c778c55SBarry Smith   PetscFunctionBegin;
39728c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
39738c778c55SBarry Smith   PetscFunctionReturn(0);
39748c778c55SBarry Smith }
39758c778c55SBarry Smith 
39764a2ae208SSatish Balay #undef __FUNCT__
39774a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
39788cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
3979273d9f13SBarry Smith {
3980273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3981dfbe8321SBarry Smith   PetscErrorCode ierr;
398238baddfdSBarry Smith   PetscMPIInt    size;
3983273d9f13SBarry Smith 
3984273d9f13SBarry Smith   PetscFunctionBegin;
3985ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
3986e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3987273d9f13SBarry Smith 
3988b00a9115SJed Brown   ierr = PetscNewLog(B,&b);CHKERRQ(ierr);
39892205254eSKarl Rupp 
3990b0a32e0cSBarry Smith   B->data = (void*)b;
39912205254eSKarl Rupp 
3992549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
39932205254eSKarl Rupp 
3994416022c9SBarry Smith   b->row                = 0;
3995416022c9SBarry Smith   b->col                = 0;
399682bf6240SBarry Smith   b->icol               = 0;
3997b810aeb4SBarry Smith   b->reallocs           = 0;
399836db0b34SBarry Smith   b->ignorezeroentries  = PETSC_FALSE;
3999f1e2ffcdSBarry Smith   b->roworiented        = PETSC_TRUE;
4000416022c9SBarry Smith   b->nonew              = 0;
4001416022c9SBarry Smith   b->diag               = 0;
4002416022c9SBarry Smith   b->solve_work         = 0;
40032a1b7f2aSHong Zhang   B->spptr              = 0;
4004be6bf707SBarry Smith   b->saved_values       = 0;
4005d7f994e1SBarry Smith   b->idiag              = 0;
400671f1c65dSBarry Smith   b->mdiag              = 0;
400771f1c65dSBarry Smith   b->ssor_work          = 0;
400871f1c65dSBarry Smith   b->omega              = 1.0;
400971f1c65dSBarry Smith   b->fshift             = 0.0;
401071f1c65dSBarry Smith   b->idiagvalid         = PETSC_FALSE;
4011bbead8a2SBarry Smith   b->ibdiagvalid        = PETSC_FALSE;
4012a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
401317ab2063SBarry Smith 
401435d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
4015bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
4016bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
40178c778c55SBarry Smith 
4018b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4019bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
4020bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
4021b3866ffcSBarry Smith #endif
402217f1a0eaSHong Zhang 
4023bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
4024bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
4025bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
4026bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
4027bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
4028bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
4029bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
4030af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
4031af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);CHKERRQ(ierr);
4032af8000cdSHong Zhang #endif
403363c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE)
403463c07aadSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);CHKERRQ(ierr);
40353dad0653Sstefano_zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMatMult_transpose_seqaij_seqaij_C",MatMatMatMult_Transpose_AIJ_AIJ);CHKERRQ(ierr);
403663c07aadSStefano Zampini #endif
4037b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);CHKERRQ(ierr);
4038bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4039bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4040bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
4041bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
4042bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
4043bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
4044bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
4045bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
40464108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
404717667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
40483a40ed3dSBarry Smith   PetscFunctionReturn(0);
404917ab2063SBarry Smith }
405017ab2063SBarry Smith 
40514a2ae208SSatish Balay #undef __FUNCT__
4052b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
4053b24902e0SBarry Smith /*
4054b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4055b24902e0SBarry Smith */
4056ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
405717ab2063SBarry Smith {
4058416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
40596849ba73SBarry Smith   PetscErrorCode ierr;
4060d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
406117ab2063SBarry Smith 
40623a40ed3dSBarry Smith   PetscFunctionBegin;
4063273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4064273d9f13SBarry Smith 
4065d5f3da31SBarry Smith   C->factortype = A->factortype;
4066416022c9SBarry Smith   c->row        = 0;
4067416022c9SBarry Smith   c->col        = 0;
406882bf6240SBarry Smith   c->icol       = 0;
40696ad4291fSHong Zhang   c->reallocs   = 0;
407017ab2063SBarry Smith 
40716ad4291fSHong Zhang   C->assembled = PETSC_TRUE;
407217ab2063SBarry Smith 
4073aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4074aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4075eec197d1SBarry Smith 
4076dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&c->imax,m,&c->ilen);CHKERRQ(ierr);
40773bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
407817ab2063SBarry Smith   for (i=0; i<m; i++) {
4079416022c9SBarry Smith     c->imax[i] = a->imax[i];
4080416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
408117ab2063SBarry Smith   }
408217ab2063SBarry Smith 
408317ab2063SBarry Smith   /* allocate the matrix space */
4084f77e22a1SHong Zhang   if (mallocmatspace) {
4085dcca6d9dSJed Brown     ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr);
40863bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
40872205254eSKarl Rupp 
4088f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
40892205254eSKarl Rupp 
409097f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
409117ab2063SBarry Smith     if (m > 0) {
409297f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4093be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4094bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4095be6bf707SBarry Smith       } else {
4096bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
409717ab2063SBarry Smith       }
409808480c60SBarry Smith     }
4099f77e22a1SHong Zhang   }
410017ab2063SBarry Smith 
41016ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4102416022c9SBarry Smith   c->roworiented       = a->roworiented;
4103416022c9SBarry Smith   c->nonew             = a->nonew;
4104416022c9SBarry Smith   if (a->diag) {
4105854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&c->diag);CHKERRQ(ierr);
41063bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
410717ab2063SBarry Smith     for (i=0; i<m; i++) {
4108416022c9SBarry Smith       c->diag[i] = a->diag[i];
410917ab2063SBarry Smith     }
41103a40ed3dSBarry Smith   } else c->diag = 0;
41112205254eSKarl Rupp 
41126ad4291fSHong Zhang   c->solve_work         = 0;
41136ad4291fSHong Zhang   c->saved_values       = 0;
41146ad4291fSHong Zhang   c->idiag              = 0;
411571f1c65dSBarry Smith   c->ssor_work          = 0;
4116a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
4117e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
4118e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
41196ad4291fSHong Zhang 
4120893ad86cSHong Zhang   c->rmax         = a->rmax;
4121416022c9SBarry Smith   c->nz           = a->nz;
41228ed568f8SMatthew G Knepley   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4123273d9f13SBarry Smith   C->preallocated = PETSC_TRUE;
4124754ec7b1SSatish Balay 
41256ad4291fSHong Zhang   c->compressedrow.use   = a->compressedrow.use;
41266ad4291fSHong Zhang   c->compressedrow.nrows = a->compressedrow.nrows;
4127cd6b891eSBarry Smith   if (a->compressedrow.use) {
41286ad4291fSHong Zhang     i    = a->compressedrow.nrows;
4129dcca6d9dSJed Brown     ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr);
41306ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
41316ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
413227ea64f8SHong Zhang   } else {
413327ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
41340298fd71SBarry Smith     c->compressedrow.i      = NULL;
41350298fd71SBarry Smith     c->compressedrow.rindex = NULL;
41366ad4291fSHong Zhang   }
4137ea632784SBarry Smith   c->nonzerorowcnt = a->nonzerorowcnt;
4138e56f5c9eSBarry Smith   C->nonzerostate  = A->nonzerostate;
41394846f1f5SKris Buschelman 
41402205254eSKarl Rupp   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4141140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
41423a40ed3dSBarry Smith   PetscFunctionReturn(0);
414317ab2063SBarry Smith }
414417ab2063SBarry Smith 
41454a2ae208SSatish Balay #undef __FUNCT__
4146b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
4147b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4148b24902e0SBarry Smith {
4149b24902e0SBarry Smith   PetscErrorCode ierr;
4150b24902e0SBarry Smith 
4151b24902e0SBarry Smith   PetscFunctionBegin;
4152ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
41534b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4154cfd3f464SBarry Smith   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
415533d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
4156cfd3f464SBarry Smith   }
4157a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4158f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4159b24902e0SBarry Smith   PetscFunctionReturn(0);
4160b24902e0SBarry Smith }
4161b24902e0SBarry Smith 
4162b24902e0SBarry Smith #undef __FUNCT__
41634a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
4164112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4165fbdbba38SShri Abhyankar {
4166fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4167fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4168fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4169fbdbba38SShri Abhyankar   int            fd;
4170fbdbba38SShri Abhyankar   PetscMPIInt    size;
4171fbdbba38SShri Abhyankar   MPI_Comm       comm;
41723059b6faSBarry Smith   PetscInt       bs = newMat->rmap->bs;
4173fbdbba38SShri Abhyankar 
4174fbdbba38SShri Abhyankar   PetscFunctionBegin;
4175c98fd787SBarry Smith   /* force binary viewer to load .info file if it has not yet done so */
4176c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
4177fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4178fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4179fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4180bbead8a2SBarry Smith 
41810298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
41820298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
4183bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
41843059b6faSBarry Smith   if (bs < 0) bs = 1;
41853059b6faSBarry Smith   ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);
4186bbead8a2SBarry Smith 
4187fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4188fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4189fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4190fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4191fbdbba38SShri Abhyankar 
4192bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4193fbdbba38SShri Abhyankar 
4194fbdbba38SShri Abhyankar   /* read in row lengths */
4195785e854fSJed Brown   ierr = PetscMalloc1(M,&rowlengths);CHKERRQ(ierr);
4196fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4197fbdbba38SShri Abhyankar 
4198fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4199fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
420060e0710aSBarry Smith   if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %dD, sum-row-lengths = %D\n",nz,sum);
4201fbdbba38SShri Abhyankar 
4202fbdbba38SShri Abhyankar   /* set global size if not set already*/
4203f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4204fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4205aabbc4fbSShri Abhyankar   } else {
42069d36ed5fSBarry Smith     /* if sizes and type are already set, check if the matrix  global sizes are correct */
4207fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
42084c5b953cSHong Zhang     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
42094c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
42104c5b953cSHong Zhang     }
421160e0710aSBarry Smith     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);
4212aabbc4fbSShri Abhyankar   }
4213fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4214fbdbba38SShri Abhyankar   a    = (Mat_SeqAIJ*)newMat->data;
4215fbdbba38SShri Abhyankar 
4216fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4217fbdbba38SShri Abhyankar 
4218fbdbba38SShri Abhyankar   /* read in nonzero values */
4219fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4220fbdbba38SShri Abhyankar 
4221fbdbba38SShri Abhyankar   /* set matrix "i" values */
4222fbdbba38SShri Abhyankar   a->i[0] = 0;
4223fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4224fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4225fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4226fbdbba38SShri Abhyankar   }
4227fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4228fbdbba38SShri Abhyankar 
4229fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4230fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4231fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4232fbdbba38SShri Abhyankar }
4233fbdbba38SShri Abhyankar 
4234fbdbba38SShri Abhyankar #undef __FUNCT__
4235b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4236ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
42377264ac53SSatish Balay {
42387264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4239dfbe8321SBarry Smith   PetscErrorCode ierr;
4240eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4241eeffb40dSHong Zhang   PetscInt k;
4242eeffb40dSHong Zhang #endif
42437264ac53SSatish Balay 
42443a40ed3dSBarry Smith   PetscFunctionBegin;
4245bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4246d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4247ca44d042SBarry Smith     *flg = PETSC_FALSE;
4248ca44d042SBarry Smith     PetscFunctionReturn(0);
4249bcd2baecSBarry Smith   }
42507264ac53SSatish Balay 
42517264ac53SSatish Balay   /* if the a->i are the same */
4252d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4253abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
42547264ac53SSatish Balay 
42557264ac53SSatish Balay   /* if a->j are the same */
425697f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4257abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4258bcd2baecSBarry Smith 
4259bcd2baecSBarry Smith   /* if a->a are the same */
4260eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4261eeffb40dSHong Zhang   for (k=0; k<a->nz; k++) {
4262eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4263eeffb40dSHong Zhang       *flg = PETSC_FALSE;
42643a40ed3dSBarry Smith       PetscFunctionReturn(0);
4265eeffb40dSHong Zhang     }
4266eeffb40dSHong Zhang   }
4267eeffb40dSHong Zhang #else
4268eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4269eeffb40dSHong Zhang #endif
4270eeffb40dSHong Zhang   PetscFunctionReturn(0);
42717264ac53SSatish Balay }
427236db0b34SBarry Smith 
42734a2ae208SSatish Balay #undef __FUNCT__
42744a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
427505869f15SSatish Balay /*@
427636db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
427736db0b34SBarry Smith               provided by the user.
427836db0b34SBarry Smith 
4279c75a6043SHong Zhang       Collective on MPI_Comm
428036db0b34SBarry Smith 
428136db0b34SBarry Smith    Input Parameters:
428236db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
428336db0b34SBarry Smith .   m - number of rows
428436db0b34SBarry Smith .   n - number of columns
428536db0b34SBarry Smith .   i - row indices
428636db0b34SBarry Smith .   j - column indices
428736db0b34SBarry Smith -   a - matrix values
428836db0b34SBarry Smith 
428936db0b34SBarry Smith    Output Parameter:
429036db0b34SBarry Smith .   mat - the matrix
429136db0b34SBarry Smith 
429236db0b34SBarry Smith    Level: intermediate
429336db0b34SBarry Smith 
429436db0b34SBarry Smith    Notes:
42950551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4296292fb18eSBarry Smith     once the matrix is destroyed and not before
429736db0b34SBarry Smith 
429836db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
429936db0b34SBarry Smith 
4300bfeeae90SHong Zhang        The i and j indices are 0 based
430136db0b34SBarry Smith 
4302a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4303a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
43048eef79e4SBarry Smith     as shown
4305a4552177SSatish Balay 
43068eef79e4SBarry Smith $        1 0 0
43078eef79e4SBarry Smith $        2 0 3
43088eef79e4SBarry Smith $        4 5 6
43098eef79e4SBarry Smith $
43108eef79e4SBarry Smith $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
43118eef79e4SBarry Smith $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
43128eef79e4SBarry Smith $        v =  {1,2,3,4,5,6}  [size = 6]
4313a4552177SSatish Balay 
43149985e31cSBarry Smith 
431569b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
431636db0b34SBarry Smith 
431736db0b34SBarry Smith @*/
43187087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
431936db0b34SBarry Smith {
4320dfbe8321SBarry Smith   PetscErrorCode ierr;
4321cbcfb4deSHong Zhang   PetscInt       ii;
432236db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4323cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4324cbcfb4deSHong Zhang   PetscInt jj;
4325cbcfb4deSHong Zhang #endif
432636db0b34SBarry Smith 
432736db0b34SBarry Smith   PetscFunctionBegin;
432841096f02SStefano Zampini   if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4329f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4330f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4331a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4332ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4333ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4334ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4335dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&aij->imax,m,&aij->ilen);CHKERRQ(ierr);
4336ab93d7beSBarry Smith 
433736db0b34SBarry Smith   aij->i            = i;
433836db0b34SBarry Smith   aij->j            = j;
433936db0b34SBarry Smith   aij->a            = a;
434036db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
434136db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4342e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4343e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
434436db0b34SBarry Smith 
434536db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
434636db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
43472515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
434860e0710aSBarry 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]);
43499985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4350e32f2f54SBarry 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);
4351e32f2f54SBarry 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);
43529985e31cSBarry Smith     }
435336db0b34SBarry Smith #endif
435436db0b34SBarry Smith   }
43552515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
435636db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
435760e0710aSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
435860e0710aSBarry 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]);
435936db0b34SBarry Smith   }
436036db0b34SBarry Smith #endif
436136db0b34SBarry Smith 
4362b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4363b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
436436db0b34SBarry Smith   PetscFunctionReturn(0);
436536db0b34SBarry Smith }
43668a0b0e6bSVictor Minden #undef __FUNCT__
43678a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
436880ef6e79SMatthew G Knepley /*@C
4369d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
43708a0b0e6bSVictor Minden               provided by the user.
43718a0b0e6bSVictor Minden 
43728a0b0e6bSVictor Minden       Collective on MPI_Comm
43738a0b0e6bSVictor Minden 
43748a0b0e6bSVictor Minden    Input Parameters:
43758a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
43768a0b0e6bSVictor Minden .   m   - number of rows
43778a0b0e6bSVictor Minden .   n   - number of columns
43788a0b0e6bSVictor Minden .   i   - row indices
43798a0b0e6bSVictor Minden .   j   - column indices
43801230e6d1SVictor Minden .   a   - matrix values
43811230e6d1SVictor Minden .   nz  - number of nonzeros
43821230e6d1SVictor Minden -   idx - 0 or 1 based
43838a0b0e6bSVictor Minden 
43848a0b0e6bSVictor Minden    Output Parameter:
43858a0b0e6bSVictor Minden .   mat - the matrix
43868a0b0e6bSVictor Minden 
43878a0b0e6bSVictor Minden    Level: intermediate
43888a0b0e6bSVictor Minden 
43898a0b0e6bSVictor Minden    Notes:
43908a0b0e6bSVictor Minden        The i and j indices are 0 based
43918a0b0e6bSVictor Minden 
43928a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
43938a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
43948a0b0e6bSVictor Minden     as shown:
43958a0b0e6bSVictor Minden 
43968a0b0e6bSVictor Minden         1 0 0
43978a0b0e6bSVictor Minden         2 0 3
43988a0b0e6bSVictor Minden         4 5 6
43998a0b0e6bSVictor Minden 
44008a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
44018a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
44028a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
44038a0b0e6bSVictor Minden 
44048a0b0e6bSVictor Minden 
440569b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
44068a0b0e6bSVictor Minden 
44078a0b0e6bSVictor Minden @*/
44081230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
44098a0b0e6bSVictor Minden {
44108a0b0e6bSVictor Minden   PetscErrorCode ierr;
4411d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
44128a0b0e6bSVictor Minden 
44138a0b0e6bSVictor Minden 
44148a0b0e6bSVictor Minden   PetscFunctionBegin;
44151795a4d1SJed Brown   ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr);
44161230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
4417c8d679ebSHong Zhang     nnz[i[ii] - !!idx] += 1;
44181230e6d1SVictor Minden   }
44198a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
44208a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
44218a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
44221230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
44231230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
44241230e6d1SVictor Minden     if (idx) {
44251230e6d1SVictor Minden       row = i[ii] - 1;
44261230e6d1SVictor Minden       col = j[ii] - 1;
44271230e6d1SVictor Minden     } else {
44281230e6d1SVictor Minden       row = i[ii];
44291230e6d1SVictor Minden       col = j[ii];
44308a0b0e6bSVictor Minden     }
44311230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
44328a0b0e6bSVictor Minden   }
44338a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
44348a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4435d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
44368a0b0e6bSVictor Minden   PetscFunctionReturn(0);
44378a0b0e6bSVictor Minden }
443836db0b34SBarry Smith 
4439cc8ba8e1SBarry Smith #undef __FUNCT__
4440acf2f550SJed Brown #define __FUNCT__ "MatSeqAIJInvalidateDiagonal"
4441acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4442acf2f550SJed Brown {
4443acf2f550SJed Brown   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4444acf2f550SJed Brown   PetscErrorCode ierr;
4445acf2f550SJed Brown 
4446acf2f550SJed Brown   PetscFunctionBegin;
4447acf2f550SJed Brown   a->idiagvalid  = PETSC_FALSE;
4448acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
44492205254eSKarl Rupp 
4450acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4451acf2f550SJed Brown   PetscFunctionReturn(0);
4452acf2f550SJed Brown }
4453acf2f550SJed Brown 
44549c8f2541SHong Zhang #undef __FUNCT__
44559c8f2541SHong Zhang #define __FUNCT__ "MatCreateMPIMatConcatenateSeqMat_SeqAIJ"
44569c8f2541SHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
44579c8f2541SHong Zhang {
44589c8f2541SHong Zhang   PetscErrorCode ierr;
44599c8f2541SHong Zhang 
44609c8f2541SHong Zhang   PetscFunctionBegin;
44619c8f2541SHong Zhang   ierr = MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr);
44629c8f2541SHong Zhang   PetscFunctionReturn(0);
44639c8f2541SHong Zhang }
44649c8f2541SHong Zhang 
446581824310SBarry Smith /*
446653dd7562SDmitry Karpeev  Permute A into C's *local* index space using rowemb,colemb.
446753dd7562SDmitry Karpeev  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
446853dd7562SDmitry Karpeev  of [0,m), colemb is in [0,n).
446953dd7562SDmitry Karpeev  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
447053dd7562SDmitry Karpeev  */
447153dd7562SDmitry Karpeev #undef __FUNCT__
447253dd7562SDmitry Karpeev #define __FUNCT__ "MatSetSeqMat_SeqAIJ"
447353dd7562SDmitry Karpeev PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
447453dd7562SDmitry Karpeev {
447553dd7562SDmitry Karpeev   /* If making this function public, change the error returned in this function away from _PLIB. */
447653dd7562SDmitry Karpeev   PetscErrorCode ierr;
447753dd7562SDmitry Karpeev   Mat_SeqAIJ     *Baij;
447853dd7562SDmitry Karpeev   PetscBool      seqaij;
447953dd7562SDmitry Karpeev   PetscInt       m,n,*nz,i,j,count;
448053dd7562SDmitry Karpeev   PetscScalar    v;
448153dd7562SDmitry Karpeev   const PetscInt *rowindices,*colindices;
448253dd7562SDmitry Karpeev 
448353dd7562SDmitry Karpeev   PetscFunctionBegin;
448453dd7562SDmitry Karpeev   if (!B) PetscFunctionReturn(0);
448553dd7562SDmitry Karpeev   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
448653dd7562SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);CHKERRQ(ierr);
448753dd7562SDmitry Karpeev   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
448853dd7562SDmitry Karpeev   if (rowemb) {
448953dd7562SDmitry Karpeev     ierr = ISGetLocalSize(rowemb,&m);CHKERRQ(ierr);
449053dd7562SDmitry Karpeev     if (m != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Row IS of size %D is incompatible with matrix row size %D",m,B->rmap->n);
449153dd7562SDmitry Karpeev   } else {
44926c4ed002SBarry Smith     if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
449353dd7562SDmitry Karpeev   }
449453dd7562SDmitry Karpeev   if (colemb) {
449553dd7562SDmitry Karpeev     ierr = ISGetLocalSize(colemb,&n);CHKERRQ(ierr);
449653dd7562SDmitry Karpeev     if (n != B->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Diag col IS of size %D is incompatible with input matrix col size %D",n,B->cmap->n);
449753dd7562SDmitry Karpeev   } else {
449853dd7562SDmitry Karpeev     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
449953dd7562SDmitry Karpeev   }
450053dd7562SDmitry Karpeev 
450153dd7562SDmitry Karpeev   Baij = (Mat_SeqAIJ*)(B->data);
450253dd7562SDmitry Karpeev   if (pattern == DIFFERENT_NONZERO_PATTERN) {
450353dd7562SDmitry Karpeev     ierr = PetscMalloc1(B->rmap->n,&nz);CHKERRQ(ierr);
450453dd7562SDmitry Karpeev     for (i=0; i<B->rmap->n; i++) {
450553dd7562SDmitry Karpeev       nz[i] = Baij->i[i+1] - Baij->i[i];
450653dd7562SDmitry Karpeev     }
450753dd7562SDmitry Karpeev     ierr = MatSeqAIJSetPreallocation(C,0,nz);CHKERRQ(ierr);
450853dd7562SDmitry Karpeev     ierr = PetscFree(nz);CHKERRQ(ierr);
450953dd7562SDmitry Karpeev   }
451053dd7562SDmitry Karpeev   if (pattern == SUBSET_NONZERO_PATTERN) {
451153dd7562SDmitry Karpeev     ierr = MatZeroEntries(C);CHKERRQ(ierr);
451253dd7562SDmitry Karpeev   }
451353dd7562SDmitry Karpeev   count = 0;
451453dd7562SDmitry Karpeev   rowindices = NULL;
451553dd7562SDmitry Karpeev   colindices = NULL;
451653dd7562SDmitry Karpeev   if (rowemb) {
451753dd7562SDmitry Karpeev     ierr = ISGetIndices(rowemb,&rowindices);CHKERRQ(ierr);
451853dd7562SDmitry Karpeev   }
451953dd7562SDmitry Karpeev   if (colemb) {
452053dd7562SDmitry Karpeev     ierr = ISGetIndices(colemb,&colindices);CHKERRQ(ierr);
452153dd7562SDmitry Karpeev   }
452253dd7562SDmitry Karpeev   for (i=0; i<B->rmap->n; i++) {
452353dd7562SDmitry Karpeev     PetscInt row;
452453dd7562SDmitry Karpeev     row = i;
452553dd7562SDmitry Karpeev     if (rowindices) row = rowindices[i];
452653dd7562SDmitry Karpeev     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
452753dd7562SDmitry Karpeev       PetscInt col;
452853dd7562SDmitry Karpeev       col  = Baij->j[count];
452953dd7562SDmitry Karpeev       if (colindices) col = colindices[col];
453053dd7562SDmitry Karpeev       v    = Baij->a[count];
453153dd7562SDmitry Karpeev       ierr = MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);CHKERRQ(ierr);
453253dd7562SDmitry Karpeev       ++count;
453353dd7562SDmitry Karpeev     }
453453dd7562SDmitry Karpeev   }
453553dd7562SDmitry Karpeev   /* FIXME: set C's nonzerostate correctly. */
453653dd7562SDmitry Karpeev   /* Assembly for C is necessary. */
453753dd7562SDmitry Karpeev   C->preallocated = PETSC_TRUE;
453853dd7562SDmitry Karpeev   C->assembled     = PETSC_TRUE;
453953dd7562SDmitry Karpeev   C->was_assembled = PETSC_FALSE;
454053dd7562SDmitry Karpeev   PetscFunctionReturn(0);
454153dd7562SDmitry Karpeev }
454253dd7562SDmitry Karpeev 
454353dd7562SDmitry Karpeev 
454453dd7562SDmitry Karpeev /*
454581824310SBarry Smith     Special version for direct calls from Fortran
454681824310SBarry Smith */
4547af0996ceSBarry Smith #include <petsc/private/fortranimpl.h>
454881824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
454981824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
455081824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
455181824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
455281824310SBarry Smith #endif
455381824310SBarry Smith 
455481824310SBarry Smith /* Change these macros so can be used in void function */
455581824310SBarry Smith #undef CHKERRQ
4556ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
455781824310SBarry Smith #undef SETERRQ2
4558e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
45594994cf47SJed Brown #undef SETERRQ3
45604994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
456181824310SBarry Smith 
456281824310SBarry Smith #undef __FUNCT__
456381824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
45648cc058d9SJed Brown PETSC_EXTERN void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
456581824310SBarry Smith {
456681824310SBarry Smith   Mat            A  = *AA;
456781824310SBarry Smith   PetscInt       m  = *mm, n = *nn;
456881824310SBarry Smith   InsertMode     is = *isis;
456981824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
457081824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
457181824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
457281824310SBarry Smith   PetscErrorCode ierr;
457381824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
457454f21887SBarry Smith   MatScalar      *ap,value,*aa;
4575ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4576ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
457781824310SBarry Smith 
457881824310SBarry Smith   PetscFunctionBegin;
45794994cf47SJed Brown   MatCheckPreallocated(A,1);
458081824310SBarry Smith   imax  = a->imax;
458181824310SBarry Smith   ai    = a->i;
458281824310SBarry Smith   ailen = a->ilen;
458381824310SBarry Smith   aj    = a->j;
458481824310SBarry Smith   aa    = a->a;
458581824310SBarry Smith 
458681824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
458781824310SBarry Smith     row = im[k];
458881824310SBarry Smith     if (row < 0) continue;
458981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4590ce94432eSBarry Smith     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
459181824310SBarry Smith #endif
459281824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
459381824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
459481824310SBarry Smith     low  = 0;
459581824310SBarry Smith     high = nrow;
459681824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
459781824310SBarry Smith       if (in[l] < 0) continue;
459881824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4599ce94432eSBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
460081824310SBarry Smith #endif
460181824310SBarry Smith       col = in[l];
46022205254eSKarl Rupp       if (roworiented) value = v[l + k*n];
46032205254eSKarl Rupp       else value = v[k + l*m];
46042205254eSKarl Rupp 
460581824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
460681824310SBarry Smith 
46072205254eSKarl Rupp       if (col <= lastcol) low = 0;
46082205254eSKarl Rupp       else high = nrow;
460981824310SBarry Smith       lastcol = col;
461081824310SBarry Smith       while (high-low > 5) {
461181824310SBarry Smith         t = (low+high)/2;
461281824310SBarry Smith         if (rp[t] > col) high = t;
461381824310SBarry Smith         else             low  = t;
461481824310SBarry Smith       }
461581824310SBarry Smith       for (i=low; i<high; i++) {
461681824310SBarry Smith         if (rp[i] > col) break;
461781824310SBarry Smith         if (rp[i] == col) {
461881824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
461981824310SBarry Smith           else                  ap[i] = value;
462081824310SBarry Smith           goto noinsert;
462181824310SBarry Smith         }
462281824310SBarry Smith       }
462381824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
462481824310SBarry Smith       if (nonew == 1) goto noinsert;
4625ce94432eSBarry Smith       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4626fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
462781824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
462881824310SBarry Smith       /* shift up all the later entries in this row */
462981824310SBarry Smith       for (ii=N; ii>=i; ii--) {
463081824310SBarry Smith         rp[ii+1] = rp[ii];
463181824310SBarry Smith         ap[ii+1] = ap[ii];
463281824310SBarry Smith       }
463381824310SBarry Smith       rp[i] = col;
463481824310SBarry Smith       ap[i] = value;
4635e56f5c9eSBarry Smith       A->nonzerostate++;
463681824310SBarry Smith noinsert:;
463781824310SBarry Smith       low = i + 1;
463881824310SBarry Smith     }
463981824310SBarry Smith     ailen[row] = nrow;
464081824310SBarry Smith   }
464181824310SBarry Smith   PetscFunctionReturnVoid();
464281824310SBarry Smith }
46439f7953f8SBarry Smith 
4644