xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 43e49210fed9b8eabc4a9bdee0eaf8889b283941)
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;
597*43e49210SHong Zhang   if (!a->a) PetscFunctionReturn(0);
598*43e49210SHong Zhang 
599b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
60071c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
60197f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
60260e0710aSBarry Smith     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
603c337ccceSJed Brown       /* Need a dummy value to ensure the dimension of the matrix. */
604d00d2cf4SBarry Smith       nofinalvalue = 1;
605d00d2cf4SBarry Smith     }
606d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
607d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
60877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
609fbfe6fa7SJed Brown #if defined(PETSC_USE_COMPLEX)
610fbfe6fa7SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
611fbfe6fa7SJed Brown #else
61277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
613fbfe6fa7SJed Brown #endif
614b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
61517ab2063SBarry Smith 
61617ab2063SBarry Smith     for (i=0; i<m; i++) {
61760e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
618aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
619a9bf72d8SJed 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);
62017ab2063SBarry Smith #else
62160e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr);
62217ab2063SBarry Smith #endif
62317ab2063SBarry Smith       }
62417ab2063SBarry Smith     }
625d00d2cf4SBarry Smith     if (nofinalvalue) {
626c337ccceSJed Brown #if defined(PETSC_USE_COMPLEX)
627c337ccceSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr);
628c337ccceSJed Brown #else
629d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
630c337ccceSJed Brown #endif
631d00d2cf4SBarry Smith     }
632317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
633fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
634d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
63568369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
636cd155464SBarry Smith     PetscFunctionReturn(0);
637fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
638d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
63944cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
64077431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
64160e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
642aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
64336db0b34SBarry Smith         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 (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
64660e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
64736db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
64860e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
6496831982aSBarry Smith         }
65044cd7ae7SLois Curfman McInnes #else
65160e0710aSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);}
65244cd7ae7SLois Curfman McInnes #endif
65344cd7ae7SLois Curfman McInnes       }
654b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
65544cd7ae7SLois Curfman McInnes     }
656d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
657fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
65897f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
659d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
660854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&sptr);CHKERRQ(ierr);
661496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
662496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
66360e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
664496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
665aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
66636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
667496be53dSLois Curfman McInnes #else
668496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
669496be53dSLois Curfman McInnes #endif
670496be53dSLois Curfman McInnes         }
671496be53dSLois Curfman McInnes       }
672496be53dSLois Curfman McInnes     }
6732e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
67477431f27SBarry Smith     ierr    = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
6752e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
6762205254eSKarl Rupp       if (i+4<m) {
6772205254eSKarl 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);
6782205254eSKarl Rupp       } else if (i+3<m) {
6792205254eSKarl 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);
6802205254eSKarl Rupp       } else if (i+2<m) {
6812205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);
6822205254eSKarl Rupp       } else if (i+1<m) {
6832205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);
6842205254eSKarl Rupp       } else if (i<m) {
6852205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);
6862205254eSKarl Rupp       } else {
6872205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);
6882205254eSKarl Rupp       }
689496be53dSLois Curfman McInnes     }
690b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
691606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
692496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
69360e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
69477431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
695496be53dSLois Curfman McInnes       }
696b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
697496be53dSLois Curfman McInnes     }
698b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
699496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
70060e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
701496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
702aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
70336db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
70460e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
7056831982aSBarry Smith           }
706496be53dSLois Curfman McInnes #else
70760e0710aSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);}
708496be53dSLois Curfman McInnes #endif
709496be53dSLois Curfman McInnes         }
710496be53dSLois Curfman McInnes       }
711b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
712496be53dSLois Curfman McInnes     }
713d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
714fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
71597f1f81fSBarry Smith     PetscInt    cnt = 0,jcnt;
71687828ca2SBarry Smith     PetscScalar value;
71768f1ed48SBarry Smith #if defined(PETSC_USE_COMPLEX)
71868f1ed48SBarry Smith     PetscBool   realonly = PETSC_TRUE;
71968f1ed48SBarry Smith 
72068f1ed48SBarry Smith     for (i=0; i<a->i[m]; i++) {
72168f1ed48SBarry Smith       if (PetscImaginaryPart(a->a[i]) != 0.0) {
72268f1ed48SBarry Smith         realonly = PETSC_FALSE;
72368f1ed48SBarry Smith         break;
72468f1ed48SBarry Smith       }
72568f1ed48SBarry Smith     }
72668f1ed48SBarry Smith #endif
72702594712SBarry Smith 
728d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
72902594712SBarry Smith     for (i=0; i<m; i++) {
73002594712SBarry Smith       jcnt = 0;
731d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
732e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
73302594712SBarry Smith           value = a->a[cnt++];
734e24b481bSBarry Smith           jcnt++;
73502594712SBarry Smith         } else {
73602594712SBarry Smith           value = 0.0;
73702594712SBarry Smith         }
738aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
73968f1ed48SBarry Smith         if (realonly) {
74060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr);
74168f1ed48SBarry Smith         } else {
74260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr);
74368f1ed48SBarry Smith         }
74402594712SBarry Smith #else
74560e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr);
74602594712SBarry Smith #endif
74702594712SBarry Smith       }
748b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
74902594712SBarry Smith     }
750d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7513c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
752150b93efSMatthew G. Knepley     PetscInt fshift=1;
753d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
7543c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
75519303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");CHKERRQ(ierr);
7563c215bfdSMatthew Knepley #else
75719303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");CHKERRQ(ierr);
7583c215bfdSMatthew Knepley #endif
759d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
7603c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
76160e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
7623c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
763a9a0e077SKarl 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);
7643c215bfdSMatthew Knepley #else
765150b93efSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr);
7663c215bfdSMatthew Knepley #endif
7673c215bfdSMatthew Knepley       }
7683c215bfdSMatthew Knepley     }
769d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7703a40ed3dSBarry Smith   } else {
771d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
772d5f3da31SBarry Smith     if (A->factortype) {
77316cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
77416cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
77516cd7e1dSShri Abhyankar         /* L part */
77660e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
77716cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
77816cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
77960e0710aSBarry 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 if (PetscImaginaryPart(a->a[j]) < 0.0) {
7816712e2f1SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
78216cd7e1dSShri Abhyankar           } else {
78360e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
78416cd7e1dSShri Abhyankar           }
78516cd7e1dSShri Abhyankar #else
78660e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
78716cd7e1dSShri Abhyankar #endif
78816cd7e1dSShri Abhyankar         }
78916cd7e1dSShri Abhyankar         /* diagonal */
79016cd7e1dSShri Abhyankar         j = a->diag[i];
79116cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
79216cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
79360e0710aSBarry 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 if (PetscImaginaryPart(a->a[j]) < 0.0) {
7956712e2f1SBarry 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);
79616cd7e1dSShri Abhyankar         } else {
79760e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
79816cd7e1dSShri Abhyankar         }
79916cd7e1dSShri Abhyankar #else
80060e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr);
80116cd7e1dSShri Abhyankar #endif
80216cd7e1dSShri Abhyankar 
80316cd7e1dSShri Abhyankar         /* U part */
80460e0710aSBarry Smith         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
80516cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
80616cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
80760e0710aSBarry 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 if (PetscImaginaryPart(a->a[j]) < 0.0) {
80922ab088eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
81016cd7e1dSShri Abhyankar           } else {
81160e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
81216cd7e1dSShri Abhyankar           }
81316cd7e1dSShri Abhyankar #else
81460e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
81516cd7e1dSShri Abhyankar #endif
81616cd7e1dSShri Abhyankar         }
81716cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
81816cd7e1dSShri Abhyankar       }
81916cd7e1dSShri Abhyankar     } else {
82017ab2063SBarry Smith       for (i=0; i<m; i++) {
82177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
82260e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
823aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
82436db0b34SBarry Smith           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);
82636db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
82760e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
8283a40ed3dSBarry Smith           } else {
82960e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
83017ab2063SBarry Smith           }
83117ab2063SBarry Smith #else
83260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
83317ab2063SBarry Smith #endif
83417ab2063SBarry Smith         }
835b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
83617ab2063SBarry Smith       }
83716cd7e1dSShri Abhyankar     }
838d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
83917ab2063SBarry Smith   }
840b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8413a40ed3dSBarry Smith   PetscFunctionReturn(0);
842416022c9SBarry Smith }
843416022c9SBarry Smith 
8449804daf3SBarry Smith #include <petscdraw.h>
8454a2ae208SSatish Balay #undef __FUNCT__
8464a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
847dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
848416022c9SBarry Smith {
849480ef9eaSBarry Smith   Mat               A  = (Mat) Aa;
850416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
851dfbe8321SBarry Smith   PetscErrorCode    ierr;
852383922c3SLisandro Dalcin   PetscInt          i,j,m = A->rmap->n;
853383922c3SLisandro Dalcin   int               color;
854b05fc000SLisandro Dalcin   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
855b0a32e0cSBarry Smith   PetscViewer       viewer;
856f3ef73ceSBarry Smith   PetscViewerFormat format;
857cddf8d76SBarry Smith 
8583a40ed3dSBarry Smith   PetscFunctionBegin;
859480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
860b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
861b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
862383922c3SLisandro Dalcin 
863416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
8640513a670SBarry Smith 
865fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
866383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
8670513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
868b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
869416022c9SBarry Smith     for (i=0; i<m; i++) {
870cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
871bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
872bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
87336db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
874b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
875cddf8d76SBarry Smith       }
876cddf8d76SBarry Smith     }
877b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
878cddf8d76SBarry Smith     for (i=0; i<m; i++) {
879cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
880bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
881bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
882cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
883b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
884cddf8d76SBarry Smith       }
885cddf8d76SBarry Smith     }
886b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
887cddf8d76SBarry Smith     for (i=0; i<m; i++) {
888cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
889bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
890bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
89136db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
892b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
893416022c9SBarry Smith       }
894416022c9SBarry Smith     }
895383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
8960513a670SBarry Smith   } else {
8970513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
8980513a670SBarry Smith     /* first determine max of all nonzero values */
899b05fc000SLisandro Dalcin     PetscReal minv = 0.0, maxv = 0.0;
900383922c3SLisandro Dalcin     PetscInt  nz = a->nz, count = 0;
901b0a32e0cSBarry Smith     PetscDraw popup;
9020513a670SBarry Smith 
9030513a670SBarry Smith     for (i=0; i<nz; i++) {
9040513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
9050513a670SBarry Smith     }
906383922c3SLisandro Dalcin     if (minv >= maxv) maxv = minv + PETSC_SMALL;
907b0a32e0cSBarry Smith     ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
90845f3bb6eSLisandro Dalcin     ierr = PetscDrawScalePopup(popup,minv,maxv);CHKERRQ(ierr);
909383922c3SLisandro Dalcin 
910383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
9110513a670SBarry Smith     for (i=0; i<m; i++) {
912383922c3SLisandro Dalcin       y_l = m - i - 1.0;
913383922c3SLisandro Dalcin       y_r = y_l + 1.0;
914bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
915383922c3SLisandro Dalcin         x_l = a->j[j];
916383922c3SLisandro Dalcin         x_r = x_l + 1.0;
917b05fc000SLisandro Dalcin         color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
918b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
9190513a670SBarry Smith         count++;
9200513a670SBarry Smith       }
9210513a670SBarry Smith     }
922383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
9230513a670SBarry Smith   }
924480ef9eaSBarry Smith   PetscFunctionReturn(0);
925480ef9eaSBarry Smith }
926cddf8d76SBarry Smith 
9279804daf3SBarry Smith #include <petscdraw.h>
9284a2ae208SSatish Balay #undef __FUNCT__
9294a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
930dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
931480ef9eaSBarry Smith {
932dfbe8321SBarry Smith   PetscErrorCode ierr;
933b0a32e0cSBarry Smith   PetscDraw      draw;
93436db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
935ace3abfcSBarry Smith   PetscBool      isnull;
936480ef9eaSBarry Smith 
937480ef9eaSBarry Smith   PetscFunctionBegin;
938b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
939b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
940480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
941480ef9eaSBarry Smith 
942d0f46423SBarry Smith   xr   = A->cmap->n; yr  = A->rmap->n; h = yr/10.0; w = xr/10.0;
943480ef9eaSBarry Smith   xr  += w;          yr += h;         xl = -w;     yl = -h;
944b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
945832b7cebSLisandro Dalcin   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
946b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
9470298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
948832b7cebSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
9493a40ed3dSBarry Smith   PetscFunctionReturn(0);
950416022c9SBarry Smith }
951416022c9SBarry Smith 
9524a2ae208SSatish Balay #undef __FUNCT__
9534a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
954dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
955416022c9SBarry Smith {
956dfbe8321SBarry Smith   PetscErrorCode ierr;
957ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
958416022c9SBarry Smith 
9593a40ed3dSBarry Smith   PetscFunctionBegin;
960251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
961251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
962251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
963c45a1595SBarry Smith   if (iascii) {
9643a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
9650f5bd95cSBarry Smith   } else if (isbinary) {
9663a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
9670f5bd95cSBarry Smith   } else if (isdraw) {
9683a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
96911aeaf0aSBarry Smith   }
9704108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
9713a40ed3dSBarry Smith   PetscFunctionReturn(0);
97217ab2063SBarry Smith }
97319bcc07fSBarry Smith 
9744a2ae208SSatish Balay #undef __FUNCT__
9754a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
976dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
97717ab2063SBarry Smith {
978416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9796849ba73SBarry Smith   PetscErrorCode ierr;
98097f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
981d0f46423SBarry Smith   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
98254f21887SBarry Smith   MatScalar      *aa    = a->a,*ap;
9833447b6efSHong Zhang   PetscReal      ratio  = 0.6;
98417ab2063SBarry Smith 
9853a40ed3dSBarry Smith   PetscFunctionBegin;
9863a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
98717ab2063SBarry Smith 
98843ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
98917ab2063SBarry Smith   for (i=1; i<m; i++) {
990416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
99117ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
99294a9d846SBarry Smith     rmax    = PetscMax(rmax,ailen[i]);
99317ab2063SBarry Smith     if (fshift) {
994bfeeae90SHong Zhang       ip = aj + ai[i];
995bfeeae90SHong Zhang       ap = aa + ai[i];
99617ab2063SBarry Smith       N  = ailen[i];
99717ab2063SBarry Smith       for (j=0; j<N; j++) {
99817ab2063SBarry Smith         ip[j-fshift] = ip[j];
99917ab2063SBarry Smith         ap[j-fshift] = ap[j];
100017ab2063SBarry Smith       }
100117ab2063SBarry Smith     }
100217ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
100317ab2063SBarry Smith   }
100417ab2063SBarry Smith   if (m) {
100517ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
100617ab2063SBarry Smith     ai[m]   = ai[m-1] + ailen[m-1];
100717ab2063SBarry Smith   }
10087b083b7cSBarry Smith 
100917ab2063SBarry Smith   /* reset ilen and imax for each row */
10107b083b7cSBarry Smith   a->nonzerorowcnt = 0;
101117ab2063SBarry Smith   for (i=0; i<m; i++) {
101217ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
10137b083b7cSBarry Smith     a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
101417ab2063SBarry Smith   }
1015bfeeae90SHong Zhang   a->nz = ai[m];
101665e19b50SBarry 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);
101717ab2063SBarry Smith 
101809f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1019d0f46423SBarry 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);
1020ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
1021ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
10222205254eSKarl Rupp 
10238e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
1024dd5f02e7SSatish Balay   a->reallocs         = 0;
10256712e2f1SBarry Smith   A->info.nz_unneeded = (PetscReal)fshift;
102636db0b34SBarry Smith   a->rmax             = rmax;
10274e220ebcSLois Curfman McInnes 
102811e456e1SBarry Smith   ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
10294108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
1030acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10313a40ed3dSBarry Smith   PetscFunctionReturn(0);
103217ab2063SBarry Smith }
103317ab2063SBarry Smith 
10344a2ae208SSatish Balay #undef __FUNCT__
103599cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
103699cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
103799cafbc1SBarry Smith {
103899cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
103999cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
104054f21887SBarry Smith   MatScalar      *aa = a->a;
1041acf2f550SJed Brown   PetscErrorCode ierr;
104299cafbc1SBarry Smith 
104399cafbc1SBarry Smith   PetscFunctionBegin;
104499cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1045acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
104699cafbc1SBarry Smith   PetscFunctionReturn(0);
104799cafbc1SBarry Smith }
104899cafbc1SBarry Smith 
104999cafbc1SBarry Smith #undef __FUNCT__
105099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
105199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
105299cafbc1SBarry Smith {
105399cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
105499cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
105554f21887SBarry Smith   MatScalar      *aa = a->a;
1056acf2f550SJed Brown   PetscErrorCode ierr;
105799cafbc1SBarry Smith 
105899cafbc1SBarry Smith   PetscFunctionBegin;
105999cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1060acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
106199cafbc1SBarry Smith   PetscFunctionReturn(0);
106299cafbc1SBarry Smith }
106399cafbc1SBarry Smith 
106499cafbc1SBarry Smith #undef __FUNCT__
10654a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
1066dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
106717ab2063SBarry Smith {
1068416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1069dfbe8321SBarry Smith   PetscErrorCode ierr;
10703a40ed3dSBarry Smith 
10713a40ed3dSBarry Smith   PetscFunctionBegin;
1072d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
1073acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10743a40ed3dSBarry Smith   PetscFunctionReturn(0);
107517ab2063SBarry Smith }
1076416022c9SBarry Smith 
10774a2ae208SSatish Balay #undef __FUNCT__
10784a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
1079dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
108017ab2063SBarry Smith {
1081416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1082dfbe8321SBarry Smith   PetscErrorCode ierr;
1083d5d45c9bSBarry Smith 
10843a40ed3dSBarry Smith   PetscFunctionBegin;
1085aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1086d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
108717ab2063SBarry Smith #endif
1088e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
10896bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
10906bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
109105b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
1092d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
109305b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
109471f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
109505b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
10966bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
109705b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
10986bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
1099cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
11000b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
1101a30b2313SHong Zhang 
11024108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
1103bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
1104901853e0SKris Buschelman 
1105dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
1106bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
1107bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1108bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1109bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr);
1110bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr);
1111bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr);
1112af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
1113af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);CHKERRQ(ierr);
1114af8000cdSHong Zhang #endif
1115b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);CHKERRQ(ierr);
1116bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1117bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1118bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1119bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr);
11203a40ed3dSBarry Smith   PetscFunctionReturn(0);
112117ab2063SBarry Smith }
112217ab2063SBarry Smith 
11234a2ae208SSatish Balay #undef __FUNCT__
11244a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
1125ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
112617ab2063SBarry Smith {
1127416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11284846f1f5SKris Buschelman   PetscErrorCode ierr;
11293a40ed3dSBarry Smith 
11303a40ed3dSBarry Smith   PetscFunctionBegin;
1131a65d3064SKris Buschelman   switch (op) {
1132a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
11334e0d8c25SBarry Smith     a->roworiented = flg;
1134a65d3064SKris Buschelman     break;
1135a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1136a9817697SBarry Smith     a->keepnonzeropattern = flg;
1137a65d3064SKris Buschelman     break;
1138512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1139512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1140a65d3064SKris Buschelman     break;
1141a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
11424e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1143a65d3064SKris Buschelman     break;
1144a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
11454e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1146a65d3064SKris Buschelman     break;
114728b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
114828b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
114928b2fa4aSMatthew Knepley     break;
1150a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11514e0d8c25SBarry Smith     a->ignorezeroentries = flg;
11520df259c2SBarry Smith     break;
11533d472b54SHong Zhang   case MAT_SPD:
1154b1646e73SJed Brown   case MAT_SYMMETRIC:
1155b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1156b1646e73SJed Brown   case MAT_HERMITIAN:
1157b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
11585021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
11595021d80fSJed Brown     break;
11604e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1161a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1162a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1163290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1164a65d3064SKris Buschelman     break;
1165b87ac2d8SJed Brown   case MAT_USE_INODES:
1166b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1167b87ac2d8SJed Brown     break;
1168a65d3064SKris Buschelman   default:
1169e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1170a65d3064SKris Buschelman   }
11714108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
11723a40ed3dSBarry Smith   PetscFunctionReturn(0);
117317ab2063SBarry Smith }
117417ab2063SBarry Smith 
11754a2ae208SSatish Balay #undef __FUNCT__
11764a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1177dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
117817ab2063SBarry Smith {
1179416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11806849ba73SBarry Smith   PetscErrorCode ierr;
1181d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
118235e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
118317ab2063SBarry Smith 
11843a40ed3dSBarry Smith   PetscFunctionBegin;
1185d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1186e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
118735e7444dSHong Zhang 
1188d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1189d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
119035e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
11912c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
119235e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
119335e7444dSHong Zhang     PetscFunctionReturn(0);
119435e7444dSHong Zhang   }
119535e7444dSHong Zhang 
11962dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
11971ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
119835e7444dSHong Zhang   for (i=0; i<n; i++) {
119935e7444dSHong Zhang     nz = ai[i+1] - ai[i];
12002f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
120135e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++) {
120235e7444dSHong Zhang       if (aj[j] == i) {
120335e7444dSHong Zhang         x[i] = aa[j];
120417ab2063SBarry Smith         break;
120517ab2063SBarry Smith       }
120617ab2063SBarry Smith     }
120717ab2063SBarry Smith   }
12081ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
12093a40ed3dSBarry Smith   PetscFunctionReturn(0);
121017ab2063SBarry Smith }
121117ab2063SBarry Smith 
1212c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
12134a2ae208SSatish Balay #undef __FUNCT__
12144a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1215dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
121617ab2063SBarry Smith {
1217416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1218d9ca1df4SBarry Smith   PetscScalar       *y;
1219d9ca1df4SBarry Smith   const PetscScalar *x;
1220dfbe8321SBarry Smith   PetscErrorCode    ierr;
1221d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
12225c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1223d9ca1df4SBarry Smith   const MatScalar   *v;
1224a77337e4SBarry Smith   PetscScalar       alpha;
1225d9ca1df4SBarry Smith   PetscInt          n,i,j;
1226d9ca1df4SBarry Smith   const PetscInt    *idx,*ii,*ridx=NULL;
12273447b6efSHong Zhang   Mat_CompressedRow cprow    = a->compressedrow;
1228ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
12295c897100SBarry Smith #endif
123017ab2063SBarry Smith 
12313a40ed3dSBarry Smith   PetscFunctionBegin;
12322e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
1233d9ca1df4SBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12341ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
12355c897100SBarry Smith 
12365c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1237bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
12385c897100SBarry Smith #else
12393447b6efSHong Zhang   if (usecprow) {
12403447b6efSHong Zhang     m    = cprow.nrows;
12413447b6efSHong Zhang     ii   = cprow.i;
12427b2bb3b9SHong Zhang     ridx = cprow.rindex;
12433447b6efSHong Zhang   } else {
12443447b6efSHong Zhang     ii = a->i;
12453447b6efSHong Zhang   }
124617ab2063SBarry Smith   for (i=0; i<m; i++) {
12473447b6efSHong Zhang     idx = a->j + ii[i];
12483447b6efSHong Zhang     v   = a->a + ii[i];
12493447b6efSHong Zhang     n   = ii[i+1] - ii[i];
12503447b6efSHong Zhang     if (usecprow) {
12517b2bb3b9SHong Zhang       alpha = x[ridx[i]];
12523447b6efSHong Zhang     } else {
125317ab2063SBarry Smith       alpha = x[i];
12543447b6efSHong Zhang     }
125504fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
125617ab2063SBarry Smith   }
12575c897100SBarry Smith #endif
1258dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1259d9ca1df4SBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12601ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12613a40ed3dSBarry Smith   PetscFunctionReturn(0);
126217ab2063SBarry Smith }
126317ab2063SBarry Smith 
12644a2ae208SSatish Balay #undef __FUNCT__
12655c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1266dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
12675c897100SBarry Smith {
1268dfbe8321SBarry Smith   PetscErrorCode ierr;
12695c897100SBarry Smith 
12705c897100SBarry Smith   PetscFunctionBegin;
1271170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
12725c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
12735c897100SBarry Smith   PetscFunctionReturn(0);
12745c897100SBarry Smith }
12755c897100SBarry Smith 
1276c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
127778b84d54SShri Abhyankar 
12785c897100SBarry Smith #undef __FUNCT__
12794a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1280dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
128117ab2063SBarry Smith {
1282416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1283d9fead3dSBarry Smith   PetscScalar       *y;
128454f21887SBarry Smith   const PetscScalar *x;
128554f21887SBarry Smith   const MatScalar   *aa;
1286dfbe8321SBarry Smith   PetscErrorCode    ierr;
1287003131ecSBarry Smith   PetscInt          m=A->rmap->n;
12880298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
12897b083b7cSBarry Smith   PetscInt          n,i;
1290362ced78SSatish Balay   PetscScalar       sum;
1291ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
129217ab2063SBarry Smith 
1293b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
129497952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1295fee21e36SBarry Smith #endif
1296fee21e36SBarry Smith 
12973a40ed3dSBarry Smith   PetscFunctionBegin;
12983649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12991ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1300416022c9SBarry Smith   ii   = a->i;
13014eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
13024f390cb1SBarry Smith     ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr);
130397952fefSHong Zhang     m    = a->compressedrow.nrows;
130497952fefSHong Zhang     ii   = a->compressedrow.i;
130597952fefSHong Zhang     ridx = a->compressedrow.rindex;
130697952fefSHong Zhang     for (i=0; i<m; i++) {
130797952fefSHong Zhang       n           = ii[i+1] - ii[i];
130897952fefSHong Zhang       aj          = a->j + ii[i];
130997952fefSHong Zhang       aa          = a->a + ii[i];
131097952fefSHong Zhang       sum         = 0.0;
1311003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1312003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
131397952fefSHong Zhang       y[*ridx++] = sum;
131497952fefSHong Zhang     }
131597952fefSHong Zhang   } else { /* do not use compressed row format */
1316b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
13173d3eaba7SBarry Smith     aj   = a->j;
13183d3eaba7SBarry Smith     aa   = a->a;
1319b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1320b05257ddSBarry Smith #else
132117ab2063SBarry Smith     for (i=0; i<m; i++) {
1322003131ecSBarry Smith       n           = ii[i+1] - ii[i];
1323003131ecSBarry Smith       aj          = a->j + ii[i];
1324003131ecSBarry Smith       aa          = a->a + ii[i];
132517ab2063SBarry Smith       sum         = 0.0;
1326003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
132717ab2063SBarry Smith       y[i] = sum;
132817ab2063SBarry Smith     }
13298d195f9aSBarry Smith #endif
1330b05257ddSBarry Smith   }
13317b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
13323649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13331ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13343a40ed3dSBarry Smith   PetscFunctionReturn(0);
133517ab2063SBarry Smith }
133617ab2063SBarry Smith 
1337b434eb95SMatthew G. Knepley #undef __FUNCT__
1338b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultMax_SeqAIJ"
1339b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1340b434eb95SMatthew G. Knepley {
1341b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1342b434eb95SMatthew G. Knepley   PetscScalar       *y;
1343b434eb95SMatthew G. Knepley   const PetscScalar *x;
1344b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1345b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1346b434eb95SMatthew G. Knepley   PetscInt          m=A->rmap->n;
1347b434eb95SMatthew G. Knepley   const PetscInt    *aj,*ii,*ridx=NULL;
1348b434eb95SMatthew G. Knepley   PetscInt          n,i,nonzerorow=0;
1349b434eb95SMatthew G. Knepley   PetscScalar       sum;
1350b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1351b434eb95SMatthew G. Knepley 
1352b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1353b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa)
1354b434eb95SMatthew G. Knepley #endif
1355b434eb95SMatthew G. Knepley 
1356b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1357b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1358b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1359b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1360b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1361b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1362b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1363b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1364b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1365b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1366b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1367b434eb95SMatthew G. Knepley       sum         = 0.0;
1368b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1369b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1370b434eb95SMatthew G. Knepley       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1371b434eb95SMatthew G. Knepley       y[*ridx++] = sum;
1372b434eb95SMatthew G. Knepley     }
1373b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
13743d3eaba7SBarry Smith     ii = a->i;
1375b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1376b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1377b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1378b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1379b434eb95SMatthew G. Knepley       sum         = 0.0;
1380b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1381b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1382b434eb95SMatthew G. Knepley       y[i] = sum;
1383b434eb95SMatthew G. Knepley     }
1384b434eb95SMatthew G. Knepley   }
1385b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1386b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1387b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1388b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1389b434eb95SMatthew G. Knepley }
1390b434eb95SMatthew G. Knepley 
1391b434eb95SMatthew G. Knepley #undef __FUNCT__
1392b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultAddMax_SeqAIJ"
1393b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1394b434eb95SMatthew G. Knepley {
1395b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1396b434eb95SMatthew G. Knepley   PetscScalar       *y,*z;
1397b434eb95SMatthew G. Knepley   const PetscScalar *x;
1398b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1399b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1400b434eb95SMatthew G. Knepley   PetscInt          m = A->rmap->n,*aj,*ii;
1401b434eb95SMatthew G. Knepley   PetscInt          n,i,*ridx=NULL;
1402b434eb95SMatthew G. Knepley   PetscScalar       sum;
1403b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1404b434eb95SMatthew G. Knepley 
1405b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1406b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1407d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1408b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1409b434eb95SMatthew G. Knepley     if (zz != yy) {
1410b434eb95SMatthew G. Knepley       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1411b434eb95SMatthew G. Knepley     }
1412b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1413b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1414b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1415b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1416b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1417b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1418b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1419b434eb95SMatthew G. Knepley       sum = y[*ridx];
1420b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1421b434eb95SMatthew G. Knepley       z[*ridx++] = sum;
1422b434eb95SMatthew G. Knepley     }
1423b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
14243d3eaba7SBarry Smith     ii = a->i;
1425b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1426b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1427b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1428b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1429b434eb95SMatthew G. Knepley       sum = y[i];
1430b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1431b434eb95SMatthew G. Knepley       z[i] = sum;
1432b434eb95SMatthew G. Knepley     }
1433b434eb95SMatthew G. Knepley   }
1434b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1435b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1436d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1437b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1438b434eb95SMatthew G. Knepley }
1439b434eb95SMatthew G. Knepley 
1440c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
14414a2ae208SSatish Balay #undef __FUNCT__
14424a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1443dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
144417ab2063SBarry Smith {
1445416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1446f15663dcSBarry Smith   PetscScalar       *y,*z;
1447f15663dcSBarry Smith   const PetscScalar *x;
144854f21887SBarry Smith   const MatScalar   *aa;
1449dfbe8321SBarry Smith   PetscErrorCode    ierr;
1450d9ca1df4SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
1451d9ca1df4SBarry Smith   PetscInt          m = A->rmap->n,n,i;
1452362ced78SSatish Balay   PetscScalar       sum;
1453ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
14549ea0dfa2SSatish Balay 
14553a40ed3dSBarry Smith   PetscFunctionBegin;
1456f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1457d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
14584eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
14594eb6d288SHong Zhang     if (zz != yy) {
14604eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
14614eb6d288SHong Zhang     }
146297952fefSHong Zhang     m    = a->compressedrow.nrows;
146397952fefSHong Zhang     ii   = a->compressedrow.i;
146497952fefSHong Zhang     ridx = a->compressedrow.rindex;
146597952fefSHong Zhang     for (i=0; i<m; i++) {
146697952fefSHong Zhang       n   = ii[i+1] - ii[i];
146797952fefSHong Zhang       aj  = a->j + ii[i];
146897952fefSHong Zhang       aa  = a->a + ii[i];
146997952fefSHong Zhang       sum = y[*ridx];
1470f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
147197952fefSHong Zhang       z[*ridx++] = sum;
147297952fefSHong Zhang     }
147397952fefSHong Zhang   } else { /* do not use compressed row format */
14743d3eaba7SBarry Smith     ii = a->i;
1475f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
14763d3eaba7SBarry Smith     aj = a->j;
14773d3eaba7SBarry Smith     aa = a->a;
1478f15663dcSBarry Smith     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1479f15663dcSBarry Smith #else
148017ab2063SBarry Smith     for (i=0; i<m; i++) {
1481f15663dcSBarry Smith       n   = ii[i+1] - ii[i];
1482f15663dcSBarry Smith       aj  = a->j + ii[i];
1483f15663dcSBarry Smith       aa  = a->a + ii[i];
148417ab2063SBarry Smith       sum = y[i];
1485f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
148617ab2063SBarry Smith       z[i] = sum;
148717ab2063SBarry Smith     }
148802ab625aSSatish Balay #endif
1489f15663dcSBarry Smith   }
1490dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1491f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1492d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
14938154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
14946b375ea7SVictor Minden   /*
1495918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1496918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1497918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
14986b375ea7SVictor Minden   */
1499918e98c3SVictor Minden #endif
15003a40ed3dSBarry Smith   PetscFunctionReturn(0);
150117ab2063SBarry Smith }
150217ab2063SBarry Smith 
150317ab2063SBarry Smith /*
150417ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
150517ab2063SBarry Smith */
15064a2ae208SSatish Balay #undef __FUNCT__
15074a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1508dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
150917ab2063SBarry Smith {
1510416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15116849ba73SBarry Smith   PetscErrorCode ierr;
1512d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
151317ab2063SBarry Smith 
15143a40ed3dSBarry Smith   PetscFunctionBegin;
151509f38230SBarry Smith   if (!a->diag) {
1516785e854fSJed Brown     ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr);
15173bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr);
151809f38230SBarry Smith   }
1519d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
152009f38230SBarry Smith     a->diag[i] = a->i[i+1];
1521bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1522bfeeae90SHong Zhang       if (a->j[j] == i) {
152309f38230SBarry Smith         a->diag[i] = j;
152417ab2063SBarry Smith         break;
152517ab2063SBarry Smith       }
152617ab2063SBarry Smith     }
152717ab2063SBarry Smith   }
15283a40ed3dSBarry Smith   PetscFunctionReturn(0);
152917ab2063SBarry Smith }
153017ab2063SBarry Smith 
1531be5855fcSBarry Smith /*
1532be5855fcSBarry Smith      Checks for missing diagonals
1533be5855fcSBarry Smith */
15344a2ae208SSatish Balay #undef __FUNCT__
15354a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1536ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1537be5855fcSBarry Smith {
1538be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
15397734d3b5SMatthew G. Knepley   PetscInt   *diag,*ii = a->i,i;
1540be5855fcSBarry Smith 
1541be5855fcSBarry Smith   PetscFunctionBegin;
154209f38230SBarry Smith   *missing = PETSC_FALSE;
15437734d3b5SMatthew G. Knepley   if (A->rmap->n > 0 && !ii) {
154409f38230SBarry Smith     *missing = PETSC_TRUE;
154509f38230SBarry Smith     if (d) *d = 0;
1546955c1f14SBarry Smith     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
154709f38230SBarry Smith   } else {
1548f1e2ffcdSBarry Smith     diag = a->diag;
1549d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
15507734d3b5SMatthew G. Knepley       if (diag[i] >= ii[i+1]) {
155109f38230SBarry Smith         *missing = PETSC_TRUE;
155209f38230SBarry Smith         if (d) *d = i;
1553955c1f14SBarry Smith         PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
1554358d2f5dSShri Abhyankar         break;
155509f38230SBarry Smith       }
1556be5855fcSBarry Smith     }
1557be5855fcSBarry Smith   }
1558be5855fcSBarry Smith   PetscFunctionReturn(0);
1559be5855fcSBarry Smith }
1560be5855fcSBarry Smith 
156171f1c65dSBarry Smith #undef __FUNCT__
156271f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
1563422a814eSBarry Smith /*
1564422a814eSBarry Smith    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1565422a814eSBarry Smith */
15667087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
156771f1c65dSBarry Smith {
156871f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
156971f1c65dSBarry Smith   PetscErrorCode ierr;
1570d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
157154f21887SBarry Smith   MatScalar      *v = a->a;
157254f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
157371f1c65dSBarry Smith 
157471f1c65dSBarry Smith   PetscFunctionBegin;
157571f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
157671f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
157771f1c65dSBarry Smith   diag = a->diag;
157871f1c65dSBarry Smith   if (!a->idiag) {
1579dcca6d9dSJed Brown     ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr);
15803bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
158171f1c65dSBarry Smith     v    = a->a;
158271f1c65dSBarry Smith   }
158371f1c65dSBarry Smith   mdiag = a->mdiag;
158471f1c65dSBarry Smith   idiag = a->idiag;
158571f1c65dSBarry Smith 
1586422a814eSBarry Smith   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
158771f1c65dSBarry Smith     for (i=0; i<m; i++) {
158871f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1589899639b0SHong Zhang       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1590899639b0SHong Zhang         if (PetscRealPart(fshift)) {
1591899639b0SHong Zhang           ierr = PetscInfo1(A,"Zero diagonal on row %D\n",i);CHKERRQ(ierr);
1592899639b0SHong Zhang           A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1593899639b0SHong Zhang         } else {
1594899639b0SHong Zhang           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1595899639b0SHong Zhang         }
1596899639b0SHong Zhang       }
159771f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
159871f1c65dSBarry Smith     }
159971f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
160071f1c65dSBarry Smith   } else {
160171f1c65dSBarry Smith     for (i=0; i<m; i++) {
160271f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
160371f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
160471f1c65dSBarry Smith     }
1605dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
160671f1c65dSBarry Smith   }
160771f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
160871f1c65dSBarry Smith   PetscFunctionReturn(0);
160971f1c65dSBarry Smith }
161071f1c65dSBarry Smith 
1611c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
16124a2ae208SSatish Balay #undef __FUNCT__
161341f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
161441f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
161517ab2063SBarry Smith {
1616416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1617e6d1f457SBarry Smith   PetscScalar       *x,d,sum,*t,scale;
16183d3eaba7SBarry Smith   const MatScalar   *v,*idiag=0,*mdiag;
161954f21887SBarry Smith   const PetscScalar *b, *bs,*xb, *ts;
1620dfbe8321SBarry Smith   PetscErrorCode    ierr;
16213d3eaba7SBarry Smith   PetscInt          n,m = A->rmap->n,i;
162297f1f81fSBarry Smith   const PetscInt    *idx,*diag;
162317ab2063SBarry Smith 
16243a40ed3dSBarry Smith   PetscFunctionBegin;
1625b965ef7fSBarry Smith   its = its*lits;
162691723122SBarry Smith 
162771f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
162871f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
162971f1c65dSBarry Smith   a->fshift = fshift;
163071f1c65dSBarry Smith   a->omega  = omega;
1631ed480e8bSBarry Smith 
163271f1c65dSBarry Smith   diag  = a->diag;
163371f1c65dSBarry Smith   t     = a->ssor_work;
1634ed480e8bSBarry Smith   idiag = a->idiag;
163571f1c65dSBarry Smith   mdiag = a->mdiag;
1636ed480e8bSBarry Smith 
16371ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
16383649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1639ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
164017ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
164117ab2063SBarry Smith     /* apply (U + D/omega) to the vector */
1642ed480e8bSBarry Smith     bs = b;
164317ab2063SBarry Smith     for (i=0; i<m; i++) {
164471f1c65dSBarry Smith       d   = fshift + mdiag[i];
1645416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1646ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1647ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
164817ab2063SBarry Smith       sum = b[i]*d/omega;
1649003131ecSBarry Smith       PetscSparseDensePlusDot(sum,bs,v,idx,n);
165017ab2063SBarry Smith       x[i] = sum;
165117ab2063SBarry Smith     }
16521ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16533649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1654efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
16553a40ed3dSBarry Smith     PetscFunctionReturn(0);
165617ab2063SBarry Smith   }
1657c783ea89SBarry Smith 
16582205254eSKarl Rupp   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
16592205254eSKarl Rupp   else if (flag & SOR_EISENSTAT) {
166017ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1661887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
166217ab2063SBarry Smith 
166317ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
166417ab2063SBarry Smith 
1665887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
166617ab2063SBarry Smith     */
166717ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
166817ab2063SBarry Smith 
166917ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
167017ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1671416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1672ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1673ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
167417ab2063SBarry Smith       sum = b[i];
1675e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1676ed480e8bSBarry Smith       x[i] = sum*idiag[i];
167717ab2063SBarry Smith     }
167817ab2063SBarry Smith 
167917ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1680416022c9SBarry Smith     v = a->a;
16812205254eSKarl Rupp     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
168217ab2063SBarry Smith 
168317ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1684ed480e8bSBarry Smith     ts   = t;
1685416022c9SBarry Smith     diag = a->diag;
168617ab2063SBarry Smith     for (i=0; i<m; i++) {
1687416022c9SBarry Smith       n   = diag[i] - a->i[i];
1688ed480e8bSBarry Smith       idx = a->j + a->i[i];
1689ed480e8bSBarry Smith       v   = a->a + a->i[i];
169017ab2063SBarry Smith       sum = t[i];
1691003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1692ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1693733d66baSBarry Smith       /*  x = x + t */
1694733d66baSBarry Smith       x[i] += t[i];
169517ab2063SBarry Smith     }
169617ab2063SBarry Smith 
1697dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
16981ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16993649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
17003a40ed3dSBarry Smith     PetscFunctionReturn(0);
170117ab2063SBarry Smith   }
170217ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
170317ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
170417ab2063SBarry Smith       for (i=0; i<m; i++) {
1705416022c9SBarry Smith         n   = diag[i] - a->i[i];
1706ed480e8bSBarry Smith         idx = a->j + a->i[i];
1707ed480e8bSBarry Smith         v   = a->a + a->i[i];
170817ab2063SBarry Smith         sum = b[i];
1709e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
17105c99c7daSBarry Smith         t[i] = sum;
1711ed480e8bSBarry Smith         x[i] = sum*idiag[i];
171217ab2063SBarry Smith       }
17135c99c7daSBarry Smith       xb   = t;
1714efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17153a40ed3dSBarry Smith     } else xb = b;
171617ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
171717ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1718416022c9SBarry Smith         n   = a->i[i+1] - diag[i] - 1;
1719ed480e8bSBarry Smith         idx = a->j + diag[i] + 1;
1720ed480e8bSBarry Smith         v   = a->a + diag[i] + 1;
172117ab2063SBarry Smith         sum = xb[i];
1722e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
17235c99c7daSBarry Smith         if (xb == b) {
1724ed480e8bSBarry Smith           x[i] = sum*idiag[i];
17255c99c7daSBarry Smith         } else {
1726b19a5dc2SMark Adams           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
172717ab2063SBarry Smith         }
17285c99c7daSBarry Smith       }
1729b19a5dc2SMark Adams       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
173017ab2063SBarry Smith     }
173117ab2063SBarry Smith     its--;
173217ab2063SBarry Smith   }
173317ab2063SBarry Smith   while (its--) {
173417ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
173517ab2063SBarry Smith       for (i=0; i<m; i++) {
1736b19a5dc2SMark Adams         /* lower */
1737b19a5dc2SMark Adams         n   = diag[i] - a->i[i];
1738ed480e8bSBarry Smith         idx = a->j + a->i[i];
1739ed480e8bSBarry Smith         v   = a->a + a->i[i];
174017ab2063SBarry Smith         sum = b[i];
1741e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1742b19a5dc2SMark Adams         t[i] = sum;             /* save application of the lower-triangular part */
1743b19a5dc2SMark Adams         /* upper */
1744b19a5dc2SMark Adams         n   = a->i[i+1] - diag[i] - 1;
1745b19a5dc2SMark Adams         idx = a->j + diag[i] + 1;
1746b19a5dc2SMark Adams         v   = a->a + diag[i] + 1;
1747b19a5dc2SMark Adams         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1748b19a5dc2SMark Adams         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
174917ab2063SBarry Smith       }
1750b19a5dc2SMark Adams       xb   = t;
17519f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1752b19a5dc2SMark Adams     } else xb = b;
175317ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
175417ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1755b19a5dc2SMark Adams         sum = xb[i];
1756b19a5dc2SMark Adams         if (xb == b) {
1757b19a5dc2SMark Adams           /* whole matrix (no checkpointing available) */
1758416022c9SBarry Smith           n   = a->i[i+1] - a->i[i];
1759ed480e8bSBarry Smith           idx = a->j + a->i[i];
1760ed480e8bSBarry Smith           v   = a->a + a->i[i];
1761e6d1f457SBarry Smith           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1762ed480e8bSBarry Smith           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1763b19a5dc2SMark Adams         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1764b19a5dc2SMark Adams           n   = a->i[i+1] - diag[i] - 1;
1765b19a5dc2SMark Adams           idx = a->j + diag[i] + 1;
1766b19a5dc2SMark Adams           v   = a->a + diag[i] + 1;
1767b19a5dc2SMark Adams           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1768b19a5dc2SMark Adams           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
176917ab2063SBarry Smith         }
1770b19a5dc2SMark Adams       }
1771b19a5dc2SMark Adams       if (xb == b) {
17729f863219SBarry Smith         ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1773b19a5dc2SMark Adams       } else {
1774b19a5dc2SMark Adams         ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1775b19a5dc2SMark Adams       }
177617ab2063SBarry Smith     }
177717ab2063SBarry Smith   }
17781ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
17793649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1780365a8a9eSBarry Smith   PetscFunctionReturn(0);
178117ab2063SBarry Smith }
178217ab2063SBarry Smith 
17832af78befSBarry Smith 
17844a2ae208SSatish Balay #undef __FUNCT__
17854a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1786dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
178717ab2063SBarry Smith {
1788416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
17894e220ebcSLois Curfman McInnes 
17903a40ed3dSBarry Smith   PetscFunctionBegin;
17914e220ebcSLois Curfman McInnes   info->block_size   = 1.0;
17924e220ebcSLois Curfman McInnes   info->nz_allocated = (double)a->maxnz;
17934e220ebcSLois Curfman McInnes   info->nz_used      = (double)a->nz;
17944e220ebcSLois Curfman McInnes   info->nz_unneeded  = (double)(a->maxnz - a->nz);
17954e220ebcSLois Curfman McInnes   info->assemblies   = (double)A->num_ass;
17968e58a170SBarry Smith   info->mallocs      = (double)A->info.mallocs;
17977adad957SLisandro Dalcin   info->memory       = ((PetscObject)A)->mem;
1798d5f3da31SBarry Smith   if (A->factortype) {
17994e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
18004e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
18014e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
18024e220ebcSLois Curfman McInnes   } else {
18034e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
18044e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
18054e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
18064e220ebcSLois Curfman McInnes   }
18073a40ed3dSBarry Smith   PetscFunctionReturn(0);
180817ab2063SBarry Smith }
180917ab2063SBarry Smith 
18104a2ae208SSatish Balay #undef __FUNCT__
18114a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
18122b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
181317ab2063SBarry Smith {
1814416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1815c7da8527SEric Chamberland   PetscInt          i,m = A->rmap->n - 1;
18166849ba73SBarry Smith   PetscErrorCode    ierr;
181797b48c8fSBarry Smith   const PetscScalar *xx;
181897b48c8fSBarry Smith   PetscScalar       *bb;
1819c7da8527SEric Chamberland #if defined(PETSC_USE_DEBUG)
1820c7da8527SEric Chamberland   PetscInt          d = 0;
1821c7da8527SEric Chamberland #endif
182217ab2063SBarry Smith 
18233a40ed3dSBarry Smith   PetscFunctionBegin;
182497b48c8fSBarry Smith   if (x && b) {
182597b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
182697b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
182797b48c8fSBarry Smith     for (i=0; i<N; i++) {
182897b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
182997b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
183097b48c8fSBarry Smith     }
183197b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
183297b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
183397b48c8fSBarry Smith   }
183497b48c8fSBarry Smith 
1835a9817697SBarry Smith   if (a->keepnonzeropattern) {
1836f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1837e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1838bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1839f1e2ffcdSBarry Smith     }
1840f4df32b1SMatthew Knepley     if (diag != 0.0) {
1841c7da8527SEric Chamberland #if defined(PETSC_USE_DEBUG)
1842c7da8527SEric Chamberland       for (i=0; i<N; i++) {
1843c7da8527SEric Chamberland         d = rows[i];
1844c7da8527SEric Chamberland         if (a->diag[d] >= a->i[d+1]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in the zeroed row %D",d);
1845c7da8527SEric Chamberland       }
1846c7da8527SEric Chamberland #endif
1847f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1848f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1849f1e2ffcdSBarry Smith       }
1850f1e2ffcdSBarry Smith     }
1851f1e2ffcdSBarry Smith   } else {
1852f4df32b1SMatthew Knepley     if (diag != 0.0) {
185317ab2063SBarry Smith       for (i=0; i<N; i++) {
1854e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
18557ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1856416022c9SBarry Smith           a->ilen[rows[i]]    = 1;
1857f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1858bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
18597ae801bdSBarry Smith         } else { /* in case row was completely empty */
1860f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
186117ab2063SBarry Smith         }
186217ab2063SBarry Smith       }
18633a40ed3dSBarry Smith     } else {
186417ab2063SBarry Smith       for (i=0; i<N; i++) {
1865e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1866416022c9SBarry Smith         a->ilen[rows[i]] = 0;
186717ab2063SBarry Smith       }
186817ab2063SBarry Smith     }
1869e56f5c9eSBarry Smith     A->nonzerostate++;
1870f1e2ffcdSBarry Smith   }
187143a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18723a40ed3dSBarry Smith   PetscFunctionReturn(0);
187317ab2063SBarry Smith }
187417ab2063SBarry Smith 
18754a2ae208SSatish Balay #undef __FUNCT__
18766e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
18776e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
18786e169961SBarry Smith {
18796e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
18806e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
18816e169961SBarry Smith   PetscErrorCode    ierr;
18822b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
18836e169961SBarry Smith   const PetscScalar *xx;
18846e169961SBarry Smith   PetscScalar       *bb;
18856e169961SBarry Smith 
18866e169961SBarry Smith   PetscFunctionBegin;
18876e169961SBarry Smith   if (x && b) {
18886e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
18896e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
18902b40b63fSBarry Smith     vecs = PETSC_TRUE;
18916e169961SBarry Smith   }
18921795a4d1SJed Brown   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
18936e169961SBarry Smith   for (i=0; i<N; i++) {
18946e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
18956e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
18962205254eSKarl Rupp 
18976e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
18986e169961SBarry Smith   }
18996e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
19006e169961SBarry Smith     if (!zeroed[i]) {
19016e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
19026e169961SBarry Smith         if (zeroed[a->j[j]]) {
19032b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
19046e169961SBarry Smith           a->a[j] = 0.0;
19056e169961SBarry Smith         }
19066e169961SBarry Smith       }
19072b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
19086e169961SBarry Smith   }
19096e169961SBarry Smith   if (x && b) {
19106e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
19116e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
19126e169961SBarry Smith   }
19136e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
19146e169961SBarry Smith   if (diag != 0.0) {
19156e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
19161d5a398dSstefano_zampini     if (missing) {
19171d5a398dSstefano_zampini       if (a->nonew) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
19181d5a398dSstefano_zampini       else {
19191d5a398dSstefano_zampini         for (i=0; i<N; i++) {
19201d5a398dSstefano_zampini           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
19211d5a398dSstefano_zampini         }
19221d5a398dSstefano_zampini       }
19231d5a398dSstefano_zampini     } else {
19246e169961SBarry Smith       for (i=0; i<N; i++) {
19256e169961SBarry Smith         a->a[a->diag[rows[i]]] = diag;
19266e169961SBarry Smith       }
19276e169961SBarry Smith     }
19281d5a398dSstefano_zampini   }
19296e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19306e169961SBarry Smith   PetscFunctionReturn(0);
19316e169961SBarry Smith }
19326e169961SBarry Smith 
19336e169961SBarry Smith #undef __FUNCT__
19344a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1935a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
193617ab2063SBarry Smith {
1937416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
193897f1f81fSBarry Smith   PetscInt   *itmp;
193917ab2063SBarry Smith 
19403a40ed3dSBarry Smith   PetscFunctionBegin;
1941e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
194217ab2063SBarry Smith 
1943416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1944bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
194517ab2063SBarry Smith   if (idx) {
1946bfeeae90SHong Zhang     itmp = a->j + a->i[row];
194726fbe8dcSKarl Rupp     if (*nz) *idx = itmp;
194817ab2063SBarry Smith     else *idx = 0;
194917ab2063SBarry Smith   }
19503a40ed3dSBarry Smith   PetscFunctionReturn(0);
195117ab2063SBarry Smith }
195217ab2063SBarry Smith 
1953bfeeae90SHong Zhang /* remove this function? */
19544a2ae208SSatish Balay #undef __FUNCT__
19554a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1956a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
195717ab2063SBarry Smith {
19583a40ed3dSBarry Smith   PetscFunctionBegin;
19593a40ed3dSBarry Smith   PetscFunctionReturn(0);
196017ab2063SBarry Smith }
196117ab2063SBarry Smith 
19624a2ae208SSatish Balay #undef __FUNCT__
19634a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1964dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
196517ab2063SBarry Smith {
1966416022c9SBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
196754f21887SBarry Smith   MatScalar      *v  = a->a;
196836db0b34SBarry Smith   PetscReal      sum = 0.0;
19696849ba73SBarry Smith   PetscErrorCode ierr;
197097f1f81fSBarry Smith   PetscInt       i,j;
197117ab2063SBarry Smith 
19723a40ed3dSBarry Smith   PetscFunctionBegin;
197317ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1974416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
197536db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
197617ab2063SBarry Smith     }
19778f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
197851f70360SJed Brown     ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
19793a40ed3dSBarry Smith   } else if (type == NORM_1) {
198036db0b34SBarry Smith     PetscReal *tmp;
198197f1f81fSBarry Smith     PetscInt  *jj = a->j;
19821795a4d1SJed Brown     ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr);
1983064f8208SBarry Smith     *nrm = 0.0;
1984416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1985bfeeae90SHong Zhang       tmp[*jj++] += PetscAbsScalar(*v);  v++;
198617ab2063SBarry Smith     }
1987d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1988064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
198917ab2063SBarry Smith     }
1990606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
199151f70360SJed Brown     ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr);
19923a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1993064f8208SBarry Smith     *nrm = 0.0;
1994d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1995bfeeae90SHong Zhang       v   = a->a + a->i[j];
199617ab2063SBarry Smith       sum = 0.0;
1997416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1998cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
199917ab2063SBarry Smith       }
2000064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
200117ab2063SBarry Smith     }
200251f70360SJed Brown     ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr);
2003f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
20043a40ed3dSBarry Smith   PetscFunctionReturn(0);
200517ab2063SBarry Smith }
200617ab2063SBarry Smith 
20074e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
20084e938277SHong Zhang #undef __FUNCT__
20094e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
20104e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
20114e938277SHong Zhang {
20124e938277SHong Zhang   PetscErrorCode ierr;
20134e938277SHong Zhang   PetscInt       i,j,anzj;
20144e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
20154e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
20164e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
20174e938277SHong Zhang 
20184e938277SHong Zhang   PetscFunctionBegin;
20194e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
2020854ce69bSBarry Smith   ierr = PetscCalloc1(an+1,&ati);CHKERRQ(ierr);
2021785e854fSJed Brown   ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr);
2022785e854fSJed Brown   ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr);
20234e938277SHong Zhang 
20244e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
20254e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
202626fbe8dcSKarl Rupp   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
20274e938277SHong Zhang   /* Form ati for csr format of A^T. */
202826fbe8dcSKarl Rupp   for (i=0;i<an;i++) ati[i+1] += ati[i];
20294e938277SHong Zhang 
20304e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
20314e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
20324e938277SHong Zhang 
20334e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
20344e938277SHong Zhang   for (i=0;i<am;i++) {
20354e938277SHong Zhang     anzj = ai[i+1] - ai[i];
20364e938277SHong Zhang     for (j=0;j<anzj;j++) {
20374e938277SHong Zhang       atj[atfill[*aj]] = i;
20384e938277SHong Zhang       atfill[*aj++]   += 1;
20394e938277SHong Zhang     }
20404e938277SHong Zhang   }
20414e938277SHong Zhang 
20424e938277SHong Zhang   /* Clean up temporary space and complete requests. */
20434e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
2044ce94432eSBarry Smith   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
204533d57670SJed Brown   ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
2046a2f3521dSMark F. Adams 
20474e938277SHong Zhang   b          = (Mat_SeqAIJ*)((*B)->data);
20484e938277SHong Zhang   b->free_a  = PETSC_FALSE;
20494e938277SHong Zhang   b->free_ij = PETSC_TRUE;
20504e938277SHong Zhang   b->nonew   = 0;
20514e938277SHong Zhang   PetscFunctionReturn(0);
20524e938277SHong Zhang }
20534e938277SHong Zhang 
20544a2ae208SSatish Balay #undef __FUNCT__
20554a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
2056fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
205717ab2063SBarry Smith {
2058416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2059416022c9SBarry Smith   Mat            C;
20606849ba73SBarry Smith   PetscErrorCode ierr;
2061d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
206254f21887SBarry Smith   MatScalar      *array = a->a;
206317ab2063SBarry Smith 
20643a40ed3dSBarry Smith   PetscFunctionBegin;
2065e32f2f54SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
2066fc4dec0aSBarry Smith 
2067fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
2068854ce69bSBarry Smith     ierr = PetscCalloc1(1+A->cmap->n,&col);CHKERRQ(ierr);
2069bfeeae90SHong Zhang 
2070bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
2071ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2072d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
207333d57670SJed Brown     ierr = MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
20747adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2075ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
2076606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
2077a541d17aSBarry Smith   } else {
2078a541d17aSBarry Smith     C = *B;
2079a541d17aSBarry Smith   }
2080a541d17aSBarry Smith 
208117ab2063SBarry Smith   for (i=0; i<m; i++) {
208217ab2063SBarry Smith     len    = ai[i+1]-ai[i];
208387d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
2084b9b97703SBarry Smith     array += len;
2085b9b97703SBarry Smith     aj    += len;
208617ab2063SBarry Smith   }
20876d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20886d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
208917ab2063SBarry Smith 
2090815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
2091416022c9SBarry Smith     *B = C;
209217ab2063SBarry Smith   } else {
209328be2f97SBarry Smith     ierr = MatHeaderMerge(A,&C);CHKERRQ(ierr);
209417ab2063SBarry Smith   }
20953a40ed3dSBarry Smith   PetscFunctionReturn(0);
209617ab2063SBarry Smith }
209717ab2063SBarry Smith 
2098cd0d46ebSvictorle #undef __FUNCT__
20995fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
21007087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2101cd0d46ebSvictorle {
21023d3eaba7SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
210354f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
210454f21887SBarry Smith   MatScalar      *va,*vb;
21056849ba73SBarry Smith   PetscErrorCode ierr;
210697f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
2107cd0d46ebSvictorle 
2108cd0d46ebSvictorle   PetscFunctionBegin;
2109cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2110cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
21115485867bSBarry Smith   if (ma!=nb || na!=mb) {
21125485867bSBarry Smith     *f = PETSC_FALSE;
21135485867bSBarry Smith     PetscFunctionReturn(0);
21145485867bSBarry Smith   }
2115cd0d46ebSvictorle   aii  = aij->i; bii = bij->i;
2116cd0d46ebSvictorle   adx  = aij->j; bdx = bij->j;
2117cd0d46ebSvictorle   va   = aij->a; vb = bij->a;
2118785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2119785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
2120cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
2121cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
2122cd0d46ebSvictorle 
2123cd0d46ebSvictorle   *f = PETSC_TRUE;
2124cd0d46ebSvictorle   for (i=0; i<ma; i++) {
2125cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
212697f1f81fSBarry Smith       PetscInt    idc,idr;
21275485867bSBarry Smith       PetscScalar vc,vr;
2128cd0d46ebSvictorle       /* column/row index/value */
21295485867bSBarry Smith       idc = adx[aptr[i]];
21305485867bSBarry Smith       idr = bdx[bptr[idc]];
21315485867bSBarry Smith       vc  = va[aptr[i]];
21325485867bSBarry Smith       vr  = vb[bptr[idc]];
21335485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
21345485867bSBarry Smith         *f = PETSC_FALSE;
21355485867bSBarry Smith         goto done;
2136cd0d46ebSvictorle       } else {
21375485867bSBarry Smith         aptr[i]++;
21385485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
2139cd0d46ebSvictorle       }
2140cd0d46ebSvictorle     }
2141cd0d46ebSvictorle   }
2142cd0d46ebSvictorle done:
2143cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
21443aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
2145cd0d46ebSvictorle   PetscFunctionReturn(0);
2146cd0d46ebSvictorle }
2147cd0d46ebSvictorle 
21481cbb95d3SBarry Smith #undef __FUNCT__
21491cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
21507087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
21511cbb95d3SBarry Smith {
21523d3eaba7SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
215354f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
215454f21887SBarry Smith   MatScalar      *va,*vb;
21551cbb95d3SBarry Smith   PetscErrorCode ierr;
21561cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
21571cbb95d3SBarry Smith 
21581cbb95d3SBarry Smith   PetscFunctionBegin;
21591cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
21601cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
21611cbb95d3SBarry Smith   if (ma!=nb || na!=mb) {
21621cbb95d3SBarry Smith     *f = PETSC_FALSE;
21631cbb95d3SBarry Smith     PetscFunctionReturn(0);
21641cbb95d3SBarry Smith   }
21651cbb95d3SBarry Smith   aii  = aij->i; bii = bij->i;
21661cbb95d3SBarry Smith   adx  = aij->j; bdx = bij->j;
21671cbb95d3SBarry Smith   va   = aij->a; vb = bij->a;
2168785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2169785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
21701cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
21711cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
21721cbb95d3SBarry Smith 
21731cbb95d3SBarry Smith   *f = PETSC_TRUE;
21741cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
21751cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
21761cbb95d3SBarry Smith       PetscInt    idc,idr;
21771cbb95d3SBarry Smith       PetscScalar vc,vr;
21781cbb95d3SBarry Smith       /* column/row index/value */
21791cbb95d3SBarry Smith       idc = adx[aptr[i]];
21801cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
21811cbb95d3SBarry Smith       vc  = va[aptr[i]];
21821cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
21831cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
21841cbb95d3SBarry Smith         *f = PETSC_FALSE;
21851cbb95d3SBarry Smith         goto done;
21861cbb95d3SBarry Smith       } else {
21871cbb95d3SBarry Smith         aptr[i]++;
21881cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
21891cbb95d3SBarry Smith       }
21901cbb95d3SBarry Smith     }
21911cbb95d3SBarry Smith   }
21921cbb95d3SBarry Smith done:
21931cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
21941cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
21951cbb95d3SBarry Smith   PetscFunctionReturn(0);
21961cbb95d3SBarry Smith }
21971cbb95d3SBarry Smith 
21989e29f15eSvictorle #undef __FUNCT__
21999e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2200ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
22019e29f15eSvictorle {
2202dfbe8321SBarry Smith   PetscErrorCode ierr;
22036e111a19SKarl Rupp 
22049e29f15eSvictorle   PetscFunctionBegin;
22055485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
22069e29f15eSvictorle   PetscFunctionReturn(0);
22079e29f15eSvictorle }
22089e29f15eSvictorle 
22094a2ae208SSatish Balay #undef __FUNCT__
22101cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2211ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
22121cbb95d3SBarry Smith {
22131cbb95d3SBarry Smith   PetscErrorCode ierr;
22146e111a19SKarl Rupp 
22151cbb95d3SBarry Smith   PetscFunctionBegin;
22161cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
22171cbb95d3SBarry Smith   PetscFunctionReturn(0);
22181cbb95d3SBarry Smith }
22191cbb95d3SBarry Smith 
22201cbb95d3SBarry Smith #undef __FUNCT__
22214a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2222dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
222317ab2063SBarry Smith {
2224416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
222554f21887SBarry Smith   PetscScalar    *l,*r,x;
222654f21887SBarry Smith   MatScalar      *v;
2227dfbe8321SBarry Smith   PetscErrorCode ierr;
2228d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
222917ab2063SBarry Smith 
22303a40ed3dSBarry Smith   PetscFunctionBegin;
223117ab2063SBarry Smith   if (ll) {
22323ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
22333ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2234e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2235e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
22361ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2237416022c9SBarry Smith     v    = a->a;
223817ab2063SBarry Smith     for (i=0; i<m; i++) {
223917ab2063SBarry Smith       x = l[i];
2240416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
22412205254eSKarl Rupp       for (j=0; j<M; j++) (*v++) *= x;
224217ab2063SBarry Smith     }
22431ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2244efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
224517ab2063SBarry Smith   }
224617ab2063SBarry Smith   if (rr) {
2247e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2248e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
22491ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2250416022c9SBarry Smith     v    = a->a; jj = a->j;
22512205254eSKarl Rupp     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
22521ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2253efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
225417ab2063SBarry Smith   }
2255acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
22563a40ed3dSBarry Smith   PetscFunctionReturn(0);
225717ab2063SBarry Smith }
225817ab2063SBarry Smith 
22594a2ae208SSatish Balay #undef __FUNCT__
22604a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
226197f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
226217ab2063SBarry Smith {
2263db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
22646849ba73SBarry Smith   PetscErrorCode ierr;
2265d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
226697f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
22675d0c19d7SBarry Smith   const PetscInt *irow,*icol;
22685d0c19d7SBarry Smith   PetscInt       nrows,ncols;
226997f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
227054f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2271416022c9SBarry Smith   Mat            C;
2272cdc6f3adSToby Isaac   PetscBool      stride;
227317ab2063SBarry Smith 
22743a40ed3dSBarry Smith   PetscFunctionBegin;
227599141d43SSatish Balay 
227617ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2277b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2278b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
227917ab2063SBarry Smith 
2280251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2281ff718158SBarry Smith   if (stride) {
2282ff718158SBarry Smith     ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2283ff718158SBarry Smith   } else {
2284ff718158SBarry Smith     first = 0;
2285ff718158SBarry Smith     step  = 0;
2286ff718158SBarry Smith   }
2287fee21e36SBarry Smith   if (stride && step == 1) {
228802834360SBarry Smith     /* special case of contiguous rows */
2289dcca6d9dSJed Brown     ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr);
229002834360SBarry Smith     /* loop over new rows determining lens and starting points */
229102834360SBarry Smith     for (i=0; i<nrows; i++) {
2292bfeeae90SHong Zhang       kstart = ai[irow[i]];
2293a2744918SBarry Smith       kend   = kstart + ailen[irow[i]];
2294a91a9bebSLisandro Dalcin       starts[i] = kstart;
229502834360SBarry Smith       for (k=kstart; k<kend; k++) {
2296bfeeae90SHong Zhang         if (aj[k] >= first) {
229702834360SBarry Smith           starts[i] = k;
229802834360SBarry Smith           break;
229902834360SBarry Smith         }
230002834360SBarry Smith       }
2301a2744918SBarry Smith       sum = 0;
230202834360SBarry Smith       while (k < kend) {
2303bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2304a2744918SBarry Smith         sum++;
230502834360SBarry Smith       }
2306a2744918SBarry Smith       lens[i] = sum;
230702834360SBarry Smith     }
230802834360SBarry Smith     /* create submatrix */
2309cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
231097f1f81fSBarry Smith       PetscInt n_cols,n_rows;
231108480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2312e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2313d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
231408480c60SBarry Smith       C    = *B;
23153a40ed3dSBarry Smith     } else {
23163bef6203SJed Brown       PetscInt rbs,cbs;
2317ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2318f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
23193bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
23203bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
23213bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
23227adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2323ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
232408480c60SBarry Smith     }
2325db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2326db02288aSLois Curfman McInnes 
232702834360SBarry Smith     /* loop over rows inserting into submatrix */
2328db02288aSLois Curfman McInnes     a_new = c->a;
2329db02288aSLois Curfman McInnes     j_new = c->j;
2330db02288aSLois Curfman McInnes     i_new = c->i;
2331bfeeae90SHong Zhang 
233202834360SBarry Smith     for (i=0; i<nrows; i++) {
2333a2744918SBarry Smith       ii    = starts[i];
2334a2744918SBarry Smith       lensi = lens[i];
2335a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2336a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
233702834360SBarry Smith       }
233887828ca2SBarry Smith       ierr       = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2339a2744918SBarry Smith       a_new     += lensi;
2340a2744918SBarry Smith       i_new[i+1] = i_new[i] + lensi;
2341a2744918SBarry Smith       c->ilen[i] = lensi;
234202834360SBarry Smith     }
23430e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
23443a40ed3dSBarry Smith   } else {
234502834360SBarry Smith     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
23461795a4d1SJed Brown     ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr);
2347854ce69bSBarry Smith     ierr = PetscMalloc1(1+nrows,&lens);CHKERRQ(ierr);
23484dcab191SBarry Smith     for (i=0; i<ncols; i++) {
23494dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
23504dcab191SBarry 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);
23514dcab191SBarry Smith #endif
23524dcab191SBarry Smith       smap[icol[i]] = i+1;
23534dcab191SBarry Smith     }
23544dcab191SBarry Smith 
235502834360SBarry Smith     /* determine lens of each row */
235602834360SBarry Smith     for (i=0; i<nrows; i++) {
2357bfeeae90SHong Zhang       kstart  = ai[irow[i]];
235802834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
235902834360SBarry Smith       lens[i] = 0;
236002834360SBarry Smith       for (k=kstart; k<kend; k++) {
2361bfeeae90SHong Zhang         if (smap[aj[k]]) {
236202834360SBarry Smith           lens[i]++;
236302834360SBarry Smith         }
236402834360SBarry Smith       }
236502834360SBarry Smith     }
236617ab2063SBarry Smith     /* Create and fill new matrix */
2367a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2368ace3abfcSBarry Smith       PetscBool equal;
23690f5bd95cSBarry Smith 
237099141d43SSatish Balay       c = (Mat_SeqAIJ*)((*B)->data);
2371e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2372d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
2373f23aa3ddSBarry Smith       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2374d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
237508480c60SBarry Smith       C    = *B;
23763a40ed3dSBarry Smith     } else {
23773bef6203SJed Brown       PetscInt rbs,cbs;
2378ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2379f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
23803bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
23813bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
23823bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
23837adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2384ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
238508480c60SBarry Smith     }
238699141d43SSatish Balay     c = (Mat_SeqAIJ*)(C->data);
238717ab2063SBarry Smith     for (i=0; i<nrows; i++) {
238899141d43SSatish Balay       row      = irow[i];
2389bfeeae90SHong Zhang       kstart   = ai[row];
239099141d43SSatish Balay       kend     = kstart + a->ilen[row];
2391bfeeae90SHong Zhang       mat_i    = c->i[i];
239299141d43SSatish Balay       mat_j    = c->j + mat_i;
239399141d43SSatish Balay       mat_a    = c->a + mat_i;
239499141d43SSatish Balay       mat_ilen = c->ilen + i;
239517ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2396bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2397ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
239899141d43SSatish Balay           *mat_a++ = a->a[k];
239999141d43SSatish Balay           (*mat_ilen)++;
240099141d43SSatish Balay 
240117ab2063SBarry Smith         }
240217ab2063SBarry Smith       }
240317ab2063SBarry Smith     }
240402834360SBarry Smith     /* Free work space */
240502834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2406606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2407606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
2408cdc6f3adSToby Isaac     /* sort */
2409cdc6f3adSToby Isaac     for (i = 0; i < nrows; i++) {
2410cdc6f3adSToby Isaac       PetscInt ilen;
2411cdc6f3adSToby Isaac 
2412cdc6f3adSToby Isaac       mat_i = c->i[i];
2413cdc6f3adSToby Isaac       mat_j = c->j + mat_i;
2414cdc6f3adSToby Isaac       mat_a = c->a + mat_i;
2415cdc6f3adSToby Isaac       ilen  = c->ilen[i];
2416cdc6f3adSToby Isaac       ierr  = PetscSortIntWithMatScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr);
2417cdc6f3adSToby Isaac     }
241802834360SBarry Smith   }
24196d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24206d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
242117ab2063SBarry Smith 
242217ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2423416022c9SBarry Smith   *B   = C;
24243a40ed3dSBarry Smith   PetscFunctionReturn(0);
242517ab2063SBarry Smith }
242617ab2063SBarry Smith 
24271df811f5SHong Zhang #undef __FUNCT__
242882d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2429fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
243082d44351SHong Zhang {
243182d44351SHong Zhang   PetscErrorCode ierr;
243282d44351SHong Zhang   Mat            B;
243382d44351SHong Zhang 
243482d44351SHong Zhang   PetscFunctionBegin;
2435c2d650bdSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
243682d44351SHong Zhang     ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
243782d44351SHong Zhang     ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
243833d57670SJed Brown     ierr    = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr);
243982d44351SHong Zhang     ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
244082d44351SHong Zhang     ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
244182d44351SHong Zhang     *subMat = B;
2442c2d650bdSHong Zhang   } else {
2443c2d650bdSHong Zhang     ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
2444c2d650bdSHong Zhang   }
244582d44351SHong Zhang   PetscFunctionReturn(0);
244682d44351SHong Zhang }
244782d44351SHong Zhang 
244882d44351SHong Zhang #undef __FUNCT__
24494a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
24509a625307SHong Zhang PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2451a871dcd8SBarry Smith {
245263b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2453dfbe8321SBarry Smith   PetscErrorCode ierr;
245463b91edcSBarry Smith   Mat            outA;
2455ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
245663b91edcSBarry Smith 
24573a40ed3dSBarry Smith   PetscFunctionBegin;
2458e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
24591df811f5SHong Zhang 
2460b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2461b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2462a871dcd8SBarry Smith 
246363b91edcSBarry Smith   outA             = inA;
2464d5f3da31SBarry Smith   outA->factortype = MAT_FACTOR_LU;
2465f6224b95SHong Zhang   ierr = PetscFree(inA->solvertype);CHKERRQ(ierr);
2466f6224b95SHong Zhang   ierr = PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);CHKERRQ(ierr);
24672205254eSKarl Rupp 
2468c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
24696bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
24702205254eSKarl Rupp 
2471c3122656SLisandro Dalcin   a->row = row;
24722205254eSKarl Rupp 
2473c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
24746bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
24752205254eSKarl Rupp 
2476c3122656SLisandro Dalcin   a->col = col;
247763b91edcSBarry Smith 
247836db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
24796bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
24804c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
24813bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
2482f0ec6fceSSatish Balay 
248394a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2484854ce69bSBarry Smith     ierr = PetscMalloc1(inA->rmap->n+1,&a->solve_work);CHKERRQ(ierr);
24853bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
248694a9d846SBarry Smith   }
248763b91edcSBarry Smith 
2488f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2489137fb511SHong Zhang   if (row_identity && col_identity) {
2490ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2491137fb511SHong Zhang   } else {
2492719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2493137fb511SHong Zhang   }
24943a40ed3dSBarry Smith   PetscFunctionReturn(0);
2495a871dcd8SBarry Smith }
2496a871dcd8SBarry Smith 
24974a2ae208SSatish Balay #undef __FUNCT__
24984a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2499f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2500f0b747eeSBarry Smith {
2501f0b747eeSBarry Smith   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2502f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2503efee365bSSatish Balay   PetscErrorCode ierr;
2504c5df96a5SBarry Smith   PetscBLASInt   one = 1,bnz;
25053a40ed3dSBarry Smith 
25063a40ed3dSBarry Smith   PetscFunctionBegin;
2507c5df96a5SBarry Smith   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
25088b83055fSJed Brown   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2509efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2510acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
25113a40ed3dSBarry Smith   PetscFunctionReturn(0);
2512f0b747eeSBarry Smith }
2513f0b747eeSBarry Smith 
25144a2ae208SSatish Balay #undef __FUNCT__
25154a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
251697f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2517cddf8d76SBarry Smith {
2518dfbe8321SBarry Smith   PetscErrorCode ierr;
251997f1f81fSBarry Smith   PetscInt       i;
2520cddf8d76SBarry Smith 
25213a40ed3dSBarry Smith   PetscFunctionBegin;
2522cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2523854ce69bSBarry Smith     ierr = PetscMalloc1(n+1,B);CHKERRQ(ierr);
2524cddf8d76SBarry Smith   }
2525cddf8d76SBarry Smith 
2526cddf8d76SBarry Smith   for (i=0; i<n; i++) {
25276a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2528cddf8d76SBarry Smith   }
25293a40ed3dSBarry Smith   PetscFunctionReturn(0);
2530cddf8d76SBarry Smith }
2531cddf8d76SBarry Smith 
25324a2ae208SSatish Balay #undef __FUNCT__
25334a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
253497f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
25354dcbc457SBarry Smith {
2536e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
25376849ba73SBarry Smith   PetscErrorCode ierr;
25385d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
25395d0c19d7SBarry Smith   const PetscInt *idx;
254097f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2541f1af5d2fSBarry Smith   PetscBT        table;
2542bbd702dbSSatish Balay 
25433a40ed3dSBarry Smith   PetscFunctionBegin;
2544d0f46423SBarry Smith   m  = A->rmap->n;
2545e4d965acSSatish Balay   ai = a->i;
2546bfeeae90SHong Zhang   aj = a->j;
25478a047759SSatish Balay 
2548e32f2f54SBarry Smith   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
254906763907SSatish Balay 
2550854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&nidx);CHKERRQ(ierr);
255153b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
255206763907SSatish Balay 
2553e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2554b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2555e4d965acSSatish Balay     isz  = 0;
25566831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2557e4d965acSSatish Balay 
2558e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
25594dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2560b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2561e4d965acSSatish Balay 
2562dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2563e4d965acSSatish Balay     for (j=0; j<n; ++j) {
25642205254eSKarl Rupp       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
25654dcbc457SBarry Smith     }
256606763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
25676bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2568e4d965acSSatish Balay 
256904a348a9SBarry Smith     k = 0;
257004a348a9SBarry Smith     for (j=0; j<ov; j++) { /* for each overlap */
257104a348a9SBarry Smith       n = isz;
257206763907SSatish Balay       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2573e4d965acSSatish Balay         row   = nidx[k];
2574e4d965acSSatish Balay         start = ai[row];
2575e4d965acSSatish Balay         end   = ai[row+1];
257604a348a9SBarry Smith         for (l = start; l<end; l++) {
2577efb16452SHong Zhang           val = aj[l];
25782205254eSKarl Rupp           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2579e4d965acSSatish Balay         }
2580e4d965acSSatish Balay       }
2581e4d965acSSatish Balay     }
258270b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2583e4d965acSSatish Balay   }
258494bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2585606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
25863a40ed3dSBarry Smith   PetscFunctionReturn(0);
25874dcbc457SBarry Smith }
258817ab2063SBarry Smith 
25890513a670SBarry Smith /* -------------------------------------------------------------- */
25904a2ae208SSatish Balay #undef __FUNCT__
25914a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2592dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
25930513a670SBarry Smith {
25940513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
25956849ba73SBarry Smith   PetscErrorCode ierr;
25963b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
25975d0c19d7SBarry Smith   const PetscInt *row,*col;
25985d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
259956cd22aeSBarry Smith   IS             icolp,irowp;
26000298fd71SBarry Smith   PetscInt       *cwork = NULL;
26010298fd71SBarry Smith   PetscScalar    *vwork = NULL;
26020513a670SBarry Smith 
26033a40ed3dSBarry Smith   PetscFunctionBegin;
26044c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
260556cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
26064c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
260756cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
26080513a670SBarry Smith 
26090513a670SBarry Smith   /* determine lengths of permuted rows */
2610854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&lens);CHKERRQ(ierr);
26112205254eSKarl Rupp   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2612ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2613f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
261433d57670SJed Brown   ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
26157adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2616ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2617606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
26180513a670SBarry Smith 
2619785e854fSJed Brown   ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr);
26200513a670SBarry Smith   for (i=0; i<m; i++) {
262132ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
26222205254eSKarl Rupp     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2623cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
262432ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
26250513a670SBarry Smith   }
2626606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
26272205254eSKarl Rupp 
26283c7d62e4SBarry Smith   (*B)->assembled = PETSC_FALSE;
26292205254eSKarl Rupp 
26300513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
26310513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
263256cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
263356cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
26346bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
26356bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
26363a40ed3dSBarry Smith   PetscFunctionReturn(0);
26370513a670SBarry Smith }
26380513a670SBarry Smith 
26394a2ae208SSatish Balay #undef __FUNCT__
26404a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2641dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2642cb5b572fSBarry Smith {
2643dfbe8321SBarry Smith   PetscErrorCode ierr;
2644cb5b572fSBarry Smith 
2645cb5b572fSBarry Smith   PetscFunctionBegin;
264633f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
264733f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2648be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2649be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2650be6bf707SBarry Smith 
2651700c5bfcSBarry 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");
2652d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2653cb5b572fSBarry Smith   } else {
2654cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2655cb5b572fSBarry Smith   }
2656cb5b572fSBarry Smith   PetscFunctionReturn(0);
2657cb5b572fSBarry Smith }
2658cb5b572fSBarry Smith 
26594a2ae208SSatish Balay #undef __FUNCT__
26604994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
26614994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2662273d9f13SBarry Smith {
2663dfbe8321SBarry Smith   PetscErrorCode ierr;
2664273d9f13SBarry Smith 
2665273d9f13SBarry Smith   PetscFunctionBegin;
2666ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2667273d9f13SBarry Smith   PetscFunctionReturn(0);
2668273d9f13SBarry Smith }
2669273d9f13SBarry Smith 
26704a2ae208SSatish Balay #undef __FUNCT__
26718c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray_SeqAIJ"
26728c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
26736c0721eeSBarry Smith {
26746c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
26756e111a19SKarl Rupp 
26766c0721eeSBarry Smith   PetscFunctionBegin;
26776c0721eeSBarry Smith   *array = a->a;
26786c0721eeSBarry Smith   PetscFunctionReturn(0);
26796c0721eeSBarry Smith }
26806c0721eeSBarry Smith 
26814a2ae208SSatish Balay #undef __FUNCT__
26828c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray_SeqAIJ"
26838c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
26846c0721eeSBarry Smith {
26856c0721eeSBarry Smith   PetscFunctionBegin;
26866c0721eeSBarry Smith   PetscFunctionReturn(0);
26876c0721eeSBarry Smith }
2688273d9f13SBarry Smith 
26898229c054SShri Abhyankar /*
26908229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
26918229c054SShri Abhyankar    have different nonzero structure.
26928229c054SShri Abhyankar */
2693ac90fabeSBarry Smith #undef __FUNCT__
2694b264fe52SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqX_private"
2695b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2696ec7775f6SShri Abhyankar {
2697b264fe52SHong Zhang   PetscInt       i,j,k,nzx,nzy;
2698ec7775f6SShri Abhyankar 
2699ec7775f6SShri Abhyankar   PetscFunctionBegin;
2700ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2701ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
2702b264fe52SHong Zhang     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2703b264fe52SHong Zhang     nzx = xi[i+1] - xi[i];
2704b264fe52SHong Zhang     nzy = yi[i+1] - yi[i];
27058af7cee1SJed Brown     nnz[i] = 0;
27068af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2707b264fe52SHong Zhang       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2708b264fe52SHong Zhang       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
27098af7cee1SJed Brown       nnz[i]++;
27108af7cee1SJed Brown     }
27118af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2712ec7775f6SShri Abhyankar   }
2713ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2714ec7775f6SShri Abhyankar }
2715ec7775f6SShri Abhyankar 
2716ec7775f6SShri Abhyankar #undef __FUNCT__
2717b264fe52SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
2718b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2719b264fe52SHong Zhang {
2720b264fe52SHong Zhang   PetscInt       m = Y->rmap->N;
2721b264fe52SHong Zhang   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2722b264fe52SHong Zhang   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;
2723b264fe52SHong Zhang   PetscErrorCode ierr;
2724b264fe52SHong Zhang 
2725b264fe52SHong Zhang   PetscFunctionBegin;
2726b264fe52SHong Zhang   /* Set the number of nonzeros in the new matrix */
2727b264fe52SHong Zhang   ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr);
2728b264fe52SHong Zhang   PetscFunctionReturn(0);
2729b264fe52SHong Zhang }
2730b264fe52SHong Zhang 
2731b264fe52SHong Zhang #undef __FUNCT__
2732ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2733f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2734ac90fabeSBarry Smith {
2735dfbe8321SBarry Smith   PetscErrorCode ierr;
2736ac90fabeSBarry Smith   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2737c5df96a5SBarry Smith   PetscBLASInt   one=1,bnz;
2738ac90fabeSBarry Smith 
2739ac90fabeSBarry Smith   PetscFunctionBegin;
2740c5df96a5SBarry Smith   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2741ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2742f4df32b1SMatthew Knepley     PetscScalar alpha = a;
27438b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2744acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2745a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2746ab784542SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2747ab784542SHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2748ac90fabeSBarry Smith   } else {
27498229c054SShri Abhyankar     Mat      B;
27508229c054SShri Abhyankar     PetscInt *nnz;
2751785e854fSJed Brown     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
2752ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2753bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
27544aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
275533d57670SJed Brown     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
2756176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
27578229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2758ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2759ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
276028be2f97SBarry Smith     ierr = MatHeaderReplace(Y,&B);CHKERRQ(ierr);
27618229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2762ac90fabeSBarry Smith   }
2763ac90fabeSBarry Smith   PetscFunctionReturn(0);
2764ac90fabeSBarry Smith }
2765ac90fabeSBarry Smith 
2766521d7252SBarry Smith #undef __FUNCT__
2767354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
27687087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2769354c94deSBarry Smith {
2770354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2771354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2772354c94deSBarry Smith   PetscInt    i,nz;
2773354c94deSBarry Smith   PetscScalar *a;
2774354c94deSBarry Smith 
2775354c94deSBarry Smith   PetscFunctionBegin;
2776354c94deSBarry Smith   nz = aij->nz;
2777354c94deSBarry Smith   a  = aij->a;
27782205254eSKarl Rupp   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2779354c94deSBarry Smith #else
2780354c94deSBarry Smith   PetscFunctionBegin;
2781354c94deSBarry Smith #endif
2782354c94deSBarry Smith   PetscFunctionReturn(0);
2783354c94deSBarry Smith }
2784354c94deSBarry Smith 
2785e34fafa9SBarry Smith #undef __FUNCT__
2786985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2787985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2788e34fafa9SBarry Smith {
2789e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2790e34fafa9SBarry Smith   PetscErrorCode ierr;
2791d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2792e34fafa9SBarry Smith   PetscReal      atmp;
2793985db425SBarry Smith   PetscScalar    *x;
2794e34fafa9SBarry Smith   MatScalar      *aa;
2795e34fafa9SBarry Smith 
2796e34fafa9SBarry Smith   PetscFunctionBegin;
2797e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2798e34fafa9SBarry Smith   aa = a->a;
2799e34fafa9SBarry Smith   ai = a->i;
2800e34fafa9SBarry Smith   aj = a->j;
2801e34fafa9SBarry Smith 
2802985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2803e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2804e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2805e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2806e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2807e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
28089189402eSHong Zhang     x[i]  = 0.0;
2809e34fafa9SBarry Smith     for (j=0; j<ncols; j++) {
2810985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2811985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2812985db425SBarry Smith       aa++; aj++;
2813985db425SBarry Smith     }
2814985db425SBarry Smith   }
2815985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2816985db425SBarry Smith   PetscFunctionReturn(0);
2817985db425SBarry Smith }
2818985db425SBarry Smith 
2819985db425SBarry Smith #undef __FUNCT__
2820985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2821985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2822985db425SBarry Smith {
2823985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2824985db425SBarry Smith   PetscErrorCode ierr;
2825d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2826985db425SBarry Smith   PetscScalar    *x;
2827985db425SBarry Smith   MatScalar      *aa;
2828985db425SBarry Smith 
2829985db425SBarry Smith   PetscFunctionBegin;
2830e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2831985db425SBarry Smith   aa = a->a;
2832985db425SBarry Smith   ai = a->i;
2833985db425SBarry Smith   aj = a->j;
2834985db425SBarry Smith 
2835985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2836985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2837985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2838e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2839985db425SBarry Smith   for (i=0; i<m; i++) {
2840985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2841d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2842985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2843985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2844985db425SBarry Smith       x[i] = 0.0;
2845985db425SBarry Smith       if (idx) {
2846985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2847985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2848985db425SBarry Smith           if (aj[j] > j) {
2849985db425SBarry Smith             idx[i] = j;
2850985db425SBarry Smith             break;
2851985db425SBarry Smith           }
2852985db425SBarry Smith         }
2853985db425SBarry Smith       }
2854985db425SBarry Smith     }
2855985db425SBarry Smith     for (j=0; j<ncols; j++) {
2856985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2857985db425SBarry Smith       aa++; aj++;
2858985db425SBarry Smith     }
2859985db425SBarry Smith   }
2860985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2861985db425SBarry Smith   PetscFunctionReturn(0);
2862985db425SBarry Smith }
2863985db425SBarry Smith 
2864985db425SBarry Smith #undef __FUNCT__
2865c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2866c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2867c87e5d42SMatthew Knepley {
2868c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2869c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2870c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2871c87e5d42SMatthew Knepley   PetscReal      atmp;
2872c87e5d42SMatthew Knepley   PetscScalar    *x;
2873c87e5d42SMatthew Knepley   MatScalar      *aa;
2874c87e5d42SMatthew Knepley 
2875c87e5d42SMatthew Knepley   PetscFunctionBegin;
2876e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2877c87e5d42SMatthew Knepley   aa = a->a;
2878c87e5d42SMatthew Knepley   ai = a->i;
2879c87e5d42SMatthew Knepley   aj = a->j;
2880c87e5d42SMatthew Knepley 
2881c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2882c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2883c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
288460e0710aSBarry 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);
2885c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2886c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2887289a08f5SMatthew Knepley     if (ncols) {
2888289a08f5SMatthew Knepley       /* Get first nonzero */
2889289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
2890289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
28912205254eSKarl Rupp         if (atmp > 1.0e-12) {
28922205254eSKarl Rupp           x[i] = atmp;
28932205254eSKarl Rupp           if (idx) idx[i] = aj[j];
28942205254eSKarl Rupp           break;
28952205254eSKarl Rupp         }
2896289a08f5SMatthew Knepley       }
289712431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2898289a08f5SMatthew Knepley     } else {
2899289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2900289a08f5SMatthew Knepley     }
2901c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
2902c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2903289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2904c87e5d42SMatthew Knepley       aa++; aj++;
2905c87e5d42SMatthew Knepley     }
2906c87e5d42SMatthew Knepley   }
2907c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2908c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2909c87e5d42SMatthew Knepley }
2910c87e5d42SMatthew Knepley 
2911c87e5d42SMatthew Knepley #undef __FUNCT__
2912985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2913985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2914985db425SBarry Smith {
2915985db425SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
2916985db425SBarry Smith   PetscErrorCode  ierr;
2917d9ca1df4SBarry Smith   PetscInt        i,j,m = A->rmap->n,ncols,n;
2918d9ca1df4SBarry Smith   const PetscInt  *ai,*aj;
2919985db425SBarry Smith   PetscScalar     *x;
2920d9ca1df4SBarry Smith   const MatScalar *aa;
2921985db425SBarry Smith 
2922985db425SBarry Smith   PetscFunctionBegin;
2923e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2924985db425SBarry Smith   aa = a->a;
2925985db425SBarry Smith   ai = a->i;
2926985db425SBarry Smith   aj = a->j;
2927985db425SBarry Smith 
2928985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2929985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2930985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2931e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2932985db425SBarry Smith   for (i=0; i<m; i++) {
2933985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2934d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2935985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2936985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2937985db425SBarry Smith       x[i] = 0.0;
2938985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2939985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2940985db425SBarry Smith         for (j=0; j<ncols; j++) {
2941985db425SBarry Smith           if (aj[j] > j) {
2942985db425SBarry Smith             idx[i] = j;
2943985db425SBarry Smith             break;
2944985db425SBarry Smith           }
2945985db425SBarry Smith         }
2946985db425SBarry Smith       }
2947985db425SBarry Smith     }
2948985db425SBarry Smith     for (j=0; j<ncols; j++) {
2949985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2950985db425SBarry Smith       aa++; aj++;
2951e34fafa9SBarry Smith     }
2952e34fafa9SBarry Smith   }
2953e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2954e34fafa9SBarry Smith   PetscFunctionReturn(0);
2955e34fafa9SBarry Smith }
2956bbead8a2SBarry Smith 
2957bbead8a2SBarry Smith #include <petscblaslapack.h>
2958af0996ceSBarry Smith #include <petsc/private/kernels/blockinvert.h>
2959bbead8a2SBarry Smith 
2960bbead8a2SBarry Smith #undef __FUNCT__
2961bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
2962713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
2963bbead8a2SBarry Smith {
2964bbead8a2SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
2965bbead8a2SBarry Smith   PetscErrorCode ierr;
296633d57670SJed Brown   PetscInt       i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
2967bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
2968bbead8a2SBarry Smith   PetscReal      shift = 0.0;
29691a9391e3SHong Zhang   PetscBool      allowzeropivot,zeropivotdetected=PETSC_FALSE;
2970bbead8a2SBarry Smith 
2971bbead8a2SBarry Smith   PetscFunctionBegin;
2972a455e926SHong Zhang   allowzeropivot = PetscNot(A->erroriffailure);
29734a0d0026SBarry Smith   if (a->ibdiagvalid) {
29744a0d0026SBarry Smith     if (values) *values = a->ibdiag;
29754a0d0026SBarry Smith     PetscFunctionReturn(0);
29764a0d0026SBarry Smith   }
2977bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
2978bbead8a2SBarry Smith   if (!a->ibdiag) {
2979785e854fSJed Brown     ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr);
29803bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
2981bbead8a2SBarry Smith   }
2982bbead8a2SBarry Smith   diag = a->ibdiag;
2983bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
2984bbead8a2SBarry Smith   /* factor and invert each block */
2985bbead8a2SBarry Smith   switch (bs) {
2986bbead8a2SBarry Smith   case 1:
2987bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2988bbead8a2SBarry Smith       ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
2989ec1892c8SHong Zhang       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
2990ec1892c8SHong Zhang         if (allowzeropivot) {
2991ec1892c8SHong Zhang           A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
2992ec1892c8SHong Zhang           ierr = PetscInfo1(A,"Zero pivot, row %D\n",i);CHKERRQ(ierr);
2993ec1892c8SHong Zhang         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D",i);
2994ec1892c8SHong Zhang       }
2995bbead8a2SBarry Smith       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
2996bbead8a2SBarry Smith     }
2997bbead8a2SBarry Smith     break;
2998bbead8a2SBarry Smith   case 2:
2999bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3000bbead8a2SBarry Smith       ij[0] = 2*i; ij[1] = 2*i + 1;
3001bbead8a2SBarry Smith       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
3002a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
3003ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
300496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
3005bbead8a2SBarry Smith       diag += 4;
3006bbead8a2SBarry Smith     }
3007bbead8a2SBarry Smith     break;
3008bbead8a2SBarry Smith   case 3:
3009bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3010bbead8a2SBarry Smith       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3011bbead8a2SBarry Smith       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
3012a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
3013ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
301496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
3015bbead8a2SBarry Smith       diag += 9;
3016bbead8a2SBarry Smith     }
3017bbead8a2SBarry Smith     break;
3018bbead8a2SBarry Smith   case 4:
3019bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3020bbead8a2SBarry Smith       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3021bbead8a2SBarry Smith       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
3022a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
3023ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
302496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
3025bbead8a2SBarry Smith       diag += 16;
3026bbead8a2SBarry Smith     }
3027bbead8a2SBarry Smith     break;
3028bbead8a2SBarry Smith   case 5:
3029bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3030bbead8a2SBarry 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;
3031bbead8a2SBarry Smith       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
3032a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
3033ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
303496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
3035bbead8a2SBarry Smith       diag += 25;
3036bbead8a2SBarry Smith     }
3037bbead8a2SBarry Smith     break;
3038bbead8a2SBarry Smith   case 6:
3039bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3040bbead8a2SBarry 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;
3041bbead8a2SBarry Smith       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
3042a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
3043ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
304496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3045bbead8a2SBarry Smith       diag += 36;
3046bbead8a2SBarry Smith     }
3047bbead8a2SBarry Smith     break;
3048bbead8a2SBarry Smith   case 7:
3049bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3050bbead8a2SBarry 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;
3051bbead8a2SBarry Smith       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
3052a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
3053ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
305496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3055bbead8a2SBarry Smith       diag += 49;
3056bbead8a2SBarry Smith     }
3057bbead8a2SBarry Smith     break;
3058bbead8a2SBarry Smith   default:
3059dcca6d9dSJed Brown     ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr);
3060bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3061bbead8a2SBarry Smith       for (j=0; j<bs; j++) {
3062bbead8a2SBarry Smith         IJ[j] = bs*i + j;
3063bbead8a2SBarry Smith       }
3064bbead8a2SBarry Smith       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
30655f8bbccaSHong Zhang       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
30665f8bbccaSHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
306796b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3068bbead8a2SBarry Smith       diag += bs2;
3069bbead8a2SBarry Smith     }
3070bbead8a2SBarry Smith     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3071bbead8a2SBarry Smith   }
3072bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3073bbead8a2SBarry Smith   PetscFunctionReturn(0);
3074bbead8a2SBarry Smith }
3075bbead8a2SBarry Smith 
307673a71a0fSBarry Smith #undef __FUNCT__
307773a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_SeqAIJ"
307873a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
307973a71a0fSBarry Smith {
308073a71a0fSBarry Smith   PetscErrorCode ierr;
308173a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
308273a71a0fSBarry Smith   PetscScalar    a;
308373a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
308473a71a0fSBarry Smith 
308573a71a0fSBarry Smith   PetscFunctionBegin;
308673a71a0fSBarry Smith   if (!x->assembled) {
308773a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
308873a71a0fSBarry Smith     for (i=0; i<m; i++) {
308973a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
309073a71a0fSBarry Smith         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
309173a71a0fSBarry Smith         col  = (PetscInt)(n*PetscRealPart(a));
309273a71a0fSBarry Smith         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
309373a71a0fSBarry Smith       }
309473a71a0fSBarry Smith     }
309573a71a0fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
309673a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
309773a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
309873a71a0fSBarry Smith   PetscFunctionReturn(0);
309973a71a0fSBarry Smith }
310073a71a0fSBarry Smith 
31017d68702bSBarry Smith #undef __FUNCT__
31027d68702bSBarry Smith #define __FUNCT__ "MatShift_SeqAIJ"
31037d68702bSBarry Smith PetscErrorCode MatShift_SeqAIJ(Mat Y,PetscScalar a)
31047d68702bSBarry Smith {
31057d68702bSBarry Smith   PetscErrorCode ierr;
31067d68702bSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)Y->data;
31077d68702bSBarry Smith 
31087d68702bSBarry Smith   PetscFunctionBegin;
31096f33a894SBarry Smith   if (!Y->preallocated || !aij->nz) {
31107d68702bSBarry Smith     ierr = MatSeqAIJSetPreallocation(Y,1,NULL);CHKERRQ(ierr);
31117d68702bSBarry Smith   }
31127d68702bSBarry Smith   ierr = MatShift_Basic(Y,a);CHKERRQ(ierr);
31137d68702bSBarry Smith   PetscFunctionReturn(0);
31147d68702bSBarry Smith }
31157d68702bSBarry Smith 
3116682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
31170a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3118cb5b572fSBarry Smith                                         MatGetRow_SeqAIJ,
3119cb5b572fSBarry Smith                                         MatRestoreRow_SeqAIJ,
3120cb5b572fSBarry Smith                                         MatMult_SeqAIJ,
312197304618SKris Buschelman                                 /*  4*/ MatMultAdd_SeqAIJ,
31227c922b88SBarry Smith                                         MatMultTranspose_SeqAIJ,
31237c922b88SBarry Smith                                         MatMultTransposeAdd_SeqAIJ,
3124db4efbfdSBarry Smith                                         0,
3125db4efbfdSBarry Smith                                         0,
3126db4efbfdSBarry Smith                                         0,
3127db4efbfdSBarry Smith                                 /* 10*/ 0,
3128cb5b572fSBarry Smith                                         MatLUFactor_SeqAIJ,
3129cb5b572fSBarry Smith                                         0,
313041f059aeSBarry Smith                                         MatSOR_SeqAIJ,
313117ab2063SBarry Smith                                         MatTranspose_SeqAIJ,
313297304618SKris Buschelman                                 /*1 5*/ MatGetInfo_SeqAIJ,
3133cb5b572fSBarry Smith                                         MatEqual_SeqAIJ,
3134cb5b572fSBarry Smith                                         MatGetDiagonal_SeqAIJ,
3135cb5b572fSBarry Smith                                         MatDiagonalScale_SeqAIJ,
3136cb5b572fSBarry Smith                                         MatNorm_SeqAIJ,
313797304618SKris Buschelman                                 /* 20*/ 0,
3138cb5b572fSBarry Smith                                         MatAssemblyEnd_SeqAIJ,
3139cb5b572fSBarry Smith                                         MatSetOption_SeqAIJ,
3140cb5b572fSBarry Smith                                         MatZeroEntries_SeqAIJ,
3141d519adbfSMatthew Knepley                                 /* 24*/ MatZeroRows_SeqAIJ,
3142db4efbfdSBarry Smith                                         0,
3143db4efbfdSBarry Smith                                         0,
3144db4efbfdSBarry Smith                                         0,
3145db4efbfdSBarry Smith                                         0,
31464994cf47SJed Brown                                 /* 29*/ MatSetUp_SeqAIJ,
3147db4efbfdSBarry Smith                                         0,
3148db4efbfdSBarry Smith                                         0,
31498c778c55SBarry Smith                                         0,
31508c778c55SBarry Smith                                         0,
3151d519adbfSMatthew Knepley                                 /* 34*/ MatDuplicate_SeqAIJ,
3152cb5b572fSBarry Smith                                         0,
3153cb5b572fSBarry Smith                                         0,
3154cb5b572fSBarry Smith                                         MatILUFactor_SeqAIJ,
3155cb5b572fSBarry Smith                                         0,
3156d519adbfSMatthew Knepley                                 /* 39*/ MatAXPY_SeqAIJ,
3157cb5b572fSBarry Smith                                         MatGetSubMatrices_SeqAIJ,
3158cb5b572fSBarry Smith                                         MatIncreaseOverlap_SeqAIJ,
3159cb5b572fSBarry Smith                                         MatGetValues_SeqAIJ,
3160cb5b572fSBarry Smith                                         MatCopy_SeqAIJ,
3161d519adbfSMatthew Knepley                                 /* 44*/ MatGetRowMax_SeqAIJ,
3162cb5b572fSBarry Smith                                         MatScale_SeqAIJ,
31637d68702bSBarry Smith                                         MatShift_SeqAIJ,
316479299369SBarry Smith                                         MatDiagonalSet_SeqAIJ,
31656e169961SBarry Smith                                         MatZeroRowsColumns_SeqAIJ,
316673a71a0fSBarry Smith                                 /* 49*/ MatSetRandom_SeqAIJ,
31673b2fbd54SBarry Smith                                         MatGetRowIJ_SeqAIJ,
31683b2fbd54SBarry Smith                                         MatRestoreRowIJ_SeqAIJ,
31693b2fbd54SBarry Smith                                         MatGetColumnIJ_SeqAIJ,
3170a93ec695SBarry Smith                                         MatRestoreColumnIJ_SeqAIJ,
317193dfae19SHong Zhang                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3172b9617806SBarry Smith                                         0,
31730513a670SBarry Smith                                         0,
3174cda55fadSBarry Smith                                         MatPermute_SeqAIJ,
3175cda55fadSBarry Smith                                         0,
3176d519adbfSMatthew Knepley                                 /* 59*/ 0,
3177b9b97703SBarry Smith                                         MatDestroy_SeqAIJ,
3178b9b97703SBarry Smith                                         MatView_SeqAIJ,
3179357abbc8SBarry Smith                                         0,
3180321b30b9SSatish Balay                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3181321b30b9SSatish Balay                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3182321b30b9SSatish Balay                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3183ee4f033dSBarry Smith                                         0,
3184ee4f033dSBarry Smith                                         0,
3185ee4f033dSBarry Smith                                         0,
3186d519adbfSMatthew Knepley                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3187c87e5d42SMatthew Knepley                                         MatGetRowMinAbs_SeqAIJ,
3188ee4f033dSBarry Smith                                         0,
3189ee4f033dSBarry Smith                                         MatSetColoring_SeqAIJ,
3190dcf5cc72SBarry Smith                                         0,
3191d519adbfSMatthew Knepley                                 /* 74*/ MatSetValuesAdifor_SeqAIJ,
31923acb8795SBarry Smith                                         MatFDColoringApply_AIJ,
319397304618SKris Buschelman                                         0,
319497304618SKris Buschelman                                         0,
319597304618SKris Buschelman                                         0,
31966ce1633cSBarry Smith                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
319797304618SKris Buschelman                                         0,
319897304618SKris Buschelman                                         0,
319997304618SKris Buschelman                                         0,
3200bc011b1eSHong Zhang                                         MatLoad_SeqAIJ,
3201d519adbfSMatthew Knepley                                 /* 84*/ MatIsSymmetric_SeqAIJ,
32021cbb95d3SBarry Smith                                         MatIsHermitian_SeqAIJ,
32036284ec50SHong Zhang                                         0,
32046284ec50SHong Zhang                                         0,
3205bc011b1eSHong Zhang                                         0,
3206d519adbfSMatthew Knepley                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
320726be0446SHong Zhang                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
320826be0446SHong Zhang                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
320965e8a0caSHong Zhang                                         MatPtAP_SeqAIJ_SeqAIJ,
32104a1b09b7SHong Zhang                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_DenseAxpy,
321165e8a0caSHong Zhang                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
32126fc122caSHong Zhang                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
32136fc122caSHong Zhang                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
32146fc122caSHong Zhang                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
32152121bac1SHong Zhang                                         0,
32162121bac1SHong Zhang                                 /* 99*/ 0,
3217609c6c4dSKris Buschelman                                         0,
3218609c6c4dSKris Buschelman                                         0,
321987d4246cSBarry Smith                                         MatConjugate_SeqAIJ,
322087d4246cSBarry Smith                                         0,
3221d519adbfSMatthew Knepley                                 /*104*/ MatSetValuesRow_SeqAIJ,
322299cafbc1SBarry Smith                                         MatRealPart_SeqAIJ,
3223f5edf698SHong Zhang                                         MatImaginaryPart_SeqAIJ,
3224f5edf698SHong Zhang                                         0,
32252bebee5dSHong Zhang                                         0,
3226cbd44569SHong Zhang                                 /*109*/ MatMatSolve_SeqAIJ,
3227985db425SBarry Smith                                         0,
32282af78befSBarry Smith                                         MatGetRowMin_SeqAIJ,
32292af78befSBarry Smith                                         0,
3230599ef60dSHong Zhang                                         MatMissingDiagonal_SeqAIJ,
3231d519adbfSMatthew Knepley                                 /*114*/ 0,
3232599ef60dSHong Zhang                                         0,
32333c2a7987SHong Zhang                                         0,
3234fe97e370SBarry Smith                                         0,
3235fbdbba38SShri Abhyankar                                         0,
3236fbdbba38SShri Abhyankar                                 /*119*/ 0,
3237fbdbba38SShri Abhyankar                                         0,
3238fbdbba38SShri Abhyankar                                         0,
323982d44351SHong Zhang                                         0,
3240b3a44c85SBarry Smith                                         MatGetMultiProcBlock_SeqAIJ,
32410716a85fSBarry Smith                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3242bbead8a2SBarry Smith                                         MatGetColumnNorms_SeqAIJ,
324337868618SMatthew G Knepley                                         MatInvertBlockDiagonal_SeqAIJ,
324437868618SMatthew G Knepley                                         0,
324537868618SMatthew G Knepley                                         0,
32465df89d91SHong Zhang                                 /*129*/ 0,
324775648e8dSHong Zhang                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
324875648e8dSHong Zhang                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
324975648e8dSHong Zhang                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3250b9af6bddSHong Zhang                                         MatTransposeColoringCreate_SeqAIJ,
3251b9af6bddSHong Zhang                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
32522b8ad9a3SHong Zhang                                         MatTransColoringApplyDenToSp_SeqAIJ,
32532b8ad9a3SHong Zhang                                         MatRARt_SeqAIJ_SeqAIJ,
32542b8ad9a3SHong Zhang                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
32553964eb88SJed Brown                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
32563964eb88SJed Brown                                  /*139*/0,
3257f9426fe0SMark Adams                                         0,
32581919a2e2SJed Brown                                         0,
32593a062f41SBarry Smith                                         MatFDColoringSetUp_SeqXAIJ,
32609c8f2541SHong Zhang                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
32619c8f2541SHong Zhang                                  /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ
32629e29f15eSvictorle };
326317ab2063SBarry Smith 
32644a2ae208SSatish Balay #undef __FUNCT__
32654a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
32667087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3267bef8e0ddSBarry Smith {
3268bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
326997f1f81fSBarry Smith   PetscInt   i,nz,n;
3270bef8e0ddSBarry Smith 
3271bef8e0ddSBarry Smith   PetscFunctionBegin;
3272bef8e0ddSBarry Smith   nz = aij->maxnz;
3273d0f46423SBarry Smith   n  = mat->rmap->n;
3274bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3275bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3276bef8e0ddSBarry Smith   }
3277bef8e0ddSBarry Smith   aij->nz = nz;
3278bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3279bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3280bef8e0ddSBarry Smith   }
3281bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3282bef8e0ddSBarry Smith }
3283bef8e0ddSBarry Smith 
32844a2ae208SSatish Balay #undef __FUNCT__
32854a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3286bef8e0ddSBarry Smith /*@
3287bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3288bef8e0ddSBarry Smith        in the matrix.
3289bef8e0ddSBarry Smith 
3290bef8e0ddSBarry Smith   Input Parameters:
3291bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3292bef8e0ddSBarry Smith -  indices - the column indices
3293bef8e0ddSBarry Smith 
329415091d37SBarry Smith   Level: advanced
329515091d37SBarry Smith 
3296bef8e0ddSBarry Smith   Notes:
3297bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3298bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3299bef8e0ddSBarry Smith   of the MatSetValues() operation.
3300bef8e0ddSBarry Smith 
3301bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3302d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3303bef8e0ddSBarry Smith 
3304bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3305bef8e0ddSBarry Smith 
3306b9617806SBarry Smith     The indices should start with zero, not one.
3307b9617806SBarry Smith 
3308bef8e0ddSBarry Smith @*/
33097087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3310bef8e0ddSBarry Smith {
33114ac538c5SBarry Smith   PetscErrorCode ierr;
3312bef8e0ddSBarry Smith 
3313bef8e0ddSBarry Smith   PetscFunctionBegin;
33140700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
33154482741eSBarry Smith   PetscValidPointer(indices,2);
33164ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3317bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3318bef8e0ddSBarry Smith }
3319bef8e0ddSBarry Smith 
3320be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3321be6bf707SBarry Smith 
33224a2ae208SSatish Balay #undef __FUNCT__
33234a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
33247087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3325be6bf707SBarry Smith {
3326be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
33276849ba73SBarry Smith   PetscErrorCode ierr;
3328d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3329be6bf707SBarry Smith 
3330be6bf707SBarry Smith   PetscFunctionBegin;
3331169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3332be6bf707SBarry Smith 
3333be6bf707SBarry Smith   /* allocate space for values if not already there */
3334be6bf707SBarry Smith   if (!aij->saved_values) {
3335854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr);
33363bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3337be6bf707SBarry Smith   }
3338be6bf707SBarry Smith 
3339be6bf707SBarry Smith   /* copy values over */
334087828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3341be6bf707SBarry Smith   PetscFunctionReturn(0);
3342be6bf707SBarry Smith }
3343be6bf707SBarry Smith 
33444a2ae208SSatish Balay #undef __FUNCT__
3345b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3346be6bf707SBarry Smith /*@
3347be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3348be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3349be6bf707SBarry Smith        nonlinear portion.
3350be6bf707SBarry Smith 
3351be6bf707SBarry Smith    Collect on Mat
3352be6bf707SBarry Smith 
3353be6bf707SBarry Smith   Input Parameters:
33540e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3355be6bf707SBarry Smith 
335615091d37SBarry Smith   Level: advanced
335715091d37SBarry Smith 
3358be6bf707SBarry Smith   Common Usage, with SNESSolve():
3359be6bf707SBarry Smith $    Create Jacobian matrix
3360be6bf707SBarry Smith $    Set linear terms into matrix
3361be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3362be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3363be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3364512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3365be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3366be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3367be6bf707SBarry Smith $    In your Jacobian routine
3368be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3369be6bf707SBarry Smith $      Set nonlinear terms in matrix
3370be6bf707SBarry Smith 
3371be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3372be6bf707SBarry Smith $    // build linear portion of Jacobian
3373512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3374be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3375be6bf707SBarry Smith $    loop over nonlinear iterations
3376be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3377be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3378be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3379be6bf707SBarry Smith $       Solve linear system with Jacobian
3380be6bf707SBarry Smith $    endloop
3381be6bf707SBarry Smith 
3382be6bf707SBarry Smith   Notes:
3383be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3384512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3385be6bf707SBarry Smith     calling this routine.
3386be6bf707SBarry Smith 
33870c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
33880c468ba9SBarry Smith     and does not allocated additional space.
33890c468ba9SBarry Smith 
3390be6bf707SBarry Smith .seealso: MatRetrieveValues()
3391be6bf707SBarry Smith 
3392be6bf707SBarry Smith @*/
33937087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3394be6bf707SBarry Smith {
33954ac538c5SBarry Smith   PetscErrorCode ierr;
3396be6bf707SBarry Smith 
3397be6bf707SBarry Smith   PetscFunctionBegin;
33980700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3399e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3400e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
34014ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3402be6bf707SBarry Smith   PetscFunctionReturn(0);
3403be6bf707SBarry Smith }
3404be6bf707SBarry Smith 
34054a2ae208SSatish Balay #undef __FUNCT__
34064a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
34077087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3408be6bf707SBarry Smith {
3409be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
34106849ba73SBarry Smith   PetscErrorCode ierr;
3411d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3412be6bf707SBarry Smith 
3413be6bf707SBarry Smith   PetscFunctionBegin;
3414169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3415f23aa3ddSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3416be6bf707SBarry Smith   /* copy values over */
341787828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3418be6bf707SBarry Smith   PetscFunctionReturn(0);
3419be6bf707SBarry Smith }
3420be6bf707SBarry Smith 
34214a2ae208SSatish Balay #undef __FUNCT__
34224a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3423be6bf707SBarry Smith /*@
3424be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3425be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3426be6bf707SBarry Smith        nonlinear portion.
3427be6bf707SBarry Smith 
3428be6bf707SBarry Smith    Collect on Mat
3429be6bf707SBarry Smith 
3430be6bf707SBarry Smith   Input Parameters:
3431be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3432be6bf707SBarry Smith 
343315091d37SBarry Smith   Level: advanced
343415091d37SBarry Smith 
3435be6bf707SBarry Smith .seealso: MatStoreValues()
3436be6bf707SBarry Smith 
3437be6bf707SBarry Smith @*/
34387087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3439be6bf707SBarry Smith {
34404ac538c5SBarry Smith   PetscErrorCode ierr;
3441be6bf707SBarry Smith 
3442be6bf707SBarry Smith   PetscFunctionBegin;
34430700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3444e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3445e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
34464ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3447be6bf707SBarry Smith   PetscFunctionReturn(0);
3448be6bf707SBarry Smith }
3449be6bf707SBarry Smith 
3450f83d6046SBarry Smith 
3451be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
34524a2ae208SSatish Balay #undef __FUNCT__
34534a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
345417ab2063SBarry Smith /*@C
3455682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
34560d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
34576e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
345851c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
34592bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
346017ab2063SBarry Smith 
3461db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3462db81eaa0SLois Curfman McInnes 
346317ab2063SBarry Smith    Input Parameters:
3464db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
346517ab2063SBarry Smith .  m - number of rows
346617ab2063SBarry Smith .  n - number of columns
346717ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
346851c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
34690298fd71SBarry Smith          (possibly different for each row) or NULL
347017ab2063SBarry Smith 
347117ab2063SBarry Smith    Output Parameter:
3472416022c9SBarry Smith .  A - the matrix
347317ab2063SBarry Smith 
3474175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3475ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3476175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3477175b88e8SBarry Smith 
3478b259b22eSLois Curfman McInnes    Notes:
347949a6f317SBarry Smith    If nnz is given then nz is ignored
348049a6f317SBarry Smith 
348117ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
348217ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
34830002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
348444cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
348517ab2063SBarry Smith 
348617ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
34870298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
34883d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
34896da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
349017ab2063SBarry Smith 
3491682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
34924fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3493682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
34946c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
34956c7ebb05SLois Curfman McInnes 
34966c7ebb05SLois Curfman McInnes    Options Database Keys:
3497698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
34989db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
349917ab2063SBarry Smith 
3500027ccd11SLois Curfman McInnes    Level: intermediate
3501027ccd11SLois Curfman McInnes 
350269b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
350336db0b34SBarry Smith 
350417ab2063SBarry Smith @*/
35057087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
350617ab2063SBarry Smith {
3507dfbe8321SBarry Smith   PetscErrorCode ierr;
35086945ee14SBarry Smith 
35093a40ed3dSBarry Smith   PetscFunctionBegin;
3510f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3511117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3512c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3513d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3514273d9f13SBarry Smith   PetscFunctionReturn(0);
3515273d9f13SBarry Smith }
3516273d9f13SBarry Smith 
35174a2ae208SSatish Balay #undef __FUNCT__
35184a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3519273d9f13SBarry Smith /*@C
3520273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3521273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3522273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3523273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3524273d9f13SBarry Smith 
3525273d9f13SBarry Smith    Collective on MPI_Comm
3526273d9f13SBarry Smith 
3527273d9f13SBarry Smith    Input Parameters:
35281c4f3114SJed Brown +  B - The matrix
3529273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3530273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
35310298fd71SBarry Smith          (possibly different for each row) or NULL
3532273d9f13SBarry Smith 
3533273d9f13SBarry Smith    Notes:
353449a6f317SBarry Smith      If nnz is given then nz is ignored
353549a6f317SBarry Smith 
3536273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3537273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3538273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3539273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3540273d9f13SBarry Smith 
3541273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
35420298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3543273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3544273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3545273d9f13SBarry Smith 
3546aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3547aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3548aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3549aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3550aa95bbe8SBarry Smith 
3551a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3552a96a251dSBarry Smith    entries or columns indices
3553a96a251dSBarry Smith 
3554273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3555273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3556273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3557273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3558273d9f13SBarry Smith 
3559273d9f13SBarry Smith    Options Database Keys:
3560698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3561698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3562273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3563273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3564273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3565273d9f13SBarry Smith 
3566273d9f13SBarry Smith    Level: intermediate
3567273d9f13SBarry Smith 
356869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3569273d9f13SBarry Smith 
3570273d9f13SBarry Smith @*/
35717087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3572273d9f13SBarry Smith {
35734ac538c5SBarry Smith   PetscErrorCode ierr;
3574a23d5eceSKris Buschelman 
3575a23d5eceSKris Buschelman   PetscFunctionBegin;
35766ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
35776ba663aaSJed Brown   PetscValidType(B,1);
35784ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3579a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3580a23d5eceSKris Buschelman }
3581a23d5eceSKris Buschelman 
3582a23d5eceSKris Buschelman #undef __FUNCT__
3583a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
35847087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3585a23d5eceSKris Buschelman {
3586273d9f13SBarry Smith   Mat_SeqAIJ     *b;
35872576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
35886849ba73SBarry Smith   PetscErrorCode ierr;
358997f1f81fSBarry Smith   PetscInt       i;
3590273d9f13SBarry Smith 
3591273d9f13SBarry Smith   PetscFunctionBegin;
35922576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3593a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3594c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3595c461c341SBarry Smith     nz             = 0;
3596c461c341SBarry Smith   }
3597c461c341SBarry Smith 
359826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
359926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3600899cda47SBarry Smith 
3601435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
360260e0710aSBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3603b73539f3SBarry Smith   if (nnz) {
3604d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
360560e0710aSBarry 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]);
360660e0710aSBarry 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);
3607b73539f3SBarry Smith     }
3608b73539f3SBarry Smith   }
3609b73539f3SBarry Smith 
3610273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
36112205254eSKarl Rupp 
3612273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3613273d9f13SBarry Smith 
3614ab93d7beSBarry Smith   if (!skipallocation) {
36152ee49352SLisandro Dalcin     if (!b->imax) {
3616dcca6d9dSJed Brown       ierr = PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);CHKERRQ(ierr);
36173bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
36182ee49352SLisandro Dalcin     }
3619273d9f13SBarry Smith     if (!nnz) {
3620435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3621c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3622d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3623d0f46423SBarry Smith       nz = nz*B->rmap->n;
3624273d9f13SBarry Smith     } else {
3625273d9f13SBarry Smith       nz = 0;
3626d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3627273d9f13SBarry Smith     }
3628ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
36292205254eSKarl Rupp     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;
3630ab93d7beSBarry Smith 
3631273d9f13SBarry Smith     /* allocate the matrix space */
363253dd7562SDmitry Karpeev     /* FIXME: should B's old memory be unlogged? */
36332ee49352SLisandro Dalcin     ierr    = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3634dcca6d9dSJed Brown     ierr    = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr);
36353bb1ff40SBarry Smith     ierr    = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3636bfeeae90SHong Zhang     b->i[0] = 0;
3637d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
36385da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
36395da197adSKris Buschelman     }
3640273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3641e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3642e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3643c461c341SBarry Smith   } else {
3644e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
3645e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
3646c461c341SBarry Smith   }
3647273d9f13SBarry Smith 
3648273d9f13SBarry Smith   b->nz               = 0;
3649273d9f13SBarry Smith   b->maxnz            = nz;
3650273d9f13SBarry Smith   B->info.nz_unneeded = (double)b->maxnz;
36512205254eSKarl Rupp   if (realalloc) {
36522205254eSKarl Rupp     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
36532205254eSKarl Rupp   }
3654273d9f13SBarry Smith   PetscFunctionReturn(0);
3655273d9f13SBarry Smith }
3656273d9f13SBarry Smith 
3657a1661176SMatthew Knepley #undef  __FUNCT__
3658a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
365958d36128SBarry Smith /*@
3660a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3661a1661176SMatthew Knepley 
3662a1661176SMatthew Knepley    Input Parameters:
3663a1661176SMatthew Knepley +  B - the matrix
3664a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3665a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3666a1661176SMatthew Knepley -  v - optional values in the matrix
3667a1661176SMatthew Knepley 
3668a1661176SMatthew Knepley    Level: developer
3669a1661176SMatthew Knepley 
367058d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
367158d36128SBarry Smith 
3672a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3673a1661176SMatthew Knepley 
3674a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3675a1661176SMatthew Knepley @*/
3676a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3677a1661176SMatthew Knepley {
3678a1661176SMatthew Knepley   PetscErrorCode ierr;
3679a1661176SMatthew Knepley 
3680a1661176SMatthew Knepley   PetscFunctionBegin;
36810700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
36826ba663aaSJed Brown   PetscValidType(B,1);
36834ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3684a1661176SMatthew Knepley   PetscFunctionReturn(0);
3685a1661176SMatthew Knepley }
3686a1661176SMatthew Knepley 
3687a1661176SMatthew Knepley #undef  __FUNCT__
3688a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
36897087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3690a1661176SMatthew Knepley {
3691a1661176SMatthew Knepley   PetscInt       i;
3692a1661176SMatthew Knepley   PetscInt       m,n;
3693a1661176SMatthew Knepley   PetscInt       nz;
3694a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3695a1661176SMatthew Knepley   PetscScalar    *values;
3696a1661176SMatthew Knepley   PetscErrorCode ierr;
3697a1661176SMatthew Knepley 
3698a1661176SMatthew Knepley   PetscFunctionBegin;
369965e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3700779a8d59SSatish Balay 
3701779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3702779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3703779a8d59SSatish Balay 
3704779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3705854ce69bSBarry Smith   ierr = PetscMalloc1(m+1, &nnz);CHKERRQ(ierr);
3706a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3707b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3708a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
370965e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3710a1661176SMatthew Knepley     nnz[i] = nz;
3711a1661176SMatthew Knepley   }
3712a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3713a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3714a1661176SMatthew Knepley 
3715a1661176SMatthew Knepley   if (v) {
3716a1661176SMatthew Knepley     values = (PetscScalar*) v;
3717a1661176SMatthew Knepley   } else {
37181795a4d1SJed Brown     ierr = PetscCalloc1(nz_max, &values);CHKERRQ(ierr);
3719a1661176SMatthew Knepley   }
3720a1661176SMatthew Knepley 
3721a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3722b7940d39SSatish Balay     nz   = Ii[i+1] - Ii[i];
3723b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3724a1661176SMatthew Knepley   }
3725a1661176SMatthew Knepley 
3726a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3727a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3728a1661176SMatthew Knepley 
3729a1661176SMatthew Knepley   if (!v) {
3730a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3731a1661176SMatthew Knepley   }
37327827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3733a1661176SMatthew Knepley   PetscFunctionReturn(0);
3734a1661176SMatthew Knepley }
3735a1661176SMatthew Knepley 
3736c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3737af0996ceSBarry Smith #include <petsc/private/kernels/petscaxpy.h>
3738170fe5c8SBarry Smith 
3739170fe5c8SBarry Smith #undef __FUNCT__
3740170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3741170fe5c8SBarry Smith /*
3742170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3743170fe5c8SBarry Smith 
3744170fe5c8SBarry Smith                n                       p                          p
3745170fe5c8SBarry Smith         (              )       (              )         (                  )
3746170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3747170fe5c8SBarry Smith         (              )       (              )         (                  )
3748170fe5c8SBarry Smith 
3749170fe5c8SBarry Smith */
3750170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3751170fe5c8SBarry Smith {
3752170fe5c8SBarry Smith   PetscErrorCode    ierr;
3753170fe5c8SBarry Smith   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3754170fe5c8SBarry Smith   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3755170fe5c8SBarry Smith   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
37561de00fd4SBarry Smith   PetscInt          i,n,m,q,p;
3757170fe5c8SBarry Smith   const PetscInt    *ii,*idx;
3758170fe5c8SBarry Smith   const PetscScalar *b,*a,*a_q;
3759170fe5c8SBarry Smith   PetscScalar       *c,*c_q;
3760170fe5c8SBarry Smith 
3761170fe5c8SBarry Smith   PetscFunctionBegin;
3762d0f46423SBarry Smith   m    = A->rmap->n;
3763d0f46423SBarry Smith   n    = A->cmap->n;
3764d0f46423SBarry Smith   p    = B->cmap->n;
3765170fe5c8SBarry Smith   a    = sub_a->v;
3766170fe5c8SBarry Smith   b    = sub_b->a;
3767170fe5c8SBarry Smith   c    = sub_c->v;
3768170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3769170fe5c8SBarry Smith 
3770170fe5c8SBarry Smith   ii  = sub_b->i;
3771170fe5c8SBarry Smith   idx = sub_b->j;
3772170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3773170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3774170fe5c8SBarry Smith     while (q-->0) {
3775170fe5c8SBarry Smith       c_q = c + m*(*idx);
3776170fe5c8SBarry Smith       a_q = a + m*i;
3777854c7f52SBarry Smith       PetscKernelAXPY(c_q,*b,a_q,m);
3778170fe5c8SBarry Smith       idx++;
3779170fe5c8SBarry Smith       b++;
3780170fe5c8SBarry Smith     }
3781170fe5c8SBarry Smith   }
3782170fe5c8SBarry Smith   PetscFunctionReturn(0);
3783170fe5c8SBarry Smith }
3784170fe5c8SBarry Smith 
3785170fe5c8SBarry Smith #undef __FUNCT__
3786170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3787170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3788170fe5c8SBarry Smith {
3789170fe5c8SBarry Smith   PetscErrorCode ierr;
3790d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3791170fe5c8SBarry Smith   Mat            Cmat;
3792170fe5c8SBarry Smith 
3793170fe5c8SBarry Smith   PetscFunctionBegin;
379460e0710aSBarry 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);
3795ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
3796170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
379733d57670SJed Brown   ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr);
3798170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
37990298fd71SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
3800d73949e8SHong Zhang 
3801d73949e8SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
38022205254eSKarl Rupp 
3803170fe5c8SBarry Smith   *C = Cmat;
3804170fe5c8SBarry Smith   PetscFunctionReturn(0);
3805170fe5c8SBarry Smith }
3806170fe5c8SBarry Smith 
3807170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3808170fe5c8SBarry Smith #undef __FUNCT__
3809170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3810150d2497SBarry Smith PETSC_INTERN PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3811170fe5c8SBarry Smith {
3812170fe5c8SBarry Smith   PetscErrorCode ierr;
3813170fe5c8SBarry Smith 
3814170fe5c8SBarry Smith   PetscFunctionBegin;
3815170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
38163ff4c91cSHong Zhang     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3817170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
38183ff4c91cSHong Zhang     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3819170fe5c8SBarry Smith   }
38203ff4c91cSHong Zhang   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3821170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
38223ff4c91cSHong Zhang   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3823170fe5c8SBarry Smith   PetscFunctionReturn(0);
3824170fe5c8SBarry Smith }
3825170fe5c8SBarry Smith 
3826170fe5c8SBarry Smith 
38270bad9183SKris Buschelman /*MC
3828fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
38290bad9183SKris Buschelman    based on compressed sparse row format.
38300bad9183SKris Buschelman 
38310bad9183SKris Buschelman    Options Database Keys:
38320bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
38330bad9183SKris Buschelman 
38340bad9183SKris Buschelman   Level: beginner
38350bad9183SKris Buschelman 
3836f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
38370bad9183SKris Buschelman M*/
38380bad9183SKris Buschelman 
3839ccd284c7SBarry Smith /*MC
3840ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
3841ccd284c7SBarry Smith 
3842ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
3843ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
3844ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
3845ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3846ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3847ccd284c7SBarry Smith 
3848ccd284c7SBarry Smith    Options Database Keys:
3849ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
3850ccd284c7SBarry Smith 
3851ccd284c7SBarry Smith   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
3852ccd284c7SBarry Smith    enough exist.
3853ccd284c7SBarry Smith 
3854ccd284c7SBarry Smith   Level: beginner
3855ccd284c7SBarry Smith 
3856ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3857ccd284c7SBarry Smith M*/
3858ccd284c7SBarry Smith 
3859ccd284c7SBarry Smith /*MC
3860ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
3861ccd284c7SBarry Smith 
3862ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3863ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3864ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3865ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3866ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3867ccd284c7SBarry Smith 
3868ccd284c7SBarry Smith    Options Database Keys:
3869ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3870ccd284c7SBarry Smith 
3871ccd284c7SBarry Smith   Level: beginner
3872ccd284c7SBarry Smith 
3873ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3874ccd284c7SBarry Smith M*/
3875ccd284c7SBarry Smith 
3876cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3877af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
3878cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
3879af8000cdSHong Zhang #endif
3880cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);
388142c9c57cSBarry Smith 
3882b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
388329b38603SBarry Smith PETSC_EXTERN PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
388429b38603SBarry Smith PETSC_EXTERN PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3885b3866ffcSBarry Smith #endif
388617667f90SBarry Smith 
3887c0c8ee5eSDmitry Karpeev 
38888c778c55SBarry Smith #undef __FUNCT__
38898c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray"
38908c778c55SBarry Smith /*@C
38918c778c55SBarry Smith    MatSeqAIJGetArray - gives access to the array where the data for a SeqSeqAIJ matrix is stored
38928c778c55SBarry Smith 
38938c778c55SBarry Smith    Not Collective
38948c778c55SBarry Smith 
38958c778c55SBarry Smith    Input Parameter:
3896579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
38978c778c55SBarry Smith 
38988c778c55SBarry Smith    Output Parameter:
38998c778c55SBarry Smith .   array - pointer to the data
39008c778c55SBarry Smith 
39018c778c55SBarry Smith    Level: intermediate
39028c778c55SBarry Smith 
3903774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
39048c778c55SBarry Smith @*/
39058c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
39068c778c55SBarry Smith {
39078c778c55SBarry Smith   PetscErrorCode ierr;
39088c778c55SBarry Smith 
39098c778c55SBarry Smith   PetscFunctionBegin;
39108c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
39118c778c55SBarry Smith   PetscFunctionReturn(0);
39128c778c55SBarry Smith }
39138c778c55SBarry Smith 
39148c778c55SBarry Smith #undef __FUNCT__
391521e72a00SBarry Smith #define __FUNCT__ "MatSeqAIJGetMaxRowNonzeros"
391621e72a00SBarry Smith /*@C
391721e72a00SBarry Smith    MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
391821e72a00SBarry Smith 
391921e72a00SBarry Smith    Not Collective
392021e72a00SBarry Smith 
392121e72a00SBarry Smith    Input Parameter:
3922579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
392321e72a00SBarry Smith 
392421e72a00SBarry Smith    Output Parameter:
392521e72a00SBarry Smith .   nz - the maximum number of nonzeros in any row
392621e72a00SBarry Smith 
392721e72a00SBarry Smith    Level: intermediate
392821e72a00SBarry Smith 
392921e72a00SBarry Smith .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
393021e72a00SBarry Smith @*/
393121e72a00SBarry Smith PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
393221e72a00SBarry Smith {
393321e72a00SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
393421e72a00SBarry Smith 
393521e72a00SBarry Smith   PetscFunctionBegin;
393621e72a00SBarry Smith   *nz = aij->rmax;
393721e72a00SBarry Smith   PetscFunctionReturn(0);
393821e72a00SBarry Smith }
393921e72a00SBarry Smith 
394021e72a00SBarry Smith #undef __FUNCT__
39418c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray"
39428c778c55SBarry Smith /*@C
3943579dbff0SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray()
39448c778c55SBarry Smith 
39458c778c55SBarry Smith    Not Collective
39468c778c55SBarry Smith 
39478c778c55SBarry Smith    Input Parameters:
3948579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
39498c778c55SBarry Smith .  array - pointer to the data
39508c778c55SBarry Smith 
39518c778c55SBarry Smith    Level: intermediate
39528c778c55SBarry Smith 
3953774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
39548c778c55SBarry Smith @*/
39558c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
39568c778c55SBarry Smith {
39578c778c55SBarry Smith   PetscErrorCode ierr;
39588c778c55SBarry Smith 
39598c778c55SBarry Smith   PetscFunctionBegin;
39608c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
39618c778c55SBarry Smith   PetscFunctionReturn(0);
39628c778c55SBarry Smith }
39638c778c55SBarry Smith 
39644a2ae208SSatish Balay #undef __FUNCT__
39654a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
39668cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
3967273d9f13SBarry Smith {
3968273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3969dfbe8321SBarry Smith   PetscErrorCode ierr;
397038baddfdSBarry Smith   PetscMPIInt    size;
3971273d9f13SBarry Smith 
3972273d9f13SBarry Smith   PetscFunctionBegin;
3973ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
3974e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3975273d9f13SBarry Smith 
3976b00a9115SJed Brown   ierr = PetscNewLog(B,&b);CHKERRQ(ierr);
39772205254eSKarl Rupp 
3978b0a32e0cSBarry Smith   B->data = (void*)b;
39792205254eSKarl Rupp 
3980549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
39812205254eSKarl Rupp 
3982416022c9SBarry Smith   b->row                = 0;
3983416022c9SBarry Smith   b->col                = 0;
398482bf6240SBarry Smith   b->icol               = 0;
3985b810aeb4SBarry Smith   b->reallocs           = 0;
398636db0b34SBarry Smith   b->ignorezeroentries  = PETSC_FALSE;
3987f1e2ffcdSBarry Smith   b->roworiented        = PETSC_TRUE;
3988416022c9SBarry Smith   b->nonew              = 0;
3989416022c9SBarry Smith   b->diag               = 0;
3990416022c9SBarry Smith   b->solve_work         = 0;
39912a1b7f2aSHong Zhang   B->spptr              = 0;
3992be6bf707SBarry Smith   b->saved_values       = 0;
3993d7f994e1SBarry Smith   b->idiag              = 0;
399471f1c65dSBarry Smith   b->mdiag              = 0;
399571f1c65dSBarry Smith   b->ssor_work          = 0;
399671f1c65dSBarry Smith   b->omega              = 1.0;
399771f1c65dSBarry Smith   b->fshift             = 0.0;
399871f1c65dSBarry Smith   b->idiagvalid         = PETSC_FALSE;
3999bbead8a2SBarry Smith   b->ibdiagvalid        = PETSC_FALSE;
4000a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
400117ab2063SBarry Smith 
400235d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
4003bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
4004bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
40058c778c55SBarry Smith 
4006b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4007bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
4008bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
4009b3866ffcSBarry Smith #endif
401017f1a0eaSHong Zhang 
4011bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
4012bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
4013bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
4014bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
4015bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
4016bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
4017bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
4018af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
4019af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);CHKERRQ(ierr);
4020af8000cdSHong Zhang #endif
4021b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);CHKERRQ(ierr);
4022bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4023bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4024bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
4025bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
4026bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
4027bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
4028bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
4029bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
40304108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
403117667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
40323a40ed3dSBarry Smith   PetscFunctionReturn(0);
403317ab2063SBarry Smith }
403417ab2063SBarry Smith 
40354a2ae208SSatish Balay #undef __FUNCT__
4036b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
4037b24902e0SBarry Smith /*
4038b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4039b24902e0SBarry Smith */
4040ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
404117ab2063SBarry Smith {
4042416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
40436849ba73SBarry Smith   PetscErrorCode ierr;
4044d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
404517ab2063SBarry Smith 
40463a40ed3dSBarry Smith   PetscFunctionBegin;
4047273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4048273d9f13SBarry Smith 
4049d5f3da31SBarry Smith   C->factortype = A->factortype;
4050416022c9SBarry Smith   c->row        = 0;
4051416022c9SBarry Smith   c->col        = 0;
405282bf6240SBarry Smith   c->icol       = 0;
40536ad4291fSHong Zhang   c->reallocs   = 0;
405417ab2063SBarry Smith 
40556ad4291fSHong Zhang   C->assembled = PETSC_TRUE;
405617ab2063SBarry Smith 
4057aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4058aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4059eec197d1SBarry Smith 
4060dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&c->imax,m,&c->ilen);CHKERRQ(ierr);
40613bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
406217ab2063SBarry Smith   for (i=0; i<m; i++) {
4063416022c9SBarry Smith     c->imax[i] = a->imax[i];
4064416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
406517ab2063SBarry Smith   }
406617ab2063SBarry Smith 
406717ab2063SBarry Smith   /* allocate the matrix space */
4068f77e22a1SHong Zhang   if (mallocmatspace) {
4069dcca6d9dSJed Brown     ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr);
40703bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
40712205254eSKarl Rupp 
4072f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
40732205254eSKarl Rupp 
407497f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
407517ab2063SBarry Smith     if (m > 0) {
407697f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4077be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4078bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4079be6bf707SBarry Smith       } else {
4080bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
408117ab2063SBarry Smith       }
408208480c60SBarry Smith     }
4083f77e22a1SHong Zhang   }
408417ab2063SBarry Smith 
40856ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4086416022c9SBarry Smith   c->roworiented       = a->roworiented;
4087416022c9SBarry Smith   c->nonew             = a->nonew;
4088416022c9SBarry Smith   if (a->diag) {
4089854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&c->diag);CHKERRQ(ierr);
40903bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
409117ab2063SBarry Smith     for (i=0; i<m; i++) {
4092416022c9SBarry Smith       c->diag[i] = a->diag[i];
409317ab2063SBarry Smith     }
40943a40ed3dSBarry Smith   } else c->diag = 0;
40952205254eSKarl Rupp 
40966ad4291fSHong Zhang   c->solve_work         = 0;
40976ad4291fSHong Zhang   c->saved_values       = 0;
40986ad4291fSHong Zhang   c->idiag              = 0;
409971f1c65dSBarry Smith   c->ssor_work          = 0;
4100a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
4101e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
4102e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
41036ad4291fSHong Zhang 
4104893ad86cSHong Zhang   c->rmax         = a->rmax;
4105416022c9SBarry Smith   c->nz           = a->nz;
41068ed568f8SMatthew G Knepley   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4107273d9f13SBarry Smith   C->preallocated = PETSC_TRUE;
4108754ec7b1SSatish Balay 
41096ad4291fSHong Zhang   c->compressedrow.use   = a->compressedrow.use;
41106ad4291fSHong Zhang   c->compressedrow.nrows = a->compressedrow.nrows;
4111cd6b891eSBarry Smith   if (a->compressedrow.use) {
41126ad4291fSHong Zhang     i    = a->compressedrow.nrows;
4113dcca6d9dSJed Brown     ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr);
41146ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
41156ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
411627ea64f8SHong Zhang   } else {
411727ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
41180298fd71SBarry Smith     c->compressedrow.i      = NULL;
41190298fd71SBarry Smith     c->compressedrow.rindex = NULL;
41206ad4291fSHong Zhang   }
4121ea632784SBarry Smith   c->nonzerorowcnt = a->nonzerorowcnt;
4122e56f5c9eSBarry Smith   C->nonzerostate  = A->nonzerostate;
41234846f1f5SKris Buschelman 
41242205254eSKarl Rupp   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4125140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
41263a40ed3dSBarry Smith   PetscFunctionReturn(0);
412717ab2063SBarry Smith }
412817ab2063SBarry Smith 
41294a2ae208SSatish Balay #undef __FUNCT__
4130b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
4131b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4132b24902e0SBarry Smith {
4133b24902e0SBarry Smith   PetscErrorCode ierr;
4134b24902e0SBarry Smith 
4135b24902e0SBarry Smith   PetscFunctionBegin;
4136ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
41374b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4138cfd3f464SBarry Smith   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
413933d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
4140cfd3f464SBarry Smith   }
4141a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4142f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4143b24902e0SBarry Smith   PetscFunctionReturn(0);
4144b24902e0SBarry Smith }
4145b24902e0SBarry Smith 
4146b24902e0SBarry Smith #undef __FUNCT__
41474a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
4148112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4149fbdbba38SShri Abhyankar {
4150fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4151fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4152fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4153fbdbba38SShri Abhyankar   int            fd;
4154fbdbba38SShri Abhyankar   PetscMPIInt    size;
4155fbdbba38SShri Abhyankar   MPI_Comm       comm;
41563059b6faSBarry Smith   PetscInt       bs = newMat->rmap->bs;
4157fbdbba38SShri Abhyankar 
4158fbdbba38SShri Abhyankar   PetscFunctionBegin;
4159c98fd787SBarry Smith   /* force binary viewer to load .info file if it has not yet done so */
4160c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
4161fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4162fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4163fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4164bbead8a2SBarry Smith 
41650298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
41660298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
4167bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
41683059b6faSBarry Smith   if (bs < 0) bs = 1;
41693059b6faSBarry Smith   ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);
4170bbead8a2SBarry Smith 
4171fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4172fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4173fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4174fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4175fbdbba38SShri Abhyankar 
4176bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4177fbdbba38SShri Abhyankar 
4178fbdbba38SShri Abhyankar   /* read in row lengths */
4179785e854fSJed Brown   ierr = PetscMalloc1(M,&rowlengths);CHKERRQ(ierr);
4180fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4181fbdbba38SShri Abhyankar 
4182fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4183fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
418460e0710aSBarry 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);
4185fbdbba38SShri Abhyankar 
4186fbdbba38SShri Abhyankar   /* set global size if not set already*/
4187f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4188fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4189aabbc4fbSShri Abhyankar   } else {
41909d36ed5fSBarry Smith     /* if sizes and type are already set, check if the matrix  global sizes are correct */
4191fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
41924c5b953cSHong Zhang     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
41934c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
41944c5b953cSHong Zhang     }
419560e0710aSBarry 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);
4196aabbc4fbSShri Abhyankar   }
4197fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4198fbdbba38SShri Abhyankar   a    = (Mat_SeqAIJ*)newMat->data;
4199fbdbba38SShri Abhyankar 
4200fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4201fbdbba38SShri Abhyankar 
4202fbdbba38SShri Abhyankar   /* read in nonzero values */
4203fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4204fbdbba38SShri Abhyankar 
4205fbdbba38SShri Abhyankar   /* set matrix "i" values */
4206fbdbba38SShri Abhyankar   a->i[0] = 0;
4207fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4208fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4209fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4210fbdbba38SShri Abhyankar   }
4211fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4212fbdbba38SShri Abhyankar 
4213fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4214fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4215fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4216fbdbba38SShri Abhyankar }
4217fbdbba38SShri Abhyankar 
4218fbdbba38SShri Abhyankar #undef __FUNCT__
4219b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4220ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
42217264ac53SSatish Balay {
42227264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4223dfbe8321SBarry Smith   PetscErrorCode ierr;
4224eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4225eeffb40dSHong Zhang   PetscInt k;
4226eeffb40dSHong Zhang #endif
42277264ac53SSatish Balay 
42283a40ed3dSBarry Smith   PetscFunctionBegin;
4229bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4230d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4231ca44d042SBarry Smith     *flg = PETSC_FALSE;
4232ca44d042SBarry Smith     PetscFunctionReturn(0);
4233bcd2baecSBarry Smith   }
42347264ac53SSatish Balay 
42357264ac53SSatish Balay   /* if the a->i are the same */
4236d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4237abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
42387264ac53SSatish Balay 
42397264ac53SSatish Balay   /* if a->j are the same */
424097f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4241abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4242bcd2baecSBarry Smith 
4243bcd2baecSBarry Smith   /* if a->a are the same */
4244eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4245eeffb40dSHong Zhang   for (k=0; k<a->nz; k++) {
4246eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4247eeffb40dSHong Zhang       *flg = PETSC_FALSE;
42483a40ed3dSBarry Smith       PetscFunctionReturn(0);
4249eeffb40dSHong Zhang     }
4250eeffb40dSHong Zhang   }
4251eeffb40dSHong Zhang #else
4252eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4253eeffb40dSHong Zhang #endif
4254eeffb40dSHong Zhang   PetscFunctionReturn(0);
42557264ac53SSatish Balay }
425636db0b34SBarry Smith 
42574a2ae208SSatish Balay #undef __FUNCT__
42584a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
425905869f15SSatish Balay /*@
426036db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
426136db0b34SBarry Smith               provided by the user.
426236db0b34SBarry Smith 
4263c75a6043SHong Zhang       Collective on MPI_Comm
426436db0b34SBarry Smith 
426536db0b34SBarry Smith    Input Parameters:
426636db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
426736db0b34SBarry Smith .   m - number of rows
426836db0b34SBarry Smith .   n - number of columns
426936db0b34SBarry Smith .   i - row indices
427036db0b34SBarry Smith .   j - column indices
427136db0b34SBarry Smith -   a - matrix values
427236db0b34SBarry Smith 
427336db0b34SBarry Smith    Output Parameter:
427436db0b34SBarry Smith .   mat - the matrix
427536db0b34SBarry Smith 
427636db0b34SBarry Smith    Level: intermediate
427736db0b34SBarry Smith 
427836db0b34SBarry Smith    Notes:
42790551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4280292fb18eSBarry Smith     once the matrix is destroyed and not before
428136db0b34SBarry Smith 
428236db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
428336db0b34SBarry Smith 
4284bfeeae90SHong Zhang        The i and j indices are 0 based
428536db0b34SBarry Smith 
4286a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4287a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
42888eef79e4SBarry Smith     as shown
4289a4552177SSatish Balay 
42908eef79e4SBarry Smith $        1 0 0
42918eef79e4SBarry Smith $        2 0 3
42928eef79e4SBarry Smith $        4 5 6
42938eef79e4SBarry Smith $
42948eef79e4SBarry Smith $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
42958eef79e4SBarry Smith $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
42968eef79e4SBarry Smith $        v =  {1,2,3,4,5,6}  [size = 6]
4297a4552177SSatish Balay 
42989985e31cSBarry Smith 
429969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
430036db0b34SBarry Smith 
430136db0b34SBarry Smith @*/
43027087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
430336db0b34SBarry Smith {
4304dfbe8321SBarry Smith   PetscErrorCode ierr;
4305cbcfb4deSHong Zhang   PetscInt       ii;
430636db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4307cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4308cbcfb4deSHong Zhang   PetscInt jj;
4309cbcfb4deSHong Zhang #endif
431036db0b34SBarry Smith 
431136db0b34SBarry Smith   PetscFunctionBegin;
431241096f02SStefano Zampini   if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4313f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4314f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4315a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4316ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4317ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4318ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4319dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&aij->imax,m,&aij->ilen);CHKERRQ(ierr);
4320ab93d7beSBarry Smith 
432136db0b34SBarry Smith   aij->i            = i;
432236db0b34SBarry Smith   aij->j            = j;
432336db0b34SBarry Smith   aij->a            = a;
432436db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
432536db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4326e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4327e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
432836db0b34SBarry Smith 
432936db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
433036db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
43312515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
433260e0710aSBarry 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]);
43339985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4334e32f2f54SBarry 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);
4335e32f2f54SBarry 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);
43369985e31cSBarry Smith     }
433736db0b34SBarry Smith #endif
433836db0b34SBarry Smith   }
43392515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
434036db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
434160e0710aSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
434260e0710aSBarry 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]);
434336db0b34SBarry Smith   }
434436db0b34SBarry Smith #endif
434536db0b34SBarry Smith 
4346b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4347b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
434836db0b34SBarry Smith   PetscFunctionReturn(0);
434936db0b34SBarry Smith }
43508a0b0e6bSVictor Minden #undef __FUNCT__
43518a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
435280ef6e79SMatthew G Knepley /*@C
4353d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
43548a0b0e6bSVictor Minden               provided by the user.
43558a0b0e6bSVictor Minden 
43568a0b0e6bSVictor Minden       Collective on MPI_Comm
43578a0b0e6bSVictor Minden 
43588a0b0e6bSVictor Minden    Input Parameters:
43598a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
43608a0b0e6bSVictor Minden .   m   - number of rows
43618a0b0e6bSVictor Minden .   n   - number of columns
43628a0b0e6bSVictor Minden .   i   - row indices
43638a0b0e6bSVictor Minden .   j   - column indices
43641230e6d1SVictor Minden .   a   - matrix values
43651230e6d1SVictor Minden .   nz  - number of nonzeros
43661230e6d1SVictor Minden -   idx - 0 or 1 based
43678a0b0e6bSVictor Minden 
43688a0b0e6bSVictor Minden    Output Parameter:
43698a0b0e6bSVictor Minden .   mat - the matrix
43708a0b0e6bSVictor Minden 
43718a0b0e6bSVictor Minden    Level: intermediate
43728a0b0e6bSVictor Minden 
43738a0b0e6bSVictor Minden    Notes:
43748a0b0e6bSVictor Minden        The i and j indices are 0 based
43758a0b0e6bSVictor Minden 
43768a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
43778a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
43788a0b0e6bSVictor Minden     as shown:
43798a0b0e6bSVictor Minden 
43808a0b0e6bSVictor Minden         1 0 0
43818a0b0e6bSVictor Minden         2 0 3
43828a0b0e6bSVictor Minden         4 5 6
43838a0b0e6bSVictor Minden 
43848a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
43858a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
43868a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
43878a0b0e6bSVictor Minden 
43888a0b0e6bSVictor Minden 
438969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
43908a0b0e6bSVictor Minden 
43918a0b0e6bSVictor Minden @*/
43921230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
43938a0b0e6bSVictor Minden {
43948a0b0e6bSVictor Minden   PetscErrorCode ierr;
4395d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
43968a0b0e6bSVictor Minden 
43978a0b0e6bSVictor Minden 
43988a0b0e6bSVictor Minden   PetscFunctionBegin;
43991795a4d1SJed Brown   ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr);
44001230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
4401c8d679ebSHong Zhang     nnz[i[ii] - !!idx] += 1;
44021230e6d1SVictor Minden   }
44038a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
44048a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
44058a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
44061230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
44071230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
44081230e6d1SVictor Minden     if (idx) {
44091230e6d1SVictor Minden       row = i[ii] - 1;
44101230e6d1SVictor Minden       col = j[ii] - 1;
44111230e6d1SVictor Minden     } else {
44121230e6d1SVictor Minden       row = i[ii];
44131230e6d1SVictor Minden       col = j[ii];
44148a0b0e6bSVictor Minden     }
44151230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
44168a0b0e6bSVictor Minden   }
44178a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
44188a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4419d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
44208a0b0e6bSVictor Minden   PetscFunctionReturn(0);
44218a0b0e6bSVictor Minden }
442236db0b34SBarry Smith 
4423cc8ba8e1SBarry Smith #undef __FUNCT__
4424ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4425dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4426cc8ba8e1SBarry Smith {
4427dfbe8321SBarry Smith   PetscErrorCode ierr;
4428cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
442936db0b34SBarry Smith 
4430cc8ba8e1SBarry Smith   PetscFunctionBegin;
44318ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4432cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4433cc8ba8e1SBarry Smith     a->coloring = coloring;
443412c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
443597f1f81fSBarry Smith     PetscInt        i,*larray;
443612c595b3SBarry Smith     ISColoring      ocoloring;
443708b6dcc0SBarry Smith     ISColoringValue *colors;
443812c595b3SBarry Smith 
443912c595b3SBarry Smith     /* set coloring for diagonal portion */
4440785e854fSJed Brown     ierr = PetscMalloc1(A->cmap->n,&larray);CHKERRQ(ierr);
44412205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) larray[i] = i;
44420298fd71SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);CHKERRQ(ierr);
4443785e854fSJed Brown     ierr = PetscMalloc1(A->cmap->n,&colors);CHKERRQ(ierr);
44442205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
444512c595b3SBarry Smith     ierr        = PetscFree(larray);CHKERRQ(ierr);
4446aaf3ff59SMatthew G. Knepley     ierr        = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,PETSC_OWN_POINTER,&ocoloring);CHKERRQ(ierr);
444712c595b3SBarry Smith     a->coloring = ocoloring;
444812c595b3SBarry Smith   }
4449cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4450cc8ba8e1SBarry Smith }
4451cc8ba8e1SBarry Smith 
4452ee4f033dSBarry Smith #undef __FUNCT__
4453ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
445497f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4455ee4f033dSBarry Smith {
4456ee4f033dSBarry Smith   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
4457d0f46423SBarry Smith   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
445854f21887SBarry Smith   MatScalar       *v      = a->a;
445954f21887SBarry Smith   PetscScalar     *values = (PetscScalar*)advalues;
446008b6dcc0SBarry Smith   ISColoringValue *color;
4461ee4f033dSBarry Smith 
4462ee4f033dSBarry Smith   PetscFunctionBegin;
4463e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4464ee4f033dSBarry Smith   color = a->coloring->colors;
4465ee4f033dSBarry Smith   /* loop over rows */
4466ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4467ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4468ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
44692205254eSKarl Rupp     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
4470ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4471cc8ba8e1SBarry Smith   }
4472cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4473cc8ba8e1SBarry Smith }
447436db0b34SBarry Smith 
4475acf2f550SJed Brown #undef __FUNCT__
4476acf2f550SJed Brown #define __FUNCT__ "MatSeqAIJInvalidateDiagonal"
4477acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4478acf2f550SJed Brown {
4479acf2f550SJed Brown   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4480acf2f550SJed Brown   PetscErrorCode ierr;
4481acf2f550SJed Brown 
4482acf2f550SJed Brown   PetscFunctionBegin;
4483acf2f550SJed Brown   a->idiagvalid  = PETSC_FALSE;
4484acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
44852205254eSKarl Rupp 
4486acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4487acf2f550SJed Brown   PetscFunctionReturn(0);
4488acf2f550SJed Brown }
4489acf2f550SJed Brown 
44909c8f2541SHong Zhang #undef __FUNCT__
44919c8f2541SHong Zhang #define __FUNCT__ "MatCreateMPIMatConcatenateSeqMat_SeqAIJ"
44929c8f2541SHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
44939c8f2541SHong Zhang {
44949c8f2541SHong Zhang   PetscErrorCode ierr;
44959c8f2541SHong Zhang 
44969c8f2541SHong Zhang   PetscFunctionBegin;
44979c8f2541SHong Zhang   ierr = MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr);
44989c8f2541SHong Zhang   PetscFunctionReturn(0);
44999c8f2541SHong Zhang }
45009c8f2541SHong Zhang 
450181824310SBarry Smith /*
450253dd7562SDmitry Karpeev  Permute A into C's *local* index space using rowemb,colemb.
450353dd7562SDmitry Karpeev  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
450453dd7562SDmitry Karpeev  of [0,m), colemb is in [0,n).
450553dd7562SDmitry Karpeev  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
450653dd7562SDmitry Karpeev  */
450753dd7562SDmitry Karpeev #undef __FUNCT__
450853dd7562SDmitry Karpeev #define __FUNCT__ "MatSetSeqMat_SeqAIJ"
450953dd7562SDmitry Karpeev PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
451053dd7562SDmitry Karpeev {
451153dd7562SDmitry Karpeev   /* If making this function public, change the error returned in this function away from _PLIB. */
451253dd7562SDmitry Karpeev   PetscErrorCode ierr;
451353dd7562SDmitry Karpeev   Mat_SeqAIJ     *Baij;
451453dd7562SDmitry Karpeev   PetscBool      seqaij;
451553dd7562SDmitry Karpeev   PetscInt       m,n,*nz,i,j,count;
451653dd7562SDmitry Karpeev   PetscScalar    v;
451753dd7562SDmitry Karpeev   const PetscInt *rowindices,*colindices;
451853dd7562SDmitry Karpeev 
451953dd7562SDmitry Karpeev   PetscFunctionBegin;
452053dd7562SDmitry Karpeev   if (!B) PetscFunctionReturn(0);
452153dd7562SDmitry Karpeev   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
452253dd7562SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);CHKERRQ(ierr);
452353dd7562SDmitry Karpeev   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
452453dd7562SDmitry Karpeev   if (rowemb) {
452553dd7562SDmitry Karpeev     ierr = ISGetLocalSize(rowemb,&m);CHKERRQ(ierr);
452653dd7562SDmitry 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);
452753dd7562SDmitry Karpeev   } else {
45286c4ed002SBarry Smith     if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
452953dd7562SDmitry Karpeev   }
453053dd7562SDmitry Karpeev   if (colemb) {
453153dd7562SDmitry Karpeev     ierr = ISGetLocalSize(colemb,&n);CHKERRQ(ierr);
453253dd7562SDmitry 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);
453353dd7562SDmitry Karpeev   } else {
453453dd7562SDmitry Karpeev     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
453553dd7562SDmitry Karpeev   }
453653dd7562SDmitry Karpeev 
453753dd7562SDmitry Karpeev   Baij = (Mat_SeqAIJ*)(B->data);
453853dd7562SDmitry Karpeev   if (pattern == DIFFERENT_NONZERO_PATTERN) {
453953dd7562SDmitry Karpeev     ierr = PetscMalloc1(B->rmap->n,&nz);CHKERRQ(ierr);
454053dd7562SDmitry Karpeev     for (i=0; i<B->rmap->n; i++) {
454153dd7562SDmitry Karpeev       nz[i] = Baij->i[i+1] - Baij->i[i];
454253dd7562SDmitry Karpeev     }
454353dd7562SDmitry Karpeev     ierr = MatSeqAIJSetPreallocation(C,0,nz);CHKERRQ(ierr);
454453dd7562SDmitry Karpeev     ierr = PetscFree(nz);CHKERRQ(ierr);
454553dd7562SDmitry Karpeev   }
454653dd7562SDmitry Karpeev   if (pattern == SUBSET_NONZERO_PATTERN) {
454753dd7562SDmitry Karpeev     ierr = MatZeroEntries(C);CHKERRQ(ierr);
454853dd7562SDmitry Karpeev   }
454953dd7562SDmitry Karpeev   count = 0;
455053dd7562SDmitry Karpeev   rowindices = NULL;
455153dd7562SDmitry Karpeev   colindices = NULL;
455253dd7562SDmitry Karpeev   if (rowemb) {
455353dd7562SDmitry Karpeev     ierr = ISGetIndices(rowemb,&rowindices);CHKERRQ(ierr);
455453dd7562SDmitry Karpeev   }
455553dd7562SDmitry Karpeev   if (colemb) {
455653dd7562SDmitry Karpeev     ierr = ISGetIndices(colemb,&colindices);CHKERRQ(ierr);
455753dd7562SDmitry Karpeev   }
455853dd7562SDmitry Karpeev   for (i=0; i<B->rmap->n; i++) {
455953dd7562SDmitry Karpeev     PetscInt row;
456053dd7562SDmitry Karpeev     row = i;
456153dd7562SDmitry Karpeev     if (rowindices) row = rowindices[i];
456253dd7562SDmitry Karpeev     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
456353dd7562SDmitry Karpeev       PetscInt col;
456453dd7562SDmitry Karpeev       col  = Baij->j[count];
456553dd7562SDmitry Karpeev       if (colindices) col = colindices[col];
456653dd7562SDmitry Karpeev       v    = Baij->a[count];
456753dd7562SDmitry Karpeev       ierr = MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);CHKERRQ(ierr);
456853dd7562SDmitry Karpeev       ++count;
456953dd7562SDmitry Karpeev     }
457053dd7562SDmitry Karpeev   }
457153dd7562SDmitry Karpeev   /* FIXME: set C's nonzerostate correctly. */
457253dd7562SDmitry Karpeev   /* Assembly for C is necessary. */
457353dd7562SDmitry Karpeev   C->preallocated = PETSC_TRUE;
457453dd7562SDmitry Karpeev   C->assembled     = PETSC_TRUE;
457553dd7562SDmitry Karpeev   C->was_assembled = PETSC_FALSE;
457653dd7562SDmitry Karpeev   PetscFunctionReturn(0);
457753dd7562SDmitry Karpeev }
457853dd7562SDmitry Karpeev 
457953dd7562SDmitry Karpeev 
458053dd7562SDmitry Karpeev /*
458181824310SBarry Smith     Special version for direct calls from Fortran
458281824310SBarry Smith */
4583af0996ceSBarry Smith #include <petsc/private/fortranimpl.h>
458481824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
458581824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
458681824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
458781824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
458881824310SBarry Smith #endif
458981824310SBarry Smith 
459081824310SBarry Smith /* Change these macros so can be used in void function */
459181824310SBarry Smith #undef CHKERRQ
4592ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
459381824310SBarry Smith #undef SETERRQ2
4594e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
45954994cf47SJed Brown #undef SETERRQ3
45964994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
459781824310SBarry Smith 
459881824310SBarry Smith #undef __FUNCT__
459981824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
46008cc058d9SJed 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)
460181824310SBarry Smith {
460281824310SBarry Smith   Mat            A  = *AA;
460381824310SBarry Smith   PetscInt       m  = *mm, n = *nn;
460481824310SBarry Smith   InsertMode     is = *isis;
460581824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
460681824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
460781824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
460881824310SBarry Smith   PetscErrorCode ierr;
460981824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
461054f21887SBarry Smith   MatScalar      *ap,value,*aa;
4611ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4612ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
461381824310SBarry Smith 
461481824310SBarry Smith   PetscFunctionBegin;
46154994cf47SJed Brown   MatCheckPreallocated(A,1);
461681824310SBarry Smith   imax  = a->imax;
461781824310SBarry Smith   ai    = a->i;
461881824310SBarry Smith   ailen = a->ilen;
461981824310SBarry Smith   aj    = a->j;
462081824310SBarry Smith   aa    = a->a;
462181824310SBarry Smith 
462281824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
462381824310SBarry Smith     row = im[k];
462481824310SBarry Smith     if (row < 0) continue;
462581824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4626ce94432eSBarry Smith     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
462781824310SBarry Smith #endif
462881824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
462981824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
463081824310SBarry Smith     low  = 0;
463181824310SBarry Smith     high = nrow;
463281824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
463381824310SBarry Smith       if (in[l] < 0) continue;
463481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4635ce94432eSBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
463681824310SBarry Smith #endif
463781824310SBarry Smith       col = in[l];
46382205254eSKarl Rupp       if (roworiented) value = v[l + k*n];
46392205254eSKarl Rupp       else value = v[k + l*m];
46402205254eSKarl Rupp 
464181824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
464281824310SBarry Smith 
46432205254eSKarl Rupp       if (col <= lastcol) low = 0;
46442205254eSKarl Rupp       else high = nrow;
464581824310SBarry Smith       lastcol = col;
464681824310SBarry Smith       while (high-low > 5) {
464781824310SBarry Smith         t = (low+high)/2;
464881824310SBarry Smith         if (rp[t] > col) high = t;
464981824310SBarry Smith         else             low  = t;
465081824310SBarry Smith       }
465181824310SBarry Smith       for (i=low; i<high; i++) {
465281824310SBarry Smith         if (rp[i] > col) break;
465381824310SBarry Smith         if (rp[i] == col) {
465481824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
465581824310SBarry Smith           else                  ap[i] = value;
465681824310SBarry Smith           goto noinsert;
465781824310SBarry Smith         }
465881824310SBarry Smith       }
465981824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
466081824310SBarry Smith       if (nonew == 1) goto noinsert;
4661ce94432eSBarry Smith       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4662fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
466381824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
466481824310SBarry Smith       /* shift up all the later entries in this row */
466581824310SBarry Smith       for (ii=N; ii>=i; ii--) {
466681824310SBarry Smith         rp[ii+1] = rp[ii];
466781824310SBarry Smith         ap[ii+1] = ap[ii];
466881824310SBarry Smith       }
466981824310SBarry Smith       rp[i] = col;
467081824310SBarry Smith       ap[i] = value;
4671e56f5c9eSBarry Smith       A->nonzerostate++;
467281824310SBarry Smith noinsert:;
467381824310SBarry Smith       low = i + 1;
467481824310SBarry Smith     }
467581824310SBarry Smith     ailen[row] = nrow;
467681824310SBarry Smith   }
467781824310SBarry Smith   PetscFunctionReturnVoid();
467881824310SBarry Smith }
46799f7953f8SBarry Smith 
4680