xref: /petsc/src/mat/impls/aij/seq/aij.c (revision cc2e6a90c05b27ffec69cb207fe793d447f14420)
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;
796ce1633cSBarry Smith   const PetscInt  *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++) {
876ce1633cSBarry Smith     if ((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++) {
946ce1633cSBarry Smith     if ((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) {
2111a83f524SJed Brown     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,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) {
2601a83f524SJed Brown     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
2613b2fbd54SBarry Smith   } else {
2621795a4d1SJed Brown     ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
263854ce69bSBarry Smith     ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr);
264854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&cja);CHKERRQ(ierr);
2653b2fbd54SBarry Smith     jj   = a->j;
2663b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
267bfeeae90SHong Zhang       collengths[jj[i]]++;
2683b2fbd54SBarry Smith     }
2693b2fbd54SBarry Smith     cia[0] = oshift;
2703b2fbd54SBarry Smith     for (i=0; i<n; i++) {
2713b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
2723b2fbd54SBarry Smith     }
27397f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
2743b2fbd54SBarry Smith     jj   = a->j;
275a93ec695SBarry Smith     for (row=0; row<m; row++) {
276a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
277a93ec695SBarry Smith       for (i=0; i<mr; i++) {
278bfeeae90SHong Zhang         col = *jj++;
2792205254eSKarl Rupp 
2803b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2813b2fbd54SBarry Smith       }
2823b2fbd54SBarry Smith     }
283606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2843b2fbd54SBarry Smith     *ia  = cia; *ja = cja;
2853b2fbd54SBarry Smith   }
2863a40ed3dSBarry Smith   PetscFunctionReturn(0);
2873b2fbd54SBarry Smith }
2883b2fbd54SBarry Smith 
2894a2ae208SSatish Balay #undef __FUNCT__
2904a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
2911a83f524SJed Brown PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2923b2fbd54SBarry Smith {
293dfbe8321SBarry Smith   PetscErrorCode ierr;
294606d414cSSatish Balay 
2953a40ed3dSBarry Smith   PetscFunctionBegin;
2963a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2973b2fbd54SBarry Smith 
298606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
299606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
3003a40ed3dSBarry Smith   PetscFunctionReturn(0);
3013b2fbd54SBarry Smith }
3023b2fbd54SBarry Smith 
3037cee066cSHong Zhang /*
3047cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
3057cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
306040ebd07SHong Zhang  spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
3077cee066cSHong Zhang */
3087cee066cSHong Zhang #undef __FUNCT__
3097cee066cSHong Zhang #define __FUNCT__ "MatGetColumnIJ_SeqAIJ_Color"
3107cee066cSHong Zhang PetscErrorCode MatGetColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
3117cee066cSHong Zhang {
3127cee066cSHong Zhang   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3137cee066cSHong Zhang   PetscErrorCode ierr;
3147cee066cSHong Zhang   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
3157cee066cSHong Zhang   PetscInt       nz = a->i[m],row,*jj,mr,col;
3167cee066cSHong Zhang   PetscInt       *cspidx;
3177cee066cSHong Zhang 
3187cee066cSHong Zhang   PetscFunctionBegin;
3197cee066cSHong Zhang   *nn = n;
3207cee066cSHong Zhang   if (!ia) PetscFunctionReturn(0);
321625f6d37SHong Zhang 
3221795a4d1SJed Brown   ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
323854ce69bSBarry Smith   ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr);
324854ce69bSBarry Smith   ierr = PetscMalloc1(nz+1,&cja);CHKERRQ(ierr);
325854ce69bSBarry Smith   ierr = PetscMalloc1(nz+1,&cspidx);CHKERRQ(ierr);
3267cee066cSHong Zhang   jj   = a->j;
3277cee066cSHong Zhang   for (i=0; i<nz; i++) {
3287cee066cSHong Zhang     collengths[jj[i]]++;
3297cee066cSHong Zhang   }
3307cee066cSHong Zhang   cia[0] = oshift;
3317cee066cSHong Zhang   for (i=0; i<n; i++) {
3327cee066cSHong Zhang     cia[i+1] = cia[i] + collengths[i];
3337cee066cSHong Zhang   }
3347cee066cSHong Zhang   ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
3357cee066cSHong Zhang   jj   = a->j;
3367cee066cSHong Zhang   for (row=0; row<m; row++) {
3377cee066cSHong Zhang     mr = a->i[row+1] - a->i[row];
3387cee066cSHong Zhang     for (i=0; i<mr; i++) {
3397cee066cSHong Zhang       col = *jj++;
3407cee066cSHong Zhang       cspidx[cia[col] + collengths[col] - oshift] = a->i[row] + i; /* index of a->j */
3417cee066cSHong Zhang       cja[cia[col] + collengths[col]++ - oshift]  = row + oshift;
3427cee066cSHong Zhang     }
3437cee066cSHong Zhang   }
3447cee066cSHong Zhang   ierr   = PetscFree(collengths);CHKERRQ(ierr);
3457cee066cSHong Zhang   *ia    = cia; *ja = cja;
3467cee066cSHong Zhang   *spidx = cspidx;
3477cee066cSHong Zhang   PetscFunctionReturn(0);
3487cee066cSHong Zhang }
3497cee066cSHong Zhang 
3507cee066cSHong Zhang #undef __FUNCT__
3517cee066cSHong Zhang #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ_Color"
3527cee066cSHong Zhang PetscErrorCode MatRestoreColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
3537cee066cSHong Zhang {
3547cee066cSHong Zhang   PetscErrorCode ierr;
3557cee066cSHong Zhang 
3567cee066cSHong Zhang   PetscFunctionBegin;
3575243ef75SHong Zhang   ierr = MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
3587cee066cSHong Zhang   ierr = PetscFree(*spidx);CHKERRQ(ierr);
3597cee066cSHong Zhang   PetscFunctionReturn(0);
3607cee066cSHong Zhang }
3617cee066cSHong Zhang 
36287d4246cSBarry Smith #undef __FUNCT__
36387d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
36487d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
36587d4246cSBarry Smith {
36687d4246cSBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
36787d4246cSBarry Smith   PetscInt       *ai = a->i;
36887d4246cSBarry Smith   PetscErrorCode ierr;
36987d4246cSBarry Smith 
37087d4246cSBarry Smith   PetscFunctionBegin;
37187d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
37287d4246cSBarry Smith   PetscFunctionReturn(0);
37387d4246cSBarry Smith }
37487d4246cSBarry Smith 
375bd04181cSBarry Smith /*
376bd04181cSBarry Smith     MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions
377bd04181cSBarry Smith 
378bd04181cSBarry Smith       -   a single row of values is set with each call
379bd04181cSBarry Smith       -   no row or column indices are negative or (in error) larger than the number of rows or columns
380bd04181cSBarry Smith       -   the values are always added to the matrix, not set
381bd04181cSBarry Smith       -   no new locations are introduced in the nonzero structure of the matrix
382bd04181cSBarry Smith 
3831f763a69SBarry Smith      This does NOT assume the global column indices are sorted
384bd04181cSBarry Smith 
3851f763a69SBarry Smith */
386bd04181cSBarry Smith 
387af0996ceSBarry Smith #include <petsc/private/isimpl.h>
388189e4007SBarry Smith #undef __FUNCT__
389189e4007SBarry Smith #define __FUNCT__ "MatSeqAIJSetValuesLocalFast"
390189e4007SBarry Smith PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
391189e4007SBarry Smith {
392189e4007SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3931f763a69SBarry Smith   PetscInt       low,high,t,row,nrow,i,col,l;
3941f763a69SBarry Smith   const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j;
3951f763a69SBarry Smith   PetscInt       lastcol = -1;
396189e4007SBarry Smith   MatScalar      *ap,value,*aa = a->a;
397189e4007SBarry Smith   const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices;
398189e4007SBarry Smith 
399f38dd0b8SBarry Smith   row = ridx[im[0]];
4001f763a69SBarry Smith   rp   = aj + ai[row];
4011f763a69SBarry Smith   ap = aa + ai[row];
4021f763a69SBarry Smith   nrow = ailen[row];
403189e4007SBarry Smith   low  = 0;
404189e4007SBarry Smith   high = nrow;
405189e4007SBarry Smith   for (l=0; l<n; l++) { /* loop over added columns */
406189e4007SBarry Smith     col = cidx[in[l]];
407f38dd0b8SBarry Smith     value = v[l];
408189e4007SBarry Smith 
409189e4007SBarry Smith     if (col <= lastcol) low = 0;
410189e4007SBarry Smith     else high = nrow;
411189e4007SBarry Smith     lastcol = col;
412189e4007SBarry Smith     while (high-low > 5) {
413189e4007SBarry Smith       t = (low+high)/2;
414189e4007SBarry Smith       if (rp[t] > col) high = t;
415189e4007SBarry Smith       else low = t;
416189e4007SBarry Smith     }
417189e4007SBarry Smith     for (i=low; i<high; i++) {
418189e4007SBarry Smith       if (rp[i] == col) {
4191f763a69SBarry Smith         ap[i] += value;
420189e4007SBarry Smith         low = i + 1;
4211f763a69SBarry Smith         break;
422189e4007SBarry Smith       }
423189e4007SBarry Smith     }
424189e4007SBarry Smith   }
425f38dd0b8SBarry Smith   return 0;
426189e4007SBarry Smith }
427189e4007SBarry Smith 
428bd04181cSBarry Smith #undef __FUNCT__
4294a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
43097f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
43117ab2063SBarry Smith {
432416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
433e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
43497f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
4356849ba73SBarry Smith   PetscErrorCode ierr;
436e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
43754f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
438ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
439ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
44017ab2063SBarry Smith 
4413a40ed3dSBarry Smith   PetscFunctionBegin;
44217ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
443416022c9SBarry Smith     row = im[k];
4445ef9f2a5SBarry Smith     if (row < 0) continue;
4452515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
446e32f2f54SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
4473b2fbd54SBarry Smith #endif
448bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
44917ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
450416022c9SBarry Smith     low  = 0;
451c71e6ed7SBarry Smith     high = nrow;
45217ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
4535ef9f2a5SBarry Smith       if (in[l] < 0) continue;
4542515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
455e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
4563b2fbd54SBarry Smith #endif
457bfeeae90SHong Zhang       col = in[l];
4584b0e389bSBarry Smith       if (roworiented) {
4595ef9f2a5SBarry Smith         value = v[l + k*n];
460bef8e0ddSBarry Smith       } else {
4614b0e389bSBarry Smith         value = v[k + l*m];
4624b0e389bSBarry Smith       }
46333b2b78bSBarry Smith       if ((value == 0.0 && ignorezeroentries) && (is == ADD_VALUES)) continue;
46436db0b34SBarry Smith 
4652205254eSKarl Rupp       if (col <= lastcol) low = 0;
4662205254eSKarl Rupp       else high = nrow;
467e2ee6c50SBarry Smith       lastcol = col;
468416022c9SBarry Smith       while (high-low > 5) {
469416022c9SBarry Smith         t = (low+high)/2;
470416022c9SBarry Smith         if (rp[t] > col) high = t;
471416022c9SBarry Smith         else low = t;
47217ab2063SBarry Smith       }
473416022c9SBarry Smith       for (i=low; i<high; i++) {
47417ab2063SBarry Smith         if (rp[i] > col) break;
47517ab2063SBarry Smith         if (rp[i] == col) {
476416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
47717ab2063SBarry Smith           else ap[i] = value;
478e44c0bd4SBarry Smith           low = i + 1;
47917ab2063SBarry Smith           goto noinsert;
48017ab2063SBarry Smith         }
48117ab2063SBarry Smith       }
482abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
483c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
484e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
485fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
486c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
487416022c9SBarry Smith       /* shift up all the later entries in this row */
488416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
48917ab2063SBarry Smith         rp[ii+1] = rp[ii];
49017ab2063SBarry Smith         ap[ii+1] = ap[ii];
49117ab2063SBarry Smith       }
49217ab2063SBarry Smith       rp[i] = col;
49317ab2063SBarry Smith       ap[i] = value;
494416022c9SBarry Smith       low   = i + 1;
495e56f5c9eSBarry Smith       A->nonzerostate++;
496e44c0bd4SBarry Smith noinsert:;
49717ab2063SBarry Smith     }
49817ab2063SBarry Smith     ailen[row] = nrow;
49917ab2063SBarry Smith   }
5003a40ed3dSBarry Smith   PetscFunctionReturn(0);
50117ab2063SBarry Smith }
50217ab2063SBarry Smith 
50381824310SBarry Smith 
5044a2ae208SSatish Balay #undef __FUNCT__
5054a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
506a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
5077eb43aa7SLois Curfman McInnes {
5087eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
50997f1f81fSBarry Smith   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
51097f1f81fSBarry Smith   PetscInt   *ai = a->i,*ailen = a->ilen;
51154f21887SBarry Smith   MatScalar  *ap,*aa = a->a;
5127eb43aa7SLois Curfman McInnes 
5133a40ed3dSBarry Smith   PetscFunctionBegin;
5147eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
5157eb43aa7SLois Curfman McInnes     row = im[k];
516e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
517e32f2f54SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
518bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
5197eb43aa7SLois Curfman McInnes     nrow = ailen[row];
5207eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
521e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
522e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
523bfeeae90SHong Zhang       col  = in[l];
5247eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
5257eb43aa7SLois Curfman McInnes       while (high-low > 5) {
5267eb43aa7SLois Curfman McInnes         t = (low+high)/2;
5277eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
5287eb43aa7SLois Curfman McInnes         else low = t;
5297eb43aa7SLois Curfman McInnes       }
5307eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
5317eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
5327eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
533b49de8d1SLois Curfman McInnes           *v++ = ap[i];
5347eb43aa7SLois Curfman McInnes           goto finished;
5357eb43aa7SLois Curfman McInnes         }
5367eb43aa7SLois Curfman McInnes       }
53797e567efSBarry Smith       *v++ = 0.0;
5387eb43aa7SLois Curfman McInnes finished:;
5397eb43aa7SLois Curfman McInnes     }
5407eb43aa7SLois Curfman McInnes   }
5413a40ed3dSBarry Smith   PetscFunctionReturn(0);
5427eb43aa7SLois Curfman McInnes }
5437eb43aa7SLois Curfman McInnes 
54417ab2063SBarry Smith 
5454a2ae208SSatish Balay #undef __FUNCT__
5464a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
547dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
54817ab2063SBarry Smith {
549416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5506849ba73SBarry Smith   PetscErrorCode ierr;
5516f69ff64SBarry Smith   PetscInt       i,*col_lens;
5526f69ff64SBarry Smith   int            fd;
553b37d52dbSMark F. Adams   FILE           *file;
55417ab2063SBarry Smith 
5553a40ed3dSBarry Smith   PetscFunctionBegin;
556b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
557854ce69bSBarry Smith   ierr = PetscMalloc1(4+A->rmap->n,&col_lens);CHKERRQ(ierr);
5582205254eSKarl Rupp 
5590700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
560d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
561d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
562416022c9SBarry Smith   col_lens[3] = a->nz;
563416022c9SBarry Smith 
564416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
565d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
566416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
56717ab2063SBarry Smith   }
568d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
569606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
570416022c9SBarry Smith 
571416022c9SBarry Smith   /* store column indices (zero start index) */
5726f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
573416022c9SBarry Smith 
574416022c9SBarry Smith   /* store nonzero values */
5756f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
576b37d52dbSMark F. Adams 
577b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
578b37d52dbSMark F. Adams   if (file) {
57933d57670SJed Brown     fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs));
580b37d52dbSMark F. Adams   }
5813a40ed3dSBarry Smith   PetscFunctionReturn(0);
58217ab2063SBarry Smith }
583416022c9SBarry Smith 
58409573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
585cd155464SBarry Smith 
5864a2ae208SSatish Balay #undef __FUNCT__
5874a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
588dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
589416022c9SBarry Smith {
590416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
591dfbe8321SBarry Smith   PetscErrorCode    ierr;
59260e0710aSBarry Smith   PetscInt          i,j,m = A->rmap->n;
593e060cb09SBarry Smith   const char        *name;
594f3ef73ceSBarry Smith   PetscViewerFormat format;
59517ab2063SBarry Smith 
5963a40ed3dSBarry Smith   PetscFunctionBegin;
597b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
59871c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
59997f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
60060e0710aSBarry Smith     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
601c337ccceSJed Brown       /* Need a dummy value to ensure the dimension of the matrix. */
602d00d2cf4SBarry Smith       nofinalvalue = 1;
603d00d2cf4SBarry Smith     }
604d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
605d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
60677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
607fbfe6fa7SJed Brown #if defined(PETSC_USE_COMPLEX)
608fbfe6fa7SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
609fbfe6fa7SJed Brown #else
61077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
611fbfe6fa7SJed Brown #endif
612b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
61317ab2063SBarry Smith 
61417ab2063SBarry Smith     for (i=0; i<m; i++) {
61560e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
616aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
617a9bf72d8SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",i+1,a->j[j]+1,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
61817ab2063SBarry Smith #else
61960e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr);
62017ab2063SBarry Smith #endif
62117ab2063SBarry Smith       }
62217ab2063SBarry Smith     }
623d00d2cf4SBarry Smith     if (nofinalvalue) {
624c337ccceSJed Brown #if defined(PETSC_USE_COMPLEX)
625c337ccceSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr);
626c337ccceSJed Brown #else
627d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
628c337ccceSJed Brown #endif
629d00d2cf4SBarry Smith     }
630317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
631fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
632d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
63368369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
634cd155464SBarry Smith     PetscFunctionReturn(0);
635fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
636d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
63744cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
63877431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
63960e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
640aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
64136db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
64260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
64336db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
64460e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
64536db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
64660e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
6476831982aSBarry Smith         }
64844cd7ae7SLois Curfman McInnes #else
64960e0710aSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);}
65044cd7ae7SLois Curfman McInnes #endif
65144cd7ae7SLois Curfman McInnes       }
652b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
65344cd7ae7SLois Curfman McInnes     }
654d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
655fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
65697f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
657d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
658854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&sptr);CHKERRQ(ierr);
659496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
660496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
66160e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
662496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
663aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
66436db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
665496be53dSLois Curfman McInnes #else
666496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
667496be53dSLois Curfman McInnes #endif
668496be53dSLois Curfman McInnes         }
669496be53dSLois Curfman McInnes       }
670496be53dSLois Curfman McInnes     }
6712e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
67277431f27SBarry Smith     ierr    = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
6732e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
6742205254eSKarl Rupp       if (i+4<m) {
6752205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]);CHKERRQ(ierr);
6762205254eSKarl Rupp       } else if (i+3<m) {
6772205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);CHKERRQ(ierr);
6782205254eSKarl Rupp       } else if (i+2<m) {
6792205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);
6802205254eSKarl Rupp       } else if (i+1<m) {
6812205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);
6822205254eSKarl Rupp       } else if (i<m) {
6832205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);
6842205254eSKarl Rupp       } else {
6852205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);
6862205254eSKarl Rupp       }
687496be53dSLois Curfman McInnes     }
688b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
689606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
690496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
69160e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
69277431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
693496be53dSLois Curfman McInnes       }
694b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
695496be53dSLois Curfman McInnes     }
696b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
697496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
69860e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
699496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
700aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
70136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
70260e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
7036831982aSBarry Smith           }
704496be53dSLois Curfman McInnes #else
70560e0710aSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);}
706496be53dSLois Curfman McInnes #endif
707496be53dSLois Curfman McInnes         }
708496be53dSLois Curfman McInnes       }
709b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
710496be53dSLois Curfman McInnes     }
711d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
712fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
71397f1f81fSBarry Smith     PetscInt    cnt = 0,jcnt;
71487828ca2SBarry Smith     PetscScalar value;
71568f1ed48SBarry Smith #if defined(PETSC_USE_COMPLEX)
71668f1ed48SBarry Smith     PetscBool   realonly = PETSC_TRUE;
71768f1ed48SBarry Smith 
71868f1ed48SBarry Smith     for (i=0; i<a->i[m]; i++) {
71968f1ed48SBarry Smith       if (PetscImaginaryPart(a->a[i]) != 0.0) {
72068f1ed48SBarry Smith         realonly = PETSC_FALSE;
72168f1ed48SBarry Smith         break;
72268f1ed48SBarry Smith       }
72368f1ed48SBarry Smith     }
72468f1ed48SBarry Smith #endif
72502594712SBarry Smith 
726d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
72702594712SBarry Smith     for (i=0; i<m; i++) {
72802594712SBarry Smith       jcnt = 0;
729d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
730e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
73102594712SBarry Smith           value = a->a[cnt++];
732e24b481bSBarry Smith           jcnt++;
73302594712SBarry Smith         } else {
73402594712SBarry Smith           value = 0.0;
73502594712SBarry Smith         }
736aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
73768f1ed48SBarry Smith         if (realonly) {
73860e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr);
73968f1ed48SBarry Smith         } else {
74060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr);
74168f1ed48SBarry Smith         }
74202594712SBarry Smith #else
74360e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr);
74402594712SBarry Smith #endif
74502594712SBarry Smith       }
746b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
74702594712SBarry Smith     }
748d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7493c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
750150b93efSMatthew G. Knepley     PetscInt fshift=1;
751d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
7523c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
75319303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");CHKERRQ(ierr);
7543c215bfdSMatthew Knepley #else
75519303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");CHKERRQ(ierr);
7563c215bfdSMatthew Knepley #endif
757d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
7583c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
75960e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
7603c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
761a9a0e077SKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
7623c215bfdSMatthew Knepley #else
763150b93efSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr);
7643c215bfdSMatthew Knepley #endif
7653c215bfdSMatthew Knepley       }
7663c215bfdSMatthew Knepley     }
767d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7683a40ed3dSBarry Smith   } else {
769d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
770d5f3da31SBarry Smith     if (A->factortype) {
77116cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
77216cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
77316cd7e1dSShri Abhyankar         /* L part */
77460e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
77516cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
77616cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
77760e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
77816cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
7796712e2f1SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
78016cd7e1dSShri Abhyankar           } else {
78160e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
78216cd7e1dSShri Abhyankar           }
78316cd7e1dSShri Abhyankar #else
78460e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
78516cd7e1dSShri Abhyankar #endif
78616cd7e1dSShri Abhyankar         }
78716cd7e1dSShri Abhyankar         /* diagonal */
78816cd7e1dSShri Abhyankar         j = a->diag[i];
78916cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
79016cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
79160e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr);
79216cd7e1dSShri Abhyankar         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
7936712e2f1SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)(-PetscImaginaryPart(1.0/a->a[j])));CHKERRQ(ierr);
79416cd7e1dSShri Abhyankar         } else {
79560e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
79616cd7e1dSShri Abhyankar         }
79716cd7e1dSShri Abhyankar #else
79860e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr);
79916cd7e1dSShri Abhyankar #endif
80016cd7e1dSShri Abhyankar 
80116cd7e1dSShri Abhyankar         /* U part */
80260e0710aSBarry Smith         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
80316cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
80416cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
80560e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
80616cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
80722ab088eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
80816cd7e1dSShri Abhyankar           } else {
80960e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
81016cd7e1dSShri Abhyankar           }
81116cd7e1dSShri Abhyankar #else
81260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
81316cd7e1dSShri Abhyankar #endif
81416cd7e1dSShri Abhyankar         }
81516cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
81616cd7e1dSShri Abhyankar       }
81716cd7e1dSShri Abhyankar     } else {
81817ab2063SBarry Smith       for (i=0; i<m; i++) {
81977431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
82060e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
821aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
82236db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
82360e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
82436db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
82560e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
8263a40ed3dSBarry Smith           } else {
82760e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
82817ab2063SBarry Smith           }
82917ab2063SBarry Smith #else
83060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
83117ab2063SBarry Smith #endif
83217ab2063SBarry Smith         }
833b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
83417ab2063SBarry Smith       }
83516cd7e1dSShri Abhyankar     }
836d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
83717ab2063SBarry Smith   }
838b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8393a40ed3dSBarry Smith   PetscFunctionReturn(0);
840416022c9SBarry Smith }
841416022c9SBarry Smith 
8429804daf3SBarry Smith #include <petscdraw.h>
8434a2ae208SSatish Balay #undef __FUNCT__
8444a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
845dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
846416022c9SBarry Smith {
847480ef9eaSBarry Smith   Mat               A  = (Mat) Aa;
848416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
849dfbe8321SBarry Smith   PetscErrorCode    ierr;
850d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
851b05fc000SLisandro Dalcin   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
852b0a32e0cSBarry Smith   PetscViewer       viewer;
853f3ef73ceSBarry Smith   PetscViewerFormat format;
854cddf8d76SBarry Smith 
8553a40ed3dSBarry Smith   PetscFunctionBegin;
856480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
857b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
85819bcc07fSBarry Smith 
859b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
860416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
8610513a670SBarry Smith 
862fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
8630513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
864b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
865416022c9SBarry Smith     for (i=0; i<m; i++) {
866cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
867bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
868bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
86936db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
870b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
871cddf8d76SBarry Smith       }
872cddf8d76SBarry Smith     }
873b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
874cddf8d76SBarry Smith     for (i=0; i<m; i++) {
875cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
876bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
877bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
878cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
879b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
880cddf8d76SBarry Smith       }
881cddf8d76SBarry Smith     }
882b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
883cddf8d76SBarry Smith     for (i=0; i<m; i++) {
884cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
885bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
886bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
88736db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
888b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
889416022c9SBarry Smith       }
890416022c9SBarry Smith     }
8910513a670SBarry Smith   } else {
8920513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
8930513a670SBarry Smith     /* first determine max of all nonzero values */
89497f1f81fSBarry Smith     PetscInt  nz = a->nz,count;
895b05fc000SLisandro Dalcin     PetscReal minv = 0.0, maxv = 0.0;
896b0a32e0cSBarry Smith     PetscDraw popup;
8970513a670SBarry Smith 
8980513a670SBarry Smith     for (i=0; i<nz; i++) {
8990513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
9000513a670SBarry Smith     }
901b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
902b05fc000SLisandro Dalcin     if (popup) {ierr = PetscDrawScalePopup(popup,minv,maxv);CHKERRQ(ierr);}
9030513a670SBarry Smith     count = 0;
9040513a670SBarry Smith     for (i=0; i<m; i++) {
9050513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
906bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
907bfeeae90SHong Zhang         x_l   = a->j[j]; x_r = x_l + 1.0;
908b05fc000SLisandro Dalcin         color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
909b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
9100513a670SBarry Smith         count++;
9110513a670SBarry Smith       }
9120513a670SBarry Smith     }
9130513a670SBarry Smith   }
914480ef9eaSBarry Smith   PetscFunctionReturn(0);
915480ef9eaSBarry Smith }
916cddf8d76SBarry Smith 
9179804daf3SBarry Smith #include <petscdraw.h>
9184a2ae208SSatish Balay #undef __FUNCT__
9194a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
920dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
921480ef9eaSBarry Smith {
922dfbe8321SBarry Smith   PetscErrorCode ierr;
923b0a32e0cSBarry Smith   PetscDraw      draw;
92436db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
925ace3abfcSBarry Smith   PetscBool      isnull;
926480ef9eaSBarry Smith 
927480ef9eaSBarry Smith   PetscFunctionBegin;
928b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
929b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
930480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
931480ef9eaSBarry Smith 
932480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
933d0f46423SBarry Smith   xr   = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
934480ef9eaSBarry Smith   xr  += w;    yr += h;  xl = -w;     yl = -h;
935b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
936b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
9370298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
9383a40ed3dSBarry Smith   PetscFunctionReturn(0);
939416022c9SBarry Smith }
940416022c9SBarry Smith 
9414a2ae208SSatish Balay #undef __FUNCT__
9424a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
943dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
944416022c9SBarry Smith {
945dfbe8321SBarry Smith   PetscErrorCode ierr;
946ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
947416022c9SBarry Smith 
9483a40ed3dSBarry Smith   PetscFunctionBegin;
949251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
950251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
951251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
952c45a1595SBarry Smith   if (iascii) {
9533a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
9540f5bd95cSBarry Smith   } else if (isbinary) {
9553a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
9560f5bd95cSBarry Smith   } else if (isdraw) {
9573a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
95811aeaf0aSBarry Smith   }
9594108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
9603a40ed3dSBarry Smith   PetscFunctionReturn(0);
96117ab2063SBarry Smith }
96219bcc07fSBarry Smith 
9634a2ae208SSatish Balay #undef __FUNCT__
9644a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
965dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
96617ab2063SBarry Smith {
967416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9686849ba73SBarry Smith   PetscErrorCode ierr;
96997f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
970d0f46423SBarry Smith   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
97154f21887SBarry Smith   MatScalar      *aa    = a->a,*ap;
9723447b6efSHong Zhang   PetscReal      ratio  = 0.6;
97317ab2063SBarry Smith 
9743a40ed3dSBarry Smith   PetscFunctionBegin;
9753a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
97617ab2063SBarry Smith 
97743ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
97817ab2063SBarry Smith   for (i=1; i<m; i++) {
979416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
98017ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
98194a9d846SBarry Smith     rmax    = PetscMax(rmax,ailen[i]);
98217ab2063SBarry Smith     if (fshift) {
983bfeeae90SHong Zhang       ip = aj + ai[i];
984bfeeae90SHong Zhang       ap = aa + ai[i];
98517ab2063SBarry Smith       N  = ailen[i];
98617ab2063SBarry Smith       for (j=0; j<N; j++) {
98717ab2063SBarry Smith         ip[j-fshift] = ip[j];
98817ab2063SBarry Smith         ap[j-fshift] = ap[j];
98917ab2063SBarry Smith       }
99017ab2063SBarry Smith     }
99117ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
99217ab2063SBarry Smith   }
99317ab2063SBarry Smith   if (m) {
99417ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
99517ab2063SBarry Smith     ai[m]   = ai[m-1] + ailen[m-1];
99617ab2063SBarry Smith   }
9977b083b7cSBarry Smith 
99817ab2063SBarry Smith   /* reset ilen and imax for each row */
9997b083b7cSBarry Smith   a->nonzerorowcnt = 0;
100017ab2063SBarry Smith   for (i=0; i<m; i++) {
100117ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
10027b083b7cSBarry Smith     a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
100317ab2063SBarry Smith   }
1004bfeeae90SHong Zhang   a->nz = ai[m];
100565e19b50SBarry 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);
100617ab2063SBarry Smith 
100709f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1008d0f46423SBarry 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);
1009ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
1010ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
10112205254eSKarl Rupp 
10128e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
1013dd5f02e7SSatish Balay   a->reallocs         = 0;
10146712e2f1SBarry Smith   A->info.nz_unneeded = (PetscReal)fshift;
101536db0b34SBarry Smith   a->rmax             = rmax;
10164e220ebcSLois Curfman McInnes 
101711e456e1SBarry Smith   ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
10184108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
1019acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10203a40ed3dSBarry Smith   PetscFunctionReturn(0);
102117ab2063SBarry Smith }
102217ab2063SBarry Smith 
10234a2ae208SSatish Balay #undef __FUNCT__
102499cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
102599cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
102699cafbc1SBarry Smith {
102799cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
102899cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
102954f21887SBarry Smith   MatScalar      *aa = a->a;
1030acf2f550SJed Brown   PetscErrorCode ierr;
103199cafbc1SBarry Smith 
103299cafbc1SBarry Smith   PetscFunctionBegin;
103399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1034acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
103599cafbc1SBarry Smith   PetscFunctionReturn(0);
103699cafbc1SBarry Smith }
103799cafbc1SBarry Smith 
103899cafbc1SBarry Smith #undef __FUNCT__
103999cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
104099cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
104199cafbc1SBarry Smith {
104299cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
104399cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
104454f21887SBarry Smith   MatScalar      *aa = a->a;
1045acf2f550SJed Brown   PetscErrorCode ierr;
104699cafbc1SBarry Smith 
104799cafbc1SBarry Smith   PetscFunctionBegin;
104899cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1049acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
105099cafbc1SBarry Smith   PetscFunctionReturn(0);
105199cafbc1SBarry Smith }
105299cafbc1SBarry Smith 
105399cafbc1SBarry Smith #undef __FUNCT__
10544a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
1055dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
105617ab2063SBarry Smith {
1057416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1058dfbe8321SBarry Smith   PetscErrorCode ierr;
10593a40ed3dSBarry Smith 
10603a40ed3dSBarry Smith   PetscFunctionBegin;
1061d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
1062acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10633a40ed3dSBarry Smith   PetscFunctionReturn(0);
106417ab2063SBarry Smith }
1065416022c9SBarry Smith 
10664a2ae208SSatish Balay #undef __FUNCT__
10674a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
1068dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
106917ab2063SBarry Smith {
1070416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1071dfbe8321SBarry Smith   PetscErrorCode ierr;
1072d5d45c9bSBarry Smith 
10733a40ed3dSBarry Smith   PetscFunctionBegin;
1074aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1075d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
107617ab2063SBarry Smith #endif
1077e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
10786bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
10796bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
108005b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
1081d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
108205b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
108371f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
108405b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
10856bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
108605b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
10876bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
1088cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
10890b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
1090a30b2313SHong Zhang 
10914108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
1092bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
1093901853e0SKris Buschelman 
1094dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
1095bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
1096bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1097bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1098bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr);
1099bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr);
1100bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr);
1101af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
1102af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);CHKERRQ(ierr);
1103af8000cdSHong Zhang #endif
1104b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);CHKERRQ(ierr);
1105bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1106bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1107bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1108bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr);
11093a40ed3dSBarry Smith   PetscFunctionReturn(0);
111017ab2063SBarry Smith }
111117ab2063SBarry Smith 
11124a2ae208SSatish Balay #undef __FUNCT__
11134a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
1114ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
111517ab2063SBarry Smith {
1116416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11174846f1f5SKris Buschelman   PetscErrorCode ierr;
11183a40ed3dSBarry Smith 
11193a40ed3dSBarry Smith   PetscFunctionBegin;
1120a65d3064SKris Buschelman   switch (op) {
1121a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
11224e0d8c25SBarry Smith     a->roworiented = flg;
1123a65d3064SKris Buschelman     break;
1124a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1125a9817697SBarry Smith     a->keepnonzeropattern = flg;
1126a65d3064SKris Buschelman     break;
1127512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1128512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1129a65d3064SKris Buschelman     break;
1130a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
11314e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1132a65d3064SKris Buschelman     break;
1133a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
11344e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1135a65d3064SKris Buschelman     break;
113628b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
113728b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
113828b2fa4aSMatthew Knepley     break;
1139a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11404e0d8c25SBarry Smith     a->ignorezeroentries = flg;
11410df259c2SBarry Smith     break;
11423d472b54SHong Zhang   case MAT_SPD:
1143b1646e73SJed Brown   case MAT_SYMMETRIC:
1144b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1145b1646e73SJed Brown   case MAT_HERMITIAN:
1146b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
11475021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
11485021d80fSJed Brown     break;
11494e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1150a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1151a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1152290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1153a65d3064SKris Buschelman     break;
1154b87ac2d8SJed Brown   case MAT_USE_INODES:
1155b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1156b87ac2d8SJed Brown     break;
1157a65d3064SKris Buschelman   default:
1158e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1159a65d3064SKris Buschelman   }
11604108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
11613a40ed3dSBarry Smith   PetscFunctionReturn(0);
116217ab2063SBarry Smith }
116317ab2063SBarry Smith 
11644a2ae208SSatish Balay #undef __FUNCT__
11654a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1166dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
116717ab2063SBarry Smith {
1168416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11696849ba73SBarry Smith   PetscErrorCode ierr;
1170d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
117135e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
117217ab2063SBarry Smith 
11733a40ed3dSBarry Smith   PetscFunctionBegin;
1174d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1175e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
117635e7444dSHong Zhang 
1177d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1178d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
117935e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
11802c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
118135e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
118235e7444dSHong Zhang     PetscFunctionReturn(0);
118335e7444dSHong Zhang   }
118435e7444dSHong Zhang 
11852dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
11861ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
118735e7444dSHong Zhang   for (i=0; i<n; i++) {
118835e7444dSHong Zhang     nz = ai[i+1] - ai[i];
11892f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
119035e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++) {
119135e7444dSHong Zhang       if (aj[j] == i) {
119235e7444dSHong Zhang         x[i] = aa[j];
119317ab2063SBarry Smith         break;
119417ab2063SBarry Smith       }
119517ab2063SBarry Smith     }
119617ab2063SBarry Smith   }
11971ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
11983a40ed3dSBarry Smith   PetscFunctionReturn(0);
119917ab2063SBarry Smith }
120017ab2063SBarry Smith 
1201c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
12024a2ae208SSatish Balay #undef __FUNCT__
12034a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1204dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
120517ab2063SBarry Smith {
1206416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1207d9ca1df4SBarry Smith   PetscScalar       *y;
1208d9ca1df4SBarry Smith   const PetscScalar *x;
1209dfbe8321SBarry Smith   PetscErrorCode    ierr;
1210d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
12115c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1212d9ca1df4SBarry Smith   const MatScalar   *v;
1213a77337e4SBarry Smith   PetscScalar       alpha;
1214d9ca1df4SBarry Smith   PetscInt          n,i,j;
1215d9ca1df4SBarry Smith   const PetscInt    *idx,*ii,*ridx=NULL;
12163447b6efSHong Zhang   Mat_CompressedRow cprow    = a->compressedrow;
1217ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
12185c897100SBarry Smith #endif
121917ab2063SBarry Smith 
12203a40ed3dSBarry Smith   PetscFunctionBegin;
12212e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
1222d9ca1df4SBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12231ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
12245c897100SBarry Smith 
12255c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1226bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
12275c897100SBarry Smith #else
12283447b6efSHong Zhang   if (usecprow) {
12293447b6efSHong Zhang     m    = cprow.nrows;
12303447b6efSHong Zhang     ii   = cprow.i;
12317b2bb3b9SHong Zhang     ridx = cprow.rindex;
12323447b6efSHong Zhang   } else {
12333447b6efSHong Zhang     ii = a->i;
12343447b6efSHong Zhang   }
123517ab2063SBarry Smith   for (i=0; i<m; i++) {
12363447b6efSHong Zhang     idx = a->j + ii[i];
12373447b6efSHong Zhang     v   = a->a + ii[i];
12383447b6efSHong Zhang     n   = ii[i+1] - ii[i];
12393447b6efSHong Zhang     if (usecprow) {
12407b2bb3b9SHong Zhang       alpha = x[ridx[i]];
12413447b6efSHong Zhang     } else {
124217ab2063SBarry Smith       alpha = x[i];
12433447b6efSHong Zhang     }
124404fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
124517ab2063SBarry Smith   }
12465c897100SBarry Smith #endif
1247dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1248d9ca1df4SBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12491ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12503a40ed3dSBarry Smith   PetscFunctionReturn(0);
125117ab2063SBarry Smith }
125217ab2063SBarry Smith 
12534a2ae208SSatish Balay #undef __FUNCT__
12545c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1255dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
12565c897100SBarry Smith {
1257dfbe8321SBarry Smith   PetscErrorCode ierr;
12585c897100SBarry Smith 
12595c897100SBarry Smith   PetscFunctionBegin;
1260170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
12615c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
12625c897100SBarry Smith   PetscFunctionReturn(0);
12635c897100SBarry Smith }
12645c897100SBarry Smith 
1265c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
126678b84d54SShri Abhyankar 
12675c897100SBarry Smith #undef __FUNCT__
12684a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1269dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
127017ab2063SBarry Smith {
1271416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1272d9fead3dSBarry Smith   PetscScalar       *y;
127354f21887SBarry Smith   const PetscScalar *x;
127454f21887SBarry Smith   const MatScalar   *aa;
1275dfbe8321SBarry Smith   PetscErrorCode    ierr;
1276003131ecSBarry Smith   PetscInt          m=A->rmap->n;
12770298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
12787b083b7cSBarry Smith   PetscInt          n,i;
1279362ced78SSatish Balay   PetscScalar       sum;
1280ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
128117ab2063SBarry Smith 
1282b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
128397952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1284fee21e36SBarry Smith #endif
1285fee21e36SBarry Smith 
12863a40ed3dSBarry Smith   PetscFunctionBegin;
12873649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12881ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
128997952fefSHong Zhang   aj   = a->j;
129097952fefSHong Zhang   aa   = a->a;
1291416022c9SBarry Smith   ii   = a->i;
12924eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
12934f390cb1SBarry Smith     ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr);
129497952fefSHong Zhang     m    = a->compressedrow.nrows;
129597952fefSHong Zhang     ii   = a->compressedrow.i;
129697952fefSHong Zhang     ridx = a->compressedrow.rindex;
129797952fefSHong Zhang     for (i=0; i<m; i++) {
129897952fefSHong Zhang       n           = ii[i+1] - ii[i];
129997952fefSHong Zhang       aj          = a->j + ii[i];
130097952fefSHong Zhang       aa          = a->a + ii[i];
130197952fefSHong Zhang       sum         = 0.0;
1302003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1303003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
130497952fefSHong Zhang       y[*ridx++] = sum;
130597952fefSHong Zhang     }
130697952fefSHong Zhang   } else { /* do not use compressed row format */
1307b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1308b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1309b05257ddSBarry Smith #else
131017ab2063SBarry Smith     for (i=0; i<m; i++) {
1311003131ecSBarry Smith       n           = ii[i+1] - ii[i];
1312003131ecSBarry Smith       aj          = a->j + ii[i];
1313003131ecSBarry Smith       aa          = a->a + ii[i];
131417ab2063SBarry Smith       sum         = 0.0;
1315003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
131617ab2063SBarry Smith       y[i] = sum;
131717ab2063SBarry Smith     }
13188d195f9aSBarry Smith #endif
1319b05257ddSBarry Smith   }
13207b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
13213649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13221ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13233a40ed3dSBarry Smith   PetscFunctionReturn(0);
132417ab2063SBarry Smith }
132517ab2063SBarry Smith 
1326b434eb95SMatthew G. Knepley #undef __FUNCT__
1327b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultMax_SeqAIJ"
1328b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1329b434eb95SMatthew G. Knepley {
1330b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1331b434eb95SMatthew G. Knepley   PetscScalar       *y;
1332b434eb95SMatthew G. Knepley   const PetscScalar *x;
1333b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1334b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1335b434eb95SMatthew G. Knepley   PetscInt          m=A->rmap->n;
1336b434eb95SMatthew G. Knepley   const PetscInt    *aj,*ii,*ridx=NULL;
1337b434eb95SMatthew G. Knepley   PetscInt          n,i,nonzerorow=0;
1338b434eb95SMatthew G. Knepley   PetscScalar       sum;
1339b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1340b434eb95SMatthew G. Knepley 
1341b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1342b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa)
1343b434eb95SMatthew G. Knepley #endif
1344b434eb95SMatthew G. Knepley 
1345b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1346b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1347b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1348b434eb95SMatthew G. Knepley   aj   = a->j;
1349b434eb95SMatthew G. Knepley   aa   = a->a;
1350b434eb95SMatthew G. Knepley   ii   = a->i;
1351b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1352b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1353b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1354b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1355b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1356b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1357b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1358b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1359b434eb95SMatthew G. Knepley       sum         = 0.0;
1360b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1361b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1362b434eb95SMatthew G. Knepley       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1363b434eb95SMatthew G. Knepley       y[*ridx++] = sum;
1364b434eb95SMatthew G. Knepley     }
1365b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
1366b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1367b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1368b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1369b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1370b434eb95SMatthew G. Knepley       sum         = 0.0;
1371b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1372b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1373b434eb95SMatthew G. Knepley       y[i] = sum;
1374b434eb95SMatthew G. Knepley     }
1375b434eb95SMatthew G. Knepley   }
1376b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1377b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1378b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1379b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1380b434eb95SMatthew G. Knepley }
1381b434eb95SMatthew G. Knepley 
1382b434eb95SMatthew G. Knepley #undef __FUNCT__
1383b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultAddMax_SeqAIJ"
1384b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1385b434eb95SMatthew G. Knepley {
1386b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1387b434eb95SMatthew G. Knepley   PetscScalar       *y,*z;
1388b434eb95SMatthew G. Knepley   const PetscScalar *x;
1389b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1390b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1391b434eb95SMatthew G. Knepley   PetscInt          m = A->rmap->n,*aj,*ii;
1392b434eb95SMatthew G. Knepley   PetscInt          n,i,*ridx=NULL;
1393b434eb95SMatthew G. Knepley   PetscScalar       sum;
1394b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1395b434eb95SMatthew G. Knepley 
1396b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1397b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1398d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1399b434eb95SMatthew G. Knepley 
1400b434eb95SMatthew G. Knepley   aj = a->j;
1401b434eb95SMatthew G. Knepley   aa = a->a;
1402b434eb95SMatthew G. Knepley   ii = a->i;
1403b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1404b434eb95SMatthew G. Knepley     if (zz != yy) {
1405b434eb95SMatthew G. Knepley       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1406b434eb95SMatthew G. Knepley     }
1407b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1408b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1409b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1410b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1411b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1412b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1413b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1414b434eb95SMatthew G. Knepley       sum = y[*ridx];
1415b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1416b434eb95SMatthew G. Knepley       z[*ridx++] = sum;
1417b434eb95SMatthew G. Knepley     }
1418b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
1419b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1420b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1421b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1422b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1423b434eb95SMatthew G. Knepley       sum = y[i];
1424b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1425b434eb95SMatthew G. Knepley       z[i] = sum;
1426b434eb95SMatthew G. Knepley     }
1427b434eb95SMatthew G. Knepley   }
1428b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1429b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1430d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1431b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1432b434eb95SMatthew G. Knepley }
1433b434eb95SMatthew G. Knepley 
1434c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
14354a2ae208SSatish Balay #undef __FUNCT__
14364a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1437dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
143817ab2063SBarry Smith {
1439416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1440f15663dcSBarry Smith   PetscScalar       *y,*z;
1441f15663dcSBarry Smith   const PetscScalar *x;
144254f21887SBarry Smith   const MatScalar   *aa;
1443dfbe8321SBarry Smith   PetscErrorCode    ierr;
1444d9ca1df4SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
1445d9ca1df4SBarry Smith   PetscInt          m = A->rmap->n,n,i;
1446362ced78SSatish Balay   PetscScalar       sum;
1447ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
14489ea0dfa2SSatish Balay 
14493a40ed3dSBarry Smith   PetscFunctionBegin;
1450f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1451d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1452bfeeae90SHong Zhang 
145397952fefSHong Zhang   aj = a->j;
145497952fefSHong Zhang   aa = a->a;
1455cddf8d76SBarry Smith   ii = a->i;
14564eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
14574eb6d288SHong Zhang     if (zz != yy) {
14584eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
14594eb6d288SHong Zhang     }
146097952fefSHong Zhang     m    = a->compressedrow.nrows;
146197952fefSHong Zhang     ii   = a->compressedrow.i;
146297952fefSHong Zhang     ridx = a->compressedrow.rindex;
146397952fefSHong Zhang     for (i=0; i<m; i++) {
146497952fefSHong Zhang       n   = ii[i+1] - ii[i];
146597952fefSHong Zhang       aj  = a->j + ii[i];
146697952fefSHong Zhang       aa  = a->a + ii[i];
146797952fefSHong Zhang       sum = y[*ridx];
1468f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
146997952fefSHong Zhang       z[*ridx++] = sum;
147097952fefSHong Zhang     }
147197952fefSHong Zhang   } else { /* do not use compressed row format */
1472f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1473f15663dcSBarry Smith     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1474f15663dcSBarry Smith #else
147517ab2063SBarry Smith     for (i=0; i<m; i++) {
1476f15663dcSBarry Smith       n   = ii[i+1] - ii[i];
1477f15663dcSBarry Smith       aj  = a->j + ii[i];
1478f15663dcSBarry Smith       aa  = a->a + ii[i];
147917ab2063SBarry Smith       sum = y[i];
1480f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
148117ab2063SBarry Smith       z[i] = sum;
148217ab2063SBarry Smith     }
148302ab625aSSatish Balay #endif
1484f15663dcSBarry Smith   }
1485dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1486f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1487d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
14888154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
14896b375ea7SVictor Minden   /*
1490918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1491918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1492918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
14936b375ea7SVictor Minden   */
1494918e98c3SVictor Minden #endif
14953a40ed3dSBarry Smith   PetscFunctionReturn(0);
149617ab2063SBarry Smith }
149717ab2063SBarry Smith 
149817ab2063SBarry Smith /*
149917ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
150017ab2063SBarry Smith */
15014a2ae208SSatish Balay #undef __FUNCT__
15024a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1503dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
150417ab2063SBarry Smith {
1505416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15066849ba73SBarry Smith   PetscErrorCode ierr;
1507d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
150817ab2063SBarry Smith 
15093a40ed3dSBarry Smith   PetscFunctionBegin;
151009f38230SBarry Smith   if (!a->diag) {
1511785e854fSJed Brown     ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr);
15123bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr);
151309f38230SBarry Smith   }
1514d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
151509f38230SBarry Smith     a->diag[i] = a->i[i+1];
1516bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1517bfeeae90SHong Zhang       if (a->j[j] == i) {
151809f38230SBarry Smith         a->diag[i] = j;
151917ab2063SBarry Smith         break;
152017ab2063SBarry Smith       }
152117ab2063SBarry Smith     }
152217ab2063SBarry Smith   }
15233a40ed3dSBarry Smith   PetscFunctionReturn(0);
152417ab2063SBarry Smith }
152517ab2063SBarry Smith 
1526be5855fcSBarry Smith /*
1527be5855fcSBarry Smith      Checks for missing diagonals
1528be5855fcSBarry Smith */
15294a2ae208SSatish Balay #undef __FUNCT__
15304a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1531ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1532be5855fcSBarry Smith {
1533be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
15347734d3b5SMatthew G. Knepley   PetscInt   *diag,*ii = a->i,i;
1535be5855fcSBarry Smith 
1536be5855fcSBarry Smith   PetscFunctionBegin;
153709f38230SBarry Smith   *missing = PETSC_FALSE;
15387734d3b5SMatthew G. Knepley   if (A->rmap->n > 0 && !ii) {
153909f38230SBarry Smith     *missing = PETSC_TRUE;
154009f38230SBarry Smith     if (d) *d = 0;
1541955c1f14SBarry Smith     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
154209f38230SBarry Smith   } else {
1543f1e2ffcdSBarry Smith     diag = a->diag;
1544d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
15457734d3b5SMatthew G. Knepley       if (diag[i] >= ii[i+1]) {
154609f38230SBarry Smith         *missing = PETSC_TRUE;
154709f38230SBarry Smith         if (d) *d = i;
1548955c1f14SBarry Smith         PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
1549358d2f5dSShri Abhyankar         break;
155009f38230SBarry Smith       }
1551be5855fcSBarry Smith     }
1552be5855fcSBarry Smith   }
1553be5855fcSBarry Smith   PetscFunctionReturn(0);
1554be5855fcSBarry Smith }
1555be5855fcSBarry Smith 
155671f1c65dSBarry Smith #undef __FUNCT__
155771f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
1558422a814eSBarry Smith /*
1559422a814eSBarry Smith    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1560422a814eSBarry Smith */
15617087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
156271f1c65dSBarry Smith {
156371f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
156471f1c65dSBarry Smith   PetscErrorCode ierr;
1565d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
156654f21887SBarry Smith   MatScalar      *v = a->a;
156754f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
156871f1c65dSBarry Smith 
156971f1c65dSBarry Smith   PetscFunctionBegin;
157071f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
157171f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
157271f1c65dSBarry Smith   diag = a->diag;
157371f1c65dSBarry Smith   if (!a->idiag) {
1574dcca6d9dSJed Brown     ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr);
15753bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
157671f1c65dSBarry Smith     v    = a->a;
157771f1c65dSBarry Smith   }
157871f1c65dSBarry Smith   mdiag = a->mdiag;
157971f1c65dSBarry Smith   idiag = a->idiag;
158071f1c65dSBarry Smith 
1581422a814eSBarry Smith   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
158271f1c65dSBarry Smith     for (i=0; i<m; i++) {
158371f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1584899639b0SHong Zhang       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1585899639b0SHong Zhang         if (PetscRealPart(fshift)) {
1586899639b0SHong Zhang           ierr = PetscInfo1(A,"Zero diagonal on row %D\n",i);CHKERRQ(ierr);
1587899639b0SHong Zhang           A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1588899639b0SHong Zhang         } else {
1589899639b0SHong Zhang           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1590899639b0SHong Zhang         }
1591899639b0SHong Zhang       }
159271f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
159371f1c65dSBarry Smith     }
159471f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
159571f1c65dSBarry Smith   } else {
159671f1c65dSBarry Smith     for (i=0; i<m; i++) {
159771f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
159871f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
159971f1c65dSBarry Smith     }
1600dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
160171f1c65dSBarry Smith   }
160271f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
160371f1c65dSBarry Smith   PetscFunctionReturn(0);
160471f1c65dSBarry Smith }
160571f1c65dSBarry Smith 
1606c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
16074a2ae208SSatish Balay #undef __FUNCT__
160841f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
160941f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
161017ab2063SBarry Smith {
1611416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1612e6d1f457SBarry Smith   PetscScalar       *x,d,sum,*t,scale;
1613e6d1f457SBarry Smith   const MatScalar   *v = a->a,*idiag=0,*mdiag;
161454f21887SBarry Smith   const PetscScalar *b, *bs,*xb, *ts;
1615dfbe8321SBarry Smith   PetscErrorCode    ierr;
1616d0f46423SBarry Smith   PetscInt          n = A->cmap->n,m = A->rmap->n,i;
161797f1f81fSBarry Smith   const PetscInt    *idx,*diag;
161817ab2063SBarry Smith 
16193a40ed3dSBarry Smith   PetscFunctionBegin;
1620b965ef7fSBarry Smith   its = its*lits;
162191723122SBarry Smith 
162271f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
162371f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
162471f1c65dSBarry Smith   a->fshift = fshift;
162571f1c65dSBarry Smith   a->omega  = omega;
1626ed480e8bSBarry Smith 
162771f1c65dSBarry Smith   diag  = a->diag;
162871f1c65dSBarry Smith   t     = a->ssor_work;
1629ed480e8bSBarry Smith   idiag = a->idiag;
163071f1c65dSBarry Smith   mdiag = a->mdiag;
1631ed480e8bSBarry Smith 
16321ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
16333649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1634ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
163517ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
163617ab2063SBarry Smith     /* apply (U + D/omega) to the vector */
1637ed480e8bSBarry Smith     bs = b;
163817ab2063SBarry Smith     for (i=0; i<m; i++) {
163971f1c65dSBarry Smith       d   = fshift + mdiag[i];
1640416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1641ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1642ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
164317ab2063SBarry Smith       sum = b[i]*d/omega;
1644003131ecSBarry Smith       PetscSparseDensePlusDot(sum,bs,v,idx,n);
164517ab2063SBarry Smith       x[i] = sum;
164617ab2063SBarry Smith     }
16471ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16483649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1649efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
16503a40ed3dSBarry Smith     PetscFunctionReturn(0);
165117ab2063SBarry Smith   }
1652c783ea89SBarry Smith 
16532205254eSKarl Rupp   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
16542205254eSKarl Rupp   else if (flag & SOR_EISENSTAT) {
165517ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1656887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
165717ab2063SBarry Smith 
165817ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
165917ab2063SBarry Smith 
1660887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
166117ab2063SBarry Smith     */
166217ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
166317ab2063SBarry Smith 
166417ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
166517ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1666416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1667ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1668ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
166917ab2063SBarry Smith       sum = b[i];
1670e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1671ed480e8bSBarry Smith       x[i] = sum*idiag[i];
167217ab2063SBarry Smith     }
167317ab2063SBarry Smith 
167417ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1675416022c9SBarry Smith     v = a->a;
16762205254eSKarl Rupp     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
167717ab2063SBarry Smith 
167817ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1679ed480e8bSBarry Smith     ts   = t;
1680416022c9SBarry Smith     diag = a->diag;
168117ab2063SBarry Smith     for (i=0; i<m; i++) {
1682416022c9SBarry Smith       n   = diag[i] - a->i[i];
1683ed480e8bSBarry Smith       idx = a->j + a->i[i];
1684ed480e8bSBarry Smith       v   = a->a + a->i[i];
168517ab2063SBarry Smith       sum = t[i];
1686003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1687ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1688733d66baSBarry Smith       /*  x = x + t */
1689733d66baSBarry Smith       x[i] += t[i];
169017ab2063SBarry Smith     }
169117ab2063SBarry Smith 
1692dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
16931ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16943649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
16953a40ed3dSBarry Smith     PetscFunctionReturn(0);
169617ab2063SBarry Smith   }
169717ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
169817ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
169917ab2063SBarry Smith       for (i=0; i<m; i++) {
1700416022c9SBarry Smith         n   = diag[i] - a->i[i];
1701ed480e8bSBarry Smith         idx = a->j + a->i[i];
1702ed480e8bSBarry Smith         v   = a->a + a->i[i];
170317ab2063SBarry Smith         sum = b[i];
1704e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
17055c99c7daSBarry Smith         t[i] = sum;
1706ed480e8bSBarry Smith         x[i] = sum*idiag[i];
170717ab2063SBarry Smith       }
17085c99c7daSBarry Smith       xb   = t;
1709efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17103a40ed3dSBarry Smith     } else xb = b;
171117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
171217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1713416022c9SBarry Smith         n   = a->i[i+1] - diag[i] - 1;
1714ed480e8bSBarry Smith         idx = a->j + diag[i] + 1;
1715ed480e8bSBarry Smith         v   = a->a + diag[i] + 1;
171617ab2063SBarry Smith         sum = xb[i];
1717e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
17185c99c7daSBarry Smith         if (xb == b) {
1719ed480e8bSBarry Smith           x[i] = sum*idiag[i];
17205c99c7daSBarry Smith         } else {
1721b19a5dc2SMark Adams           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
172217ab2063SBarry Smith         }
17235c99c7daSBarry Smith       }
1724b19a5dc2SMark Adams       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
172517ab2063SBarry Smith     }
172617ab2063SBarry Smith     its--;
172717ab2063SBarry Smith   }
172817ab2063SBarry Smith   while (its--) {
172917ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
173017ab2063SBarry Smith       for (i=0; i<m; i++) {
1731b19a5dc2SMark Adams         /* lower */
1732b19a5dc2SMark Adams         n   = diag[i] - a->i[i];
1733ed480e8bSBarry Smith         idx = a->j + a->i[i];
1734ed480e8bSBarry Smith         v   = a->a + a->i[i];
173517ab2063SBarry Smith         sum = b[i];
1736e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1737b19a5dc2SMark Adams         t[i] = sum;             /* save application of the lower-triangular part */
1738b19a5dc2SMark Adams         /* upper */
1739b19a5dc2SMark Adams         n   = a->i[i+1] - diag[i] - 1;
1740b19a5dc2SMark Adams         idx = a->j + diag[i] + 1;
1741b19a5dc2SMark Adams         v   = a->a + diag[i] + 1;
1742b19a5dc2SMark Adams         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1743b19a5dc2SMark Adams         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
174417ab2063SBarry Smith       }
1745b19a5dc2SMark Adams       xb   = t;
17469f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1747b19a5dc2SMark Adams     } else xb = b;
174817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
174917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1750b19a5dc2SMark Adams         sum = xb[i];
1751b19a5dc2SMark Adams         if (xb == b) {
1752b19a5dc2SMark Adams           /* whole matrix (no checkpointing available) */
1753416022c9SBarry Smith           n   = a->i[i+1] - a->i[i];
1754ed480e8bSBarry Smith           idx = a->j + a->i[i];
1755ed480e8bSBarry Smith           v   = a->a + a->i[i];
1756e6d1f457SBarry Smith           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1757ed480e8bSBarry Smith           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1758b19a5dc2SMark Adams         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1759b19a5dc2SMark Adams           n   = a->i[i+1] - diag[i] - 1;
1760b19a5dc2SMark Adams           idx = a->j + diag[i] + 1;
1761b19a5dc2SMark Adams           v   = a->a + diag[i] + 1;
1762b19a5dc2SMark Adams           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1763b19a5dc2SMark Adams           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
176417ab2063SBarry Smith         }
1765b19a5dc2SMark Adams       }
1766b19a5dc2SMark Adams       if (xb == b) {
17679f863219SBarry Smith         ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1768b19a5dc2SMark Adams       } else {
1769b19a5dc2SMark Adams         ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1770b19a5dc2SMark Adams       }
177117ab2063SBarry Smith     }
177217ab2063SBarry Smith   }
17731ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
17743649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1775365a8a9eSBarry Smith   PetscFunctionReturn(0);
177617ab2063SBarry Smith }
177717ab2063SBarry Smith 
17782af78befSBarry Smith 
17794a2ae208SSatish Balay #undef __FUNCT__
17804a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1781dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
178217ab2063SBarry Smith {
1783416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
17844e220ebcSLois Curfman McInnes 
17853a40ed3dSBarry Smith   PetscFunctionBegin;
17864e220ebcSLois Curfman McInnes   info->block_size   = 1.0;
17874e220ebcSLois Curfman McInnes   info->nz_allocated = (double)a->maxnz;
17884e220ebcSLois Curfman McInnes   info->nz_used      = (double)a->nz;
17894e220ebcSLois Curfman McInnes   info->nz_unneeded  = (double)(a->maxnz - a->nz);
17904e220ebcSLois Curfman McInnes   info->assemblies   = (double)A->num_ass;
17918e58a170SBarry Smith   info->mallocs      = (double)A->info.mallocs;
17927adad957SLisandro Dalcin   info->memory       = ((PetscObject)A)->mem;
1793d5f3da31SBarry Smith   if (A->factortype) {
17944e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
17954e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
17964e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
17974e220ebcSLois Curfman McInnes   } else {
17984e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
17994e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
18004e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
18014e220ebcSLois Curfman McInnes   }
18023a40ed3dSBarry Smith   PetscFunctionReturn(0);
180317ab2063SBarry Smith }
180417ab2063SBarry Smith 
18054a2ae208SSatish Balay #undef __FUNCT__
18064a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
18072b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
180817ab2063SBarry Smith {
1809416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
18103b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
18116849ba73SBarry Smith   PetscErrorCode    ierr;
181297b48c8fSBarry Smith   const PetscScalar *xx;
181397b48c8fSBarry Smith   PetscScalar       *bb;
1814ace3abfcSBarry Smith   PetscBool         missing;
181517ab2063SBarry Smith 
18163a40ed3dSBarry Smith   PetscFunctionBegin;
181797b48c8fSBarry Smith   if (x && b) {
181897b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
181997b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
182097b48c8fSBarry Smith     for (i=0; i<N; i++) {
182197b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
182297b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
182397b48c8fSBarry Smith     }
182497b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
182597b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
182697b48c8fSBarry Smith   }
182797b48c8fSBarry Smith 
1828a9817697SBarry Smith   if (a->keepnonzeropattern) {
1829f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1830e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1831bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1832f1e2ffcdSBarry Smith     }
1833f4df32b1SMatthew Knepley     if (diag != 0.0) {
183409f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1835e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1836f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1837f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1838f1e2ffcdSBarry Smith       }
1839f1e2ffcdSBarry Smith     }
1840f1e2ffcdSBarry Smith   } else {
1841f4df32b1SMatthew Knepley     if (diag != 0.0) {
184217ab2063SBarry Smith       for (i=0; i<N; i++) {
1843e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
18447ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1845416022c9SBarry Smith           a->ilen[rows[i]]    = 1;
1846f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1847bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
18487ae801bdSBarry Smith         } else { /* in case row was completely empty */
1849f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
185017ab2063SBarry Smith         }
185117ab2063SBarry Smith       }
18523a40ed3dSBarry Smith     } else {
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]);
1855416022c9SBarry Smith         a->ilen[rows[i]] = 0;
185617ab2063SBarry Smith       }
185717ab2063SBarry Smith     }
1858e56f5c9eSBarry Smith     A->nonzerostate++;
1859f1e2ffcdSBarry Smith   }
186043a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18613a40ed3dSBarry Smith   PetscFunctionReturn(0);
186217ab2063SBarry Smith }
186317ab2063SBarry Smith 
18644a2ae208SSatish Balay #undef __FUNCT__
18656e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
18666e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
18676e169961SBarry Smith {
18686e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
18696e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
18706e169961SBarry Smith   PetscErrorCode    ierr;
18712b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
18726e169961SBarry Smith   const PetscScalar *xx;
18736e169961SBarry Smith   PetscScalar       *bb;
18746e169961SBarry Smith 
18756e169961SBarry Smith   PetscFunctionBegin;
18766e169961SBarry Smith   if (x && b) {
18776e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
18786e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
18792b40b63fSBarry Smith     vecs = PETSC_TRUE;
18806e169961SBarry Smith   }
18811795a4d1SJed Brown   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
18826e169961SBarry Smith   for (i=0; i<N; i++) {
18836e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
18846e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
18852205254eSKarl Rupp 
18866e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
18876e169961SBarry Smith   }
18886e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
18896e169961SBarry Smith     if (!zeroed[i]) {
18906e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
18916e169961SBarry Smith         if (zeroed[a->j[j]]) {
18922b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
18936e169961SBarry Smith           a->a[j] = 0.0;
18946e169961SBarry Smith         }
18956e169961SBarry Smith       }
18962b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
18976e169961SBarry Smith   }
18986e169961SBarry Smith   if (x && b) {
18996e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
19006e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
19016e169961SBarry Smith   }
19026e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
19036e169961SBarry Smith   if (diag != 0.0) {
19046e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
19056e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
19066e169961SBarry Smith     for (i=0; i<N; i++) {
19076e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
19086e169961SBarry Smith     }
19096e169961SBarry Smith   }
19106e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19116e169961SBarry Smith   PetscFunctionReturn(0);
19126e169961SBarry Smith }
19136e169961SBarry Smith 
19146e169961SBarry Smith #undef __FUNCT__
19154a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1916a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
191717ab2063SBarry Smith {
1918416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
191997f1f81fSBarry Smith   PetscInt   *itmp;
192017ab2063SBarry Smith 
19213a40ed3dSBarry Smith   PetscFunctionBegin;
1922e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
192317ab2063SBarry Smith 
1924416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1925bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
192617ab2063SBarry Smith   if (idx) {
1927bfeeae90SHong Zhang     itmp = a->j + a->i[row];
192826fbe8dcSKarl Rupp     if (*nz) *idx = itmp;
192917ab2063SBarry Smith     else *idx = 0;
193017ab2063SBarry Smith   }
19313a40ed3dSBarry Smith   PetscFunctionReturn(0);
193217ab2063SBarry Smith }
193317ab2063SBarry Smith 
1934bfeeae90SHong Zhang /* remove this function? */
19354a2ae208SSatish Balay #undef __FUNCT__
19364a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1937a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
193817ab2063SBarry Smith {
19393a40ed3dSBarry Smith   PetscFunctionBegin;
19403a40ed3dSBarry Smith   PetscFunctionReturn(0);
194117ab2063SBarry Smith }
194217ab2063SBarry Smith 
19434a2ae208SSatish Balay #undef __FUNCT__
19444a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1945dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
194617ab2063SBarry Smith {
1947416022c9SBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
194854f21887SBarry Smith   MatScalar      *v  = a->a;
194936db0b34SBarry Smith   PetscReal      sum = 0.0;
19506849ba73SBarry Smith   PetscErrorCode ierr;
195197f1f81fSBarry Smith   PetscInt       i,j;
195217ab2063SBarry Smith 
19533a40ed3dSBarry Smith   PetscFunctionBegin;
195417ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1955416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
195636db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
195717ab2063SBarry Smith     }
19588f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
19593a40ed3dSBarry Smith   } else if (type == NORM_1) {
196036db0b34SBarry Smith     PetscReal *tmp;
196197f1f81fSBarry Smith     PetscInt  *jj = a->j;
19621795a4d1SJed Brown     ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr);
1963064f8208SBarry Smith     *nrm = 0.0;
1964416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1965bfeeae90SHong Zhang       tmp[*jj++] += PetscAbsScalar(*v);  v++;
196617ab2063SBarry Smith     }
1967d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1968064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
196917ab2063SBarry Smith     }
1970606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
19713a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1972064f8208SBarry Smith     *nrm = 0.0;
1973d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1974bfeeae90SHong Zhang       v   = a->a + a->i[j];
197517ab2063SBarry Smith       sum = 0.0;
1976416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1977cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
197817ab2063SBarry Smith       }
1979064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
198017ab2063SBarry Smith     }
1981f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
19823a40ed3dSBarry Smith   PetscFunctionReturn(0);
198317ab2063SBarry Smith }
198417ab2063SBarry Smith 
19854e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
19864e938277SHong Zhang #undef __FUNCT__
19874e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
19884e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
19894e938277SHong Zhang {
19904e938277SHong Zhang   PetscErrorCode ierr;
19914e938277SHong Zhang   PetscInt       i,j,anzj;
19924e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
19934e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
19944e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
19954e938277SHong Zhang 
19964e938277SHong Zhang   PetscFunctionBegin;
19974e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
1998854ce69bSBarry Smith   ierr = PetscCalloc1(an+1,&ati);CHKERRQ(ierr);
1999785e854fSJed Brown   ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr);
2000785e854fSJed Brown   ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr);
20014e938277SHong Zhang 
20024e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
20034e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
200426fbe8dcSKarl Rupp   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
20054e938277SHong Zhang   /* Form ati for csr format of A^T. */
200626fbe8dcSKarl Rupp   for (i=0;i<an;i++) ati[i+1] += ati[i];
20074e938277SHong Zhang 
20084e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
20094e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
20104e938277SHong Zhang 
20114e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
20124e938277SHong Zhang   for (i=0;i<am;i++) {
20134e938277SHong Zhang     anzj = ai[i+1] - ai[i];
20144e938277SHong Zhang     for (j=0;j<anzj;j++) {
20154e938277SHong Zhang       atj[atfill[*aj]] = i;
20164e938277SHong Zhang       atfill[*aj++]   += 1;
20174e938277SHong Zhang     }
20184e938277SHong Zhang   }
20194e938277SHong Zhang 
20204e938277SHong Zhang   /* Clean up temporary space and complete requests. */
20214e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
2022ce94432eSBarry Smith   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
202333d57670SJed Brown   ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
2024a2f3521dSMark F. Adams 
20254e938277SHong Zhang   b          = (Mat_SeqAIJ*)((*B)->data);
20264e938277SHong Zhang   b->free_a  = PETSC_FALSE;
20274e938277SHong Zhang   b->free_ij = PETSC_TRUE;
20284e938277SHong Zhang   b->nonew   = 0;
20294e938277SHong Zhang   PetscFunctionReturn(0);
20304e938277SHong Zhang }
20314e938277SHong Zhang 
20324a2ae208SSatish Balay #undef __FUNCT__
20334a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
2034fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
203517ab2063SBarry Smith {
2036416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2037416022c9SBarry Smith   Mat            C;
20386849ba73SBarry Smith   PetscErrorCode ierr;
2039d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
204054f21887SBarry Smith   MatScalar      *array = a->a;
204117ab2063SBarry Smith 
20423a40ed3dSBarry Smith   PetscFunctionBegin;
2043e32f2f54SBarry 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");
2044fc4dec0aSBarry Smith 
2045fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
2046854ce69bSBarry Smith     ierr = PetscCalloc1(1+A->cmap->n,&col);CHKERRQ(ierr);
2047bfeeae90SHong Zhang 
2048bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
2049ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2050d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
205133d57670SJed Brown     ierr = MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
20527adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2053ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
2054606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
2055a541d17aSBarry Smith   } else {
2056a541d17aSBarry Smith     C = *B;
2057a541d17aSBarry Smith   }
2058a541d17aSBarry Smith 
205917ab2063SBarry Smith   for (i=0; i<m; i++) {
206017ab2063SBarry Smith     len    = ai[i+1]-ai[i];
206187d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
2062b9b97703SBarry Smith     array += len;
2063b9b97703SBarry Smith     aj    += len;
206417ab2063SBarry Smith   }
20656d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20666d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
206717ab2063SBarry Smith 
2068815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
2069416022c9SBarry Smith     *B = C;
207017ab2063SBarry Smith   } else {
207128be2f97SBarry Smith     ierr = MatHeaderMerge(A,&C);CHKERRQ(ierr);
207217ab2063SBarry Smith   }
20733a40ed3dSBarry Smith   PetscFunctionReturn(0);
207417ab2063SBarry Smith }
207517ab2063SBarry Smith 
2076cd0d46ebSvictorle #undef __FUNCT__
20775fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
20787087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2079cd0d46ebSvictorle {
2080cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
208154f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
208254f21887SBarry Smith   MatScalar      *va,*vb;
20836849ba73SBarry Smith   PetscErrorCode ierr;
208497f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
2085cd0d46ebSvictorle 
2086cd0d46ebSvictorle   PetscFunctionBegin;
2087cd0d46ebSvictorle   bij = (Mat_SeqAIJ*) B->data;
2088cd0d46ebSvictorle 
2089cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2090cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
20915485867bSBarry Smith   if (ma!=nb || na!=mb) {
20925485867bSBarry Smith     *f = PETSC_FALSE;
20935485867bSBarry Smith     PetscFunctionReturn(0);
20945485867bSBarry Smith   }
2095cd0d46ebSvictorle   aii  = aij->i; bii = bij->i;
2096cd0d46ebSvictorle   adx  = aij->j; bdx = bij->j;
2097cd0d46ebSvictorle   va   = aij->a; vb = bij->a;
2098785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2099785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
2100cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
2101cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
2102cd0d46ebSvictorle 
2103cd0d46ebSvictorle   *f = PETSC_TRUE;
2104cd0d46ebSvictorle   for (i=0; i<ma; i++) {
2105cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
210697f1f81fSBarry Smith       PetscInt    idc,idr;
21075485867bSBarry Smith       PetscScalar vc,vr;
2108cd0d46ebSvictorle       /* column/row index/value */
21095485867bSBarry Smith       idc = adx[aptr[i]];
21105485867bSBarry Smith       idr = bdx[bptr[idc]];
21115485867bSBarry Smith       vc  = va[aptr[i]];
21125485867bSBarry Smith       vr  = vb[bptr[idc]];
21135485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
21145485867bSBarry Smith         *f = PETSC_FALSE;
21155485867bSBarry Smith         goto done;
2116cd0d46ebSvictorle       } else {
21175485867bSBarry Smith         aptr[i]++;
21185485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
2119cd0d46ebSvictorle       }
2120cd0d46ebSvictorle     }
2121cd0d46ebSvictorle   }
2122cd0d46ebSvictorle done:
2123cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
21243aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
2125cd0d46ebSvictorle   PetscFunctionReturn(0);
2126cd0d46ebSvictorle }
2127cd0d46ebSvictorle 
21281cbb95d3SBarry Smith #undef __FUNCT__
21291cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
21307087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
21311cbb95d3SBarry Smith {
21321cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
213354f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
213454f21887SBarry Smith   MatScalar      *va,*vb;
21351cbb95d3SBarry Smith   PetscErrorCode ierr;
21361cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
21371cbb95d3SBarry Smith 
21381cbb95d3SBarry Smith   PetscFunctionBegin;
21391cbb95d3SBarry Smith   bij = (Mat_SeqAIJ*) B->data;
21401cbb95d3SBarry Smith 
21411cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
21421cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
21431cbb95d3SBarry Smith   if (ma!=nb || na!=mb) {
21441cbb95d3SBarry Smith     *f = PETSC_FALSE;
21451cbb95d3SBarry Smith     PetscFunctionReturn(0);
21461cbb95d3SBarry Smith   }
21471cbb95d3SBarry Smith   aii  = aij->i; bii = bij->i;
21481cbb95d3SBarry Smith   adx  = aij->j; bdx = bij->j;
21491cbb95d3SBarry Smith   va   = aij->a; vb = bij->a;
2150785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2151785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
21521cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
21531cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
21541cbb95d3SBarry Smith 
21551cbb95d3SBarry Smith   *f = PETSC_TRUE;
21561cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
21571cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
21581cbb95d3SBarry Smith       PetscInt    idc,idr;
21591cbb95d3SBarry Smith       PetscScalar vc,vr;
21601cbb95d3SBarry Smith       /* column/row index/value */
21611cbb95d3SBarry Smith       idc = adx[aptr[i]];
21621cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
21631cbb95d3SBarry Smith       vc  = va[aptr[i]];
21641cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
21651cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
21661cbb95d3SBarry Smith         *f = PETSC_FALSE;
21671cbb95d3SBarry Smith         goto done;
21681cbb95d3SBarry Smith       } else {
21691cbb95d3SBarry Smith         aptr[i]++;
21701cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
21711cbb95d3SBarry Smith       }
21721cbb95d3SBarry Smith     }
21731cbb95d3SBarry Smith   }
21741cbb95d3SBarry Smith done:
21751cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
21761cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
21771cbb95d3SBarry Smith   PetscFunctionReturn(0);
21781cbb95d3SBarry Smith }
21791cbb95d3SBarry Smith 
21809e29f15eSvictorle #undef __FUNCT__
21819e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2182ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
21839e29f15eSvictorle {
2184dfbe8321SBarry Smith   PetscErrorCode ierr;
21856e111a19SKarl Rupp 
21869e29f15eSvictorle   PetscFunctionBegin;
21875485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
21889e29f15eSvictorle   PetscFunctionReturn(0);
21899e29f15eSvictorle }
21909e29f15eSvictorle 
21914a2ae208SSatish Balay #undef __FUNCT__
21921cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2193ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
21941cbb95d3SBarry Smith {
21951cbb95d3SBarry Smith   PetscErrorCode ierr;
21966e111a19SKarl Rupp 
21971cbb95d3SBarry Smith   PetscFunctionBegin;
21981cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
21991cbb95d3SBarry Smith   PetscFunctionReturn(0);
22001cbb95d3SBarry Smith }
22011cbb95d3SBarry Smith 
22021cbb95d3SBarry Smith #undef __FUNCT__
22034a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2204dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
220517ab2063SBarry Smith {
2206416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
220754f21887SBarry Smith   PetscScalar    *l,*r,x;
220854f21887SBarry Smith   MatScalar      *v;
2209dfbe8321SBarry Smith   PetscErrorCode ierr;
2210d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
221117ab2063SBarry Smith 
22123a40ed3dSBarry Smith   PetscFunctionBegin;
221317ab2063SBarry Smith   if (ll) {
22143ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
22153ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2216e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2217e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
22181ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2219416022c9SBarry Smith     v    = a->a;
222017ab2063SBarry Smith     for (i=0; i<m; i++) {
222117ab2063SBarry Smith       x = l[i];
2222416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
22232205254eSKarl Rupp       for (j=0; j<M; j++) (*v++) *= x;
222417ab2063SBarry Smith     }
22251ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2226efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
222717ab2063SBarry Smith   }
222817ab2063SBarry Smith   if (rr) {
2229e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2230e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
22311ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2232416022c9SBarry Smith     v    = a->a; jj = a->j;
22332205254eSKarl Rupp     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
22341ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2235efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
223617ab2063SBarry Smith   }
2237acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
22383a40ed3dSBarry Smith   PetscFunctionReturn(0);
223917ab2063SBarry Smith }
224017ab2063SBarry Smith 
22414a2ae208SSatish Balay #undef __FUNCT__
22424a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
224397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
224417ab2063SBarry Smith {
2245db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
22466849ba73SBarry Smith   PetscErrorCode ierr;
2247d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
224897f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
22495d0c19d7SBarry Smith   const PetscInt *irow,*icol;
22505d0c19d7SBarry Smith   PetscInt       nrows,ncols;
225197f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
225254f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2253416022c9SBarry Smith   Mat            C;
2254cdc6f3adSToby Isaac   PetscBool      stride;
225517ab2063SBarry Smith 
22563a40ed3dSBarry Smith   PetscFunctionBegin;
225799141d43SSatish Balay 
225817ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2259b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2260b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
226117ab2063SBarry Smith 
2262251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2263ff718158SBarry Smith   if (stride) {
2264ff718158SBarry Smith     ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2265ff718158SBarry Smith   } else {
2266ff718158SBarry Smith     first = 0;
2267ff718158SBarry Smith     step  = 0;
2268ff718158SBarry Smith   }
2269fee21e36SBarry Smith   if (stride && step == 1) {
227002834360SBarry Smith     /* special case of contiguous rows */
2271dcca6d9dSJed Brown     ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr);
227202834360SBarry Smith     /* loop over new rows determining lens and starting points */
227302834360SBarry Smith     for (i=0; i<nrows; i++) {
2274bfeeae90SHong Zhang       kstart = ai[irow[i]];
2275a2744918SBarry Smith       kend   = kstart + ailen[irow[i]];
2276a91a9bebSLisandro Dalcin       starts[i] = kstart;
227702834360SBarry Smith       for (k=kstart; k<kend; k++) {
2278bfeeae90SHong Zhang         if (aj[k] >= first) {
227902834360SBarry Smith           starts[i] = k;
228002834360SBarry Smith           break;
228102834360SBarry Smith         }
228202834360SBarry Smith       }
2283a2744918SBarry Smith       sum = 0;
228402834360SBarry Smith       while (k < kend) {
2285bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2286a2744918SBarry Smith         sum++;
228702834360SBarry Smith       }
2288a2744918SBarry Smith       lens[i] = sum;
228902834360SBarry Smith     }
229002834360SBarry Smith     /* create submatrix */
2291cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
229297f1f81fSBarry Smith       PetscInt n_cols,n_rows;
229308480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2294e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2295d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
229608480c60SBarry Smith       C    = *B;
22973a40ed3dSBarry Smith     } else {
22983bef6203SJed Brown       PetscInt rbs,cbs;
2299ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2300f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
23013bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
23023bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
23033bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
23047adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2305ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
230608480c60SBarry Smith     }
2307db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2308db02288aSLois Curfman McInnes 
230902834360SBarry Smith     /* loop over rows inserting into submatrix */
2310db02288aSLois Curfman McInnes     a_new = c->a;
2311db02288aSLois Curfman McInnes     j_new = c->j;
2312db02288aSLois Curfman McInnes     i_new = c->i;
2313bfeeae90SHong Zhang 
231402834360SBarry Smith     for (i=0; i<nrows; i++) {
2315a2744918SBarry Smith       ii    = starts[i];
2316a2744918SBarry Smith       lensi = lens[i];
2317a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2318a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
231902834360SBarry Smith       }
232087828ca2SBarry Smith       ierr       = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2321a2744918SBarry Smith       a_new     += lensi;
2322a2744918SBarry Smith       i_new[i+1] = i_new[i] + lensi;
2323a2744918SBarry Smith       c->ilen[i] = lensi;
232402834360SBarry Smith     }
23250e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
23263a40ed3dSBarry Smith   } else {
232702834360SBarry Smith     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
23281795a4d1SJed Brown     ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr);
2329854ce69bSBarry Smith     ierr = PetscMalloc1(1+nrows,&lens);CHKERRQ(ierr);
23304dcab191SBarry Smith     for (i=0; i<ncols; i++) {
23314dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
23324dcab191SBarry 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);
23334dcab191SBarry Smith #endif
23344dcab191SBarry Smith       smap[icol[i]] = i+1;
23354dcab191SBarry Smith     }
23364dcab191SBarry Smith 
233702834360SBarry Smith     /* determine lens of each row */
233802834360SBarry Smith     for (i=0; i<nrows; i++) {
2339bfeeae90SHong Zhang       kstart  = ai[irow[i]];
234002834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
234102834360SBarry Smith       lens[i] = 0;
234202834360SBarry Smith       for (k=kstart; k<kend; k++) {
2343bfeeae90SHong Zhang         if (smap[aj[k]]) {
234402834360SBarry Smith           lens[i]++;
234502834360SBarry Smith         }
234602834360SBarry Smith       }
234702834360SBarry Smith     }
234817ab2063SBarry Smith     /* Create and fill new matrix */
2349a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2350ace3abfcSBarry Smith       PetscBool equal;
23510f5bd95cSBarry Smith 
235299141d43SSatish Balay       c = (Mat_SeqAIJ*)((*B)->data);
2353e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2354d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
2355f23aa3ddSBarry Smith       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2356d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
235708480c60SBarry Smith       C    = *B;
23583a40ed3dSBarry Smith     } else {
23593bef6203SJed Brown       PetscInt rbs,cbs;
2360ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2361f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
23623bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
23633bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
23643bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
23657adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2366ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
236708480c60SBarry Smith     }
236899141d43SSatish Balay     c = (Mat_SeqAIJ*)(C->data);
236917ab2063SBarry Smith     for (i=0; i<nrows; i++) {
237099141d43SSatish Balay       row      = irow[i];
2371bfeeae90SHong Zhang       kstart   = ai[row];
237299141d43SSatish Balay       kend     = kstart + a->ilen[row];
2373bfeeae90SHong Zhang       mat_i    = c->i[i];
237499141d43SSatish Balay       mat_j    = c->j + mat_i;
237599141d43SSatish Balay       mat_a    = c->a + mat_i;
237699141d43SSatish Balay       mat_ilen = c->ilen + i;
237717ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2378bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2379ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
238099141d43SSatish Balay           *mat_a++ = a->a[k];
238199141d43SSatish Balay           (*mat_ilen)++;
238299141d43SSatish Balay 
238317ab2063SBarry Smith         }
238417ab2063SBarry Smith       }
238517ab2063SBarry Smith     }
238602834360SBarry Smith     /* Free work space */
238702834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2388606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2389606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
2390cdc6f3adSToby Isaac     /* sort */
2391cdc6f3adSToby Isaac     for (i = 0; i < nrows; i++) {
2392cdc6f3adSToby Isaac       PetscInt ilen;
2393cdc6f3adSToby Isaac 
2394cdc6f3adSToby Isaac       mat_i = c->i[i];
2395cdc6f3adSToby Isaac       mat_j = c->j + mat_i;
2396cdc6f3adSToby Isaac       mat_a = c->a + mat_i;
2397cdc6f3adSToby Isaac       ilen  = c->ilen[i];
2398cdc6f3adSToby Isaac       ierr  = PetscSortIntWithMatScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr);
2399cdc6f3adSToby Isaac     }
240002834360SBarry Smith   }
24016d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24026d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
240317ab2063SBarry Smith 
240417ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2405416022c9SBarry Smith   *B   = C;
24063a40ed3dSBarry Smith   PetscFunctionReturn(0);
240717ab2063SBarry Smith }
240817ab2063SBarry Smith 
24091df811f5SHong Zhang #undef __FUNCT__
241082d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2411fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
241282d44351SHong Zhang {
241382d44351SHong Zhang   PetscErrorCode ierr;
241482d44351SHong Zhang   Mat            B;
241582d44351SHong Zhang 
241682d44351SHong Zhang   PetscFunctionBegin;
2417c2d650bdSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
241882d44351SHong Zhang     ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
241982d44351SHong Zhang     ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
242033d57670SJed Brown     ierr    = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr);
242182d44351SHong Zhang     ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
242282d44351SHong Zhang     ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
242382d44351SHong Zhang     *subMat = B;
2424c2d650bdSHong Zhang   } else {
2425c2d650bdSHong Zhang     ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
2426c2d650bdSHong Zhang   }
242782d44351SHong Zhang   PetscFunctionReturn(0);
242882d44351SHong Zhang }
242982d44351SHong Zhang 
243082d44351SHong Zhang #undef __FUNCT__
24314a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
24329a625307SHong Zhang PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2433a871dcd8SBarry Smith {
243463b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2435dfbe8321SBarry Smith   PetscErrorCode ierr;
243663b91edcSBarry Smith   Mat            outA;
2437ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
243863b91edcSBarry Smith 
24393a40ed3dSBarry Smith   PetscFunctionBegin;
2440e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
24411df811f5SHong Zhang 
2442b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2443b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2444a871dcd8SBarry Smith 
244563b91edcSBarry Smith   outA             = inA;
2446d5f3da31SBarry Smith   outA->factortype = MAT_FACTOR_LU;
24472205254eSKarl Rupp 
2448c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
24496bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
24502205254eSKarl Rupp 
2451c3122656SLisandro Dalcin   a->row = row;
24522205254eSKarl Rupp 
2453c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
24546bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
24552205254eSKarl Rupp 
2456c3122656SLisandro Dalcin   a->col = col;
245763b91edcSBarry Smith 
245836db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
24596bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
24604c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
24613bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
2462f0ec6fceSSatish Balay 
246394a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2464854ce69bSBarry Smith     ierr = PetscMalloc1(inA->rmap->n+1,&a->solve_work);CHKERRQ(ierr);
24653bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
246694a9d846SBarry Smith   }
246763b91edcSBarry Smith 
2468f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2469137fb511SHong Zhang   if (row_identity && col_identity) {
2470ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2471137fb511SHong Zhang   } else {
2472719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2473137fb511SHong Zhang   }
24743a40ed3dSBarry Smith   PetscFunctionReturn(0);
2475a871dcd8SBarry Smith }
2476a871dcd8SBarry Smith 
24774a2ae208SSatish Balay #undef __FUNCT__
24784a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2479f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2480f0b747eeSBarry Smith {
2481f0b747eeSBarry Smith   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2482f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2483efee365bSSatish Balay   PetscErrorCode ierr;
2484c5df96a5SBarry Smith   PetscBLASInt   one = 1,bnz;
24853a40ed3dSBarry Smith 
24863a40ed3dSBarry Smith   PetscFunctionBegin;
2487c5df96a5SBarry Smith   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
24888b83055fSJed Brown   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2489efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2490acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
24913a40ed3dSBarry Smith   PetscFunctionReturn(0);
2492f0b747eeSBarry Smith }
2493f0b747eeSBarry Smith 
24944a2ae208SSatish Balay #undef __FUNCT__
24954a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
249697f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2497cddf8d76SBarry Smith {
2498dfbe8321SBarry Smith   PetscErrorCode ierr;
249997f1f81fSBarry Smith   PetscInt       i;
2500cddf8d76SBarry Smith 
25013a40ed3dSBarry Smith   PetscFunctionBegin;
2502cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2503854ce69bSBarry Smith     ierr = PetscMalloc1(n+1,B);CHKERRQ(ierr);
2504cddf8d76SBarry Smith   }
2505cddf8d76SBarry Smith 
2506cddf8d76SBarry Smith   for (i=0; i<n; i++) {
25076a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2508cddf8d76SBarry Smith   }
25093a40ed3dSBarry Smith   PetscFunctionReturn(0);
2510cddf8d76SBarry Smith }
2511cddf8d76SBarry Smith 
25124a2ae208SSatish Balay #undef __FUNCT__
25134a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
251497f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
25154dcbc457SBarry Smith {
2516e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
25176849ba73SBarry Smith   PetscErrorCode ierr;
25185d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
25195d0c19d7SBarry Smith   const PetscInt *idx;
252097f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2521f1af5d2fSBarry Smith   PetscBT        table;
2522bbd702dbSSatish Balay 
25233a40ed3dSBarry Smith   PetscFunctionBegin;
2524d0f46423SBarry Smith   m  = A->rmap->n;
2525e4d965acSSatish Balay   ai = a->i;
2526bfeeae90SHong Zhang   aj = a->j;
25278a047759SSatish Balay 
2528e32f2f54SBarry Smith   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
252906763907SSatish Balay 
2530854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&nidx);CHKERRQ(ierr);
253153b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
253206763907SSatish Balay 
2533e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2534b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2535e4d965acSSatish Balay     isz  = 0;
25366831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2537e4d965acSSatish Balay 
2538e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
25394dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2540b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2541e4d965acSSatish Balay 
2542dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2543e4d965acSSatish Balay     for (j=0; j<n; ++j) {
25442205254eSKarl Rupp       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
25454dcbc457SBarry Smith     }
254606763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
25476bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2548e4d965acSSatish Balay 
254904a348a9SBarry Smith     k = 0;
255004a348a9SBarry Smith     for (j=0; j<ov; j++) { /* for each overlap */
255104a348a9SBarry Smith       n = isz;
255206763907SSatish Balay       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2553e4d965acSSatish Balay         row   = nidx[k];
2554e4d965acSSatish Balay         start = ai[row];
2555e4d965acSSatish Balay         end   = ai[row+1];
255604a348a9SBarry Smith         for (l = start; l<end; l++) {
2557efb16452SHong Zhang           val = aj[l];
25582205254eSKarl Rupp           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2559e4d965acSSatish Balay         }
2560e4d965acSSatish Balay       }
2561e4d965acSSatish Balay     }
256270b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2563e4d965acSSatish Balay   }
256494bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2565606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
25663a40ed3dSBarry Smith   PetscFunctionReturn(0);
25674dcbc457SBarry Smith }
256817ab2063SBarry Smith 
25690513a670SBarry Smith /* -------------------------------------------------------------- */
25704a2ae208SSatish Balay #undef __FUNCT__
25714a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2572dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
25730513a670SBarry Smith {
25740513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
25756849ba73SBarry Smith   PetscErrorCode ierr;
25763b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
25775d0c19d7SBarry Smith   const PetscInt *row,*col;
25785d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
257956cd22aeSBarry Smith   IS             icolp,irowp;
25800298fd71SBarry Smith   PetscInt       *cwork = NULL;
25810298fd71SBarry Smith   PetscScalar    *vwork = NULL;
25820513a670SBarry Smith 
25833a40ed3dSBarry Smith   PetscFunctionBegin;
25844c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
258556cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
25864c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
258756cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
25880513a670SBarry Smith 
25890513a670SBarry Smith   /* determine lengths of permuted rows */
2590854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&lens);CHKERRQ(ierr);
25912205254eSKarl Rupp   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2592ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2593f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
259433d57670SJed Brown   ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
25957adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2596ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2597606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
25980513a670SBarry Smith 
2599785e854fSJed Brown   ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr);
26000513a670SBarry Smith   for (i=0; i<m; i++) {
260132ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
26022205254eSKarl Rupp     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2603cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
260432ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
26050513a670SBarry Smith   }
2606606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
26072205254eSKarl Rupp 
26083c7d62e4SBarry Smith   (*B)->assembled = PETSC_FALSE;
26092205254eSKarl Rupp 
26100513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
26110513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
261256cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
261356cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
26146bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
26156bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
26163a40ed3dSBarry Smith   PetscFunctionReturn(0);
26170513a670SBarry Smith }
26180513a670SBarry Smith 
26194a2ae208SSatish Balay #undef __FUNCT__
26204a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2621dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2622cb5b572fSBarry Smith {
2623dfbe8321SBarry Smith   PetscErrorCode ierr;
2624cb5b572fSBarry Smith 
2625cb5b572fSBarry Smith   PetscFunctionBegin;
262633f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
262733f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2628be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2629be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2630be6bf707SBarry Smith 
2631700c5bfcSBarry 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");
2632d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2633cb5b572fSBarry Smith   } else {
2634cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2635cb5b572fSBarry Smith   }
2636cb5b572fSBarry Smith   PetscFunctionReturn(0);
2637cb5b572fSBarry Smith }
2638cb5b572fSBarry Smith 
26394a2ae208SSatish Balay #undef __FUNCT__
26404994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
26414994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2642273d9f13SBarry Smith {
2643dfbe8321SBarry Smith   PetscErrorCode ierr;
2644273d9f13SBarry Smith 
2645273d9f13SBarry Smith   PetscFunctionBegin;
2646ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2647273d9f13SBarry Smith   PetscFunctionReturn(0);
2648273d9f13SBarry Smith }
2649273d9f13SBarry Smith 
26504a2ae208SSatish Balay #undef __FUNCT__
26518c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray_SeqAIJ"
26528c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
26536c0721eeSBarry Smith {
26546c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
26556e111a19SKarl Rupp 
26566c0721eeSBarry Smith   PetscFunctionBegin;
26576c0721eeSBarry Smith   *array = a->a;
26586c0721eeSBarry Smith   PetscFunctionReturn(0);
26596c0721eeSBarry Smith }
26606c0721eeSBarry Smith 
26614a2ae208SSatish Balay #undef __FUNCT__
26628c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray_SeqAIJ"
26638c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
26646c0721eeSBarry Smith {
26656c0721eeSBarry Smith   PetscFunctionBegin;
26666c0721eeSBarry Smith   PetscFunctionReturn(0);
26676c0721eeSBarry Smith }
2668273d9f13SBarry Smith 
26698229c054SShri Abhyankar /*
26708229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
26718229c054SShri Abhyankar    have different nonzero structure.
26728229c054SShri Abhyankar */
2673ac90fabeSBarry Smith #undef __FUNCT__
2674b264fe52SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqX_private"
2675b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2676ec7775f6SShri Abhyankar {
2677b264fe52SHong Zhang   PetscInt       i,j,k,nzx,nzy;
2678ec7775f6SShri Abhyankar 
2679ec7775f6SShri Abhyankar   PetscFunctionBegin;
2680ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2681ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
2682b264fe52SHong Zhang     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2683b264fe52SHong Zhang     nzx = xi[i+1] - xi[i];
2684b264fe52SHong Zhang     nzy = yi[i+1] - yi[i];
26858af7cee1SJed Brown     nnz[i] = 0;
26868af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2687b264fe52SHong Zhang       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2688b264fe52SHong Zhang       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
26898af7cee1SJed Brown       nnz[i]++;
26908af7cee1SJed Brown     }
26918af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2692ec7775f6SShri Abhyankar   }
2693ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2694ec7775f6SShri Abhyankar }
2695ec7775f6SShri Abhyankar 
2696ec7775f6SShri Abhyankar #undef __FUNCT__
2697b264fe52SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
2698b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2699b264fe52SHong Zhang {
2700b264fe52SHong Zhang   PetscInt       m = Y->rmap->N;
2701b264fe52SHong Zhang   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2702b264fe52SHong Zhang   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;
2703b264fe52SHong Zhang   PetscErrorCode ierr;
2704b264fe52SHong Zhang 
2705b264fe52SHong Zhang   PetscFunctionBegin;
2706b264fe52SHong Zhang   /* Set the number of nonzeros in the new matrix */
2707b264fe52SHong Zhang   ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr);
2708b264fe52SHong Zhang   PetscFunctionReturn(0);
2709b264fe52SHong Zhang }
2710b264fe52SHong Zhang 
2711b264fe52SHong Zhang #undef __FUNCT__
2712ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2713f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2714ac90fabeSBarry Smith {
2715dfbe8321SBarry Smith   PetscErrorCode ierr;
2716ac90fabeSBarry Smith   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2717c5df96a5SBarry Smith   PetscBLASInt   one=1,bnz;
2718ac90fabeSBarry Smith 
2719ac90fabeSBarry Smith   PetscFunctionBegin;
2720c5df96a5SBarry Smith   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2721ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2722f4df32b1SMatthew Knepley     PetscScalar alpha = a;
27238b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2724acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2725a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2726ab784542SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2727ab784542SHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2728ac90fabeSBarry Smith   } else {
27298229c054SShri Abhyankar     Mat      B;
27308229c054SShri Abhyankar     PetscInt *nnz;
2731785e854fSJed Brown     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
2732ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2733bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
27344aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
273533d57670SJed Brown     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
2736176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
27378229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2738ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2739ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
274028be2f97SBarry Smith     ierr = MatHeaderReplace(Y,&B);CHKERRQ(ierr);
27418229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2742ac90fabeSBarry Smith   }
2743ac90fabeSBarry Smith   PetscFunctionReturn(0);
2744ac90fabeSBarry Smith }
2745ac90fabeSBarry Smith 
2746521d7252SBarry Smith #undef __FUNCT__
2747354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
27487087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2749354c94deSBarry Smith {
2750354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2751354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2752354c94deSBarry Smith   PetscInt    i,nz;
2753354c94deSBarry Smith   PetscScalar *a;
2754354c94deSBarry Smith 
2755354c94deSBarry Smith   PetscFunctionBegin;
2756354c94deSBarry Smith   nz = aij->nz;
2757354c94deSBarry Smith   a  = aij->a;
27582205254eSKarl Rupp   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2759354c94deSBarry Smith #else
2760354c94deSBarry Smith   PetscFunctionBegin;
2761354c94deSBarry Smith #endif
2762354c94deSBarry Smith   PetscFunctionReturn(0);
2763354c94deSBarry Smith }
2764354c94deSBarry Smith 
2765e34fafa9SBarry Smith #undef __FUNCT__
2766985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2767985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2768e34fafa9SBarry Smith {
2769e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2770e34fafa9SBarry Smith   PetscErrorCode ierr;
2771d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2772e34fafa9SBarry Smith   PetscReal      atmp;
2773985db425SBarry Smith   PetscScalar    *x;
2774e34fafa9SBarry Smith   MatScalar      *aa;
2775e34fafa9SBarry Smith 
2776e34fafa9SBarry Smith   PetscFunctionBegin;
2777e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2778e34fafa9SBarry Smith   aa = a->a;
2779e34fafa9SBarry Smith   ai = a->i;
2780e34fafa9SBarry Smith   aj = a->j;
2781e34fafa9SBarry Smith 
2782985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2783e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2784e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2785e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2786e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2787e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
27889189402eSHong Zhang     x[i]  = 0.0;
2789e34fafa9SBarry Smith     for (j=0; j<ncols; j++) {
2790985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2791985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2792985db425SBarry Smith       aa++; aj++;
2793985db425SBarry Smith     }
2794985db425SBarry Smith   }
2795985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2796985db425SBarry Smith   PetscFunctionReturn(0);
2797985db425SBarry Smith }
2798985db425SBarry Smith 
2799985db425SBarry Smith #undef __FUNCT__
2800985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2801985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2802985db425SBarry Smith {
2803985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2804985db425SBarry Smith   PetscErrorCode ierr;
2805d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2806985db425SBarry Smith   PetscScalar    *x;
2807985db425SBarry Smith   MatScalar      *aa;
2808985db425SBarry Smith 
2809985db425SBarry Smith   PetscFunctionBegin;
2810e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2811985db425SBarry Smith   aa = a->a;
2812985db425SBarry Smith   ai = a->i;
2813985db425SBarry Smith   aj = a->j;
2814985db425SBarry Smith 
2815985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2816985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2817985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2818e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2819985db425SBarry Smith   for (i=0; i<m; i++) {
2820985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2821d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2822985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2823985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2824985db425SBarry Smith       x[i] = 0.0;
2825985db425SBarry Smith       if (idx) {
2826985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2827985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2828985db425SBarry Smith           if (aj[j] > j) {
2829985db425SBarry Smith             idx[i] = j;
2830985db425SBarry Smith             break;
2831985db425SBarry Smith           }
2832985db425SBarry Smith         }
2833985db425SBarry Smith       }
2834985db425SBarry Smith     }
2835985db425SBarry Smith     for (j=0; j<ncols; j++) {
2836985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2837985db425SBarry Smith       aa++; aj++;
2838985db425SBarry Smith     }
2839985db425SBarry Smith   }
2840985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2841985db425SBarry Smith   PetscFunctionReturn(0);
2842985db425SBarry Smith }
2843985db425SBarry Smith 
2844985db425SBarry Smith #undef __FUNCT__
2845c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2846c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2847c87e5d42SMatthew Knepley {
2848c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2849c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2850c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2851c87e5d42SMatthew Knepley   PetscReal      atmp;
2852c87e5d42SMatthew Knepley   PetscScalar    *x;
2853c87e5d42SMatthew Knepley   MatScalar      *aa;
2854c87e5d42SMatthew Knepley 
2855c87e5d42SMatthew Knepley   PetscFunctionBegin;
2856e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2857c87e5d42SMatthew Knepley   aa = a->a;
2858c87e5d42SMatthew Knepley   ai = a->i;
2859c87e5d42SMatthew Knepley   aj = a->j;
2860c87e5d42SMatthew Knepley 
2861c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2862c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2863c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
286460e0710aSBarry 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);
2865c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2866c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2867289a08f5SMatthew Knepley     if (ncols) {
2868289a08f5SMatthew Knepley       /* Get first nonzero */
2869289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
2870289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
28712205254eSKarl Rupp         if (atmp > 1.0e-12) {
28722205254eSKarl Rupp           x[i] = atmp;
28732205254eSKarl Rupp           if (idx) idx[i] = aj[j];
28742205254eSKarl Rupp           break;
28752205254eSKarl Rupp         }
2876289a08f5SMatthew Knepley       }
287712431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2878289a08f5SMatthew Knepley     } else {
2879289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2880289a08f5SMatthew Knepley     }
2881c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
2882c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2883289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2884c87e5d42SMatthew Knepley       aa++; aj++;
2885c87e5d42SMatthew Knepley     }
2886c87e5d42SMatthew Knepley   }
2887c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2888c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2889c87e5d42SMatthew Knepley }
2890c87e5d42SMatthew Knepley 
2891c87e5d42SMatthew Knepley #undef __FUNCT__
2892985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2893985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2894985db425SBarry Smith {
2895985db425SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
2896985db425SBarry Smith   PetscErrorCode  ierr;
2897d9ca1df4SBarry Smith   PetscInt        i,j,m = A->rmap->n,ncols,n;
2898d9ca1df4SBarry Smith   const PetscInt  *ai,*aj;
2899985db425SBarry Smith   PetscScalar     *x;
2900d9ca1df4SBarry Smith   const MatScalar *aa;
2901985db425SBarry Smith 
2902985db425SBarry Smith   PetscFunctionBegin;
2903e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2904985db425SBarry Smith   aa = a->a;
2905985db425SBarry Smith   ai = a->i;
2906985db425SBarry Smith   aj = a->j;
2907985db425SBarry Smith 
2908985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2909985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2910985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2911e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2912985db425SBarry Smith   for (i=0; i<m; i++) {
2913985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2914d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2915985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2916985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2917985db425SBarry Smith       x[i] = 0.0;
2918985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2919985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2920985db425SBarry Smith         for (j=0; j<ncols; j++) {
2921985db425SBarry Smith           if (aj[j] > j) {
2922985db425SBarry Smith             idx[i] = j;
2923985db425SBarry Smith             break;
2924985db425SBarry Smith           }
2925985db425SBarry Smith         }
2926985db425SBarry Smith       }
2927985db425SBarry Smith     }
2928985db425SBarry Smith     for (j=0; j<ncols; j++) {
2929985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2930985db425SBarry Smith       aa++; aj++;
2931e34fafa9SBarry Smith     }
2932e34fafa9SBarry Smith   }
2933e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2934e34fafa9SBarry Smith   PetscFunctionReturn(0);
2935e34fafa9SBarry Smith }
2936bbead8a2SBarry Smith 
2937bbead8a2SBarry Smith #include <petscblaslapack.h>
2938af0996ceSBarry Smith #include <petsc/private/kernels/blockinvert.h>
2939bbead8a2SBarry Smith 
2940bbead8a2SBarry Smith #undef __FUNCT__
2941bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
2942713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
2943bbead8a2SBarry Smith {
2944bbead8a2SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
2945bbead8a2SBarry Smith   PetscErrorCode ierr;
294633d57670SJed Brown   PetscInt       i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
2947bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
2948bbead8a2SBarry Smith   PetscReal      shift = 0.0;
29491a9391e3SHong Zhang   PetscBool      allowzeropivot,zeropivotdetected=PETSC_FALSE;
2950bbead8a2SBarry Smith 
2951bbead8a2SBarry Smith   PetscFunctionBegin;
2952a455e926SHong Zhang   allowzeropivot = PetscNot(A->erroriffailure);
29534a0d0026SBarry Smith   if (a->ibdiagvalid) {
29544a0d0026SBarry Smith     if (values) *values = a->ibdiag;
29554a0d0026SBarry Smith     PetscFunctionReturn(0);
29564a0d0026SBarry Smith   }
2957bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
2958bbead8a2SBarry Smith   if (!a->ibdiag) {
2959785e854fSJed Brown     ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr);
29603bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
2961bbead8a2SBarry Smith   }
2962bbead8a2SBarry Smith   diag = a->ibdiag;
2963bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
2964bbead8a2SBarry Smith   /* factor and invert each block */
2965bbead8a2SBarry Smith   switch (bs) {
2966bbead8a2SBarry Smith   case 1:
2967bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2968bbead8a2SBarry Smith       ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
2969ec1892c8SHong Zhang       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
2970ec1892c8SHong Zhang         if (allowzeropivot) {
2971ec1892c8SHong Zhang           A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
2972ec1892c8SHong Zhang           ierr = PetscInfo1(A,"Zero pivot, row %D\n",i);CHKERRQ(ierr);
2973ec1892c8SHong Zhang         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D",i);
2974ec1892c8SHong Zhang       }
2975bbead8a2SBarry Smith       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
2976bbead8a2SBarry Smith     }
2977bbead8a2SBarry Smith     break;
2978bbead8a2SBarry Smith   case 2:
2979bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2980bbead8a2SBarry Smith       ij[0] = 2*i; ij[1] = 2*i + 1;
2981bbead8a2SBarry Smith       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
2982a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
2983ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
298496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
2985bbead8a2SBarry Smith       diag += 4;
2986bbead8a2SBarry Smith     }
2987bbead8a2SBarry Smith     break;
2988bbead8a2SBarry Smith   case 3:
2989bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
2990bbead8a2SBarry Smith       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
2991bbead8a2SBarry Smith       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
2992a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
2993ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
299496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
2995bbead8a2SBarry Smith       diag += 9;
2996bbead8a2SBarry Smith     }
2997bbead8a2SBarry Smith     break;
2998bbead8a2SBarry Smith   case 4:
2999bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3000bbead8a2SBarry Smith       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3001bbead8a2SBarry Smith       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
3002a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
3003ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
300496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
3005bbead8a2SBarry Smith       diag += 16;
3006bbead8a2SBarry Smith     }
3007bbead8a2SBarry Smith     break;
3008bbead8a2SBarry Smith   case 5:
3009bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3010bbead8a2SBarry 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;
3011bbead8a2SBarry Smith       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
3012a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
3013ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
301496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
3015bbead8a2SBarry Smith       diag += 25;
3016bbead8a2SBarry Smith     }
3017bbead8a2SBarry Smith     break;
3018bbead8a2SBarry Smith   case 6:
3019bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3020bbead8a2SBarry 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;
3021bbead8a2SBarry Smith       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
3022a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
3023ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
302496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3025bbead8a2SBarry Smith       diag += 36;
3026bbead8a2SBarry Smith     }
3027bbead8a2SBarry Smith     break;
3028bbead8a2SBarry Smith   case 7:
3029bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3030bbead8a2SBarry 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;
3031bbead8a2SBarry Smith       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
3032a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
3033ec1892c8SHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
303496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3035bbead8a2SBarry Smith       diag += 49;
3036bbead8a2SBarry Smith     }
3037bbead8a2SBarry Smith     break;
3038bbead8a2SBarry Smith   default:
3039dcca6d9dSJed Brown     ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr);
3040bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3041bbead8a2SBarry Smith       for (j=0; j<bs; j++) {
3042bbead8a2SBarry Smith         IJ[j] = bs*i + j;
3043bbead8a2SBarry Smith       }
3044bbead8a2SBarry Smith       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
30455f8bbccaSHong Zhang       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
30465f8bbccaSHong Zhang       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
304796b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3048bbead8a2SBarry Smith       diag += bs2;
3049bbead8a2SBarry Smith     }
3050bbead8a2SBarry Smith     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3051bbead8a2SBarry Smith   }
3052bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3053bbead8a2SBarry Smith   PetscFunctionReturn(0);
3054bbead8a2SBarry Smith }
3055bbead8a2SBarry Smith 
305673a71a0fSBarry Smith #undef __FUNCT__
305773a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_SeqAIJ"
305873a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
305973a71a0fSBarry Smith {
306073a71a0fSBarry Smith   PetscErrorCode ierr;
306173a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
306273a71a0fSBarry Smith   PetscScalar    a;
306373a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
306473a71a0fSBarry Smith 
306573a71a0fSBarry Smith   PetscFunctionBegin;
306673a71a0fSBarry Smith   if (!x->assembled) {
306773a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
306873a71a0fSBarry Smith     for (i=0; i<m; i++) {
306973a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
307073a71a0fSBarry Smith         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
307173a71a0fSBarry Smith         col  = (PetscInt)(n*PetscRealPart(a));
307273a71a0fSBarry Smith         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
307373a71a0fSBarry Smith       }
307473a71a0fSBarry Smith     }
307573a71a0fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
307673a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
307773a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
307873a71a0fSBarry Smith   PetscFunctionReturn(0);
307973a71a0fSBarry Smith }
308073a71a0fSBarry Smith 
30817d68702bSBarry Smith #undef __FUNCT__
30827d68702bSBarry Smith #define __FUNCT__ "MatShift_SeqAIJ"
30837d68702bSBarry Smith PetscErrorCode MatShift_SeqAIJ(Mat Y,PetscScalar a)
30847d68702bSBarry Smith {
30857d68702bSBarry Smith   PetscErrorCode ierr;
30867d68702bSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)Y->data;
30877d68702bSBarry Smith 
30887d68702bSBarry Smith   PetscFunctionBegin;
30896f33a894SBarry Smith   if (!Y->preallocated || !aij->nz) {
30907d68702bSBarry Smith     ierr = MatSeqAIJSetPreallocation(Y,1,NULL);CHKERRQ(ierr);
30917d68702bSBarry Smith   }
30927d68702bSBarry Smith   ierr = MatShift_Basic(Y,a);CHKERRQ(ierr);
30937d68702bSBarry Smith   PetscFunctionReturn(0);
30947d68702bSBarry Smith }
30957d68702bSBarry Smith 
3096682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
30970a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3098cb5b572fSBarry Smith                                         MatGetRow_SeqAIJ,
3099cb5b572fSBarry Smith                                         MatRestoreRow_SeqAIJ,
3100cb5b572fSBarry Smith                                         MatMult_SeqAIJ,
310197304618SKris Buschelman                                 /*  4*/ MatMultAdd_SeqAIJ,
31027c922b88SBarry Smith                                         MatMultTranspose_SeqAIJ,
31037c922b88SBarry Smith                                         MatMultTransposeAdd_SeqAIJ,
3104db4efbfdSBarry Smith                                         0,
3105db4efbfdSBarry Smith                                         0,
3106db4efbfdSBarry Smith                                         0,
3107db4efbfdSBarry Smith                                 /* 10*/ 0,
3108cb5b572fSBarry Smith                                         MatLUFactor_SeqAIJ,
3109cb5b572fSBarry Smith                                         0,
311041f059aeSBarry Smith                                         MatSOR_SeqAIJ,
311117ab2063SBarry Smith                                         MatTranspose_SeqAIJ,
311297304618SKris Buschelman                                 /*1 5*/ MatGetInfo_SeqAIJ,
3113cb5b572fSBarry Smith                                         MatEqual_SeqAIJ,
3114cb5b572fSBarry Smith                                         MatGetDiagonal_SeqAIJ,
3115cb5b572fSBarry Smith                                         MatDiagonalScale_SeqAIJ,
3116cb5b572fSBarry Smith                                         MatNorm_SeqAIJ,
311797304618SKris Buschelman                                 /* 20*/ 0,
3118cb5b572fSBarry Smith                                         MatAssemblyEnd_SeqAIJ,
3119cb5b572fSBarry Smith                                         MatSetOption_SeqAIJ,
3120cb5b572fSBarry Smith                                         MatZeroEntries_SeqAIJ,
3121d519adbfSMatthew Knepley                                 /* 24*/ MatZeroRows_SeqAIJ,
3122db4efbfdSBarry Smith                                         0,
3123db4efbfdSBarry Smith                                         0,
3124db4efbfdSBarry Smith                                         0,
3125db4efbfdSBarry Smith                                         0,
31264994cf47SJed Brown                                 /* 29*/ MatSetUp_SeqAIJ,
3127db4efbfdSBarry Smith                                         0,
3128db4efbfdSBarry Smith                                         0,
31298c778c55SBarry Smith                                         0,
31308c778c55SBarry Smith                                         0,
3131d519adbfSMatthew Knepley                                 /* 34*/ MatDuplicate_SeqAIJ,
3132cb5b572fSBarry Smith                                         0,
3133cb5b572fSBarry Smith                                         0,
3134cb5b572fSBarry Smith                                         MatILUFactor_SeqAIJ,
3135cb5b572fSBarry Smith                                         0,
3136d519adbfSMatthew Knepley                                 /* 39*/ MatAXPY_SeqAIJ,
3137cb5b572fSBarry Smith                                         MatGetSubMatrices_SeqAIJ,
3138cb5b572fSBarry Smith                                         MatIncreaseOverlap_SeqAIJ,
3139cb5b572fSBarry Smith                                         MatGetValues_SeqAIJ,
3140cb5b572fSBarry Smith                                         MatCopy_SeqAIJ,
3141d519adbfSMatthew Knepley                                 /* 44*/ MatGetRowMax_SeqAIJ,
3142cb5b572fSBarry Smith                                         MatScale_SeqAIJ,
31437d68702bSBarry Smith                                         MatShift_SeqAIJ,
314479299369SBarry Smith                                         MatDiagonalSet_SeqAIJ,
31456e169961SBarry Smith                                         MatZeroRowsColumns_SeqAIJ,
314673a71a0fSBarry Smith                                 /* 49*/ MatSetRandom_SeqAIJ,
31473b2fbd54SBarry Smith                                         MatGetRowIJ_SeqAIJ,
31483b2fbd54SBarry Smith                                         MatRestoreRowIJ_SeqAIJ,
31493b2fbd54SBarry Smith                                         MatGetColumnIJ_SeqAIJ,
3150a93ec695SBarry Smith                                         MatRestoreColumnIJ_SeqAIJ,
315193dfae19SHong Zhang                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3152b9617806SBarry Smith                                         0,
31530513a670SBarry Smith                                         0,
3154cda55fadSBarry Smith                                         MatPermute_SeqAIJ,
3155cda55fadSBarry Smith                                         0,
3156d519adbfSMatthew Knepley                                 /* 59*/ 0,
3157b9b97703SBarry Smith                                         MatDestroy_SeqAIJ,
3158b9b97703SBarry Smith                                         MatView_SeqAIJ,
3159357abbc8SBarry Smith                                         0,
3160321b30b9SSatish Balay                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3161321b30b9SSatish Balay                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3162321b30b9SSatish Balay                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3163ee4f033dSBarry Smith                                         0,
3164ee4f033dSBarry Smith                                         0,
3165ee4f033dSBarry Smith                                         0,
3166d519adbfSMatthew Knepley                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3167c87e5d42SMatthew Knepley                                         MatGetRowMinAbs_SeqAIJ,
3168ee4f033dSBarry Smith                                         0,
3169ee4f033dSBarry Smith                                         MatSetColoring_SeqAIJ,
3170dcf5cc72SBarry Smith                                         0,
3171d519adbfSMatthew Knepley                                 /* 74*/ MatSetValuesAdifor_SeqAIJ,
31723acb8795SBarry Smith                                         MatFDColoringApply_AIJ,
317397304618SKris Buschelman                                         0,
317497304618SKris Buschelman                                         0,
317597304618SKris Buschelman                                         0,
31766ce1633cSBarry Smith                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
317797304618SKris Buschelman                                         0,
317897304618SKris Buschelman                                         0,
317997304618SKris Buschelman                                         0,
3180bc011b1eSHong Zhang                                         MatLoad_SeqAIJ,
3181d519adbfSMatthew Knepley                                 /* 84*/ MatIsSymmetric_SeqAIJ,
31821cbb95d3SBarry Smith                                         MatIsHermitian_SeqAIJ,
31836284ec50SHong Zhang                                         0,
31846284ec50SHong Zhang                                         0,
3185bc011b1eSHong Zhang                                         0,
3186d519adbfSMatthew Knepley                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
318726be0446SHong Zhang                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
318826be0446SHong Zhang                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
318965e8a0caSHong Zhang                                         MatPtAP_SeqAIJ_SeqAIJ,
31904a1b09b7SHong Zhang                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_DenseAxpy,
319165e8a0caSHong Zhang                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
31926fc122caSHong Zhang                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
31936fc122caSHong Zhang                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
31946fc122caSHong Zhang                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
31952121bac1SHong Zhang                                         0,
31962121bac1SHong Zhang                                 /* 99*/ 0,
3197609c6c4dSKris Buschelman                                         0,
3198609c6c4dSKris Buschelman                                         0,
319987d4246cSBarry Smith                                         MatConjugate_SeqAIJ,
320087d4246cSBarry Smith                                         0,
3201d519adbfSMatthew Knepley                                 /*104*/ MatSetValuesRow_SeqAIJ,
320299cafbc1SBarry Smith                                         MatRealPart_SeqAIJ,
3203f5edf698SHong Zhang                                         MatImaginaryPart_SeqAIJ,
3204f5edf698SHong Zhang                                         0,
32052bebee5dSHong Zhang                                         0,
3206cbd44569SHong Zhang                                 /*109*/ MatMatSolve_SeqAIJ,
3207985db425SBarry Smith                                         0,
32082af78befSBarry Smith                                         MatGetRowMin_SeqAIJ,
32092af78befSBarry Smith                                         0,
3210599ef60dSHong Zhang                                         MatMissingDiagonal_SeqAIJ,
3211d519adbfSMatthew Knepley                                 /*114*/ 0,
3212599ef60dSHong Zhang                                         0,
32133c2a7987SHong Zhang                                         0,
3214fe97e370SBarry Smith                                         0,
3215fbdbba38SShri Abhyankar                                         0,
3216fbdbba38SShri Abhyankar                                 /*119*/ 0,
3217fbdbba38SShri Abhyankar                                         0,
3218fbdbba38SShri Abhyankar                                         0,
321982d44351SHong Zhang                                         0,
3220b3a44c85SBarry Smith                                         MatGetMultiProcBlock_SeqAIJ,
32210716a85fSBarry Smith                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3222bbead8a2SBarry Smith                                         MatGetColumnNorms_SeqAIJ,
322337868618SMatthew G Knepley                                         MatInvertBlockDiagonal_SeqAIJ,
322437868618SMatthew G Knepley                                         0,
322537868618SMatthew G Knepley                                         0,
32265df89d91SHong Zhang                                 /*129*/ 0,
322775648e8dSHong Zhang                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
322875648e8dSHong Zhang                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
322975648e8dSHong Zhang                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3230b9af6bddSHong Zhang                                         MatTransposeColoringCreate_SeqAIJ,
3231b9af6bddSHong Zhang                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
32322b8ad9a3SHong Zhang                                         MatTransColoringApplyDenToSp_SeqAIJ,
32332b8ad9a3SHong Zhang                                         MatRARt_SeqAIJ_SeqAIJ,
32342b8ad9a3SHong Zhang                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
32353964eb88SJed Brown                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
32363964eb88SJed Brown                                  /*139*/0,
3237f9426fe0SMark Adams                                         0,
32381919a2e2SJed Brown                                         0,
32393a062f41SBarry Smith                                         MatFDColoringSetUp_SeqXAIJ,
32409c8f2541SHong Zhang                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
32419c8f2541SHong Zhang                                  /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ
32429e29f15eSvictorle };
324317ab2063SBarry Smith 
32444a2ae208SSatish Balay #undef __FUNCT__
32454a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
32467087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3247bef8e0ddSBarry Smith {
3248bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
324997f1f81fSBarry Smith   PetscInt   i,nz,n;
3250bef8e0ddSBarry Smith 
3251bef8e0ddSBarry Smith   PetscFunctionBegin;
3252bef8e0ddSBarry Smith   nz = aij->maxnz;
3253d0f46423SBarry Smith   n  = mat->rmap->n;
3254bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3255bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3256bef8e0ddSBarry Smith   }
3257bef8e0ddSBarry Smith   aij->nz = nz;
3258bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3259bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3260bef8e0ddSBarry Smith   }
3261bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3262bef8e0ddSBarry Smith }
3263bef8e0ddSBarry Smith 
32644a2ae208SSatish Balay #undef __FUNCT__
32654a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3266bef8e0ddSBarry Smith /*@
3267bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3268bef8e0ddSBarry Smith        in the matrix.
3269bef8e0ddSBarry Smith 
3270bef8e0ddSBarry Smith   Input Parameters:
3271bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3272bef8e0ddSBarry Smith -  indices - the column indices
3273bef8e0ddSBarry Smith 
327415091d37SBarry Smith   Level: advanced
327515091d37SBarry Smith 
3276bef8e0ddSBarry Smith   Notes:
3277bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3278bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3279bef8e0ddSBarry Smith   of the MatSetValues() operation.
3280bef8e0ddSBarry Smith 
3281bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3282d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3283bef8e0ddSBarry Smith 
3284bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3285bef8e0ddSBarry Smith 
3286b9617806SBarry Smith     The indices should start with zero, not one.
3287b9617806SBarry Smith 
3288bef8e0ddSBarry Smith @*/
32897087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3290bef8e0ddSBarry Smith {
32914ac538c5SBarry Smith   PetscErrorCode ierr;
3292bef8e0ddSBarry Smith 
3293bef8e0ddSBarry Smith   PetscFunctionBegin;
32940700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
32954482741eSBarry Smith   PetscValidPointer(indices,2);
32964ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3297bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3298bef8e0ddSBarry Smith }
3299bef8e0ddSBarry Smith 
3300be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3301be6bf707SBarry Smith 
33024a2ae208SSatish Balay #undef __FUNCT__
33034a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
33047087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3305be6bf707SBarry Smith {
3306be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
33076849ba73SBarry Smith   PetscErrorCode ierr;
3308d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3309be6bf707SBarry Smith 
3310be6bf707SBarry Smith   PetscFunctionBegin;
3311169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3312be6bf707SBarry Smith 
3313be6bf707SBarry Smith   /* allocate space for values if not already there */
3314be6bf707SBarry Smith   if (!aij->saved_values) {
3315854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr);
33163bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3317be6bf707SBarry Smith   }
3318be6bf707SBarry Smith 
3319be6bf707SBarry Smith   /* copy values over */
332087828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3321be6bf707SBarry Smith   PetscFunctionReturn(0);
3322be6bf707SBarry Smith }
3323be6bf707SBarry Smith 
33244a2ae208SSatish Balay #undef __FUNCT__
3325b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3326be6bf707SBarry Smith /*@
3327be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3328be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3329be6bf707SBarry Smith        nonlinear portion.
3330be6bf707SBarry Smith 
3331be6bf707SBarry Smith    Collect on Mat
3332be6bf707SBarry Smith 
3333be6bf707SBarry Smith   Input Parameters:
33340e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3335be6bf707SBarry Smith 
333615091d37SBarry Smith   Level: advanced
333715091d37SBarry Smith 
3338be6bf707SBarry Smith   Common Usage, with SNESSolve():
3339be6bf707SBarry Smith $    Create Jacobian matrix
3340be6bf707SBarry Smith $    Set linear terms into matrix
3341be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3342be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3343be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3344512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3345be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3346be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3347be6bf707SBarry Smith $    In your Jacobian routine
3348be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3349be6bf707SBarry Smith $      Set nonlinear terms in matrix
3350be6bf707SBarry Smith 
3351be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3352be6bf707SBarry Smith $    // build linear portion of Jacobian
3353512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3354be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3355be6bf707SBarry Smith $    loop over nonlinear iterations
3356be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3357be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3358be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3359be6bf707SBarry Smith $       Solve linear system with Jacobian
3360be6bf707SBarry Smith $    endloop
3361be6bf707SBarry Smith 
3362be6bf707SBarry Smith   Notes:
3363be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3364512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3365be6bf707SBarry Smith     calling this routine.
3366be6bf707SBarry Smith 
33670c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
33680c468ba9SBarry Smith     and does not allocated additional space.
33690c468ba9SBarry Smith 
3370be6bf707SBarry Smith .seealso: MatRetrieveValues()
3371be6bf707SBarry Smith 
3372be6bf707SBarry Smith @*/
33737087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3374be6bf707SBarry Smith {
33754ac538c5SBarry Smith   PetscErrorCode ierr;
3376be6bf707SBarry Smith 
3377be6bf707SBarry Smith   PetscFunctionBegin;
33780700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3379e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3380e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
33814ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3382be6bf707SBarry Smith   PetscFunctionReturn(0);
3383be6bf707SBarry Smith }
3384be6bf707SBarry Smith 
33854a2ae208SSatish Balay #undef __FUNCT__
33864a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
33877087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3388be6bf707SBarry Smith {
3389be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
33906849ba73SBarry Smith   PetscErrorCode ierr;
3391d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3392be6bf707SBarry Smith 
3393be6bf707SBarry Smith   PetscFunctionBegin;
3394169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3395f23aa3ddSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3396be6bf707SBarry Smith   /* copy values over */
339787828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3398be6bf707SBarry Smith   PetscFunctionReturn(0);
3399be6bf707SBarry Smith }
3400be6bf707SBarry Smith 
34014a2ae208SSatish Balay #undef __FUNCT__
34024a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3403be6bf707SBarry Smith /*@
3404be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3405be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3406be6bf707SBarry Smith        nonlinear portion.
3407be6bf707SBarry Smith 
3408be6bf707SBarry Smith    Collect on Mat
3409be6bf707SBarry Smith 
3410be6bf707SBarry Smith   Input Parameters:
3411be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3412be6bf707SBarry Smith 
341315091d37SBarry Smith   Level: advanced
341415091d37SBarry Smith 
3415be6bf707SBarry Smith .seealso: MatStoreValues()
3416be6bf707SBarry Smith 
3417be6bf707SBarry Smith @*/
34187087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3419be6bf707SBarry Smith {
34204ac538c5SBarry Smith   PetscErrorCode ierr;
3421be6bf707SBarry Smith 
3422be6bf707SBarry Smith   PetscFunctionBegin;
34230700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3424e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3425e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
34264ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3427be6bf707SBarry Smith   PetscFunctionReturn(0);
3428be6bf707SBarry Smith }
3429be6bf707SBarry Smith 
3430f83d6046SBarry Smith 
3431be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
34324a2ae208SSatish Balay #undef __FUNCT__
34334a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
343417ab2063SBarry Smith /*@C
3435682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
34360d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
34376e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
343851c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
34392bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
344017ab2063SBarry Smith 
3441db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3442db81eaa0SLois Curfman McInnes 
344317ab2063SBarry Smith    Input Parameters:
3444db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
344517ab2063SBarry Smith .  m - number of rows
344617ab2063SBarry Smith .  n - number of columns
344717ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
344851c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
34490298fd71SBarry Smith          (possibly different for each row) or NULL
345017ab2063SBarry Smith 
345117ab2063SBarry Smith    Output Parameter:
3452416022c9SBarry Smith .  A - the matrix
345317ab2063SBarry Smith 
3454175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3455ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3456175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3457175b88e8SBarry Smith 
3458b259b22eSLois Curfman McInnes    Notes:
345949a6f317SBarry Smith    If nnz is given then nz is ignored
346049a6f317SBarry Smith 
346117ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
346217ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
34630002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
346444cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
346517ab2063SBarry Smith 
346617ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
34670298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
34683d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
34696da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
347017ab2063SBarry Smith 
3471682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
34724fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3473682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
34746c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
34756c7ebb05SLois Curfman McInnes 
34766c7ebb05SLois Curfman McInnes    Options Database Keys:
3477698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
34789db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
347917ab2063SBarry Smith 
3480027ccd11SLois Curfman McInnes    Level: intermediate
3481027ccd11SLois Curfman McInnes 
348269b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
348336db0b34SBarry Smith 
348417ab2063SBarry Smith @*/
34857087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
348617ab2063SBarry Smith {
3487dfbe8321SBarry Smith   PetscErrorCode ierr;
34886945ee14SBarry Smith 
34893a40ed3dSBarry Smith   PetscFunctionBegin;
3490f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3491117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3492c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3493d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3494273d9f13SBarry Smith   PetscFunctionReturn(0);
3495273d9f13SBarry Smith }
3496273d9f13SBarry Smith 
34974a2ae208SSatish Balay #undef __FUNCT__
34984a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3499273d9f13SBarry Smith /*@C
3500273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3501273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3502273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3503273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3504273d9f13SBarry Smith 
3505273d9f13SBarry Smith    Collective on MPI_Comm
3506273d9f13SBarry Smith 
3507273d9f13SBarry Smith    Input Parameters:
35081c4f3114SJed Brown +  B - The matrix
3509273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3510273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
35110298fd71SBarry Smith          (possibly different for each row) or NULL
3512273d9f13SBarry Smith 
3513273d9f13SBarry Smith    Notes:
351449a6f317SBarry Smith      If nnz is given then nz is ignored
351549a6f317SBarry Smith 
3516273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3517273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3518273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3519273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3520273d9f13SBarry Smith 
3521273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
35220298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3523273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3524273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3525273d9f13SBarry Smith 
3526aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3527aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3528aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3529aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3530aa95bbe8SBarry Smith 
3531a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3532a96a251dSBarry Smith    entries or columns indices
3533a96a251dSBarry Smith 
3534273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3535273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3536273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3537273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3538273d9f13SBarry Smith 
3539273d9f13SBarry Smith    Options Database Keys:
3540698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3541698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3542273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3543273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3544273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3545273d9f13SBarry Smith 
3546273d9f13SBarry Smith    Level: intermediate
3547273d9f13SBarry Smith 
354869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3549273d9f13SBarry Smith 
3550273d9f13SBarry Smith @*/
35517087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3552273d9f13SBarry Smith {
35534ac538c5SBarry Smith   PetscErrorCode ierr;
3554a23d5eceSKris Buschelman 
3555a23d5eceSKris Buschelman   PetscFunctionBegin;
35566ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
35576ba663aaSJed Brown   PetscValidType(B,1);
35584ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3559a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3560a23d5eceSKris Buschelman }
3561a23d5eceSKris Buschelman 
3562a23d5eceSKris Buschelman #undef __FUNCT__
3563a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
35647087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3565a23d5eceSKris Buschelman {
3566273d9f13SBarry Smith   Mat_SeqAIJ     *b;
35672576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
35686849ba73SBarry Smith   PetscErrorCode ierr;
356997f1f81fSBarry Smith   PetscInt       i;
3570273d9f13SBarry Smith 
3571273d9f13SBarry Smith   PetscFunctionBegin;
35722576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3573a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3574c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3575c461c341SBarry Smith     nz             = 0;
3576c461c341SBarry Smith   }
3577c461c341SBarry Smith 
357826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
357926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3580899cda47SBarry Smith 
3581435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
358260e0710aSBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3583b73539f3SBarry Smith   if (nnz) {
3584d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
358560e0710aSBarry 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]);
358660e0710aSBarry 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);
3587b73539f3SBarry Smith     }
3588b73539f3SBarry Smith   }
3589b73539f3SBarry Smith 
3590273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
35912205254eSKarl Rupp 
3592273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3593273d9f13SBarry Smith 
3594ab93d7beSBarry Smith   if (!skipallocation) {
35952ee49352SLisandro Dalcin     if (!b->imax) {
3596dcca6d9dSJed Brown       ierr = PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);CHKERRQ(ierr);
35973bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
35982ee49352SLisandro Dalcin     }
3599273d9f13SBarry Smith     if (!nnz) {
3600435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3601c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3602d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3603d0f46423SBarry Smith       nz = nz*B->rmap->n;
3604273d9f13SBarry Smith     } else {
3605273d9f13SBarry Smith       nz = 0;
3606d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3607273d9f13SBarry Smith     }
3608ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
36092205254eSKarl Rupp     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;
3610ab93d7beSBarry Smith 
3611273d9f13SBarry Smith     /* allocate the matrix space */
361253dd7562SDmitry Karpeev     /* FIXME: should B's old memory be unlogged? */
36132ee49352SLisandro Dalcin     ierr    = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3614dcca6d9dSJed Brown     ierr    = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr);
36153bb1ff40SBarry Smith     ierr    = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3616bfeeae90SHong Zhang     b->i[0] = 0;
3617d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
36185da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
36195da197adSKris Buschelman     }
3620273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3621e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3622e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3623c461c341SBarry Smith   } else {
3624e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
3625e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
3626c461c341SBarry Smith   }
3627273d9f13SBarry Smith 
3628273d9f13SBarry Smith   b->nz               = 0;
3629273d9f13SBarry Smith   b->maxnz            = nz;
3630273d9f13SBarry Smith   B->info.nz_unneeded = (double)b->maxnz;
36312205254eSKarl Rupp   if (realalloc) {
36322205254eSKarl Rupp     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
36332205254eSKarl Rupp   }
3634273d9f13SBarry Smith   PetscFunctionReturn(0);
3635273d9f13SBarry Smith }
3636273d9f13SBarry Smith 
3637a1661176SMatthew Knepley #undef  __FUNCT__
3638a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
363958d36128SBarry Smith /*@
3640a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3641a1661176SMatthew Knepley 
3642a1661176SMatthew Knepley    Input Parameters:
3643a1661176SMatthew Knepley +  B - the matrix
3644a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3645a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3646a1661176SMatthew Knepley -  v - optional values in the matrix
3647a1661176SMatthew Knepley 
3648a1661176SMatthew Knepley    Level: developer
3649a1661176SMatthew Knepley 
365058d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
365158d36128SBarry Smith 
3652a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3653a1661176SMatthew Knepley 
3654a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3655a1661176SMatthew Knepley @*/
3656a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3657a1661176SMatthew Knepley {
3658a1661176SMatthew Knepley   PetscErrorCode ierr;
3659a1661176SMatthew Knepley 
3660a1661176SMatthew Knepley   PetscFunctionBegin;
36610700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
36626ba663aaSJed Brown   PetscValidType(B,1);
36634ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3664a1661176SMatthew Knepley   PetscFunctionReturn(0);
3665a1661176SMatthew Knepley }
3666a1661176SMatthew Knepley 
3667a1661176SMatthew Knepley #undef  __FUNCT__
3668a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
36697087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3670a1661176SMatthew Knepley {
3671a1661176SMatthew Knepley   PetscInt       i;
3672a1661176SMatthew Knepley   PetscInt       m,n;
3673a1661176SMatthew Knepley   PetscInt       nz;
3674a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3675a1661176SMatthew Knepley   PetscScalar    *values;
3676a1661176SMatthew Knepley   PetscErrorCode ierr;
3677a1661176SMatthew Knepley 
3678a1661176SMatthew Knepley   PetscFunctionBegin;
367965e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3680779a8d59SSatish Balay 
3681779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3682779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3683779a8d59SSatish Balay 
3684779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3685854ce69bSBarry Smith   ierr = PetscMalloc1(m+1, &nnz);CHKERRQ(ierr);
3686a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3687b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3688a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
368965e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3690a1661176SMatthew Knepley     nnz[i] = nz;
3691a1661176SMatthew Knepley   }
3692a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3693a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3694a1661176SMatthew Knepley 
3695a1661176SMatthew Knepley   if (v) {
3696a1661176SMatthew Knepley     values = (PetscScalar*) v;
3697a1661176SMatthew Knepley   } else {
36981795a4d1SJed Brown     ierr = PetscCalloc1(nz_max, &values);CHKERRQ(ierr);
3699a1661176SMatthew Knepley   }
3700a1661176SMatthew Knepley 
3701a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3702b7940d39SSatish Balay     nz   = Ii[i+1] - Ii[i];
3703b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3704a1661176SMatthew Knepley   }
3705a1661176SMatthew Knepley 
3706a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3707a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3708a1661176SMatthew Knepley 
3709a1661176SMatthew Knepley   if (!v) {
3710a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3711a1661176SMatthew Knepley   }
37127827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3713a1661176SMatthew Knepley   PetscFunctionReturn(0);
3714a1661176SMatthew Knepley }
3715a1661176SMatthew Knepley 
3716c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3717af0996ceSBarry Smith #include <petsc/private/kernels/petscaxpy.h>
3718170fe5c8SBarry Smith 
3719170fe5c8SBarry Smith #undef __FUNCT__
3720170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3721170fe5c8SBarry Smith /*
3722170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3723170fe5c8SBarry Smith 
3724170fe5c8SBarry Smith                n                       p                          p
3725170fe5c8SBarry Smith         (              )       (              )         (                  )
3726170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3727170fe5c8SBarry Smith         (              )       (              )         (                  )
3728170fe5c8SBarry Smith 
3729170fe5c8SBarry Smith */
3730170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3731170fe5c8SBarry Smith {
3732170fe5c8SBarry Smith   PetscErrorCode    ierr;
3733170fe5c8SBarry Smith   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3734170fe5c8SBarry Smith   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3735170fe5c8SBarry Smith   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
37361de00fd4SBarry Smith   PetscInt          i,n,m,q,p;
3737170fe5c8SBarry Smith   const PetscInt    *ii,*idx;
3738170fe5c8SBarry Smith   const PetscScalar *b,*a,*a_q;
3739170fe5c8SBarry Smith   PetscScalar       *c,*c_q;
3740170fe5c8SBarry Smith 
3741170fe5c8SBarry Smith   PetscFunctionBegin;
3742d0f46423SBarry Smith   m    = A->rmap->n;
3743d0f46423SBarry Smith   n    = A->cmap->n;
3744d0f46423SBarry Smith   p    = B->cmap->n;
3745170fe5c8SBarry Smith   a    = sub_a->v;
3746170fe5c8SBarry Smith   b    = sub_b->a;
3747170fe5c8SBarry Smith   c    = sub_c->v;
3748170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3749170fe5c8SBarry Smith 
3750170fe5c8SBarry Smith   ii  = sub_b->i;
3751170fe5c8SBarry Smith   idx = sub_b->j;
3752170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3753170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3754170fe5c8SBarry Smith     while (q-->0) {
3755170fe5c8SBarry Smith       c_q = c + m*(*idx);
3756170fe5c8SBarry Smith       a_q = a + m*i;
3757854c7f52SBarry Smith       PetscKernelAXPY(c_q,*b,a_q,m);
3758170fe5c8SBarry Smith       idx++;
3759170fe5c8SBarry Smith       b++;
3760170fe5c8SBarry Smith     }
3761170fe5c8SBarry Smith   }
3762170fe5c8SBarry Smith   PetscFunctionReturn(0);
3763170fe5c8SBarry Smith }
3764170fe5c8SBarry Smith 
3765170fe5c8SBarry Smith #undef __FUNCT__
3766170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3767170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3768170fe5c8SBarry Smith {
3769170fe5c8SBarry Smith   PetscErrorCode ierr;
3770d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3771170fe5c8SBarry Smith   Mat            Cmat;
3772170fe5c8SBarry Smith 
3773170fe5c8SBarry Smith   PetscFunctionBegin;
377460e0710aSBarry 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);
3775ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
3776170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
377733d57670SJed Brown   ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr);
3778170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
37790298fd71SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
3780d73949e8SHong Zhang 
3781d73949e8SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
37822205254eSKarl Rupp 
3783170fe5c8SBarry Smith   *C = Cmat;
3784170fe5c8SBarry Smith   PetscFunctionReturn(0);
3785170fe5c8SBarry Smith }
3786170fe5c8SBarry Smith 
3787170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3788170fe5c8SBarry Smith #undef __FUNCT__
3789170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3790170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3791170fe5c8SBarry Smith {
3792170fe5c8SBarry Smith   PetscErrorCode ierr;
3793170fe5c8SBarry Smith 
3794170fe5c8SBarry Smith   PetscFunctionBegin;
3795170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
37963ff4c91cSHong Zhang     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3797170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
37983ff4c91cSHong Zhang     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3799170fe5c8SBarry Smith   }
38003ff4c91cSHong Zhang   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3801170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
38023ff4c91cSHong Zhang   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3803170fe5c8SBarry Smith   PetscFunctionReturn(0);
3804170fe5c8SBarry Smith }
3805170fe5c8SBarry Smith 
3806170fe5c8SBarry Smith 
38070bad9183SKris Buschelman /*MC
3808fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
38090bad9183SKris Buschelman    based on compressed sparse row format.
38100bad9183SKris Buschelman 
38110bad9183SKris Buschelman    Options Database Keys:
38120bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
38130bad9183SKris Buschelman 
38140bad9183SKris Buschelman   Level: beginner
38150bad9183SKris Buschelman 
3816f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
38170bad9183SKris Buschelman M*/
38180bad9183SKris Buschelman 
3819ccd284c7SBarry Smith /*MC
3820ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
3821ccd284c7SBarry Smith 
3822ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
3823ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
3824ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
3825ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3826ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3827ccd284c7SBarry Smith 
3828ccd284c7SBarry Smith    Options Database Keys:
3829ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
3830ccd284c7SBarry Smith 
3831ccd284c7SBarry Smith   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
3832ccd284c7SBarry Smith    enough exist.
3833ccd284c7SBarry Smith 
3834ccd284c7SBarry Smith   Level: beginner
3835ccd284c7SBarry Smith 
3836ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3837ccd284c7SBarry Smith M*/
3838ccd284c7SBarry Smith 
3839ccd284c7SBarry Smith /*MC
3840ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
3841ccd284c7SBarry Smith 
3842ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3843ccd284c7SBarry Smith    and MATMPIAIJCRL 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 aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3850ccd284c7SBarry Smith 
3851ccd284c7SBarry Smith   Level: beginner
3852ccd284c7SBarry Smith 
3853ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3854ccd284c7SBarry Smith M*/
3855ccd284c7SBarry Smith 
3856*cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3857af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
3858*cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
3859af8000cdSHong Zhang #endif
3860*cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);
386142c9c57cSBarry Smith 
3862b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
386329b38603SBarry Smith PETSC_EXTERN PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
386429b38603SBarry Smith PETSC_EXTERN PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3865b3866ffcSBarry Smith #endif
386617667f90SBarry Smith 
3867c0c8ee5eSDmitry Karpeev 
38688c778c55SBarry Smith #undef __FUNCT__
38698c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray"
38708c778c55SBarry Smith /*@C
38718c778c55SBarry Smith    MatSeqAIJGetArray - gives access to the array where the data for a SeqSeqAIJ matrix is stored
38728c778c55SBarry Smith 
38738c778c55SBarry Smith    Not Collective
38748c778c55SBarry Smith 
38758c778c55SBarry Smith    Input Parameter:
3876579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
38778c778c55SBarry Smith 
38788c778c55SBarry Smith    Output Parameter:
38798c778c55SBarry Smith .   array - pointer to the data
38808c778c55SBarry Smith 
38818c778c55SBarry Smith    Level: intermediate
38828c778c55SBarry Smith 
3883774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
38848c778c55SBarry Smith @*/
38858c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
38868c778c55SBarry Smith {
38878c778c55SBarry Smith   PetscErrorCode ierr;
38888c778c55SBarry Smith 
38898c778c55SBarry Smith   PetscFunctionBegin;
38908c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
38918c778c55SBarry Smith   PetscFunctionReturn(0);
38928c778c55SBarry Smith }
38938c778c55SBarry Smith 
38948c778c55SBarry Smith #undef __FUNCT__
389521e72a00SBarry Smith #define __FUNCT__ "MatSeqAIJGetMaxRowNonzeros"
389621e72a00SBarry Smith /*@C
389721e72a00SBarry Smith    MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
389821e72a00SBarry Smith 
389921e72a00SBarry Smith    Not Collective
390021e72a00SBarry Smith 
390121e72a00SBarry Smith    Input Parameter:
3902579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
390321e72a00SBarry Smith 
390421e72a00SBarry Smith    Output Parameter:
390521e72a00SBarry Smith .   nz - the maximum number of nonzeros in any row
390621e72a00SBarry Smith 
390721e72a00SBarry Smith    Level: intermediate
390821e72a00SBarry Smith 
390921e72a00SBarry Smith .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
391021e72a00SBarry Smith @*/
391121e72a00SBarry Smith PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
391221e72a00SBarry Smith {
391321e72a00SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
391421e72a00SBarry Smith 
391521e72a00SBarry Smith   PetscFunctionBegin;
391621e72a00SBarry Smith   *nz = aij->rmax;
391721e72a00SBarry Smith   PetscFunctionReturn(0);
391821e72a00SBarry Smith }
391921e72a00SBarry Smith 
392021e72a00SBarry Smith #undef __FUNCT__
39218c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray"
39228c778c55SBarry Smith /*@C
3923579dbff0SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray()
39248c778c55SBarry Smith 
39258c778c55SBarry Smith    Not Collective
39268c778c55SBarry Smith 
39278c778c55SBarry Smith    Input Parameters:
3928579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
39298c778c55SBarry Smith .  array - pointer to the data
39308c778c55SBarry Smith 
39318c778c55SBarry Smith    Level: intermediate
39328c778c55SBarry Smith 
3933774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
39348c778c55SBarry Smith @*/
39358c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
39368c778c55SBarry Smith {
39378c778c55SBarry Smith   PetscErrorCode ierr;
39388c778c55SBarry Smith 
39398c778c55SBarry Smith   PetscFunctionBegin;
39408c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
39418c778c55SBarry Smith   PetscFunctionReturn(0);
39428c778c55SBarry Smith }
39438c778c55SBarry Smith 
39444a2ae208SSatish Balay #undef __FUNCT__
39454a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
39468cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
3947273d9f13SBarry Smith {
3948273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3949dfbe8321SBarry Smith   PetscErrorCode ierr;
395038baddfdSBarry Smith   PetscMPIInt    size;
3951273d9f13SBarry Smith 
3952273d9f13SBarry Smith   PetscFunctionBegin;
3953ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
3954e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3955273d9f13SBarry Smith 
3956b00a9115SJed Brown   ierr = PetscNewLog(B,&b);CHKERRQ(ierr);
39572205254eSKarl Rupp 
3958b0a32e0cSBarry Smith   B->data = (void*)b;
39592205254eSKarl Rupp 
3960549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
39612205254eSKarl Rupp 
3962416022c9SBarry Smith   b->row                = 0;
3963416022c9SBarry Smith   b->col                = 0;
396482bf6240SBarry Smith   b->icol               = 0;
3965b810aeb4SBarry Smith   b->reallocs           = 0;
396636db0b34SBarry Smith   b->ignorezeroentries  = PETSC_FALSE;
3967f1e2ffcdSBarry Smith   b->roworiented        = PETSC_TRUE;
3968416022c9SBarry Smith   b->nonew              = 0;
3969416022c9SBarry Smith   b->diag               = 0;
3970416022c9SBarry Smith   b->solve_work         = 0;
39712a1b7f2aSHong Zhang   B->spptr              = 0;
3972be6bf707SBarry Smith   b->saved_values       = 0;
3973d7f994e1SBarry Smith   b->idiag              = 0;
397471f1c65dSBarry Smith   b->mdiag              = 0;
397571f1c65dSBarry Smith   b->ssor_work          = 0;
397671f1c65dSBarry Smith   b->omega              = 1.0;
397771f1c65dSBarry Smith   b->fshift             = 0.0;
397871f1c65dSBarry Smith   b->idiagvalid         = PETSC_FALSE;
3979bbead8a2SBarry Smith   b->ibdiagvalid        = PETSC_FALSE;
3980a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
398117ab2063SBarry Smith 
398235d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3983bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
3984bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
39858c778c55SBarry Smith 
3986b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3987bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3988bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3989b3866ffcSBarry Smith #endif
399017f1a0eaSHong Zhang 
3991bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3992bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3993bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3994bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3995bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3996bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3997bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3998af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
3999af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);CHKERRQ(ierr);
4000af8000cdSHong Zhang #endif
4001b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);CHKERRQ(ierr);
4002bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4003bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4004bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
4005bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
4006bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
4007bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
4008bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
4009bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
40104108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
401117667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
40123a40ed3dSBarry Smith   PetscFunctionReturn(0);
401317ab2063SBarry Smith }
401417ab2063SBarry Smith 
40154a2ae208SSatish Balay #undef __FUNCT__
4016b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
4017b24902e0SBarry Smith /*
4018b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4019b24902e0SBarry Smith */
4020ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
402117ab2063SBarry Smith {
4022416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
40236849ba73SBarry Smith   PetscErrorCode ierr;
4024d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
402517ab2063SBarry Smith 
40263a40ed3dSBarry Smith   PetscFunctionBegin;
4027273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4028273d9f13SBarry Smith 
4029d5f3da31SBarry Smith   C->factortype = A->factortype;
4030416022c9SBarry Smith   c->row        = 0;
4031416022c9SBarry Smith   c->col        = 0;
403282bf6240SBarry Smith   c->icol       = 0;
40336ad4291fSHong Zhang   c->reallocs   = 0;
403417ab2063SBarry Smith 
40356ad4291fSHong Zhang   C->assembled = PETSC_TRUE;
403617ab2063SBarry Smith 
4037aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4038aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4039eec197d1SBarry Smith 
4040dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&c->imax,m,&c->ilen);CHKERRQ(ierr);
40413bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
404217ab2063SBarry Smith   for (i=0; i<m; i++) {
4043416022c9SBarry Smith     c->imax[i] = a->imax[i];
4044416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
404517ab2063SBarry Smith   }
404617ab2063SBarry Smith 
404717ab2063SBarry Smith   /* allocate the matrix space */
4048f77e22a1SHong Zhang   if (mallocmatspace) {
4049dcca6d9dSJed Brown     ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr);
40503bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
40512205254eSKarl Rupp 
4052f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
40532205254eSKarl Rupp 
405497f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
405517ab2063SBarry Smith     if (m > 0) {
405697f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4057be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4058bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4059be6bf707SBarry Smith       } else {
4060bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
406117ab2063SBarry Smith       }
406208480c60SBarry Smith     }
4063f77e22a1SHong Zhang   }
406417ab2063SBarry Smith 
40656ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4066416022c9SBarry Smith   c->roworiented       = a->roworiented;
4067416022c9SBarry Smith   c->nonew             = a->nonew;
4068416022c9SBarry Smith   if (a->diag) {
4069854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&c->diag);CHKERRQ(ierr);
40703bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
407117ab2063SBarry Smith     for (i=0; i<m; i++) {
4072416022c9SBarry Smith       c->diag[i] = a->diag[i];
407317ab2063SBarry Smith     }
40743a40ed3dSBarry Smith   } else c->diag = 0;
40752205254eSKarl Rupp 
40766ad4291fSHong Zhang   c->solve_work         = 0;
40776ad4291fSHong Zhang   c->saved_values       = 0;
40786ad4291fSHong Zhang   c->idiag              = 0;
407971f1c65dSBarry Smith   c->ssor_work          = 0;
4080a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
4081e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
4082e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
40836ad4291fSHong Zhang 
4084893ad86cSHong Zhang   c->rmax         = a->rmax;
4085416022c9SBarry Smith   c->nz           = a->nz;
40868ed568f8SMatthew G Knepley   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4087273d9f13SBarry Smith   C->preallocated = PETSC_TRUE;
4088754ec7b1SSatish Balay 
40896ad4291fSHong Zhang   c->compressedrow.use   = a->compressedrow.use;
40906ad4291fSHong Zhang   c->compressedrow.nrows = a->compressedrow.nrows;
4091cd6b891eSBarry Smith   if (a->compressedrow.use) {
40926ad4291fSHong Zhang     i    = a->compressedrow.nrows;
4093dcca6d9dSJed Brown     ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr);
40946ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
40956ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
409627ea64f8SHong Zhang   } else {
409727ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
40980298fd71SBarry Smith     c->compressedrow.i      = NULL;
40990298fd71SBarry Smith     c->compressedrow.rindex = NULL;
41006ad4291fSHong Zhang   }
4101ea632784SBarry Smith   c->nonzerorowcnt = a->nonzerorowcnt;
4102e56f5c9eSBarry Smith   C->nonzerostate  = A->nonzerostate;
41034846f1f5SKris Buschelman 
41042205254eSKarl Rupp   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4105140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
41063a40ed3dSBarry Smith   PetscFunctionReturn(0);
410717ab2063SBarry Smith }
410817ab2063SBarry Smith 
41094a2ae208SSatish Balay #undef __FUNCT__
4110b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
4111b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4112b24902e0SBarry Smith {
4113b24902e0SBarry Smith   PetscErrorCode ierr;
4114b24902e0SBarry Smith 
4115b24902e0SBarry Smith   PetscFunctionBegin;
4116ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
41174b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4118cfd3f464SBarry Smith   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
411933d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
4120cfd3f464SBarry Smith   }
4121a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4122f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4123b24902e0SBarry Smith   PetscFunctionReturn(0);
4124b24902e0SBarry Smith }
4125b24902e0SBarry Smith 
4126b24902e0SBarry Smith #undef __FUNCT__
41274a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
4128112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4129fbdbba38SShri Abhyankar {
4130fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4131fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4132fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4133fbdbba38SShri Abhyankar   int            fd;
4134fbdbba38SShri Abhyankar   PetscMPIInt    size;
4135fbdbba38SShri Abhyankar   MPI_Comm       comm;
41363059b6faSBarry Smith   PetscInt       bs = newMat->rmap->bs;
4137fbdbba38SShri Abhyankar 
4138fbdbba38SShri Abhyankar   PetscFunctionBegin;
4139c98fd787SBarry Smith   /* force binary viewer to load .info file if it has not yet done so */
4140c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
4141fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4142fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4143fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4144bbead8a2SBarry Smith 
41450298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
41460298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
4147bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
41483059b6faSBarry Smith   if (bs < 0) bs = 1;
41493059b6faSBarry Smith   ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);
4150bbead8a2SBarry Smith 
4151fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4152fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4153fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4154fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4155fbdbba38SShri Abhyankar 
4156bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4157fbdbba38SShri Abhyankar 
4158fbdbba38SShri Abhyankar   /* read in row lengths */
4159785e854fSJed Brown   ierr = PetscMalloc1(M,&rowlengths);CHKERRQ(ierr);
4160fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4161fbdbba38SShri Abhyankar 
4162fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4163fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
416460e0710aSBarry 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);
4165fbdbba38SShri Abhyankar 
4166fbdbba38SShri Abhyankar   /* set global size if not set already*/
4167f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4168fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4169aabbc4fbSShri Abhyankar   } else {
41709d36ed5fSBarry Smith     /* if sizes and type are already set, check if the matrix  global sizes are correct */
4171fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
41724c5b953cSHong Zhang     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
41734c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
41744c5b953cSHong Zhang     }
417560e0710aSBarry 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);
4176aabbc4fbSShri Abhyankar   }
4177fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4178fbdbba38SShri Abhyankar   a    = (Mat_SeqAIJ*)newMat->data;
4179fbdbba38SShri Abhyankar 
4180fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4181fbdbba38SShri Abhyankar 
4182fbdbba38SShri Abhyankar   /* read in nonzero values */
4183fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4184fbdbba38SShri Abhyankar 
4185fbdbba38SShri Abhyankar   /* set matrix "i" values */
4186fbdbba38SShri Abhyankar   a->i[0] = 0;
4187fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4188fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4189fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4190fbdbba38SShri Abhyankar   }
4191fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4192fbdbba38SShri Abhyankar 
4193fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4194fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4195fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4196fbdbba38SShri Abhyankar }
4197fbdbba38SShri Abhyankar 
4198fbdbba38SShri Abhyankar #undef __FUNCT__
4199b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4200ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
42017264ac53SSatish Balay {
42027264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4203dfbe8321SBarry Smith   PetscErrorCode ierr;
4204eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4205eeffb40dSHong Zhang   PetscInt k;
4206eeffb40dSHong Zhang #endif
42077264ac53SSatish Balay 
42083a40ed3dSBarry Smith   PetscFunctionBegin;
4209bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4210d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4211ca44d042SBarry Smith     *flg = PETSC_FALSE;
4212ca44d042SBarry Smith     PetscFunctionReturn(0);
4213bcd2baecSBarry Smith   }
42147264ac53SSatish Balay 
42157264ac53SSatish Balay   /* if the a->i are the same */
4216d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4217abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
42187264ac53SSatish Balay 
42197264ac53SSatish Balay   /* if a->j are the same */
422097f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4221abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4222bcd2baecSBarry Smith 
4223bcd2baecSBarry Smith   /* if a->a are the same */
4224eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4225eeffb40dSHong Zhang   for (k=0; k<a->nz; k++) {
4226eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4227eeffb40dSHong Zhang       *flg = PETSC_FALSE;
42283a40ed3dSBarry Smith       PetscFunctionReturn(0);
4229eeffb40dSHong Zhang     }
4230eeffb40dSHong Zhang   }
4231eeffb40dSHong Zhang #else
4232eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4233eeffb40dSHong Zhang #endif
4234eeffb40dSHong Zhang   PetscFunctionReturn(0);
42357264ac53SSatish Balay }
423636db0b34SBarry Smith 
42374a2ae208SSatish Balay #undef __FUNCT__
42384a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
423905869f15SSatish Balay /*@
424036db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
424136db0b34SBarry Smith               provided by the user.
424236db0b34SBarry Smith 
4243c75a6043SHong Zhang       Collective on MPI_Comm
424436db0b34SBarry Smith 
424536db0b34SBarry Smith    Input Parameters:
424636db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
424736db0b34SBarry Smith .   m - number of rows
424836db0b34SBarry Smith .   n - number of columns
424936db0b34SBarry Smith .   i - row indices
425036db0b34SBarry Smith .   j - column indices
425136db0b34SBarry Smith -   a - matrix values
425236db0b34SBarry Smith 
425336db0b34SBarry Smith    Output Parameter:
425436db0b34SBarry Smith .   mat - the matrix
425536db0b34SBarry Smith 
425636db0b34SBarry Smith    Level: intermediate
425736db0b34SBarry Smith 
425836db0b34SBarry Smith    Notes:
42590551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4260292fb18eSBarry Smith     once the matrix is destroyed and not before
426136db0b34SBarry Smith 
426236db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
426336db0b34SBarry Smith 
4264bfeeae90SHong Zhang        The i and j indices are 0 based
426536db0b34SBarry Smith 
4266a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4267a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
42688eef79e4SBarry Smith     as shown
4269a4552177SSatish Balay 
42708eef79e4SBarry Smith $        1 0 0
42718eef79e4SBarry Smith $        2 0 3
42728eef79e4SBarry Smith $        4 5 6
42738eef79e4SBarry Smith $
42748eef79e4SBarry Smith $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
42758eef79e4SBarry Smith $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
42768eef79e4SBarry Smith $        v =  {1,2,3,4,5,6}  [size = 6]
4277a4552177SSatish Balay 
42789985e31cSBarry Smith 
427969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
428036db0b34SBarry Smith 
428136db0b34SBarry Smith @*/
42827087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
428336db0b34SBarry Smith {
4284dfbe8321SBarry Smith   PetscErrorCode ierr;
4285cbcfb4deSHong Zhang   PetscInt       ii;
428636db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4287cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4288cbcfb4deSHong Zhang   PetscInt jj;
4289cbcfb4deSHong Zhang #endif
429036db0b34SBarry Smith 
429136db0b34SBarry Smith   PetscFunctionBegin;
4292f23aa3ddSBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4293f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4294f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4295a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4296ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4297ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4298ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4299dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&aij->imax,m,&aij->ilen);CHKERRQ(ierr);
4300ab93d7beSBarry Smith 
430136db0b34SBarry Smith   aij->i            = i;
430236db0b34SBarry Smith   aij->j            = j;
430336db0b34SBarry Smith   aij->a            = a;
430436db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
430536db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4306e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4307e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
430836db0b34SBarry Smith 
430936db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
431036db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
43112515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
431260e0710aSBarry 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]);
43139985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4314e32f2f54SBarry 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);
4315e32f2f54SBarry 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);
43169985e31cSBarry Smith     }
431736db0b34SBarry Smith #endif
431836db0b34SBarry Smith   }
43192515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
432036db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
432160e0710aSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
432260e0710aSBarry 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]);
432336db0b34SBarry Smith   }
432436db0b34SBarry Smith #endif
432536db0b34SBarry Smith 
4326b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4327b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
432836db0b34SBarry Smith   PetscFunctionReturn(0);
432936db0b34SBarry Smith }
43308a0b0e6bSVictor Minden #undef __FUNCT__
43318a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
433280ef6e79SMatthew G Knepley /*@C
4333d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
43348a0b0e6bSVictor Minden               provided by the user.
43358a0b0e6bSVictor Minden 
43368a0b0e6bSVictor Minden       Collective on MPI_Comm
43378a0b0e6bSVictor Minden 
43388a0b0e6bSVictor Minden    Input Parameters:
43398a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
43408a0b0e6bSVictor Minden .   m   - number of rows
43418a0b0e6bSVictor Minden .   n   - number of columns
43428a0b0e6bSVictor Minden .   i   - row indices
43438a0b0e6bSVictor Minden .   j   - column indices
43441230e6d1SVictor Minden .   a   - matrix values
43451230e6d1SVictor Minden .   nz  - number of nonzeros
43461230e6d1SVictor Minden -   idx - 0 or 1 based
43478a0b0e6bSVictor Minden 
43488a0b0e6bSVictor Minden    Output Parameter:
43498a0b0e6bSVictor Minden .   mat - the matrix
43508a0b0e6bSVictor Minden 
43518a0b0e6bSVictor Minden    Level: intermediate
43528a0b0e6bSVictor Minden 
43538a0b0e6bSVictor Minden    Notes:
43548a0b0e6bSVictor Minden        The i and j indices are 0 based
43558a0b0e6bSVictor Minden 
43568a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
43578a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
43588a0b0e6bSVictor Minden     as shown:
43598a0b0e6bSVictor Minden 
43608a0b0e6bSVictor Minden         1 0 0
43618a0b0e6bSVictor Minden         2 0 3
43628a0b0e6bSVictor Minden         4 5 6
43638a0b0e6bSVictor Minden 
43648a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
43658a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
43668a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
43678a0b0e6bSVictor Minden 
43688a0b0e6bSVictor Minden 
436969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
43708a0b0e6bSVictor Minden 
43718a0b0e6bSVictor Minden @*/
43721230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
43738a0b0e6bSVictor Minden {
43748a0b0e6bSVictor Minden   PetscErrorCode ierr;
4375d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
43768a0b0e6bSVictor Minden 
43778a0b0e6bSVictor Minden 
43788a0b0e6bSVictor Minden   PetscFunctionBegin;
43791795a4d1SJed Brown   ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr);
43801230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
4381c8d679ebSHong Zhang     nnz[i[ii] - !!idx] += 1;
43821230e6d1SVictor Minden   }
43838a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
43848a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
43858a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
43861230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
43871230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
43881230e6d1SVictor Minden     if (idx) {
43891230e6d1SVictor Minden       row = i[ii] - 1;
43901230e6d1SVictor Minden       col = j[ii] - 1;
43911230e6d1SVictor Minden     } else {
43921230e6d1SVictor Minden       row = i[ii];
43931230e6d1SVictor Minden       col = j[ii];
43948a0b0e6bSVictor Minden     }
43951230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
43968a0b0e6bSVictor Minden   }
43978a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
43988a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4399d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
44008a0b0e6bSVictor Minden   PetscFunctionReturn(0);
44018a0b0e6bSVictor Minden }
440236db0b34SBarry Smith 
4403cc8ba8e1SBarry Smith #undef __FUNCT__
4404ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4405dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4406cc8ba8e1SBarry Smith {
4407dfbe8321SBarry Smith   PetscErrorCode ierr;
4408cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
440936db0b34SBarry Smith 
4410cc8ba8e1SBarry Smith   PetscFunctionBegin;
44118ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4412cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4413cc8ba8e1SBarry Smith     a->coloring = coloring;
441412c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
441597f1f81fSBarry Smith     PetscInt        i,*larray;
441612c595b3SBarry Smith     ISColoring      ocoloring;
441708b6dcc0SBarry Smith     ISColoringValue *colors;
441812c595b3SBarry Smith 
441912c595b3SBarry Smith     /* set coloring for diagonal portion */
4420785e854fSJed Brown     ierr = PetscMalloc1(A->cmap->n,&larray);CHKERRQ(ierr);
44212205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) larray[i] = i;
44220298fd71SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);CHKERRQ(ierr);
4423785e854fSJed Brown     ierr = PetscMalloc1(A->cmap->n,&colors);CHKERRQ(ierr);
44242205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
442512c595b3SBarry Smith     ierr        = PetscFree(larray);CHKERRQ(ierr);
4426aaf3ff59SMatthew G. Knepley     ierr        = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,PETSC_OWN_POINTER,&ocoloring);CHKERRQ(ierr);
442712c595b3SBarry Smith     a->coloring = ocoloring;
442812c595b3SBarry Smith   }
4429cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4430cc8ba8e1SBarry Smith }
4431cc8ba8e1SBarry Smith 
4432ee4f033dSBarry Smith #undef __FUNCT__
4433ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
443497f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4435ee4f033dSBarry Smith {
4436ee4f033dSBarry Smith   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
4437d0f46423SBarry Smith   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
443854f21887SBarry Smith   MatScalar       *v      = a->a;
443954f21887SBarry Smith   PetscScalar     *values = (PetscScalar*)advalues;
444008b6dcc0SBarry Smith   ISColoringValue *color;
4441ee4f033dSBarry Smith 
4442ee4f033dSBarry Smith   PetscFunctionBegin;
4443e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4444ee4f033dSBarry Smith   color = a->coloring->colors;
4445ee4f033dSBarry Smith   /* loop over rows */
4446ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4447ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4448ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
44492205254eSKarl Rupp     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
4450ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4451cc8ba8e1SBarry Smith   }
4452cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4453cc8ba8e1SBarry Smith }
445436db0b34SBarry Smith 
4455acf2f550SJed Brown #undef __FUNCT__
4456acf2f550SJed Brown #define __FUNCT__ "MatSeqAIJInvalidateDiagonal"
4457acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4458acf2f550SJed Brown {
4459acf2f550SJed Brown   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4460acf2f550SJed Brown   PetscErrorCode ierr;
4461acf2f550SJed Brown 
4462acf2f550SJed Brown   PetscFunctionBegin;
4463acf2f550SJed Brown   a->idiagvalid  = PETSC_FALSE;
4464acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
44652205254eSKarl Rupp 
4466acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4467acf2f550SJed Brown   PetscFunctionReturn(0);
4468acf2f550SJed Brown }
4469acf2f550SJed Brown 
44709c8f2541SHong Zhang #undef __FUNCT__
44719c8f2541SHong Zhang #define __FUNCT__ "MatCreateMPIMatConcatenateSeqMat_SeqAIJ"
44729c8f2541SHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
44739c8f2541SHong Zhang {
44749c8f2541SHong Zhang   PetscErrorCode ierr;
44759c8f2541SHong Zhang 
44769c8f2541SHong Zhang   PetscFunctionBegin;
44779c8f2541SHong Zhang   ierr = MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr);
44789c8f2541SHong Zhang   PetscFunctionReturn(0);
44799c8f2541SHong Zhang }
44809c8f2541SHong Zhang 
448181824310SBarry Smith /*
448253dd7562SDmitry Karpeev  Permute A into C's *local* index space using rowemb,colemb.
448353dd7562SDmitry Karpeev  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
448453dd7562SDmitry Karpeev  of [0,m), colemb is in [0,n).
448553dd7562SDmitry Karpeev  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
448653dd7562SDmitry Karpeev  */
448753dd7562SDmitry Karpeev #undef __FUNCT__
448853dd7562SDmitry Karpeev #define __FUNCT__ "MatSetSeqMat_SeqAIJ"
448953dd7562SDmitry Karpeev PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
449053dd7562SDmitry Karpeev {
449153dd7562SDmitry Karpeev   /* If making this function public, change the error returned in this function away from _PLIB. */
449253dd7562SDmitry Karpeev   PetscErrorCode ierr;
449353dd7562SDmitry Karpeev   Mat_SeqAIJ     *Baij;
449453dd7562SDmitry Karpeev   PetscBool      seqaij;
449553dd7562SDmitry Karpeev   PetscInt       m,n,*nz,i,j,count;
449653dd7562SDmitry Karpeev   PetscScalar    v;
449753dd7562SDmitry Karpeev   const PetscInt *rowindices,*colindices;
449853dd7562SDmitry Karpeev 
449953dd7562SDmitry Karpeev   PetscFunctionBegin;
450053dd7562SDmitry Karpeev   if (!B) PetscFunctionReturn(0);
450153dd7562SDmitry Karpeev   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
450253dd7562SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);CHKERRQ(ierr);
450353dd7562SDmitry Karpeev   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
450453dd7562SDmitry Karpeev   if (rowemb) {
450553dd7562SDmitry Karpeev     ierr = ISGetLocalSize(rowemb,&m);CHKERRQ(ierr);
450653dd7562SDmitry 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);
450753dd7562SDmitry Karpeev   } else {
45086c4ed002SBarry Smith     if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
450953dd7562SDmitry Karpeev   }
451053dd7562SDmitry Karpeev   if (colemb) {
451153dd7562SDmitry Karpeev     ierr = ISGetLocalSize(colemb,&n);CHKERRQ(ierr);
451253dd7562SDmitry 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);
451353dd7562SDmitry Karpeev   } else {
451453dd7562SDmitry Karpeev     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
451553dd7562SDmitry Karpeev   }
451653dd7562SDmitry Karpeev 
451753dd7562SDmitry Karpeev   Baij = (Mat_SeqAIJ*)(B->data);
451853dd7562SDmitry Karpeev   if (pattern == DIFFERENT_NONZERO_PATTERN) {
451953dd7562SDmitry Karpeev     ierr = PetscMalloc1(B->rmap->n,&nz);CHKERRQ(ierr);
452053dd7562SDmitry Karpeev     for (i=0; i<B->rmap->n; i++) {
452153dd7562SDmitry Karpeev       nz[i] = Baij->i[i+1] - Baij->i[i];
452253dd7562SDmitry Karpeev     }
452353dd7562SDmitry Karpeev     ierr = MatSeqAIJSetPreallocation(C,0,nz);CHKERRQ(ierr);
452453dd7562SDmitry Karpeev     ierr = PetscFree(nz);CHKERRQ(ierr);
452553dd7562SDmitry Karpeev   }
452653dd7562SDmitry Karpeev   if (pattern == SUBSET_NONZERO_PATTERN) {
452753dd7562SDmitry Karpeev     ierr = MatZeroEntries(C);CHKERRQ(ierr);
452853dd7562SDmitry Karpeev   }
452953dd7562SDmitry Karpeev   count = 0;
453053dd7562SDmitry Karpeev   rowindices = NULL;
453153dd7562SDmitry Karpeev   colindices = NULL;
453253dd7562SDmitry Karpeev   if (rowemb) {
453353dd7562SDmitry Karpeev     ierr = ISGetIndices(rowemb,&rowindices);CHKERRQ(ierr);
453453dd7562SDmitry Karpeev   }
453553dd7562SDmitry Karpeev   if (colemb) {
453653dd7562SDmitry Karpeev     ierr = ISGetIndices(colemb,&colindices);CHKERRQ(ierr);
453753dd7562SDmitry Karpeev   }
453853dd7562SDmitry Karpeev   for (i=0; i<B->rmap->n; i++) {
453953dd7562SDmitry Karpeev     PetscInt row;
454053dd7562SDmitry Karpeev     row = i;
454153dd7562SDmitry Karpeev     if (rowindices) row = rowindices[i];
454253dd7562SDmitry Karpeev     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
454353dd7562SDmitry Karpeev       PetscInt col;
454453dd7562SDmitry Karpeev       col  = Baij->j[count];
454553dd7562SDmitry Karpeev       if (colindices) col = colindices[col];
454653dd7562SDmitry Karpeev       v    = Baij->a[count];
454753dd7562SDmitry Karpeev       ierr = MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);CHKERRQ(ierr);
454853dd7562SDmitry Karpeev       ++count;
454953dd7562SDmitry Karpeev     }
455053dd7562SDmitry Karpeev   }
455153dd7562SDmitry Karpeev   /* FIXME: set C's nonzerostate correctly. */
455253dd7562SDmitry Karpeev   /* Assembly for C is necessary. */
455353dd7562SDmitry Karpeev   C->preallocated = PETSC_TRUE;
455453dd7562SDmitry Karpeev   C->assembled     = PETSC_TRUE;
455553dd7562SDmitry Karpeev   C->was_assembled = PETSC_FALSE;
455653dd7562SDmitry Karpeev   PetscFunctionReturn(0);
455753dd7562SDmitry Karpeev }
455853dd7562SDmitry Karpeev 
455953dd7562SDmitry Karpeev 
456053dd7562SDmitry Karpeev /*
456181824310SBarry Smith     Special version for direct calls from Fortran
456281824310SBarry Smith */
4563af0996ceSBarry Smith #include <petsc/private/fortranimpl.h>
456481824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
456581824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
456681824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
456781824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
456881824310SBarry Smith #endif
456981824310SBarry Smith 
457081824310SBarry Smith /* Change these macros so can be used in void function */
457181824310SBarry Smith #undef CHKERRQ
4572ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
457381824310SBarry Smith #undef SETERRQ2
4574e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
45754994cf47SJed Brown #undef SETERRQ3
45764994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
457781824310SBarry Smith 
457881824310SBarry Smith #undef __FUNCT__
457981824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
45808cc058d9SJed 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)
458181824310SBarry Smith {
458281824310SBarry Smith   Mat            A  = *AA;
458381824310SBarry Smith   PetscInt       m  = *mm, n = *nn;
458481824310SBarry Smith   InsertMode     is = *isis;
458581824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
458681824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
458781824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
458881824310SBarry Smith   PetscErrorCode ierr;
458981824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
459054f21887SBarry Smith   MatScalar      *ap,value,*aa;
4591ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4592ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
459381824310SBarry Smith 
459481824310SBarry Smith   PetscFunctionBegin;
45954994cf47SJed Brown   MatCheckPreallocated(A,1);
459681824310SBarry Smith   imax  = a->imax;
459781824310SBarry Smith   ai    = a->i;
459881824310SBarry Smith   ailen = a->ilen;
459981824310SBarry Smith   aj    = a->j;
460081824310SBarry Smith   aa    = a->a;
460181824310SBarry Smith 
460281824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
460381824310SBarry Smith     row = im[k];
460481824310SBarry Smith     if (row < 0) continue;
460581824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4606ce94432eSBarry Smith     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
460781824310SBarry Smith #endif
460881824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
460981824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
461081824310SBarry Smith     low  = 0;
461181824310SBarry Smith     high = nrow;
461281824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
461381824310SBarry Smith       if (in[l] < 0) continue;
461481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4615ce94432eSBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
461681824310SBarry Smith #endif
461781824310SBarry Smith       col = in[l];
46182205254eSKarl Rupp       if (roworiented) value = v[l + k*n];
46192205254eSKarl Rupp       else value = v[k + l*m];
46202205254eSKarl Rupp 
462181824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
462281824310SBarry Smith 
46232205254eSKarl Rupp       if (col <= lastcol) low = 0;
46242205254eSKarl Rupp       else high = nrow;
462581824310SBarry Smith       lastcol = col;
462681824310SBarry Smith       while (high-low > 5) {
462781824310SBarry Smith         t = (low+high)/2;
462881824310SBarry Smith         if (rp[t] > col) high = t;
462981824310SBarry Smith         else             low  = t;
463081824310SBarry Smith       }
463181824310SBarry Smith       for (i=low; i<high; i++) {
463281824310SBarry Smith         if (rp[i] > col) break;
463381824310SBarry Smith         if (rp[i] == col) {
463481824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
463581824310SBarry Smith           else                  ap[i] = value;
463681824310SBarry Smith           goto noinsert;
463781824310SBarry Smith         }
463881824310SBarry Smith       }
463981824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
464081824310SBarry Smith       if (nonew == 1) goto noinsert;
4641ce94432eSBarry Smith       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4642fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
464381824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
464481824310SBarry Smith       /* shift up all the later entries in this row */
464581824310SBarry Smith       for (ii=N; ii>=i; ii--) {
464681824310SBarry Smith         rp[ii+1] = rp[ii];
464781824310SBarry Smith         ap[ii+1] = ap[ii];
464881824310SBarry Smith       }
464981824310SBarry Smith       rp[i] = col;
465081824310SBarry Smith       ap[i] = value;
4651e56f5c9eSBarry Smith       A->nonzerostate++;
465281824310SBarry Smith noinsert:;
465381824310SBarry Smith       low = i + 1;
465481824310SBarry Smith     }
465581824310SBarry Smith     ailen[row] = nrow;
465681824310SBarry Smith   }
465781824310SBarry Smith   PetscFunctionReturnVoid();
465881824310SBarry Smith }
46599f7953f8SBarry Smith 
4660