xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 53dd7562704f85dd7aef0227c74b9380a0de8b1a)
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>
1278b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
1378b84d54SShri Abhyankar #include <petscthreadcomm.h>
1478b84d54SShri Abhyankar #endif
150716a85fSBarry Smith 
160716a85fSBarry Smith #undef __FUNCT__
170716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_SeqAIJ"
180716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
190716a85fSBarry Smith {
200716a85fSBarry Smith   PetscErrorCode ierr;
210716a85fSBarry Smith   PetscInt       i,m,n;
220716a85fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
230716a85fSBarry Smith 
240716a85fSBarry Smith   PetscFunctionBegin;
250716a85fSBarry Smith   ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr);
260716a85fSBarry Smith   ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr);
270716a85fSBarry Smith   if (type == NORM_2) {
280716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
290716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
300716a85fSBarry Smith     }
310716a85fSBarry Smith   } else if (type == NORM_1) {
320716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
330716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
340716a85fSBarry Smith     }
350716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
360716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
370716a85fSBarry Smith       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
380716a85fSBarry Smith     }
390716a85fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");
400716a85fSBarry Smith 
410716a85fSBarry Smith   if (type == NORM_2) {
428f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
430716a85fSBarry Smith   }
440716a85fSBarry Smith   PetscFunctionReturn(0);
450716a85fSBarry Smith }
460716a85fSBarry Smith 
474a2ae208SSatish Balay #undef __FUNCT__
483a062f41SBarry Smith #define __FUNCT__ "MatFindOffBlockDiagonalEntries_SeqAIJ"
493a062f41SBarry Smith PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A,IS *is)
503a062f41SBarry Smith {
513a062f41SBarry Smith   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
523a062f41SBarry Smith   PetscInt        i,m=A->rmap->n,cnt = 0, bs = A->rmap->bs;
533a062f41SBarry Smith   const PetscInt  *jj = a->j,*ii = a->i;
543a062f41SBarry Smith   PetscInt        *rows;
553a062f41SBarry Smith   PetscErrorCode  ierr;
563a062f41SBarry Smith 
573a062f41SBarry Smith   PetscFunctionBegin;
583a062f41SBarry Smith   for (i=0; i<m; i++) {
593a062f41SBarry Smith     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
603a062f41SBarry Smith       cnt++;
613a062f41SBarry Smith     }
623a062f41SBarry Smith   }
633a062f41SBarry Smith   ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr);
643a062f41SBarry Smith   cnt  = 0;
653a062f41SBarry Smith   for (i=0; i<m; i++) {
663a062f41SBarry Smith     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
673a062f41SBarry Smith       rows[cnt] = i;
683a062f41SBarry Smith       cnt++;
693a062f41SBarry Smith     }
703a062f41SBarry Smith   }
713a062f41SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,is);CHKERRQ(ierr);
723a062f41SBarry Smith   PetscFunctionReturn(0);
733a062f41SBarry Smith }
743a062f41SBarry Smith 
753a062f41SBarry Smith #undef __FUNCT__
76f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ_Private"
77f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows)
786ce1633cSBarry Smith {
796ce1633cSBarry Smith   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
806ce1633cSBarry Smith   const MatScalar *aa = a->a;
816ce1633cSBarry Smith   PetscInt        i,m=A->rmap->n,cnt = 0;
826ce1633cSBarry Smith   const PetscInt  *jj = a->j,*diag;
836ce1633cSBarry Smith   PetscInt        *rows;
846ce1633cSBarry Smith   PetscErrorCode  ierr;
856ce1633cSBarry Smith 
866ce1633cSBarry Smith   PetscFunctionBegin;
876ce1633cSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
886ce1633cSBarry Smith   diag = a->diag;
896ce1633cSBarry Smith   for (i=0; i<m; i++) {
906ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
916ce1633cSBarry Smith       cnt++;
926ce1633cSBarry Smith     }
936ce1633cSBarry Smith   }
94785e854fSJed Brown   ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr);
956ce1633cSBarry Smith   cnt  = 0;
966ce1633cSBarry Smith   for (i=0; i<m; i++) {
976ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
986ce1633cSBarry Smith       rows[cnt++] = i;
996ce1633cSBarry Smith     }
1006ce1633cSBarry Smith   }
101f1f41ecbSJed Brown   *nrows = cnt;
102f1f41ecbSJed Brown   *zrows = rows;
103f1f41ecbSJed Brown   PetscFunctionReturn(0);
104f1f41ecbSJed Brown }
105f1f41ecbSJed Brown 
106f1f41ecbSJed Brown #undef __FUNCT__
107f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ"
108f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
109f1f41ecbSJed Brown {
110f1f41ecbSJed Brown   PetscInt       nrows,*rows;
111f1f41ecbSJed Brown   PetscErrorCode ierr;
112f1f41ecbSJed Brown 
113f1f41ecbSJed Brown   PetscFunctionBegin;
1140298fd71SBarry Smith   *zrows = NULL;
115f1f41ecbSJed Brown   ierr   = MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);CHKERRQ(ierr);
116ce94432eSBarry Smith   ierr   = ISCreateGeneral(PetscObjectComm((PetscObject)A),nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
1176ce1633cSBarry Smith   PetscFunctionReturn(0);
1186ce1633cSBarry Smith }
1196ce1633cSBarry Smith 
1206ce1633cSBarry Smith #undef __FUNCT__
121b3a44c85SBarry Smith #define __FUNCT__ "MatFindNonzeroRows_SeqAIJ"
122b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
123b3a44c85SBarry Smith {
124b3a44c85SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
125b3a44c85SBarry Smith   const MatScalar *aa;
126b3a44c85SBarry Smith   PetscInt        m=A->rmap->n,cnt = 0;
127b3a44c85SBarry Smith   const PetscInt  *ii;
128b3a44c85SBarry Smith   PetscInt        n,i,j,*rows;
129b3a44c85SBarry Smith   PetscErrorCode  ierr;
130b3a44c85SBarry Smith 
131b3a44c85SBarry Smith   PetscFunctionBegin;
132b3a44c85SBarry Smith   *keptrows = 0;
133b3a44c85SBarry Smith   ii        = a->i;
134b3a44c85SBarry Smith   for (i=0; i<m; i++) {
135b3a44c85SBarry Smith     n = ii[i+1] - ii[i];
136b3a44c85SBarry Smith     if (!n) {
137b3a44c85SBarry Smith       cnt++;
138b3a44c85SBarry Smith       goto ok1;
139b3a44c85SBarry Smith     }
140b3a44c85SBarry Smith     aa = a->a + ii[i];
141b3a44c85SBarry Smith     for (j=0; j<n; j++) {
142b3a44c85SBarry Smith       if (aa[j] != 0.0) goto ok1;
143b3a44c85SBarry Smith     }
144b3a44c85SBarry Smith     cnt++;
145b3a44c85SBarry Smith ok1:;
146b3a44c85SBarry Smith   }
147b3a44c85SBarry Smith   if (!cnt) PetscFunctionReturn(0);
148854ce69bSBarry Smith   ierr = PetscMalloc1(A->rmap->n-cnt,&rows);CHKERRQ(ierr);
149b3a44c85SBarry Smith   cnt  = 0;
150b3a44c85SBarry Smith   for (i=0; i<m; i++) {
151b3a44c85SBarry Smith     n = ii[i+1] - ii[i];
152b3a44c85SBarry Smith     if (!n) continue;
153b3a44c85SBarry Smith     aa = a->a + ii[i];
154b3a44c85SBarry Smith     for (j=0; j<n; j++) {
155b3a44c85SBarry Smith       if (aa[j] != 0.0) {
156b3a44c85SBarry Smith         rows[cnt++] = i;
157b3a44c85SBarry Smith         break;
158b3a44c85SBarry Smith       }
159b3a44c85SBarry Smith     }
160b3a44c85SBarry Smith   }
161b3a44c85SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
162b3a44c85SBarry Smith   PetscFunctionReturn(0);
163b3a44c85SBarry Smith }
164b3a44c85SBarry Smith 
165b3a44c85SBarry Smith #undef __FUNCT__
16679299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1677087cfbeSBarry Smith PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
16879299369SBarry Smith {
16979299369SBarry Smith   PetscErrorCode    ierr;
17079299369SBarry Smith   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*) Y->data;
17199e65526SBarry Smith   PetscInt          i,m = Y->rmap->n;
17299e65526SBarry Smith   const PetscInt    *diag;
17354f21887SBarry Smith   MatScalar         *aa = aij->a;
17499e65526SBarry Smith   const PetscScalar *v;
175ace3abfcSBarry Smith   PetscBool         missing;
17679299369SBarry Smith 
17779299369SBarry Smith   PetscFunctionBegin;
17809f38230SBarry Smith   if (Y->assembled) {
1790298fd71SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);CHKERRQ(ierr);
18009f38230SBarry Smith     if (!missing) {
18179299369SBarry Smith       diag = aij->diag;
18299e65526SBarry Smith       ierr = VecGetArrayRead(D,&v);CHKERRQ(ierr);
18379299369SBarry Smith       if (is == INSERT_VALUES) {
18479299369SBarry Smith         for (i=0; i<m; i++) {
18579299369SBarry Smith           aa[diag[i]] = v[i];
18679299369SBarry Smith         }
18779299369SBarry Smith       } else {
18879299369SBarry Smith         for (i=0; i<m; i++) {
18979299369SBarry Smith           aa[diag[i]] += v[i];
19079299369SBarry Smith         }
19179299369SBarry Smith       }
19299e65526SBarry Smith       ierr = VecRestoreArrayRead(D,&v);CHKERRQ(ierr);
19379299369SBarry Smith       PetscFunctionReturn(0);
19479299369SBarry Smith     }
195acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
19609f38230SBarry Smith   }
19709f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
19809f38230SBarry Smith   PetscFunctionReturn(0);
19909f38230SBarry Smith }
20079299369SBarry Smith 
20179299369SBarry Smith #undef __FUNCT__
2024a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
2031a83f524SJed Brown PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
20417ab2063SBarry Smith {
205416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
206dfbe8321SBarry Smith   PetscErrorCode ierr;
20797f1f81fSBarry Smith   PetscInt       i,ishift;
20817ab2063SBarry Smith 
2093a40ed3dSBarry Smith   PetscFunctionBegin;
210d0f46423SBarry Smith   *m = A->rmap->n;
2113a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
212bfeeae90SHong Zhang   ishift = 0;
21353e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
2141a83f524SJed Brown     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
215bfeeae90SHong Zhang   } else if (oshift == 1) {
2161a83f524SJed Brown     PetscInt *tia;
217d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
2183b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
219854ce69bSBarry Smith     ierr = PetscMalloc1(A->rmap->n+1,&tia);CHKERRQ(ierr);
2201a83f524SJed Brown     for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1;
2211a83f524SJed Brown     *ia = tia;
222ecc77c7aSBarry Smith     if (ja) {
2231a83f524SJed Brown       PetscInt *tja;
224854ce69bSBarry Smith       ierr = PetscMalloc1(nz+1,&tja);CHKERRQ(ierr);
2251a83f524SJed Brown       for (i=0; i<nz; i++) tja[i] = a->j[i] + 1;
2261a83f524SJed Brown       *ja = tja;
227ecc77c7aSBarry Smith     }
2286945ee14SBarry Smith   } else {
229ecc77c7aSBarry Smith     *ia = a->i;
230ecc77c7aSBarry Smith     if (ja) *ja = a->j;
231a2ce50c7SBarry Smith   }
2323a40ed3dSBarry Smith   PetscFunctionReturn(0);
233a2744918SBarry Smith }
234a2744918SBarry Smith 
2354a2ae208SSatish Balay #undef __FUNCT__
2364a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
2371a83f524SJed Brown PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2386945ee14SBarry Smith {
239dfbe8321SBarry Smith   PetscErrorCode ierr;
2406945ee14SBarry Smith 
2413a40ed3dSBarry Smith   PetscFunctionBegin;
2423a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
243bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
244606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
245ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
246bcd2baecSBarry Smith   }
2473a40ed3dSBarry Smith   PetscFunctionReturn(0);
24817ab2063SBarry Smith }
24917ab2063SBarry Smith 
2504a2ae208SSatish Balay #undef __FUNCT__
2514a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
2521a83f524SJed Brown PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2533b2fbd54SBarry Smith {
2543b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
255dfbe8321SBarry Smith   PetscErrorCode ierr;
256d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
25797f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
2583b2fbd54SBarry Smith 
2593a40ed3dSBarry Smith   PetscFunctionBegin;
260899cda47SBarry Smith   *nn = n;
2613a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2623b2fbd54SBarry Smith   if (symmetric) {
2631a83f524SJed Brown     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
2643b2fbd54SBarry Smith   } else {
2651795a4d1SJed Brown     ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
266854ce69bSBarry Smith     ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr);
267854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&cja);CHKERRQ(ierr);
2683b2fbd54SBarry Smith     jj   = a->j;
2693b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
270bfeeae90SHong Zhang       collengths[jj[i]]++;
2713b2fbd54SBarry Smith     }
2723b2fbd54SBarry Smith     cia[0] = oshift;
2733b2fbd54SBarry Smith     for (i=0; i<n; i++) {
2743b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
2753b2fbd54SBarry Smith     }
27697f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
2773b2fbd54SBarry Smith     jj   = a->j;
278a93ec695SBarry Smith     for (row=0; row<m; row++) {
279a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
280a93ec695SBarry Smith       for (i=0; i<mr; i++) {
281bfeeae90SHong Zhang         col = *jj++;
2822205254eSKarl Rupp 
2833b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2843b2fbd54SBarry Smith       }
2853b2fbd54SBarry Smith     }
286606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2873b2fbd54SBarry Smith     *ia  = cia; *ja = cja;
2883b2fbd54SBarry Smith   }
2893a40ed3dSBarry Smith   PetscFunctionReturn(0);
2903b2fbd54SBarry Smith }
2913b2fbd54SBarry Smith 
2924a2ae208SSatish Balay #undef __FUNCT__
2934a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
2941a83f524SJed Brown PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2953b2fbd54SBarry Smith {
296dfbe8321SBarry Smith   PetscErrorCode ierr;
297606d414cSSatish Balay 
2983a40ed3dSBarry Smith   PetscFunctionBegin;
2993a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
3003b2fbd54SBarry Smith 
301606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
302606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
3033a40ed3dSBarry Smith   PetscFunctionReturn(0);
3043b2fbd54SBarry Smith }
3053b2fbd54SBarry Smith 
3067cee066cSHong Zhang /*
3077cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
3087cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
309040ebd07SHong Zhang  spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
3107cee066cSHong Zhang */
3117cee066cSHong Zhang #undef __FUNCT__
3127cee066cSHong Zhang #define __FUNCT__ "MatGetColumnIJ_SeqAIJ_Color"
3137cee066cSHong 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)
3147cee066cSHong Zhang {
3157cee066cSHong Zhang   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3167cee066cSHong Zhang   PetscErrorCode ierr;
3177cee066cSHong Zhang   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
3187cee066cSHong Zhang   PetscInt       nz = a->i[m],row,*jj,mr,col;
3197cee066cSHong Zhang   PetscInt       *cspidx;
3207cee066cSHong Zhang 
3217cee066cSHong Zhang   PetscFunctionBegin;
3227cee066cSHong Zhang   *nn = n;
3237cee066cSHong Zhang   if (!ia) PetscFunctionReturn(0);
324625f6d37SHong Zhang 
3251795a4d1SJed Brown   ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
326854ce69bSBarry Smith   ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr);
327854ce69bSBarry Smith   ierr = PetscMalloc1(nz+1,&cja);CHKERRQ(ierr);
328854ce69bSBarry Smith   ierr = PetscMalloc1(nz+1,&cspidx);CHKERRQ(ierr);
3297cee066cSHong Zhang   jj   = a->j;
3307cee066cSHong Zhang   for (i=0; i<nz; i++) {
3317cee066cSHong Zhang     collengths[jj[i]]++;
3327cee066cSHong Zhang   }
3337cee066cSHong Zhang   cia[0] = oshift;
3347cee066cSHong Zhang   for (i=0; i<n; i++) {
3357cee066cSHong Zhang     cia[i+1] = cia[i] + collengths[i];
3367cee066cSHong Zhang   }
3377cee066cSHong Zhang   ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
3387cee066cSHong Zhang   jj   = a->j;
3397cee066cSHong Zhang   for (row=0; row<m; row++) {
3407cee066cSHong Zhang     mr = a->i[row+1] - a->i[row];
3417cee066cSHong Zhang     for (i=0; i<mr; i++) {
3427cee066cSHong Zhang       col = *jj++;
3437cee066cSHong Zhang       cspidx[cia[col] + collengths[col] - oshift] = a->i[row] + i; /* index of a->j */
3447cee066cSHong Zhang       cja[cia[col] + collengths[col]++ - oshift]  = row + oshift;
3457cee066cSHong Zhang     }
3467cee066cSHong Zhang   }
3477cee066cSHong Zhang   ierr   = PetscFree(collengths);CHKERRQ(ierr);
3487cee066cSHong Zhang   *ia    = cia; *ja = cja;
3497cee066cSHong Zhang   *spidx = cspidx;
3507cee066cSHong Zhang   PetscFunctionReturn(0);
3517cee066cSHong Zhang }
3527cee066cSHong Zhang 
3537cee066cSHong Zhang #undef __FUNCT__
3547cee066cSHong Zhang #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ_Color"
3557cee066cSHong 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)
3567cee066cSHong Zhang {
3577cee066cSHong Zhang   PetscErrorCode ierr;
3587cee066cSHong Zhang 
3597cee066cSHong Zhang   PetscFunctionBegin;
3605243ef75SHong Zhang   ierr = MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
3617cee066cSHong Zhang   ierr = PetscFree(*spidx);CHKERRQ(ierr);
3627cee066cSHong Zhang   PetscFunctionReturn(0);
3637cee066cSHong Zhang }
3647cee066cSHong Zhang 
36587d4246cSBarry Smith #undef __FUNCT__
36687d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
36787d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
36887d4246cSBarry Smith {
36987d4246cSBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
37087d4246cSBarry Smith   PetscInt       *ai = a->i;
37187d4246cSBarry Smith   PetscErrorCode ierr;
37287d4246cSBarry Smith 
37387d4246cSBarry Smith   PetscFunctionBegin;
37487d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
37587d4246cSBarry Smith   PetscFunctionReturn(0);
37687d4246cSBarry Smith }
37787d4246cSBarry Smith 
378bd04181cSBarry Smith /*
379bd04181cSBarry Smith     MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions
380bd04181cSBarry Smith 
381bd04181cSBarry Smith       -   a single row of values is set with each call
382bd04181cSBarry Smith       -   no row or column indices are negative or (in error) larger than the number of rows or columns
383bd04181cSBarry Smith       -   the values are always added to the matrix, not set
384bd04181cSBarry Smith       -   no new locations are introduced in the nonzero structure of the matrix
385bd04181cSBarry Smith 
3861f763a69SBarry Smith      This does NOT assume the global column indices are sorted
387bd04181cSBarry Smith 
3881f763a69SBarry Smith */
389bd04181cSBarry Smith 
390af0996ceSBarry Smith #include <petsc/private/isimpl.h>
391189e4007SBarry Smith #undef __FUNCT__
392189e4007SBarry Smith #define __FUNCT__ "MatSeqAIJSetValuesLocalFast"
393189e4007SBarry Smith PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
394189e4007SBarry Smith {
395189e4007SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3961f763a69SBarry Smith   PetscInt       low,high,t,row,nrow,i,col,l;
3971f763a69SBarry Smith   const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j;
3981f763a69SBarry Smith   PetscInt       lastcol = -1;
399189e4007SBarry Smith   MatScalar      *ap,value,*aa = a->a;
400189e4007SBarry Smith   const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices;
401189e4007SBarry Smith 
402f38dd0b8SBarry Smith   row = ridx[im[0]];
4031f763a69SBarry Smith   rp   = aj + ai[row];
4041f763a69SBarry Smith   ap = aa + ai[row];
4051f763a69SBarry Smith   nrow = ailen[row];
406189e4007SBarry Smith   low  = 0;
407189e4007SBarry Smith   high = nrow;
408189e4007SBarry Smith   for (l=0; l<n; l++) { /* loop over added columns */
409189e4007SBarry Smith     col = cidx[in[l]];
410f38dd0b8SBarry Smith     value = v[l];
411189e4007SBarry Smith 
412189e4007SBarry Smith     if (col <= lastcol) low = 0;
413189e4007SBarry Smith     else high = nrow;
414189e4007SBarry Smith     lastcol = col;
415189e4007SBarry Smith     while (high-low > 5) {
416189e4007SBarry Smith       t = (low+high)/2;
417189e4007SBarry Smith       if (rp[t] > col) high = t;
418189e4007SBarry Smith       else low = t;
419189e4007SBarry Smith     }
420189e4007SBarry Smith     for (i=low; i<high; i++) {
421189e4007SBarry Smith       if (rp[i] == col) {
4221f763a69SBarry Smith         ap[i] += value;
423189e4007SBarry Smith         low = i + 1;
4241f763a69SBarry Smith         break;
425189e4007SBarry Smith       }
426189e4007SBarry Smith     }
427189e4007SBarry Smith   }
428f38dd0b8SBarry Smith   return 0;
429189e4007SBarry Smith }
430189e4007SBarry Smith 
431bd04181cSBarry Smith #undef __FUNCT__
4324a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
43397f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
43417ab2063SBarry Smith {
435416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
436e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
43797f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
4386849ba73SBarry Smith   PetscErrorCode ierr;
439e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
44054f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
441ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
442ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
44317ab2063SBarry Smith 
4443a40ed3dSBarry Smith   PetscFunctionBegin;
44517ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
446416022c9SBarry Smith     row = im[k];
4475ef9f2a5SBarry Smith     if (row < 0) continue;
4482515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
449e32f2f54SBarry 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);
4503b2fbd54SBarry Smith #endif
451bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
45217ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
453416022c9SBarry Smith     low  = 0;
454c71e6ed7SBarry Smith     high = nrow;
45517ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
4565ef9f2a5SBarry Smith       if (in[l] < 0) continue;
4572515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
458e32f2f54SBarry 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);
4593b2fbd54SBarry Smith #endif
460bfeeae90SHong Zhang       col = in[l];
4614b0e389bSBarry Smith       if (roworiented) {
4625ef9f2a5SBarry Smith         value = v[l + k*n];
463bef8e0ddSBarry Smith       } else {
4644b0e389bSBarry Smith         value = v[k + l*m];
4654b0e389bSBarry Smith       }
46633b2b78bSBarry Smith       if ((value == 0.0 && ignorezeroentries) && (is == ADD_VALUES)) continue;
46736db0b34SBarry Smith 
4682205254eSKarl Rupp       if (col <= lastcol) low = 0;
4692205254eSKarl Rupp       else high = nrow;
470e2ee6c50SBarry Smith       lastcol = col;
471416022c9SBarry Smith       while (high-low > 5) {
472416022c9SBarry Smith         t = (low+high)/2;
473416022c9SBarry Smith         if (rp[t] > col) high = t;
474416022c9SBarry Smith         else low = t;
47517ab2063SBarry Smith       }
476416022c9SBarry Smith       for (i=low; i<high; i++) {
47717ab2063SBarry Smith         if (rp[i] > col) break;
47817ab2063SBarry Smith         if (rp[i] == col) {
479416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
48017ab2063SBarry Smith           else ap[i] = value;
481e44c0bd4SBarry Smith           low = i + 1;
48217ab2063SBarry Smith           goto noinsert;
48317ab2063SBarry Smith         }
48417ab2063SBarry Smith       }
485abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
486c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
487e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
488fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
489c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
490416022c9SBarry Smith       /* shift up all the later entries in this row */
491416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
49217ab2063SBarry Smith         rp[ii+1] = rp[ii];
49317ab2063SBarry Smith         ap[ii+1] = ap[ii];
49417ab2063SBarry Smith       }
49517ab2063SBarry Smith       rp[i] = col;
49617ab2063SBarry Smith       ap[i] = value;
497416022c9SBarry Smith       low   = i + 1;
498e56f5c9eSBarry Smith       A->nonzerostate++;
499e44c0bd4SBarry Smith noinsert:;
50017ab2063SBarry Smith     }
50117ab2063SBarry Smith     ailen[row] = nrow;
50217ab2063SBarry Smith   }
5033a40ed3dSBarry Smith   PetscFunctionReturn(0);
50417ab2063SBarry Smith }
50517ab2063SBarry Smith 
50681824310SBarry Smith 
5074a2ae208SSatish Balay #undef __FUNCT__
5084a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
509a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
5107eb43aa7SLois Curfman McInnes {
5117eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
51297f1f81fSBarry Smith   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
51397f1f81fSBarry Smith   PetscInt   *ai = a->i,*ailen = a->ilen;
51454f21887SBarry Smith   MatScalar  *ap,*aa = a->a;
5157eb43aa7SLois Curfman McInnes 
5163a40ed3dSBarry Smith   PetscFunctionBegin;
5177eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
5187eb43aa7SLois Curfman McInnes     row = im[k];
519e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
520e32f2f54SBarry 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);
521bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
5227eb43aa7SLois Curfman McInnes     nrow = ailen[row];
5237eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
524e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
525e32f2f54SBarry 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);
526bfeeae90SHong Zhang       col  = in[l];
5277eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
5287eb43aa7SLois Curfman McInnes       while (high-low > 5) {
5297eb43aa7SLois Curfman McInnes         t = (low+high)/2;
5307eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
5317eb43aa7SLois Curfman McInnes         else low = t;
5327eb43aa7SLois Curfman McInnes       }
5337eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
5347eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
5357eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
536b49de8d1SLois Curfman McInnes           *v++ = ap[i];
5377eb43aa7SLois Curfman McInnes           goto finished;
5387eb43aa7SLois Curfman McInnes         }
5397eb43aa7SLois Curfman McInnes       }
54097e567efSBarry Smith       *v++ = 0.0;
5417eb43aa7SLois Curfman McInnes finished:;
5427eb43aa7SLois Curfman McInnes     }
5437eb43aa7SLois Curfman McInnes   }
5443a40ed3dSBarry Smith   PetscFunctionReturn(0);
5457eb43aa7SLois Curfman McInnes }
5467eb43aa7SLois Curfman McInnes 
54717ab2063SBarry Smith 
5484a2ae208SSatish Balay #undef __FUNCT__
5494a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
550dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
55117ab2063SBarry Smith {
552416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5536849ba73SBarry Smith   PetscErrorCode ierr;
5546f69ff64SBarry Smith   PetscInt       i,*col_lens;
5556f69ff64SBarry Smith   int            fd;
556b37d52dbSMark F. Adams   FILE           *file;
55717ab2063SBarry Smith 
5583a40ed3dSBarry Smith   PetscFunctionBegin;
559b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
560854ce69bSBarry Smith   ierr = PetscMalloc1(4+A->rmap->n,&col_lens);CHKERRQ(ierr);
5612205254eSKarl Rupp 
5620700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
563d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
564d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
565416022c9SBarry Smith   col_lens[3] = a->nz;
566416022c9SBarry Smith 
567416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
568d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
569416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
57017ab2063SBarry Smith   }
571d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
572606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
573416022c9SBarry Smith 
574416022c9SBarry Smith   /* store column indices (zero start index) */
5756f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
576416022c9SBarry Smith 
577416022c9SBarry Smith   /* store nonzero values */
5786f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
579b37d52dbSMark F. Adams 
580b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
581b37d52dbSMark F. Adams   if (file) {
58233d57670SJed Brown     fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs));
583b37d52dbSMark F. Adams   }
5843a40ed3dSBarry Smith   PetscFunctionReturn(0);
58517ab2063SBarry Smith }
586416022c9SBarry Smith 
58709573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
588cd155464SBarry Smith 
5894a2ae208SSatish Balay #undef __FUNCT__
5904a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
591dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
592416022c9SBarry Smith {
593416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
594dfbe8321SBarry Smith   PetscErrorCode    ierr;
59560e0710aSBarry Smith   PetscInt          i,j,m = A->rmap->n;
596e060cb09SBarry Smith   const char        *name;
597f3ef73ceSBarry Smith   PetscViewerFormat format;
59817ab2063SBarry Smith 
5993a40ed3dSBarry Smith   PetscFunctionBegin;
600b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
60171c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
60297f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
60360e0710aSBarry Smith     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
604c337ccceSJed Brown       /* Need a dummy value to ensure the dimension of the matrix. */
605d00d2cf4SBarry Smith       nofinalvalue = 1;
606d00d2cf4SBarry Smith     }
607d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
608d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
60977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
610fbfe6fa7SJed Brown #if defined(PETSC_USE_COMPLEX)
611fbfe6fa7SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
612fbfe6fa7SJed Brown #else
61377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
614fbfe6fa7SJed Brown #endif
615b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
61617ab2063SBarry Smith 
61717ab2063SBarry Smith     for (i=0; i<m; i++) {
61860e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
619aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
620a9bf72d8SJed 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);
62117ab2063SBarry Smith #else
62260e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr);
62317ab2063SBarry Smith #endif
62417ab2063SBarry Smith       }
62517ab2063SBarry Smith     }
626d00d2cf4SBarry Smith     if (nofinalvalue) {
627c337ccceSJed Brown #if defined(PETSC_USE_COMPLEX)
628c337ccceSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr);
629c337ccceSJed Brown #else
630d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
631c337ccceSJed Brown #endif
632d00d2cf4SBarry Smith     }
633317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
634fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
635d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
63668369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
637cd155464SBarry Smith     PetscFunctionReturn(0);
638fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
639d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
64044cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
64177431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
64260e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
643aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
64436db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
64560e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
64636db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
64760e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
64836db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
64960e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
6506831982aSBarry Smith         }
65144cd7ae7SLois Curfman McInnes #else
65260e0710aSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);}
65344cd7ae7SLois Curfman McInnes #endif
65444cd7ae7SLois Curfman McInnes       }
655b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
65644cd7ae7SLois Curfman McInnes     }
657d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
658fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
65997f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
660d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
661854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&sptr);CHKERRQ(ierr);
662496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
663496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
66460e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
665496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
666aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
66736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
668496be53dSLois Curfman McInnes #else
669496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
670496be53dSLois Curfman McInnes #endif
671496be53dSLois Curfman McInnes         }
672496be53dSLois Curfman McInnes       }
673496be53dSLois Curfman McInnes     }
6742e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
67577431f27SBarry Smith     ierr    = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
6762e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
6772205254eSKarl Rupp       if (i+4<m) {
6782205254eSKarl 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);
6792205254eSKarl Rupp       } else if (i+3<m) {
6802205254eSKarl 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);
6812205254eSKarl Rupp       } else if (i+2<m) {
6822205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);
6832205254eSKarl Rupp       } else if (i+1<m) {
6842205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);
6852205254eSKarl Rupp       } else if (i<m) {
6862205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);
6872205254eSKarl Rupp       } else {
6882205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);
6892205254eSKarl Rupp       }
690496be53dSLois Curfman McInnes     }
691b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
692606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
693496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
69460e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
69577431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
696496be53dSLois Curfman McInnes       }
697b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
698496be53dSLois Curfman McInnes     }
699b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
700496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
70160e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
702496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
703aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
70436db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
70560e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
7066831982aSBarry Smith           }
707496be53dSLois Curfman McInnes #else
70860e0710aSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);}
709496be53dSLois Curfman McInnes #endif
710496be53dSLois Curfman McInnes         }
711496be53dSLois Curfman McInnes       }
712b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
713496be53dSLois Curfman McInnes     }
714d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
715fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
71697f1f81fSBarry Smith     PetscInt    cnt = 0,jcnt;
71787828ca2SBarry Smith     PetscScalar value;
71868f1ed48SBarry Smith #if defined(PETSC_USE_COMPLEX)
71968f1ed48SBarry Smith     PetscBool   realonly = PETSC_TRUE;
72068f1ed48SBarry Smith 
72168f1ed48SBarry Smith     for (i=0; i<a->i[m]; i++) {
72268f1ed48SBarry Smith       if (PetscImaginaryPart(a->a[i]) != 0.0) {
72368f1ed48SBarry Smith         realonly = PETSC_FALSE;
72468f1ed48SBarry Smith         break;
72568f1ed48SBarry Smith       }
72668f1ed48SBarry Smith     }
72768f1ed48SBarry Smith #endif
72802594712SBarry Smith 
729d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
73002594712SBarry Smith     for (i=0; i<m; i++) {
73102594712SBarry Smith       jcnt = 0;
732d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
733e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
73402594712SBarry Smith           value = a->a[cnt++];
735e24b481bSBarry Smith           jcnt++;
73602594712SBarry Smith         } else {
73702594712SBarry Smith           value = 0.0;
73802594712SBarry Smith         }
739aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
74068f1ed48SBarry Smith         if (realonly) {
74160e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr);
74268f1ed48SBarry Smith         } else {
74360e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr);
74468f1ed48SBarry Smith         }
74502594712SBarry Smith #else
74660e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr);
74702594712SBarry Smith #endif
74802594712SBarry Smith       }
749b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
75002594712SBarry Smith     }
751d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7523c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
753150b93efSMatthew G. Knepley     PetscInt fshift=1;
754d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
7553c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
75619303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");CHKERRQ(ierr);
7573c215bfdSMatthew Knepley #else
75819303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");CHKERRQ(ierr);
7593c215bfdSMatthew Knepley #endif
760d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
7613c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
76260e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
7633c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
7643c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
76560e0710aSBarry Smith           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);
7663c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
76760e0710aSBarry Smith           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);
7683c215bfdSMatthew Knepley         } else {
76960e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
7703c215bfdSMatthew Knepley         }
7713c215bfdSMatthew Knepley #else
772150b93efSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr);
7733c215bfdSMatthew Knepley #endif
7743c215bfdSMatthew Knepley       }
7753c215bfdSMatthew Knepley     }
776d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7773a40ed3dSBarry Smith   } else {
778d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
779d5f3da31SBarry Smith     if (A->factortype) {
78016cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
78116cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
78216cd7e1dSShri Abhyankar         /* L part */
78360e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
78416cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
78516cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
78660e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
78716cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
7886712e2f1SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
78916cd7e1dSShri Abhyankar           } else {
79060e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
79116cd7e1dSShri Abhyankar           }
79216cd7e1dSShri Abhyankar #else
79360e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
79416cd7e1dSShri Abhyankar #endif
79516cd7e1dSShri Abhyankar         }
79616cd7e1dSShri Abhyankar         /* diagonal */
79716cd7e1dSShri Abhyankar         j = a->diag[i];
79816cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
79916cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
80060e0710aSBarry 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);
80116cd7e1dSShri Abhyankar         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
8026712e2f1SBarry 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);
80316cd7e1dSShri Abhyankar         } else {
80460e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
80516cd7e1dSShri Abhyankar         }
80616cd7e1dSShri Abhyankar #else
80760e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr);
80816cd7e1dSShri Abhyankar #endif
80916cd7e1dSShri Abhyankar 
81016cd7e1dSShri Abhyankar         /* U part */
81160e0710aSBarry Smith         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
81216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
81316cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
81460e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
81516cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
81622ab088eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
81716cd7e1dSShri Abhyankar           } else {
81860e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
81916cd7e1dSShri Abhyankar           }
82016cd7e1dSShri Abhyankar #else
82160e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
82216cd7e1dSShri Abhyankar #endif
82316cd7e1dSShri Abhyankar         }
82416cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
82516cd7e1dSShri Abhyankar       }
82616cd7e1dSShri Abhyankar     } else {
82717ab2063SBarry Smith       for (i=0; i<m; i++) {
82877431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
82960e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
830aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
83136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
83260e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
83336db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
83460e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
8353a40ed3dSBarry Smith           } else {
83660e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
83717ab2063SBarry Smith           }
83817ab2063SBarry Smith #else
83960e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
84017ab2063SBarry Smith #endif
84117ab2063SBarry Smith         }
842b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
84317ab2063SBarry Smith       }
84416cd7e1dSShri Abhyankar     }
845d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
84617ab2063SBarry Smith   }
847b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8483a40ed3dSBarry Smith   PetscFunctionReturn(0);
849416022c9SBarry Smith }
850416022c9SBarry Smith 
8519804daf3SBarry Smith #include <petscdraw.h>
8524a2ae208SSatish Balay #undef __FUNCT__
8534a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
854dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
855416022c9SBarry Smith {
856480ef9eaSBarry Smith   Mat               A  = (Mat) Aa;
857416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
858dfbe8321SBarry Smith   PetscErrorCode    ierr;
859d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
86036db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
861b0a32e0cSBarry Smith   PetscViewer       viewer;
862f3ef73ceSBarry Smith   PetscViewerFormat format;
863cddf8d76SBarry Smith 
8643a40ed3dSBarry Smith   PetscFunctionBegin;
865480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
866b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
86719bcc07fSBarry Smith 
868b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
869416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
8700513a670SBarry Smith 
871fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
8720513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
873b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
874416022c9SBarry 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;
87836db0b34SBarry Smith         if (PetscRealPart(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_CYAN;
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;
887cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
888b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
889cddf8d76SBarry Smith       }
890cddf8d76SBarry Smith     }
891b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
892cddf8d76SBarry Smith     for (i=0; i<m; i++) {
893cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
894bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
895bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
89636db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
897b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
898416022c9SBarry Smith       }
899416022c9SBarry Smith     }
9000513a670SBarry Smith   } else {
9010513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
9020513a670SBarry Smith     /* first determine max of all nonzero values */
90397f1f81fSBarry Smith     PetscInt  nz = a->nz,count;
904b0a32e0cSBarry Smith     PetscDraw popup;
90536db0b34SBarry Smith     PetscReal scale;
9060513a670SBarry Smith 
9070513a670SBarry Smith     for (i=0; i<nz; i++) {
9080513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
9090513a670SBarry Smith     }
910b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
911b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
9122205254eSKarl Rupp     if (popup) {
9132205254eSKarl Rupp       ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);
9142205254eSKarl Rupp     }
9150513a670SBarry Smith     count = 0;
9160513a670SBarry Smith     for (i=0; i<m; i++) {
9170513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
918bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
919bfeeae90SHong Zhang         x_l   = a->j[j]; x_r = x_l + 1.0;
92097f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
921b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
9220513a670SBarry Smith         count++;
9230513a670SBarry Smith       }
9240513a670SBarry Smith     }
9250513a670SBarry Smith   }
926480ef9eaSBarry Smith   PetscFunctionReturn(0);
927480ef9eaSBarry Smith }
928cddf8d76SBarry Smith 
9299804daf3SBarry Smith #include <petscdraw.h>
9304a2ae208SSatish Balay #undef __FUNCT__
9314a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
932dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
933480ef9eaSBarry Smith {
934dfbe8321SBarry Smith   PetscErrorCode ierr;
935b0a32e0cSBarry Smith   PetscDraw      draw;
93636db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
937ace3abfcSBarry Smith   PetscBool      isnull;
938480ef9eaSBarry Smith 
939480ef9eaSBarry Smith   PetscFunctionBegin;
940b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
941b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
942480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
943480ef9eaSBarry Smith 
944480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
945d0f46423SBarry Smith   xr   = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
946480ef9eaSBarry Smith   xr  += w;    yr += h;  xl = -w;     yl = -h;
947b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
948b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
9490298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
9503a40ed3dSBarry Smith   PetscFunctionReturn(0);
951416022c9SBarry Smith }
952416022c9SBarry Smith 
9534a2ae208SSatish Balay #undef __FUNCT__
9544a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
955dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
956416022c9SBarry Smith {
957dfbe8321SBarry Smith   PetscErrorCode ierr;
958ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
959416022c9SBarry Smith 
9603a40ed3dSBarry Smith   PetscFunctionBegin;
961251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
962251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
963251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
964c45a1595SBarry Smith   if (iascii) {
9653a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
9660f5bd95cSBarry Smith   } else if (isbinary) {
9673a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
9680f5bd95cSBarry Smith   } else if (isdraw) {
9693a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
97011aeaf0aSBarry Smith   }
9714108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
9723a40ed3dSBarry Smith   PetscFunctionReturn(0);
97317ab2063SBarry Smith }
97419bcc07fSBarry Smith 
9754a2ae208SSatish Balay #undef __FUNCT__
9764a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
977dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
97817ab2063SBarry Smith {
979416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9806849ba73SBarry Smith   PetscErrorCode ierr;
98197f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
982d0f46423SBarry Smith   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
98354f21887SBarry Smith   MatScalar      *aa    = a->a,*ap;
9843447b6efSHong Zhang   PetscReal      ratio  = 0.6;
98517ab2063SBarry Smith 
9863a40ed3dSBarry Smith   PetscFunctionBegin;
9873a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
98817ab2063SBarry Smith 
98943ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
99017ab2063SBarry Smith   for (i=1; i<m; i++) {
991416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
99217ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
99394a9d846SBarry Smith     rmax    = PetscMax(rmax,ailen[i]);
99417ab2063SBarry Smith     if (fshift) {
995bfeeae90SHong Zhang       ip = aj + ai[i];
996bfeeae90SHong Zhang       ap = aa + ai[i];
99717ab2063SBarry Smith       N  = ailen[i];
99817ab2063SBarry Smith       for (j=0; j<N; j++) {
99917ab2063SBarry Smith         ip[j-fshift] = ip[j];
100017ab2063SBarry Smith         ap[j-fshift] = ap[j];
100117ab2063SBarry Smith       }
100217ab2063SBarry Smith     }
100317ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
100417ab2063SBarry Smith   }
100517ab2063SBarry Smith   if (m) {
100617ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
100717ab2063SBarry Smith     ai[m]   = ai[m-1] + ailen[m-1];
100817ab2063SBarry Smith   }
10097b083b7cSBarry Smith 
101017ab2063SBarry Smith   /* reset ilen and imax for each row */
10117b083b7cSBarry Smith   a->nonzerorowcnt = 0;
101217ab2063SBarry Smith   for (i=0; i<m; i++) {
101317ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
10147b083b7cSBarry Smith     a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
101517ab2063SBarry Smith   }
1016bfeeae90SHong Zhang   a->nz = ai[m];
101765e19b50SBarry 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);
101817ab2063SBarry Smith 
101909f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1020d0f46423SBarry 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);
1021ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
1022ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
10232205254eSKarl Rupp 
10248e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
1025dd5f02e7SSatish Balay   a->reallocs         = 0;
10266712e2f1SBarry Smith   A->info.nz_unneeded = (PetscReal)fshift;
102736db0b34SBarry Smith   a->rmax             = rmax;
10284e220ebcSLois Curfman McInnes 
102911e456e1SBarry Smith   ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
10304108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
1031acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10323a40ed3dSBarry Smith   PetscFunctionReturn(0);
103317ab2063SBarry Smith }
103417ab2063SBarry Smith 
10354a2ae208SSatish Balay #undef __FUNCT__
103699cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
103799cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
103899cafbc1SBarry Smith {
103999cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
104099cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
104154f21887SBarry Smith   MatScalar      *aa = a->a;
1042acf2f550SJed Brown   PetscErrorCode ierr;
104399cafbc1SBarry Smith 
104499cafbc1SBarry Smith   PetscFunctionBegin;
104599cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1046acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
104799cafbc1SBarry Smith   PetscFunctionReturn(0);
104899cafbc1SBarry Smith }
104999cafbc1SBarry Smith 
105099cafbc1SBarry Smith #undef __FUNCT__
105199cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
105299cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
105399cafbc1SBarry Smith {
105499cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
105599cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
105654f21887SBarry Smith   MatScalar      *aa = a->a;
1057acf2f550SJed Brown   PetscErrorCode ierr;
105899cafbc1SBarry Smith 
105999cafbc1SBarry Smith   PetscFunctionBegin;
106099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1061acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
106299cafbc1SBarry Smith   PetscFunctionReturn(0);
106399cafbc1SBarry Smith }
106499cafbc1SBarry Smith 
106578b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
106678b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ_Kernel(PetscInt thread_id,Mat A)
106778b84d54SShri Abhyankar {
106878b84d54SShri Abhyankar   PetscErrorCode ierr;
106978b84d54SShri Abhyankar   PetscInt       *trstarts=A->rmap->trstarts;
107078b84d54SShri Abhyankar   PetscInt       n,start,end;
107178b84d54SShri Abhyankar   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
107278b84d54SShri Abhyankar 
107378b84d54SShri Abhyankar   start = trstarts[thread_id];
107478b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
107519baf141SJed Brown   n     = a->i[end] - a->i[start];
107619baf141SJed Brown   ierr  = PetscMemzero(a->a+a->i[start],n*sizeof(PetscScalar));CHKERRQ(ierr);
107778b84d54SShri Abhyankar   return 0;
107878b84d54SShri Abhyankar }
107978b84d54SShri Abhyankar 
108078b84d54SShri Abhyankar #undef __FUNCT__
108178b84d54SShri Abhyankar #define __FUNCT__ "MatZeroEntries_SeqAIJ"
108278b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
108378b84d54SShri Abhyankar {
108478b84d54SShri Abhyankar   PetscErrorCode ierr;
108578b84d54SShri Abhyankar 
108678b84d54SShri Abhyankar   PetscFunctionBegin;
1087ce94432eSBarry Smith   ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatZeroEntries_SeqAIJ_Kernel,1,A);CHKERRQ(ierr);
1088acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
108978b84d54SShri Abhyankar   PetscFunctionReturn(0);
109078b84d54SShri Abhyankar }
109178b84d54SShri Abhyankar #else
109299cafbc1SBarry Smith #undef __FUNCT__
10934a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
1094dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
109517ab2063SBarry Smith {
1096416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1097dfbe8321SBarry Smith   PetscErrorCode ierr;
10983a40ed3dSBarry Smith 
10993a40ed3dSBarry Smith   PetscFunctionBegin;
1100d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
1101acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
11023a40ed3dSBarry Smith   PetscFunctionReturn(0);
110317ab2063SBarry Smith }
110478b84d54SShri Abhyankar #endif
1105416022c9SBarry Smith 
11064a2ae208SSatish Balay #undef __FUNCT__
11074a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
1108dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
110917ab2063SBarry Smith {
1110416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1111dfbe8321SBarry Smith   PetscErrorCode ierr;
1112d5d45c9bSBarry Smith 
11133a40ed3dSBarry Smith   PetscFunctionBegin;
1114aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1115d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
111617ab2063SBarry Smith #endif
1117e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
11186bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
11196bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
112005b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
1121d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
112205b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
112371f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
112405b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
11256bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
112605b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
11276bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
1128cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
11290b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
1130a30b2313SHong Zhang 
11314108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
1132bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
1133901853e0SKris Buschelman 
1134dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
1135bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
1136bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1137bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1138bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr);
1139bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr);
1140bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr);
1141af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
1142af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);CHKERRQ(ierr);
1143af8000cdSHong Zhang #endif
1144b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);CHKERRQ(ierr);
1145bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1146bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1147bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1148bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr);
11493a40ed3dSBarry Smith   PetscFunctionReturn(0);
115017ab2063SBarry Smith }
115117ab2063SBarry Smith 
11524a2ae208SSatish Balay #undef __FUNCT__
11534a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
1154ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
115517ab2063SBarry Smith {
1156416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11574846f1f5SKris Buschelman   PetscErrorCode ierr;
11583a40ed3dSBarry Smith 
11593a40ed3dSBarry Smith   PetscFunctionBegin;
1160a65d3064SKris Buschelman   switch (op) {
1161a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
11624e0d8c25SBarry Smith     a->roworiented = flg;
1163a65d3064SKris Buschelman     break;
1164a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1165a9817697SBarry Smith     a->keepnonzeropattern = flg;
1166a65d3064SKris Buschelman     break;
1167512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1168512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1169a65d3064SKris Buschelman     break;
1170a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
11714e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1172a65d3064SKris Buschelman     break;
1173a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
11744e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1175a65d3064SKris Buschelman     break;
117628b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
117728b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
117828b2fa4aSMatthew Knepley     break;
1179a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11804e0d8c25SBarry Smith     a->ignorezeroentries = flg;
11810df259c2SBarry Smith     break;
11823d472b54SHong Zhang   case MAT_SPD:
1183b1646e73SJed Brown   case MAT_SYMMETRIC:
1184b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1185b1646e73SJed Brown   case MAT_HERMITIAN:
1186b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
11875021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
11885021d80fSJed Brown     break;
11894e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1190a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1191a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1192290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1193a65d3064SKris Buschelman     break;
1194b87ac2d8SJed Brown   case MAT_USE_INODES:
1195b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1196b87ac2d8SJed Brown     break;
1197a65d3064SKris Buschelman   default:
1198e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1199a65d3064SKris Buschelman   }
12004108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
12013a40ed3dSBarry Smith   PetscFunctionReturn(0);
120217ab2063SBarry Smith }
120317ab2063SBarry Smith 
12044a2ae208SSatish Balay #undef __FUNCT__
12054a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1206dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
120717ab2063SBarry Smith {
1208416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12096849ba73SBarry Smith   PetscErrorCode ierr;
1210d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
121135e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
121217ab2063SBarry Smith 
12133a40ed3dSBarry Smith   PetscFunctionBegin;
1214d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1215e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
121635e7444dSHong Zhang 
1217d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1218d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
121935e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
12202c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
122135e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
122235e7444dSHong Zhang     PetscFunctionReturn(0);
122335e7444dSHong Zhang   }
122435e7444dSHong Zhang 
12252dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
12261ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
122735e7444dSHong Zhang   for (i=0; i<n; i++) {
122835e7444dSHong Zhang     nz = ai[i+1] - ai[i];
12292f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
123035e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++) {
123135e7444dSHong Zhang       if (aj[j] == i) {
123235e7444dSHong Zhang         x[i] = aa[j];
123317ab2063SBarry Smith         break;
123417ab2063SBarry Smith       }
123517ab2063SBarry Smith     }
123617ab2063SBarry Smith   }
12371ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
12383a40ed3dSBarry Smith   PetscFunctionReturn(0);
123917ab2063SBarry Smith }
124017ab2063SBarry Smith 
1241c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
12424a2ae208SSatish Balay #undef __FUNCT__
12434a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1244dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
124517ab2063SBarry Smith {
1246416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1247d9ca1df4SBarry Smith   PetscScalar       *y;
1248d9ca1df4SBarry Smith   const PetscScalar *x;
1249dfbe8321SBarry Smith   PetscErrorCode    ierr;
1250d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
12515c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1252d9ca1df4SBarry Smith   const MatScalar   *v;
1253a77337e4SBarry Smith   PetscScalar       alpha;
1254d9ca1df4SBarry Smith   PetscInt          n,i,j;
1255d9ca1df4SBarry Smith   const PetscInt    *idx,*ii,*ridx=NULL;
12563447b6efSHong Zhang   Mat_CompressedRow cprow    = a->compressedrow;
1257ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
12585c897100SBarry Smith #endif
125917ab2063SBarry Smith 
12603a40ed3dSBarry Smith   PetscFunctionBegin;
12612e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
1262d9ca1df4SBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12631ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
12645c897100SBarry Smith 
12655c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1266bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
12675c897100SBarry Smith #else
12683447b6efSHong Zhang   if (usecprow) {
12693447b6efSHong Zhang     m    = cprow.nrows;
12703447b6efSHong Zhang     ii   = cprow.i;
12717b2bb3b9SHong Zhang     ridx = cprow.rindex;
12723447b6efSHong Zhang   } else {
12733447b6efSHong Zhang     ii = a->i;
12743447b6efSHong Zhang   }
127517ab2063SBarry Smith   for (i=0; i<m; i++) {
12763447b6efSHong Zhang     idx = a->j + ii[i];
12773447b6efSHong Zhang     v   = a->a + ii[i];
12783447b6efSHong Zhang     n   = ii[i+1] - ii[i];
12793447b6efSHong Zhang     if (usecprow) {
12807b2bb3b9SHong Zhang       alpha = x[ridx[i]];
12813447b6efSHong Zhang     } else {
128217ab2063SBarry Smith       alpha = x[i];
12833447b6efSHong Zhang     }
128404fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
128517ab2063SBarry Smith   }
12865c897100SBarry Smith #endif
1287dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1288d9ca1df4SBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12891ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12903a40ed3dSBarry Smith   PetscFunctionReturn(0);
129117ab2063SBarry Smith }
129217ab2063SBarry Smith 
12934a2ae208SSatish Balay #undef __FUNCT__
12945c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1295dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
12965c897100SBarry Smith {
1297dfbe8321SBarry Smith   PetscErrorCode ierr;
12985c897100SBarry Smith 
12995c897100SBarry Smith   PetscFunctionBegin;
1300170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
13015c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
13025c897100SBarry Smith   PetscFunctionReturn(0);
13035c897100SBarry Smith }
13045c897100SBarry Smith 
1305c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
130678b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
130778b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ_Kernel(PetscInt thread_id,Mat A,Vec xx,Vec yy)
130878b84d54SShri Abhyankar {
130978b84d54SShri Abhyankar   PetscErrorCode    ierr;
131078b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
131178b84d54SShri Abhyankar   PetscScalar       *y;
131278b84d54SShri Abhyankar   const PetscScalar *x;
131378b84d54SShri Abhyankar   const MatScalar   *aa;
131478b84d54SShri Abhyankar   PetscInt          *trstarts=A->rmap->trstarts;
131578b84d54SShri Abhyankar   PetscInt          n,start,end,i;
131678b84d54SShri Abhyankar   const PetscInt    *aj,*ai;
131778b84d54SShri Abhyankar   PetscScalar       sum;
131878b84d54SShri Abhyankar 
131978b84d54SShri Abhyankar   ierr  = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
132078b84d54SShri Abhyankar   ierr  = VecGetArray(yy,&y);CHKERRQ(ierr);
132178b84d54SShri Abhyankar   start = trstarts[thread_id];
132278b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
132378b84d54SShri Abhyankar   aj    = a->j;
132478b84d54SShri Abhyankar   aa    = a->a;
132578b84d54SShri Abhyankar   ai    = a->i;
132678b84d54SShri Abhyankar   for (i=start; i<end; i++) {
132778b84d54SShri Abhyankar     n   = ai[i+1] - ai[i];
132878b84d54SShri Abhyankar     aj  = a->j + ai[i];
132978b84d54SShri Abhyankar     aa  = a->a + ai[i];
133078b84d54SShri Abhyankar     sum = 0.0;
133178b84d54SShri Abhyankar     PetscSparseDensePlusDot(sum,x,aa,aj,n);
133278b84d54SShri Abhyankar     y[i] = sum;
133378b84d54SShri Abhyankar   }
133478b84d54SShri Abhyankar   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
133578b84d54SShri Abhyankar   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
133678b84d54SShri Abhyankar   return 0;
133778b84d54SShri Abhyankar }
133878b84d54SShri Abhyankar 
133978b84d54SShri Abhyankar #undef __FUNCT__
134078b84d54SShri Abhyankar #define __FUNCT__ "MatMult_SeqAIJ"
134178b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
134278b84d54SShri Abhyankar {
134378b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
134478b84d54SShri Abhyankar   PetscScalar       *y;
134578b84d54SShri Abhyankar   const PetscScalar *x;
134678b84d54SShri Abhyankar   const MatScalar   *aa;
134778b84d54SShri Abhyankar   PetscErrorCode    ierr;
134878b84d54SShri Abhyankar   PetscInt          m=A->rmap->n;
13490298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
13507b083b7cSBarry Smith   PetscInt          n,i;
135178b84d54SShri Abhyankar   PetscScalar       sum;
135278b84d54SShri Abhyankar   PetscBool         usecprow=a->compressedrow.use;
135378b84d54SShri Abhyankar 
135478b84d54SShri Abhyankar #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
135578b84d54SShri Abhyankar #pragma disjoint(*x,*y,*aa)
135678b84d54SShri Abhyankar #endif
135778b84d54SShri Abhyankar 
135878b84d54SShri Abhyankar   PetscFunctionBegin;
135978b84d54SShri Abhyankar   aj = a->j;
136078b84d54SShri Abhyankar   aa = a->a;
136178b84d54SShri Abhyankar   ii = a->i;
136278b84d54SShri Abhyankar   if (usecprow) { /* use compressed row format */
136378b84d54SShri Abhyankar     ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
136478b84d54SShri Abhyankar     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
13654f390cb1SBarry Smith     ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr);
136678b84d54SShri Abhyankar     m    = a->compressedrow.nrows;
136778b84d54SShri Abhyankar     ii   = a->compressedrow.i;
136878b84d54SShri Abhyankar     ridx = a->compressedrow.rindex;
136978b84d54SShri Abhyankar     for (i=0; i<m; i++) {
137078b84d54SShri Abhyankar       n           = ii[i+1] - ii[i];
137178b84d54SShri Abhyankar       aj          = a->j + ii[i];
137278b84d54SShri Abhyankar       aa          = a->a + ii[i];
137378b84d54SShri Abhyankar       sum         = 0.0;
137478b84d54SShri Abhyankar       PetscSparseDensePlusDot(sum,x,aa,aj,n);
137578b84d54SShri Abhyankar       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
137678b84d54SShri Abhyankar       y[*ridx++] = sum;
137778b84d54SShri Abhyankar     }
137878b84d54SShri Abhyankar     ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
137978b84d54SShri Abhyankar     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
138078b84d54SShri Abhyankar   } else { /* do not use compressed row format */
138178b84d54SShri Abhyankar #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
138278b84d54SShri Abhyankar     fortranmultaij_(&m,x,ii,aj,aa,y);
138378b84d54SShri Abhyankar #else
1384ce94432eSBarry Smith     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
138578b84d54SShri Abhyankar #endif
138678b84d54SShri Abhyankar   }
13877b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
138878b84d54SShri Abhyankar   PetscFunctionReturn(0);
138978b84d54SShri Abhyankar }
139078b84d54SShri Abhyankar #else
13915c897100SBarry Smith #undef __FUNCT__
13924a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1393dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
139417ab2063SBarry Smith {
1395416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1396d9fead3dSBarry Smith   PetscScalar       *y;
139754f21887SBarry Smith   const PetscScalar *x;
139854f21887SBarry Smith   const MatScalar   *aa;
1399dfbe8321SBarry Smith   PetscErrorCode    ierr;
1400003131ecSBarry Smith   PetscInt          m=A->rmap->n;
14010298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
14027b083b7cSBarry Smith   PetscInt          n,i;
1403362ced78SSatish Balay   PetscScalar       sum;
1404ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
140517ab2063SBarry Smith 
1406b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
140797952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1408fee21e36SBarry Smith #endif
1409fee21e36SBarry Smith 
14103a40ed3dSBarry Smith   PetscFunctionBegin;
14113649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
14121ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
141397952fefSHong Zhang   aj   = a->j;
141497952fefSHong Zhang   aa   = a->a;
1415416022c9SBarry Smith   ii   = a->i;
14164eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
14174f390cb1SBarry Smith     ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr);
141897952fefSHong Zhang     m    = a->compressedrow.nrows;
141997952fefSHong Zhang     ii   = a->compressedrow.i;
142097952fefSHong Zhang     ridx = a->compressedrow.rindex;
142197952fefSHong Zhang     for (i=0; i<m; i++) {
142297952fefSHong Zhang       n           = ii[i+1] - ii[i];
142397952fefSHong Zhang       aj          = a->j + ii[i];
142497952fefSHong Zhang       aa          = a->a + ii[i];
142597952fefSHong Zhang       sum         = 0.0;
1426003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1427003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
142897952fefSHong Zhang       y[*ridx++] = sum;
142997952fefSHong Zhang     }
143097952fefSHong Zhang   } else { /* do not use compressed row format */
1431b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1432b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1433b05257ddSBarry Smith #else
143478b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
1435ce94432eSBarry Smith     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
143678b84d54SShri Abhyankar #else
143717ab2063SBarry Smith     for (i=0; i<m; i++) {
1438003131ecSBarry Smith       n           = ii[i+1] - ii[i];
1439003131ecSBarry Smith       aj          = a->j + ii[i];
1440003131ecSBarry Smith       aa          = a->a + ii[i];
144117ab2063SBarry Smith       sum         = 0.0;
1442003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
144317ab2063SBarry Smith       y[i] = sum;
144417ab2063SBarry Smith     }
14458d195f9aSBarry Smith #endif
144678b84d54SShri Abhyankar #endif
1447b05257ddSBarry Smith   }
14487b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
14493649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
14501ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
14513a40ed3dSBarry Smith   PetscFunctionReturn(0);
145217ab2063SBarry Smith }
145378b84d54SShri Abhyankar #endif
145417ab2063SBarry Smith 
1455b434eb95SMatthew G. Knepley #undef __FUNCT__
1456b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultMax_SeqAIJ"
1457b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1458b434eb95SMatthew G. Knepley {
1459b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1460b434eb95SMatthew G. Knepley   PetscScalar       *y;
1461b434eb95SMatthew G. Knepley   const PetscScalar *x;
1462b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1463b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1464b434eb95SMatthew G. Knepley   PetscInt          m=A->rmap->n;
1465b434eb95SMatthew G. Knepley   const PetscInt    *aj,*ii,*ridx=NULL;
1466b434eb95SMatthew G. Knepley   PetscInt          n,i,nonzerorow=0;
1467b434eb95SMatthew G. Knepley   PetscScalar       sum;
1468b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1469b434eb95SMatthew G. Knepley 
1470b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1471b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa)
1472b434eb95SMatthew G. Knepley #endif
1473b434eb95SMatthew G. Knepley 
1474b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1475b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1476b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1477b434eb95SMatthew G. Knepley   aj   = a->j;
1478b434eb95SMatthew G. Knepley   aa   = a->a;
1479b434eb95SMatthew G. Knepley   ii   = a->i;
1480b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1481b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1482b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1483b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1484b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1485b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1486b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1487b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1488b434eb95SMatthew G. Knepley       sum         = 0.0;
1489b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1490b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1491b434eb95SMatthew G. Knepley       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1492b434eb95SMatthew G. Knepley       y[*ridx++] = sum;
1493b434eb95SMatthew G. Knepley     }
1494b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
1495b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1496b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1497b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1498b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1499b434eb95SMatthew G. Knepley       sum         = 0.0;
1500b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1501b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1502b434eb95SMatthew G. Knepley       y[i] = sum;
1503b434eb95SMatthew G. Knepley     }
1504b434eb95SMatthew G. Knepley   }
1505b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1506b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1507b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1508b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1509b434eb95SMatthew G. Knepley }
1510b434eb95SMatthew G. Knepley 
1511b434eb95SMatthew G. Knepley #undef __FUNCT__
1512b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultAddMax_SeqAIJ"
1513b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1514b434eb95SMatthew G. Knepley {
1515b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1516b434eb95SMatthew G. Knepley   PetscScalar       *y,*z;
1517b434eb95SMatthew G. Knepley   const PetscScalar *x;
1518b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1519b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1520b434eb95SMatthew G. Knepley   PetscInt          m = A->rmap->n,*aj,*ii;
1521b434eb95SMatthew G. Knepley   PetscInt          n,i,*ridx=NULL;
1522b434eb95SMatthew G. Knepley   PetscScalar       sum;
1523b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1524b434eb95SMatthew G. Knepley 
1525b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1526b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1527d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1528b434eb95SMatthew G. Knepley 
1529b434eb95SMatthew G. Knepley   aj = a->j;
1530b434eb95SMatthew G. Knepley   aa = a->a;
1531b434eb95SMatthew G. Knepley   ii = a->i;
1532b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1533b434eb95SMatthew G. Knepley     if (zz != yy) {
1534b434eb95SMatthew G. Knepley       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1535b434eb95SMatthew G. Knepley     }
1536b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1537b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1538b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1539b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1540b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1541b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1542b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1543b434eb95SMatthew G. Knepley       sum = y[*ridx];
1544b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1545b434eb95SMatthew G. Knepley       z[*ridx++] = sum;
1546b434eb95SMatthew G. Knepley     }
1547b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
1548b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1549b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1550b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1551b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1552b434eb95SMatthew G. Knepley       sum = y[i];
1553b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1554b434eb95SMatthew G. Knepley       z[i] = sum;
1555b434eb95SMatthew G. Knepley     }
1556b434eb95SMatthew G. Knepley   }
1557b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1558b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1559d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1560b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1561b434eb95SMatthew G. Knepley }
1562b434eb95SMatthew G. Knepley 
1563c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
15644a2ae208SSatish Balay #undef __FUNCT__
15654a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1566dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
156717ab2063SBarry Smith {
1568416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1569f15663dcSBarry Smith   PetscScalar       *y,*z;
1570f15663dcSBarry Smith   const PetscScalar *x;
157154f21887SBarry Smith   const MatScalar   *aa;
1572dfbe8321SBarry Smith   PetscErrorCode    ierr;
1573d9ca1df4SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
1574d9ca1df4SBarry Smith   PetscInt          m = A->rmap->n,n,i;
1575362ced78SSatish Balay   PetscScalar       sum;
1576ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
15779ea0dfa2SSatish Balay 
15783a40ed3dSBarry Smith   PetscFunctionBegin;
1579f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1580d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1581bfeeae90SHong Zhang 
158297952fefSHong Zhang   aj = a->j;
158397952fefSHong Zhang   aa = a->a;
1584cddf8d76SBarry Smith   ii = a->i;
15854eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
15864eb6d288SHong Zhang     if (zz != yy) {
15874eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
15884eb6d288SHong Zhang     }
158997952fefSHong Zhang     m    = a->compressedrow.nrows;
159097952fefSHong Zhang     ii   = a->compressedrow.i;
159197952fefSHong Zhang     ridx = a->compressedrow.rindex;
159297952fefSHong Zhang     for (i=0; i<m; i++) {
159397952fefSHong Zhang       n   = ii[i+1] - ii[i];
159497952fefSHong Zhang       aj  = a->j + ii[i];
159597952fefSHong Zhang       aa  = a->a + ii[i];
159697952fefSHong Zhang       sum = y[*ridx];
1597f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
159897952fefSHong Zhang       z[*ridx++] = sum;
159997952fefSHong Zhang     }
160097952fefSHong Zhang   } else { /* do not use compressed row format */
1601f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1602f15663dcSBarry Smith     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1603f15663dcSBarry Smith #else
160417ab2063SBarry Smith     for (i=0; i<m; i++) {
1605f15663dcSBarry Smith       n   = ii[i+1] - ii[i];
1606f15663dcSBarry Smith       aj  = a->j + ii[i];
1607f15663dcSBarry Smith       aa  = a->a + ii[i];
160817ab2063SBarry Smith       sum = y[i];
1609f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
161017ab2063SBarry Smith       z[i] = sum;
161117ab2063SBarry Smith     }
161202ab625aSSatish Balay #endif
1613f15663dcSBarry Smith   }
1614dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1615f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1616d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
16178154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
16186b375ea7SVictor Minden   /*
1619918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1620918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1621918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
16226b375ea7SVictor Minden   */
1623918e98c3SVictor Minden #endif
16243a40ed3dSBarry Smith   PetscFunctionReturn(0);
162517ab2063SBarry Smith }
162617ab2063SBarry Smith 
162717ab2063SBarry Smith /*
162817ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
162917ab2063SBarry Smith */
16304a2ae208SSatish Balay #undef __FUNCT__
16314a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1632dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
163317ab2063SBarry Smith {
1634416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
16356849ba73SBarry Smith   PetscErrorCode ierr;
1636d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
163717ab2063SBarry Smith 
16383a40ed3dSBarry Smith   PetscFunctionBegin;
163909f38230SBarry Smith   if (!a->diag) {
1640785e854fSJed Brown     ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr);
16413bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr);
164209f38230SBarry Smith   }
1643d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
164409f38230SBarry Smith     a->diag[i] = a->i[i+1];
1645bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1646bfeeae90SHong Zhang       if (a->j[j] == i) {
164709f38230SBarry Smith         a->diag[i] = j;
164817ab2063SBarry Smith         break;
164917ab2063SBarry Smith       }
165017ab2063SBarry Smith     }
165117ab2063SBarry Smith   }
16523a40ed3dSBarry Smith   PetscFunctionReturn(0);
165317ab2063SBarry Smith }
165417ab2063SBarry Smith 
1655be5855fcSBarry Smith /*
1656be5855fcSBarry Smith      Checks for missing diagonals
1657be5855fcSBarry Smith */
16584a2ae208SSatish Balay #undef __FUNCT__
16594a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1660ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1661be5855fcSBarry Smith {
1662be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
16637734d3b5SMatthew G. Knepley   PetscInt   *diag,*ii = a->i,i;
1664be5855fcSBarry Smith 
1665be5855fcSBarry Smith   PetscFunctionBegin;
166609f38230SBarry Smith   *missing = PETSC_FALSE;
16677734d3b5SMatthew G. Knepley   if (A->rmap->n > 0 && !ii) {
166809f38230SBarry Smith     *missing = PETSC_TRUE;
166909f38230SBarry Smith     if (d) *d = 0;
1670955c1f14SBarry Smith     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
167109f38230SBarry Smith   } else {
1672f1e2ffcdSBarry Smith     diag = a->diag;
1673d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
16747734d3b5SMatthew G. Knepley       if (diag[i] >= ii[i+1]) {
167509f38230SBarry Smith         *missing = PETSC_TRUE;
167609f38230SBarry Smith         if (d) *d = i;
1677955c1f14SBarry Smith         PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
1678358d2f5dSShri Abhyankar         break;
167909f38230SBarry Smith       }
1680be5855fcSBarry Smith     }
1681be5855fcSBarry Smith   }
1682be5855fcSBarry Smith   PetscFunctionReturn(0);
1683be5855fcSBarry Smith }
1684be5855fcSBarry Smith 
168571f1c65dSBarry Smith #undef __FUNCT__
168671f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
16877087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
168871f1c65dSBarry Smith {
168971f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
169071f1c65dSBarry Smith   PetscErrorCode ierr;
1691d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
169254f21887SBarry Smith   MatScalar      *v = a->a;
169354f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
169471f1c65dSBarry Smith 
169571f1c65dSBarry Smith   PetscFunctionBegin;
169671f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
169771f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
169871f1c65dSBarry Smith   diag = a->diag;
169971f1c65dSBarry Smith   if (!a->idiag) {
1700dcca6d9dSJed Brown     ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr);
17013bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
170271f1c65dSBarry Smith     v    = a->a;
170371f1c65dSBarry Smith   }
170471f1c65dSBarry Smith   mdiag = a->mdiag;
170571f1c65dSBarry Smith   idiag = a->idiag;
170671f1c65dSBarry Smith 
1707028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
170871f1c65dSBarry Smith     for (i=0; i<m; i++) {
170971f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1710e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
171171f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
171271f1c65dSBarry Smith     }
171371f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
171471f1c65dSBarry Smith   } else {
171571f1c65dSBarry Smith     for (i=0; i<m; i++) {
171671f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
171771f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
171871f1c65dSBarry Smith     }
1719dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
172071f1c65dSBarry Smith   }
172171f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
172271f1c65dSBarry Smith   PetscFunctionReturn(0);
172371f1c65dSBarry Smith }
172471f1c65dSBarry Smith 
1725c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
17264a2ae208SSatish Balay #undef __FUNCT__
172741f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
172841f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
172917ab2063SBarry Smith {
1730416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1731e6d1f457SBarry Smith   PetscScalar       *x,d,sum,*t,scale;
1732e6d1f457SBarry Smith   const MatScalar   *v = a->a,*idiag=0,*mdiag;
173354f21887SBarry Smith   const PetscScalar *b, *bs,*xb, *ts;
1734dfbe8321SBarry Smith   PetscErrorCode    ierr;
1735d0f46423SBarry Smith   PetscInt          n = A->cmap->n,m = A->rmap->n,i;
173697f1f81fSBarry Smith   const PetscInt    *idx,*diag;
173717ab2063SBarry Smith 
17383a40ed3dSBarry Smith   PetscFunctionBegin;
1739b965ef7fSBarry Smith   its = its*lits;
174091723122SBarry Smith 
174171f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
174271f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
174371f1c65dSBarry Smith   a->fshift = fshift;
174471f1c65dSBarry Smith   a->omega  = omega;
1745ed480e8bSBarry Smith 
174671f1c65dSBarry Smith   diag  = a->diag;
174771f1c65dSBarry Smith   t     = a->ssor_work;
1748ed480e8bSBarry Smith   idiag = a->idiag;
174971f1c65dSBarry Smith   mdiag = a->mdiag;
1750ed480e8bSBarry Smith 
17511ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
17523649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1753ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
175417ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
175517ab2063SBarry Smith     /* apply (U + D/omega) to the vector */
1756ed480e8bSBarry Smith     bs = b;
175717ab2063SBarry Smith     for (i=0; i<m; i++) {
175871f1c65dSBarry Smith       d   = fshift + mdiag[i];
1759416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1760ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1761ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
176217ab2063SBarry Smith       sum = b[i]*d/omega;
1763003131ecSBarry Smith       PetscSparseDensePlusDot(sum,bs,v,idx,n);
176417ab2063SBarry Smith       x[i] = sum;
176517ab2063SBarry Smith     }
17661ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
17673649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1768efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17693a40ed3dSBarry Smith     PetscFunctionReturn(0);
177017ab2063SBarry Smith   }
1771c783ea89SBarry Smith 
17722205254eSKarl Rupp   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
17732205254eSKarl Rupp   else if (flag & SOR_EISENSTAT) {
177417ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1775887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
177617ab2063SBarry Smith 
177717ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
177817ab2063SBarry Smith 
1779887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
178017ab2063SBarry Smith     */
178117ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
178217ab2063SBarry Smith 
178317ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
178417ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1785416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1786ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1787ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
178817ab2063SBarry Smith       sum = b[i];
1789e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1790ed480e8bSBarry Smith       x[i] = sum*idiag[i];
179117ab2063SBarry Smith     }
179217ab2063SBarry Smith 
179317ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1794416022c9SBarry Smith     v = a->a;
17952205254eSKarl Rupp     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
179617ab2063SBarry Smith 
179717ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1798ed480e8bSBarry Smith     ts   = t;
1799416022c9SBarry Smith     diag = a->diag;
180017ab2063SBarry Smith     for (i=0; i<m; i++) {
1801416022c9SBarry Smith       n   = diag[i] - a->i[i];
1802ed480e8bSBarry Smith       idx = a->j + a->i[i];
1803ed480e8bSBarry Smith       v   = a->a + a->i[i];
180417ab2063SBarry Smith       sum = t[i];
1805003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1806ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1807733d66baSBarry Smith       /*  x = x + t */
1808733d66baSBarry Smith       x[i] += t[i];
180917ab2063SBarry Smith     }
181017ab2063SBarry Smith 
1811dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
18121ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
18133649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
18143a40ed3dSBarry Smith     PetscFunctionReturn(0);
181517ab2063SBarry Smith   }
181617ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
181717ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
181817ab2063SBarry Smith       for (i=0; i<m; i++) {
1819416022c9SBarry Smith         n   = diag[i] - a->i[i];
1820ed480e8bSBarry Smith         idx = a->j + a->i[i];
1821ed480e8bSBarry Smith         v   = a->a + a->i[i];
182217ab2063SBarry Smith         sum = b[i];
1823e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
18245c99c7daSBarry Smith         t[i] = sum;
1825ed480e8bSBarry Smith         x[i] = sum*idiag[i];
182617ab2063SBarry Smith       }
18275c99c7daSBarry Smith       xb   = t;
1828efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18293a40ed3dSBarry Smith     } else xb = b;
183017ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
183117ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1832416022c9SBarry Smith         n   = a->i[i+1] - diag[i] - 1;
1833ed480e8bSBarry Smith         idx = a->j + diag[i] + 1;
1834ed480e8bSBarry Smith         v   = a->a + diag[i] + 1;
183517ab2063SBarry Smith         sum = xb[i];
1836e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
18375c99c7daSBarry Smith         if (xb == b) {
1838ed480e8bSBarry Smith           x[i] = sum*idiag[i];
18395c99c7daSBarry Smith         } else {
1840b19a5dc2SMark Adams           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
184117ab2063SBarry Smith         }
18425c99c7daSBarry Smith       }
1843b19a5dc2SMark Adams       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
184417ab2063SBarry Smith     }
184517ab2063SBarry Smith     its--;
184617ab2063SBarry Smith   }
184717ab2063SBarry Smith   while (its--) {
184817ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
184917ab2063SBarry Smith       for (i=0; i<m; i++) {
1850b19a5dc2SMark Adams         /* lower */
1851b19a5dc2SMark Adams         n   = diag[i] - a->i[i];
1852ed480e8bSBarry Smith         idx = a->j + a->i[i];
1853ed480e8bSBarry Smith         v   = a->a + a->i[i];
185417ab2063SBarry Smith         sum = b[i];
1855e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1856b19a5dc2SMark Adams         t[i] = sum;             /* save application of the lower-triangular part */
1857b19a5dc2SMark Adams         /* upper */
1858b19a5dc2SMark Adams         n   = a->i[i+1] - diag[i] - 1;
1859b19a5dc2SMark Adams         idx = a->j + diag[i] + 1;
1860b19a5dc2SMark Adams         v   = a->a + diag[i] + 1;
1861b19a5dc2SMark Adams         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1862b19a5dc2SMark Adams         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
186317ab2063SBarry Smith       }
1864b19a5dc2SMark Adams       xb   = t;
18659f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1866b19a5dc2SMark Adams     } else xb = b;
186717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
186817ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1869b19a5dc2SMark Adams         sum = xb[i];
1870b19a5dc2SMark Adams         if (xb == b) {
1871b19a5dc2SMark Adams           /* whole matrix (no checkpointing available) */
1872416022c9SBarry Smith           n   = a->i[i+1] - a->i[i];
1873ed480e8bSBarry Smith           idx = a->j + a->i[i];
1874ed480e8bSBarry Smith           v   = a->a + a->i[i];
1875e6d1f457SBarry Smith           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1876ed480e8bSBarry Smith           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1877b19a5dc2SMark Adams         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1878b19a5dc2SMark Adams           n   = a->i[i+1] - diag[i] - 1;
1879b19a5dc2SMark Adams           idx = a->j + diag[i] + 1;
1880b19a5dc2SMark Adams           v   = a->a + diag[i] + 1;
1881b19a5dc2SMark Adams           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1882b19a5dc2SMark Adams           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
188317ab2063SBarry Smith         }
1884b19a5dc2SMark Adams       }
1885b19a5dc2SMark Adams       if (xb == b) {
18869f863219SBarry Smith         ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1887b19a5dc2SMark Adams       } else {
1888b19a5dc2SMark Adams         ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1889b19a5dc2SMark Adams       }
189017ab2063SBarry Smith     }
189117ab2063SBarry Smith   }
18921ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
18933649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1894365a8a9eSBarry Smith   PetscFunctionReturn(0);
189517ab2063SBarry Smith }
189617ab2063SBarry Smith 
18972af78befSBarry Smith 
18984a2ae208SSatish Balay #undef __FUNCT__
18994a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1900dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
190117ab2063SBarry Smith {
1902416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
19034e220ebcSLois Curfman McInnes 
19043a40ed3dSBarry Smith   PetscFunctionBegin;
19054e220ebcSLois Curfman McInnes   info->block_size   = 1.0;
19064e220ebcSLois Curfman McInnes   info->nz_allocated = (double)a->maxnz;
19074e220ebcSLois Curfman McInnes   info->nz_used      = (double)a->nz;
19084e220ebcSLois Curfman McInnes   info->nz_unneeded  = (double)(a->maxnz - a->nz);
19094e220ebcSLois Curfman McInnes   info->assemblies   = (double)A->num_ass;
19108e58a170SBarry Smith   info->mallocs      = (double)A->info.mallocs;
19117adad957SLisandro Dalcin   info->memory       = ((PetscObject)A)->mem;
1912d5f3da31SBarry Smith   if (A->factortype) {
19134e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
19144e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
19154e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
19164e220ebcSLois Curfman McInnes   } else {
19174e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
19184e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
19194e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
19204e220ebcSLois Curfman McInnes   }
19213a40ed3dSBarry Smith   PetscFunctionReturn(0);
192217ab2063SBarry Smith }
192317ab2063SBarry Smith 
19244a2ae208SSatish Balay #undef __FUNCT__
19254a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
19262b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
192717ab2063SBarry Smith {
1928416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
19293b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
19306849ba73SBarry Smith   PetscErrorCode    ierr;
193197b48c8fSBarry Smith   const PetscScalar *xx;
193297b48c8fSBarry Smith   PetscScalar       *bb;
1933ace3abfcSBarry Smith   PetscBool         missing;
193417ab2063SBarry Smith 
19353a40ed3dSBarry Smith   PetscFunctionBegin;
193697b48c8fSBarry Smith   if (x && b) {
193797b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
193897b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
193997b48c8fSBarry Smith     for (i=0; i<N; i++) {
194097b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
194197b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
194297b48c8fSBarry Smith     }
194397b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
194497b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
194597b48c8fSBarry Smith   }
194697b48c8fSBarry Smith 
1947a9817697SBarry Smith   if (a->keepnonzeropattern) {
1948f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1949e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1950bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1951f1e2ffcdSBarry Smith     }
1952f4df32b1SMatthew Knepley     if (diag != 0.0) {
195309f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1954e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1955f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1956f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1957f1e2ffcdSBarry Smith       }
1958f1e2ffcdSBarry Smith     }
1959f1e2ffcdSBarry Smith   } else {
1960f4df32b1SMatthew Knepley     if (diag != 0.0) {
196117ab2063SBarry Smith       for (i=0; i<N; i++) {
1962e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
19637ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1964416022c9SBarry Smith           a->ilen[rows[i]]    = 1;
1965f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1966bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
19677ae801bdSBarry Smith         } else { /* in case row was completely empty */
1968f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
196917ab2063SBarry Smith         }
197017ab2063SBarry Smith       }
19713a40ed3dSBarry Smith     } else {
197217ab2063SBarry Smith       for (i=0; i<N; i++) {
1973e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1974416022c9SBarry Smith         a->ilen[rows[i]] = 0;
197517ab2063SBarry Smith       }
197617ab2063SBarry Smith     }
1977e56f5c9eSBarry Smith     A->nonzerostate++;
1978f1e2ffcdSBarry Smith   }
197943a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19803a40ed3dSBarry Smith   PetscFunctionReturn(0);
198117ab2063SBarry Smith }
198217ab2063SBarry Smith 
19834a2ae208SSatish Balay #undef __FUNCT__
19846e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
19856e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
19866e169961SBarry Smith {
19876e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
19886e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
19896e169961SBarry Smith   PetscErrorCode    ierr;
19902b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
19916e169961SBarry Smith   const PetscScalar *xx;
19926e169961SBarry Smith   PetscScalar       *bb;
19936e169961SBarry Smith 
19946e169961SBarry Smith   PetscFunctionBegin;
19956e169961SBarry Smith   if (x && b) {
19966e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
19976e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
19982b40b63fSBarry Smith     vecs = PETSC_TRUE;
19996e169961SBarry Smith   }
20001795a4d1SJed Brown   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
20016e169961SBarry Smith   for (i=0; i<N; i++) {
20026e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
20036e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
20042205254eSKarl Rupp 
20056e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
20066e169961SBarry Smith   }
20076e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
20086e169961SBarry Smith     if (!zeroed[i]) {
20096e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
20106e169961SBarry Smith         if (zeroed[a->j[j]]) {
20112b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
20126e169961SBarry Smith           a->a[j] = 0.0;
20136e169961SBarry Smith         }
20146e169961SBarry Smith       }
20152b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
20166e169961SBarry Smith   }
20176e169961SBarry Smith   if (x && b) {
20186e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
20196e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
20206e169961SBarry Smith   }
20216e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
20226e169961SBarry Smith   if (diag != 0.0) {
20236e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
20246e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
20256e169961SBarry Smith     for (i=0; i<N; i++) {
20266e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
20276e169961SBarry Smith     }
20286e169961SBarry Smith   }
20296e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20306e169961SBarry Smith   PetscFunctionReturn(0);
20316e169961SBarry Smith }
20326e169961SBarry Smith 
20336e169961SBarry Smith #undef __FUNCT__
20344a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
2035a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
203617ab2063SBarry Smith {
2037416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
203897f1f81fSBarry Smith   PetscInt   *itmp;
203917ab2063SBarry Smith 
20403a40ed3dSBarry Smith   PetscFunctionBegin;
2041e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
204217ab2063SBarry Smith 
2043416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
2044bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
204517ab2063SBarry Smith   if (idx) {
2046bfeeae90SHong Zhang     itmp = a->j + a->i[row];
204726fbe8dcSKarl Rupp     if (*nz) *idx = itmp;
204817ab2063SBarry Smith     else *idx = 0;
204917ab2063SBarry Smith   }
20503a40ed3dSBarry Smith   PetscFunctionReturn(0);
205117ab2063SBarry Smith }
205217ab2063SBarry Smith 
2053bfeeae90SHong Zhang /* remove this function? */
20544a2ae208SSatish Balay #undef __FUNCT__
20554a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
2056a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
205717ab2063SBarry Smith {
20583a40ed3dSBarry Smith   PetscFunctionBegin;
20593a40ed3dSBarry Smith   PetscFunctionReturn(0);
206017ab2063SBarry Smith }
206117ab2063SBarry Smith 
20624a2ae208SSatish Balay #undef __FUNCT__
20634a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
2064dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
206517ab2063SBarry Smith {
2066416022c9SBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
206754f21887SBarry Smith   MatScalar      *v  = a->a;
206836db0b34SBarry Smith   PetscReal      sum = 0.0;
20696849ba73SBarry Smith   PetscErrorCode ierr;
207097f1f81fSBarry Smith   PetscInt       i,j;
207117ab2063SBarry Smith 
20723a40ed3dSBarry Smith   PetscFunctionBegin;
207317ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
2074416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
207536db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
207617ab2063SBarry Smith     }
20778f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
20783a40ed3dSBarry Smith   } else if (type == NORM_1) {
207936db0b34SBarry Smith     PetscReal *tmp;
208097f1f81fSBarry Smith     PetscInt  *jj = a->j;
20811795a4d1SJed Brown     ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr);
2082064f8208SBarry Smith     *nrm = 0.0;
2083416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
2084bfeeae90SHong Zhang       tmp[*jj++] += PetscAbsScalar(*v);  v++;
208517ab2063SBarry Smith     }
2086d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
2087064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
208817ab2063SBarry Smith     }
2089606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
20903a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
2091064f8208SBarry Smith     *nrm = 0.0;
2092d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
2093bfeeae90SHong Zhang       v   = a->a + a->i[j];
209417ab2063SBarry Smith       sum = 0.0;
2095416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2096cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
209717ab2063SBarry Smith       }
2098064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
209917ab2063SBarry Smith     }
2100f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
21013a40ed3dSBarry Smith   PetscFunctionReturn(0);
210217ab2063SBarry Smith }
210317ab2063SBarry Smith 
21044e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
21054e938277SHong Zhang #undef __FUNCT__
21064e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
21074e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
21084e938277SHong Zhang {
21094e938277SHong Zhang   PetscErrorCode ierr;
21104e938277SHong Zhang   PetscInt       i,j,anzj;
21114e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
21124e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
21134e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
21144e938277SHong Zhang 
21154e938277SHong Zhang   PetscFunctionBegin;
21164e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
2117854ce69bSBarry Smith   ierr = PetscCalloc1(an+1,&ati);CHKERRQ(ierr);
2118785e854fSJed Brown   ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr);
2119785e854fSJed Brown   ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr);
21204e938277SHong Zhang 
21214e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
21224e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
212326fbe8dcSKarl Rupp   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
21244e938277SHong Zhang   /* Form ati for csr format of A^T. */
212526fbe8dcSKarl Rupp   for (i=0;i<an;i++) ati[i+1] += ati[i];
21264e938277SHong Zhang 
21274e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
21284e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
21294e938277SHong Zhang 
21304e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
21314e938277SHong Zhang   for (i=0;i<am;i++) {
21324e938277SHong Zhang     anzj = ai[i+1] - ai[i];
21334e938277SHong Zhang     for (j=0;j<anzj;j++) {
21344e938277SHong Zhang       atj[atfill[*aj]] = i;
21354e938277SHong Zhang       atfill[*aj++]   += 1;
21364e938277SHong Zhang     }
21374e938277SHong Zhang   }
21384e938277SHong Zhang 
21394e938277SHong Zhang   /* Clean up temporary space and complete requests. */
21404e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
2141ce94432eSBarry Smith   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
214233d57670SJed Brown   ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
2143a2f3521dSMark F. Adams 
21444e938277SHong Zhang   b          = (Mat_SeqAIJ*)((*B)->data);
21454e938277SHong Zhang   b->free_a  = PETSC_FALSE;
21464e938277SHong Zhang   b->free_ij = PETSC_TRUE;
21474e938277SHong Zhang   b->nonew   = 0;
21484e938277SHong Zhang   PetscFunctionReturn(0);
21494e938277SHong Zhang }
21504e938277SHong Zhang 
21514a2ae208SSatish Balay #undef __FUNCT__
21524a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
2153fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
215417ab2063SBarry Smith {
2155416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2156416022c9SBarry Smith   Mat            C;
21576849ba73SBarry Smith   PetscErrorCode ierr;
2158d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
215954f21887SBarry Smith   MatScalar      *array = a->a;
216017ab2063SBarry Smith 
21613a40ed3dSBarry Smith   PetscFunctionBegin;
2162e32f2f54SBarry 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");
2163fc4dec0aSBarry Smith 
2164fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
2165854ce69bSBarry Smith     ierr = PetscCalloc1(1+A->cmap->n,&col);CHKERRQ(ierr);
2166bfeeae90SHong Zhang 
2167bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
2168ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2169d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
217033d57670SJed Brown     ierr = MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
21717adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2172ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
2173606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
2174a541d17aSBarry Smith   } else {
2175a541d17aSBarry Smith     C = *B;
2176a541d17aSBarry Smith   }
2177a541d17aSBarry Smith 
217817ab2063SBarry Smith   for (i=0; i<m; i++) {
217917ab2063SBarry Smith     len    = ai[i+1]-ai[i];
218087d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
2181b9b97703SBarry Smith     array += len;
2182b9b97703SBarry Smith     aj    += len;
218317ab2063SBarry Smith   }
21846d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21856d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
218617ab2063SBarry Smith 
2187815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
2188416022c9SBarry Smith     *B = C;
218917ab2063SBarry Smith   } else {
2190eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
219117ab2063SBarry Smith   }
21923a40ed3dSBarry Smith   PetscFunctionReturn(0);
219317ab2063SBarry Smith }
219417ab2063SBarry Smith 
2195cd0d46ebSvictorle #undef __FUNCT__
21965fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
21977087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2198cd0d46ebSvictorle {
2199cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
220054f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
220154f21887SBarry Smith   MatScalar      *va,*vb;
22026849ba73SBarry Smith   PetscErrorCode ierr;
220397f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
2204cd0d46ebSvictorle 
2205cd0d46ebSvictorle   PetscFunctionBegin;
2206cd0d46ebSvictorle   bij = (Mat_SeqAIJ*) B->data;
2207cd0d46ebSvictorle 
2208cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2209cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
22105485867bSBarry Smith   if (ma!=nb || na!=mb) {
22115485867bSBarry Smith     *f = PETSC_FALSE;
22125485867bSBarry Smith     PetscFunctionReturn(0);
22135485867bSBarry Smith   }
2214cd0d46ebSvictorle   aii  = aij->i; bii = bij->i;
2215cd0d46ebSvictorle   adx  = aij->j; bdx = bij->j;
2216cd0d46ebSvictorle   va   = aij->a; vb = bij->a;
2217785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2218785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
2219cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
2220cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
2221cd0d46ebSvictorle 
2222cd0d46ebSvictorle   *f = PETSC_TRUE;
2223cd0d46ebSvictorle   for (i=0; i<ma; i++) {
2224cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
222597f1f81fSBarry Smith       PetscInt    idc,idr;
22265485867bSBarry Smith       PetscScalar vc,vr;
2227cd0d46ebSvictorle       /* column/row index/value */
22285485867bSBarry Smith       idc = adx[aptr[i]];
22295485867bSBarry Smith       idr = bdx[bptr[idc]];
22305485867bSBarry Smith       vc  = va[aptr[i]];
22315485867bSBarry Smith       vr  = vb[bptr[idc]];
22325485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
22335485867bSBarry Smith         *f = PETSC_FALSE;
22345485867bSBarry Smith         goto done;
2235cd0d46ebSvictorle       } else {
22365485867bSBarry Smith         aptr[i]++;
22375485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
2238cd0d46ebSvictorle       }
2239cd0d46ebSvictorle     }
2240cd0d46ebSvictorle   }
2241cd0d46ebSvictorle done:
2242cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
22433aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
2244cd0d46ebSvictorle   PetscFunctionReturn(0);
2245cd0d46ebSvictorle }
2246cd0d46ebSvictorle 
22471cbb95d3SBarry Smith #undef __FUNCT__
22481cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
22497087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
22501cbb95d3SBarry Smith {
22511cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
225254f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
225354f21887SBarry Smith   MatScalar      *va,*vb;
22541cbb95d3SBarry Smith   PetscErrorCode ierr;
22551cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
22561cbb95d3SBarry Smith 
22571cbb95d3SBarry Smith   PetscFunctionBegin;
22581cbb95d3SBarry Smith   bij = (Mat_SeqAIJ*) B->data;
22591cbb95d3SBarry Smith 
22601cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
22611cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
22621cbb95d3SBarry Smith   if (ma!=nb || na!=mb) {
22631cbb95d3SBarry Smith     *f = PETSC_FALSE;
22641cbb95d3SBarry Smith     PetscFunctionReturn(0);
22651cbb95d3SBarry Smith   }
22661cbb95d3SBarry Smith   aii  = aij->i; bii = bij->i;
22671cbb95d3SBarry Smith   adx  = aij->j; bdx = bij->j;
22681cbb95d3SBarry Smith   va   = aij->a; vb = bij->a;
2269785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2270785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
22711cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
22721cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
22731cbb95d3SBarry Smith 
22741cbb95d3SBarry Smith   *f = PETSC_TRUE;
22751cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
22761cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
22771cbb95d3SBarry Smith       PetscInt    idc,idr;
22781cbb95d3SBarry Smith       PetscScalar vc,vr;
22791cbb95d3SBarry Smith       /* column/row index/value */
22801cbb95d3SBarry Smith       idc = adx[aptr[i]];
22811cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
22821cbb95d3SBarry Smith       vc  = va[aptr[i]];
22831cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
22841cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
22851cbb95d3SBarry Smith         *f = PETSC_FALSE;
22861cbb95d3SBarry Smith         goto done;
22871cbb95d3SBarry Smith       } else {
22881cbb95d3SBarry Smith         aptr[i]++;
22891cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
22901cbb95d3SBarry Smith       }
22911cbb95d3SBarry Smith     }
22921cbb95d3SBarry Smith   }
22931cbb95d3SBarry Smith done:
22941cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
22951cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
22961cbb95d3SBarry Smith   PetscFunctionReturn(0);
22971cbb95d3SBarry Smith }
22981cbb95d3SBarry Smith 
22999e29f15eSvictorle #undef __FUNCT__
23009e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2301ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
23029e29f15eSvictorle {
2303dfbe8321SBarry Smith   PetscErrorCode ierr;
23046e111a19SKarl Rupp 
23059e29f15eSvictorle   PetscFunctionBegin;
23065485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
23079e29f15eSvictorle   PetscFunctionReturn(0);
23089e29f15eSvictorle }
23099e29f15eSvictorle 
23104a2ae208SSatish Balay #undef __FUNCT__
23111cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2312ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
23131cbb95d3SBarry Smith {
23141cbb95d3SBarry Smith   PetscErrorCode ierr;
23156e111a19SKarl Rupp 
23161cbb95d3SBarry Smith   PetscFunctionBegin;
23171cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
23181cbb95d3SBarry Smith   PetscFunctionReturn(0);
23191cbb95d3SBarry Smith }
23201cbb95d3SBarry Smith 
23211cbb95d3SBarry Smith #undef __FUNCT__
23224a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2323dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
232417ab2063SBarry Smith {
2325416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
232654f21887SBarry Smith   PetscScalar    *l,*r,x;
232754f21887SBarry Smith   MatScalar      *v;
2328dfbe8321SBarry Smith   PetscErrorCode ierr;
2329d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
233017ab2063SBarry Smith 
23313a40ed3dSBarry Smith   PetscFunctionBegin;
233217ab2063SBarry Smith   if (ll) {
23333ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
23343ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2335e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2336e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
23371ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2338416022c9SBarry Smith     v    = a->a;
233917ab2063SBarry Smith     for (i=0; i<m; i++) {
234017ab2063SBarry Smith       x = l[i];
2341416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
23422205254eSKarl Rupp       for (j=0; j<M; j++) (*v++) *= x;
234317ab2063SBarry Smith     }
23441ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2345efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
234617ab2063SBarry Smith   }
234717ab2063SBarry Smith   if (rr) {
2348e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2349e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
23501ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2351416022c9SBarry Smith     v    = a->a; jj = a->j;
23522205254eSKarl Rupp     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
23531ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2354efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
235517ab2063SBarry Smith   }
2356acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
23573a40ed3dSBarry Smith   PetscFunctionReturn(0);
235817ab2063SBarry Smith }
235917ab2063SBarry Smith 
23604a2ae208SSatish Balay #undef __FUNCT__
23614a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
236297f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
236317ab2063SBarry Smith {
2364db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
23656849ba73SBarry Smith   PetscErrorCode ierr;
2366d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
236797f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
23685d0c19d7SBarry Smith   const PetscInt *irow,*icol;
23695d0c19d7SBarry Smith   PetscInt       nrows,ncols;
237097f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
237154f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2372416022c9SBarry Smith   Mat            C;
2373cdc6f3adSToby Isaac   PetscBool      stride;
237417ab2063SBarry Smith 
23753a40ed3dSBarry Smith   PetscFunctionBegin;
237699141d43SSatish Balay 
237717ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2378b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2379b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
238017ab2063SBarry Smith 
2381251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2382ff718158SBarry Smith   if (stride) {
2383ff718158SBarry Smith     ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2384ff718158SBarry Smith   } else {
2385ff718158SBarry Smith     first = 0;
2386ff718158SBarry Smith     step  = 0;
2387ff718158SBarry Smith   }
2388fee21e36SBarry Smith   if (stride && step == 1) {
238902834360SBarry Smith     /* special case of contiguous rows */
2390dcca6d9dSJed Brown     ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr);
239102834360SBarry Smith     /* loop over new rows determining lens and starting points */
239202834360SBarry Smith     for (i=0; i<nrows; i++) {
2393bfeeae90SHong Zhang       kstart = ai[irow[i]];
2394a2744918SBarry Smith       kend   = kstart + ailen[irow[i]];
2395a91a9bebSLisandro Dalcin       starts[i] = kstart;
239602834360SBarry Smith       for (k=kstart; k<kend; k++) {
2397bfeeae90SHong Zhang         if (aj[k] >= first) {
239802834360SBarry Smith           starts[i] = k;
239902834360SBarry Smith           break;
240002834360SBarry Smith         }
240102834360SBarry Smith       }
2402a2744918SBarry Smith       sum = 0;
240302834360SBarry Smith       while (k < kend) {
2404bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2405a2744918SBarry Smith         sum++;
240602834360SBarry Smith       }
2407a2744918SBarry Smith       lens[i] = sum;
240802834360SBarry Smith     }
240902834360SBarry Smith     /* create submatrix */
2410cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
241197f1f81fSBarry Smith       PetscInt n_cols,n_rows;
241208480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2413e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2414d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
241508480c60SBarry Smith       C    = *B;
24163a40ed3dSBarry Smith     } else {
24173bef6203SJed Brown       PetscInt rbs,cbs;
2418ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2419f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
24203bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
24213bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
24223bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
24237adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2424ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
242508480c60SBarry Smith     }
2426db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2427db02288aSLois Curfman McInnes 
242802834360SBarry Smith     /* loop over rows inserting into submatrix */
2429db02288aSLois Curfman McInnes     a_new = c->a;
2430db02288aSLois Curfman McInnes     j_new = c->j;
2431db02288aSLois Curfman McInnes     i_new = c->i;
2432bfeeae90SHong Zhang 
243302834360SBarry Smith     for (i=0; i<nrows; i++) {
2434a2744918SBarry Smith       ii    = starts[i];
2435a2744918SBarry Smith       lensi = lens[i];
2436a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2437a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
243802834360SBarry Smith       }
243987828ca2SBarry Smith       ierr       = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2440a2744918SBarry Smith       a_new     += lensi;
2441a2744918SBarry Smith       i_new[i+1] = i_new[i] + lensi;
2442a2744918SBarry Smith       c->ilen[i] = lensi;
244302834360SBarry Smith     }
24440e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
24453a40ed3dSBarry Smith   } else {
244602834360SBarry Smith     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
24471795a4d1SJed Brown     ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr);
2448854ce69bSBarry Smith     ierr = PetscMalloc1(1+nrows,&lens);CHKERRQ(ierr);
24494dcab191SBarry Smith     for (i=0; i<ncols; i++) {
24504dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
24514dcab191SBarry 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);
24524dcab191SBarry Smith #endif
24534dcab191SBarry Smith       smap[icol[i]] = i+1;
24544dcab191SBarry Smith     }
24554dcab191SBarry Smith 
245602834360SBarry Smith     /* determine lens of each row */
245702834360SBarry Smith     for (i=0; i<nrows; i++) {
2458bfeeae90SHong Zhang       kstart  = ai[irow[i]];
245902834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
246002834360SBarry Smith       lens[i] = 0;
246102834360SBarry Smith       for (k=kstart; k<kend; k++) {
2462bfeeae90SHong Zhang         if (smap[aj[k]]) {
246302834360SBarry Smith           lens[i]++;
246402834360SBarry Smith         }
246502834360SBarry Smith       }
246602834360SBarry Smith     }
246717ab2063SBarry Smith     /* Create and fill new matrix */
2468a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2469ace3abfcSBarry Smith       PetscBool equal;
24700f5bd95cSBarry Smith 
247199141d43SSatish Balay       c = (Mat_SeqAIJ*)((*B)->data);
2472e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2473d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
2474f23aa3ddSBarry Smith       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2475d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
247608480c60SBarry Smith       C    = *B;
24773a40ed3dSBarry Smith     } else {
24783bef6203SJed Brown       PetscInt rbs,cbs;
2479ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2480f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
24813bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
24823bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
24833bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
24847adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2485ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
248608480c60SBarry Smith     }
248799141d43SSatish Balay     c = (Mat_SeqAIJ*)(C->data);
248817ab2063SBarry Smith     for (i=0; i<nrows; i++) {
248999141d43SSatish Balay       row      = irow[i];
2490bfeeae90SHong Zhang       kstart   = ai[row];
249199141d43SSatish Balay       kend     = kstart + a->ilen[row];
2492bfeeae90SHong Zhang       mat_i    = c->i[i];
249399141d43SSatish Balay       mat_j    = c->j + mat_i;
249499141d43SSatish Balay       mat_a    = c->a + mat_i;
249599141d43SSatish Balay       mat_ilen = c->ilen + i;
249617ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2497bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2498ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
249999141d43SSatish Balay           *mat_a++ = a->a[k];
250099141d43SSatish Balay           (*mat_ilen)++;
250199141d43SSatish Balay 
250217ab2063SBarry Smith         }
250317ab2063SBarry Smith       }
250417ab2063SBarry Smith     }
250502834360SBarry Smith     /* Free work space */
250602834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2507606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2508606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
2509cdc6f3adSToby Isaac     /* sort */
2510cdc6f3adSToby Isaac     for (i = 0; i < nrows; i++) {
2511cdc6f3adSToby Isaac       PetscInt ilen;
2512cdc6f3adSToby Isaac 
2513cdc6f3adSToby Isaac       mat_i = c->i[i];
2514cdc6f3adSToby Isaac       mat_j = c->j + mat_i;
2515cdc6f3adSToby Isaac       mat_a = c->a + mat_i;
2516cdc6f3adSToby Isaac       ilen  = c->ilen[i];
2517cdc6f3adSToby Isaac       ierr  = PetscSortIntWithMatScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr);
2518cdc6f3adSToby Isaac     }
251902834360SBarry Smith   }
25206d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
25216d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
252217ab2063SBarry Smith 
252317ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2524416022c9SBarry Smith   *B   = C;
25253a40ed3dSBarry Smith   PetscFunctionReturn(0);
252617ab2063SBarry Smith }
252717ab2063SBarry Smith 
25281df811f5SHong Zhang #undef __FUNCT__
252982d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2530fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
253182d44351SHong Zhang {
253282d44351SHong Zhang   PetscErrorCode ierr;
253382d44351SHong Zhang   Mat            B;
253482d44351SHong Zhang 
253582d44351SHong Zhang   PetscFunctionBegin;
2536c2d650bdSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
253782d44351SHong Zhang     ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
253882d44351SHong Zhang     ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
253933d57670SJed Brown     ierr    = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr);
254082d44351SHong Zhang     ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
254182d44351SHong Zhang     ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
254282d44351SHong Zhang     *subMat = B;
2543c2d650bdSHong Zhang   } else {
2544c2d650bdSHong Zhang     ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
2545c2d650bdSHong Zhang   }
254682d44351SHong Zhang   PetscFunctionReturn(0);
254782d44351SHong Zhang }
254882d44351SHong Zhang 
254982d44351SHong Zhang #undef __FUNCT__
25504a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
25510481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2552a871dcd8SBarry Smith {
255363b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2554dfbe8321SBarry Smith   PetscErrorCode ierr;
255563b91edcSBarry Smith   Mat            outA;
2556ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
255763b91edcSBarry Smith 
25583a40ed3dSBarry Smith   PetscFunctionBegin;
2559e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
25601df811f5SHong Zhang 
2561b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2562b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2563a871dcd8SBarry Smith 
256463b91edcSBarry Smith   outA             = inA;
2565d5f3da31SBarry Smith   outA->factortype = MAT_FACTOR_LU;
25662205254eSKarl Rupp 
2567c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
25686bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
25692205254eSKarl Rupp 
2570c3122656SLisandro Dalcin   a->row = row;
25712205254eSKarl Rupp 
2572c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
25736bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
25742205254eSKarl Rupp 
2575c3122656SLisandro Dalcin   a->col = col;
257663b91edcSBarry Smith 
257736db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
25786bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
25794c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
25803bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
2581f0ec6fceSSatish Balay 
258294a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2583854ce69bSBarry Smith     ierr = PetscMalloc1(inA->rmap->n+1,&a->solve_work);CHKERRQ(ierr);
25843bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
258594a9d846SBarry Smith   }
258663b91edcSBarry Smith 
2587f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2588137fb511SHong Zhang   if (row_identity && col_identity) {
2589ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2590137fb511SHong Zhang   } else {
2591719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2592137fb511SHong Zhang   }
25933a40ed3dSBarry Smith   PetscFunctionReturn(0);
2594a871dcd8SBarry Smith }
2595a871dcd8SBarry Smith 
25964a2ae208SSatish Balay #undef __FUNCT__
25974a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2598f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2599f0b747eeSBarry Smith {
2600f0b747eeSBarry Smith   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2601f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2602efee365bSSatish Balay   PetscErrorCode ierr;
2603c5df96a5SBarry Smith   PetscBLASInt   one = 1,bnz;
26043a40ed3dSBarry Smith 
26053a40ed3dSBarry Smith   PetscFunctionBegin;
2606c5df96a5SBarry Smith   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
26078b83055fSJed Brown   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2608efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2609acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
26103a40ed3dSBarry Smith   PetscFunctionReturn(0);
2611f0b747eeSBarry Smith }
2612f0b747eeSBarry Smith 
26134a2ae208SSatish Balay #undef __FUNCT__
26144a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
261597f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2616cddf8d76SBarry Smith {
2617dfbe8321SBarry Smith   PetscErrorCode ierr;
261897f1f81fSBarry Smith   PetscInt       i;
2619cddf8d76SBarry Smith 
26203a40ed3dSBarry Smith   PetscFunctionBegin;
2621cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2622854ce69bSBarry Smith     ierr = PetscMalloc1(n+1,B);CHKERRQ(ierr);
2623cddf8d76SBarry Smith   }
2624cddf8d76SBarry Smith 
2625cddf8d76SBarry Smith   for (i=0; i<n; i++) {
26266a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2627cddf8d76SBarry Smith   }
26283a40ed3dSBarry Smith   PetscFunctionReturn(0);
2629cddf8d76SBarry Smith }
2630cddf8d76SBarry Smith 
26314a2ae208SSatish Balay #undef __FUNCT__
26324a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
263397f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
26344dcbc457SBarry Smith {
2635e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
26366849ba73SBarry Smith   PetscErrorCode ierr;
26375d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
26385d0c19d7SBarry Smith   const PetscInt *idx;
263997f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2640f1af5d2fSBarry Smith   PetscBT        table;
2641bbd702dbSSatish Balay 
26423a40ed3dSBarry Smith   PetscFunctionBegin;
2643d0f46423SBarry Smith   m  = A->rmap->n;
2644e4d965acSSatish Balay   ai = a->i;
2645bfeeae90SHong Zhang   aj = a->j;
26468a047759SSatish Balay 
2647e32f2f54SBarry Smith   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
264806763907SSatish Balay 
2649854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&nidx);CHKERRQ(ierr);
265053b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
265106763907SSatish Balay 
2652e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2653b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2654e4d965acSSatish Balay     isz  = 0;
26556831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2656e4d965acSSatish Balay 
2657e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
26584dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2659b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2660e4d965acSSatish Balay 
2661dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2662e4d965acSSatish Balay     for (j=0; j<n; ++j) {
26632205254eSKarl Rupp       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
26644dcbc457SBarry Smith     }
266506763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
26666bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2667e4d965acSSatish Balay 
266804a348a9SBarry Smith     k = 0;
266904a348a9SBarry Smith     for (j=0; j<ov; j++) { /* for each overlap */
267004a348a9SBarry Smith       n = isz;
267106763907SSatish Balay       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2672e4d965acSSatish Balay         row   = nidx[k];
2673e4d965acSSatish Balay         start = ai[row];
2674e4d965acSSatish Balay         end   = ai[row+1];
267504a348a9SBarry Smith         for (l = start; l<end; l++) {
2676efb16452SHong Zhang           val = aj[l];
26772205254eSKarl Rupp           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2678e4d965acSSatish Balay         }
2679e4d965acSSatish Balay       }
2680e4d965acSSatish Balay     }
268170b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2682e4d965acSSatish Balay   }
268394bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2684606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
26853a40ed3dSBarry Smith   PetscFunctionReturn(0);
26864dcbc457SBarry Smith }
268717ab2063SBarry Smith 
26880513a670SBarry Smith /* -------------------------------------------------------------- */
26894a2ae208SSatish Balay #undef __FUNCT__
26904a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2691dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
26920513a670SBarry Smith {
26930513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
26946849ba73SBarry Smith   PetscErrorCode ierr;
26953b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
26965d0c19d7SBarry Smith   const PetscInt *row,*col;
26975d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
269856cd22aeSBarry Smith   IS             icolp,irowp;
26990298fd71SBarry Smith   PetscInt       *cwork = NULL;
27000298fd71SBarry Smith   PetscScalar    *vwork = NULL;
27010513a670SBarry Smith 
27023a40ed3dSBarry Smith   PetscFunctionBegin;
27034c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
270456cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
27054c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
270656cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
27070513a670SBarry Smith 
27080513a670SBarry Smith   /* determine lengths of permuted rows */
2709854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&lens);CHKERRQ(ierr);
27102205254eSKarl Rupp   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2711ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2712f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
271333d57670SJed Brown   ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
27147adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2715ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2716606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
27170513a670SBarry Smith 
2718785e854fSJed Brown   ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr);
27190513a670SBarry Smith   for (i=0; i<m; i++) {
272032ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
27212205254eSKarl Rupp     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2722cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
272332ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
27240513a670SBarry Smith   }
2725606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
27262205254eSKarl Rupp 
27273c7d62e4SBarry Smith   (*B)->assembled = PETSC_FALSE;
27282205254eSKarl Rupp 
27290513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27300513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
273156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
273256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
27336bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
27346bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
27353a40ed3dSBarry Smith   PetscFunctionReturn(0);
27360513a670SBarry Smith }
27370513a670SBarry Smith 
27384a2ae208SSatish Balay #undef __FUNCT__
27394a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2740dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2741cb5b572fSBarry Smith {
2742dfbe8321SBarry Smith   PetscErrorCode ierr;
2743cb5b572fSBarry Smith 
2744cb5b572fSBarry Smith   PetscFunctionBegin;
274533f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
274633f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2747be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2748be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2749be6bf707SBarry Smith 
2750700c5bfcSBarry 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");
2751d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2752cb5b572fSBarry Smith   } else {
2753cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2754cb5b572fSBarry Smith   }
2755cb5b572fSBarry Smith   PetscFunctionReturn(0);
2756cb5b572fSBarry Smith }
2757cb5b572fSBarry Smith 
27584a2ae208SSatish Balay #undef __FUNCT__
27594994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
27604994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2761273d9f13SBarry Smith {
2762dfbe8321SBarry Smith   PetscErrorCode ierr;
2763273d9f13SBarry Smith 
2764273d9f13SBarry Smith   PetscFunctionBegin;
2765ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2766273d9f13SBarry Smith   PetscFunctionReturn(0);
2767273d9f13SBarry Smith }
2768273d9f13SBarry Smith 
27694a2ae208SSatish Balay #undef __FUNCT__
27708c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray_SeqAIJ"
27718c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
27726c0721eeSBarry Smith {
27736c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
27746e111a19SKarl Rupp 
27756c0721eeSBarry Smith   PetscFunctionBegin;
27766c0721eeSBarry Smith   *array = a->a;
27776c0721eeSBarry Smith   PetscFunctionReturn(0);
27786c0721eeSBarry Smith }
27796c0721eeSBarry Smith 
27804a2ae208SSatish Balay #undef __FUNCT__
27818c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray_SeqAIJ"
27828c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
27836c0721eeSBarry Smith {
27846c0721eeSBarry Smith   PetscFunctionBegin;
27856c0721eeSBarry Smith   PetscFunctionReturn(0);
27866c0721eeSBarry Smith }
2787273d9f13SBarry Smith 
27888229c054SShri Abhyankar /*
27898229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
27908229c054SShri Abhyankar    have different nonzero structure.
27918229c054SShri Abhyankar */
2792ac90fabeSBarry Smith #undef __FUNCT__
2793b264fe52SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqX_private"
2794b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2795ec7775f6SShri Abhyankar {
2796b264fe52SHong Zhang   PetscInt       i,j,k,nzx,nzy;
2797ec7775f6SShri Abhyankar 
2798ec7775f6SShri Abhyankar   PetscFunctionBegin;
2799ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2800ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
2801b264fe52SHong Zhang     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2802b264fe52SHong Zhang     nzx = xi[i+1] - xi[i];
2803b264fe52SHong Zhang     nzy = yi[i+1] - yi[i];
28048af7cee1SJed Brown     nnz[i] = 0;
28058af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2806b264fe52SHong Zhang       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2807b264fe52SHong Zhang       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
28088af7cee1SJed Brown       nnz[i]++;
28098af7cee1SJed Brown     }
28108af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2811ec7775f6SShri Abhyankar   }
2812ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2813ec7775f6SShri Abhyankar }
2814ec7775f6SShri Abhyankar 
2815ec7775f6SShri Abhyankar #undef __FUNCT__
2816b264fe52SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
2817b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2818b264fe52SHong Zhang {
2819b264fe52SHong Zhang   PetscInt       m = Y->rmap->N;
2820b264fe52SHong Zhang   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2821b264fe52SHong Zhang   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;
2822b264fe52SHong Zhang   PetscErrorCode ierr;
2823b264fe52SHong Zhang 
2824b264fe52SHong Zhang   PetscFunctionBegin;
2825b264fe52SHong Zhang   /* Set the number of nonzeros in the new matrix */
2826b264fe52SHong Zhang   ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr);
2827b264fe52SHong Zhang   PetscFunctionReturn(0);
2828b264fe52SHong Zhang }
2829b264fe52SHong Zhang 
2830b264fe52SHong Zhang #undef __FUNCT__
2831ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2832f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2833ac90fabeSBarry Smith {
2834dfbe8321SBarry Smith   PetscErrorCode ierr;
2835ac90fabeSBarry Smith   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2836c5df96a5SBarry Smith   PetscBLASInt   one=1,bnz;
2837ac90fabeSBarry Smith 
2838ac90fabeSBarry Smith   PetscFunctionBegin;
2839c5df96a5SBarry Smith   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2840ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2841f4df32b1SMatthew Knepley     PetscScalar alpha = a;
28428b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2843acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2844a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2845ab784542SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2846ab784542SHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2847ac90fabeSBarry Smith   } else {
28488229c054SShri Abhyankar     Mat      B;
28498229c054SShri Abhyankar     PetscInt *nnz;
2850785e854fSJed Brown     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
2851ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2852bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
28534aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
285433d57670SJed Brown     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
2855176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
28568229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2857ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2858ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2859ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
28608229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2861ac90fabeSBarry Smith   }
2862ac90fabeSBarry Smith   PetscFunctionReturn(0);
2863ac90fabeSBarry Smith }
2864ac90fabeSBarry Smith 
2865521d7252SBarry Smith #undef __FUNCT__
2866354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
28677087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2868354c94deSBarry Smith {
2869354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2870354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2871354c94deSBarry Smith   PetscInt    i,nz;
2872354c94deSBarry Smith   PetscScalar *a;
2873354c94deSBarry Smith 
2874354c94deSBarry Smith   PetscFunctionBegin;
2875354c94deSBarry Smith   nz = aij->nz;
2876354c94deSBarry Smith   a  = aij->a;
28772205254eSKarl Rupp   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2878354c94deSBarry Smith #else
2879354c94deSBarry Smith   PetscFunctionBegin;
2880354c94deSBarry Smith #endif
2881354c94deSBarry Smith   PetscFunctionReturn(0);
2882354c94deSBarry Smith }
2883354c94deSBarry Smith 
2884e34fafa9SBarry Smith #undef __FUNCT__
2885985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2886985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2887e34fafa9SBarry Smith {
2888e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2889e34fafa9SBarry Smith   PetscErrorCode ierr;
2890d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2891e34fafa9SBarry Smith   PetscReal      atmp;
2892985db425SBarry Smith   PetscScalar    *x;
2893e34fafa9SBarry Smith   MatScalar      *aa;
2894e34fafa9SBarry Smith 
2895e34fafa9SBarry Smith   PetscFunctionBegin;
2896e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2897e34fafa9SBarry Smith   aa = a->a;
2898e34fafa9SBarry Smith   ai = a->i;
2899e34fafa9SBarry Smith   aj = a->j;
2900e34fafa9SBarry Smith 
2901985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2902e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2903e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2904e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2905e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2906e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
29079189402eSHong Zhang     x[i]  = 0.0;
2908e34fafa9SBarry Smith     for (j=0; j<ncols; j++) {
2909985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2910985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2911985db425SBarry Smith       aa++; aj++;
2912985db425SBarry Smith     }
2913985db425SBarry Smith   }
2914985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2915985db425SBarry Smith   PetscFunctionReturn(0);
2916985db425SBarry Smith }
2917985db425SBarry Smith 
2918985db425SBarry Smith #undef __FUNCT__
2919985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2920985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2921985db425SBarry Smith {
2922985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2923985db425SBarry Smith   PetscErrorCode ierr;
2924d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2925985db425SBarry Smith   PetscScalar    *x;
2926985db425SBarry Smith   MatScalar      *aa;
2927985db425SBarry Smith 
2928985db425SBarry Smith   PetscFunctionBegin;
2929e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2930985db425SBarry Smith   aa = a->a;
2931985db425SBarry Smith   ai = a->i;
2932985db425SBarry Smith   aj = a->j;
2933985db425SBarry Smith 
2934985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2935985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2936985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2937e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2938985db425SBarry Smith   for (i=0; i<m; i++) {
2939985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2940d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2941985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2942985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2943985db425SBarry Smith       x[i] = 0.0;
2944985db425SBarry Smith       if (idx) {
2945985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2946985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2947985db425SBarry Smith           if (aj[j] > j) {
2948985db425SBarry Smith             idx[i] = j;
2949985db425SBarry Smith             break;
2950985db425SBarry Smith           }
2951985db425SBarry Smith         }
2952985db425SBarry Smith       }
2953985db425SBarry Smith     }
2954985db425SBarry Smith     for (j=0; j<ncols; j++) {
2955985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2956985db425SBarry Smith       aa++; aj++;
2957985db425SBarry Smith     }
2958985db425SBarry Smith   }
2959985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2960985db425SBarry Smith   PetscFunctionReturn(0);
2961985db425SBarry Smith }
2962985db425SBarry Smith 
2963985db425SBarry Smith #undef __FUNCT__
2964c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2965c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2966c87e5d42SMatthew Knepley {
2967c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2968c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2969c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2970c87e5d42SMatthew Knepley   PetscReal      atmp;
2971c87e5d42SMatthew Knepley   PetscScalar    *x;
2972c87e5d42SMatthew Knepley   MatScalar      *aa;
2973c87e5d42SMatthew Knepley 
2974c87e5d42SMatthew Knepley   PetscFunctionBegin;
2975e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2976c87e5d42SMatthew Knepley   aa = a->a;
2977c87e5d42SMatthew Knepley   ai = a->i;
2978c87e5d42SMatthew Knepley   aj = a->j;
2979c87e5d42SMatthew Knepley 
2980c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2981c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2982c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
298360e0710aSBarry 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);
2984c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2985c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2986289a08f5SMatthew Knepley     if (ncols) {
2987289a08f5SMatthew Knepley       /* Get first nonzero */
2988289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
2989289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
29902205254eSKarl Rupp         if (atmp > 1.0e-12) {
29912205254eSKarl Rupp           x[i] = atmp;
29922205254eSKarl Rupp           if (idx) idx[i] = aj[j];
29932205254eSKarl Rupp           break;
29942205254eSKarl Rupp         }
2995289a08f5SMatthew Knepley       }
299612431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2997289a08f5SMatthew Knepley     } else {
2998289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2999289a08f5SMatthew Knepley     }
3000c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
3001c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
3002289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3003c87e5d42SMatthew Knepley       aa++; aj++;
3004c87e5d42SMatthew Knepley     }
3005c87e5d42SMatthew Knepley   }
3006c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3007c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
3008c87e5d42SMatthew Knepley }
3009c87e5d42SMatthew Knepley 
3010c87e5d42SMatthew Knepley #undef __FUNCT__
3011985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
3012985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3013985db425SBarry Smith {
3014985db425SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3015985db425SBarry Smith   PetscErrorCode  ierr;
3016d9ca1df4SBarry Smith   PetscInt        i,j,m = A->rmap->n,ncols,n;
3017d9ca1df4SBarry Smith   const PetscInt  *ai,*aj;
3018985db425SBarry Smith   PetscScalar     *x;
3019d9ca1df4SBarry Smith   const MatScalar *aa;
3020985db425SBarry Smith 
3021985db425SBarry Smith   PetscFunctionBegin;
3022e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3023985db425SBarry Smith   aa = a->a;
3024985db425SBarry Smith   ai = a->i;
3025985db425SBarry Smith   aj = a->j;
3026985db425SBarry Smith 
3027985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3028985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3029985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
3030e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3031985db425SBarry Smith   for (i=0; i<m; i++) {
3032985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
3033d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
3034985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
3035985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3036985db425SBarry Smith       x[i] = 0.0;
3037985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
3038985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
3039985db425SBarry Smith         for (j=0; j<ncols; j++) {
3040985db425SBarry Smith           if (aj[j] > j) {
3041985db425SBarry Smith             idx[i] = j;
3042985db425SBarry Smith             break;
3043985db425SBarry Smith           }
3044985db425SBarry Smith         }
3045985db425SBarry Smith       }
3046985db425SBarry Smith     }
3047985db425SBarry Smith     for (j=0; j<ncols; j++) {
3048985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3049985db425SBarry Smith       aa++; aj++;
3050e34fafa9SBarry Smith     }
3051e34fafa9SBarry Smith   }
3052e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3053e34fafa9SBarry Smith   PetscFunctionReturn(0);
3054e34fafa9SBarry Smith }
3055bbead8a2SBarry Smith 
3056bbead8a2SBarry Smith #include <petscblaslapack.h>
3057af0996ceSBarry Smith #include <petsc/private/kernels/blockinvert.h>
3058bbead8a2SBarry Smith 
3059bbead8a2SBarry Smith #undef __FUNCT__
3060bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
3061713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3062bbead8a2SBarry Smith {
3063bbead8a2SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
3064bbead8a2SBarry Smith   PetscErrorCode ierr;
306533d57670SJed Brown   PetscInt       i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3066bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
3067bbead8a2SBarry Smith   PetscReal      shift = 0.0;
3068bbead8a2SBarry Smith 
3069bbead8a2SBarry Smith   PetscFunctionBegin;
30704a0d0026SBarry Smith   if (a->ibdiagvalid) {
30714a0d0026SBarry Smith     if (values) *values = a->ibdiag;
30724a0d0026SBarry Smith     PetscFunctionReturn(0);
30734a0d0026SBarry Smith   }
3074bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
3075bbead8a2SBarry Smith   if (!a->ibdiag) {
3076785e854fSJed Brown     ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr);
30773bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
3078bbead8a2SBarry Smith   }
3079bbead8a2SBarry Smith   diag = a->ibdiag;
3080bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
3081bbead8a2SBarry Smith   /* factor and invert each block */
3082bbead8a2SBarry Smith   switch (bs) {
3083bbead8a2SBarry Smith   case 1:
3084bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3085bbead8a2SBarry Smith       ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
3086bbead8a2SBarry Smith       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3087bbead8a2SBarry Smith     }
3088bbead8a2SBarry Smith     break;
3089bbead8a2SBarry Smith   case 2:
3090bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3091bbead8a2SBarry Smith       ij[0] = 2*i; ij[1] = 2*i + 1;
3092bbead8a2SBarry Smith       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
309396b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
309496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
3095bbead8a2SBarry Smith       diag += 4;
3096bbead8a2SBarry Smith     }
3097bbead8a2SBarry Smith     break;
3098bbead8a2SBarry Smith   case 3:
3099bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3100bbead8a2SBarry Smith       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3101bbead8a2SBarry Smith       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
310296b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
310396b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
3104bbead8a2SBarry Smith       diag += 9;
3105bbead8a2SBarry Smith     }
3106bbead8a2SBarry Smith     break;
3107bbead8a2SBarry Smith   case 4:
3108bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3109bbead8a2SBarry Smith       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3110bbead8a2SBarry Smith       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
311196b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
311296b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
3113bbead8a2SBarry Smith       diag += 16;
3114bbead8a2SBarry Smith     }
3115bbead8a2SBarry Smith     break;
3116bbead8a2SBarry Smith   case 5:
3117bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3118bbead8a2SBarry 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;
3119bbead8a2SBarry Smith       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
312096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
312196b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
3122bbead8a2SBarry Smith       diag += 25;
3123bbead8a2SBarry Smith     }
3124bbead8a2SBarry Smith     break;
3125bbead8a2SBarry Smith   case 6:
3126bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3127bbead8a2SBarry 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;
3128bbead8a2SBarry Smith       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
312996b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
313096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3131bbead8a2SBarry Smith       diag += 36;
3132bbead8a2SBarry Smith     }
3133bbead8a2SBarry Smith     break;
3134bbead8a2SBarry Smith   case 7:
3135bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3136bbead8a2SBarry 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;
3137bbead8a2SBarry Smith       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
313896b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
313996b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3140bbead8a2SBarry Smith       diag += 49;
3141bbead8a2SBarry Smith     }
3142bbead8a2SBarry Smith     break;
3143bbead8a2SBarry Smith   default:
3144dcca6d9dSJed Brown     ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr);
3145bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3146bbead8a2SBarry Smith       for (j=0; j<bs; j++) {
3147bbead8a2SBarry Smith         IJ[j] = bs*i + j;
3148bbead8a2SBarry Smith       }
3149bbead8a2SBarry Smith       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
315096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
315196b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3152bbead8a2SBarry Smith       diag += bs2;
3153bbead8a2SBarry Smith     }
3154bbead8a2SBarry Smith     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3155bbead8a2SBarry Smith   }
3156bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3157bbead8a2SBarry Smith   PetscFunctionReturn(0);
3158bbead8a2SBarry Smith }
3159bbead8a2SBarry Smith 
316073a71a0fSBarry Smith #undef __FUNCT__
316173a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_SeqAIJ"
316273a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
316373a71a0fSBarry Smith {
316473a71a0fSBarry Smith   PetscErrorCode ierr;
316573a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
316673a71a0fSBarry Smith   PetscScalar    a;
316773a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
316873a71a0fSBarry Smith 
316973a71a0fSBarry Smith   PetscFunctionBegin;
317073a71a0fSBarry Smith   if (!x->assembled) {
317173a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
317273a71a0fSBarry Smith     for (i=0; i<m; i++) {
317373a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
317473a71a0fSBarry Smith         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
317573a71a0fSBarry Smith         col  = (PetscInt)(n*PetscRealPart(a));
317673a71a0fSBarry Smith         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
317773a71a0fSBarry Smith       }
317873a71a0fSBarry Smith     }
317973a71a0fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
318073a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
318173a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
318273a71a0fSBarry Smith   PetscFunctionReturn(0);
318373a71a0fSBarry Smith }
318473a71a0fSBarry Smith 
31857d68702bSBarry Smith #undef __FUNCT__
31867d68702bSBarry Smith #define __FUNCT__ "MatShift_SeqAIJ"
31877d68702bSBarry Smith PetscErrorCode MatShift_SeqAIJ(Mat Y,PetscScalar a)
31887d68702bSBarry Smith {
31897d68702bSBarry Smith   PetscErrorCode ierr;
31907d68702bSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)Y->data;
31917d68702bSBarry Smith 
31927d68702bSBarry Smith   PetscFunctionBegin;
31937d68702bSBarry Smith   if (!aij->nz) {
31947d68702bSBarry Smith     ierr = MatSeqAIJSetPreallocation(Y,1,NULL);CHKERRQ(ierr);
31957d68702bSBarry Smith   }
31967d68702bSBarry Smith   ierr = MatShift_Basic(Y,a);CHKERRQ(ierr);
31977d68702bSBarry Smith   PetscFunctionReturn(0);
31987d68702bSBarry Smith }
31997d68702bSBarry Smith 
3200682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
32010a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3202cb5b572fSBarry Smith                                         MatGetRow_SeqAIJ,
3203cb5b572fSBarry Smith                                         MatRestoreRow_SeqAIJ,
3204cb5b572fSBarry Smith                                         MatMult_SeqAIJ,
320597304618SKris Buschelman                                 /*  4*/ MatMultAdd_SeqAIJ,
32067c922b88SBarry Smith                                         MatMultTranspose_SeqAIJ,
32077c922b88SBarry Smith                                         MatMultTransposeAdd_SeqAIJ,
3208db4efbfdSBarry Smith                                         0,
3209db4efbfdSBarry Smith                                         0,
3210db4efbfdSBarry Smith                                         0,
3211db4efbfdSBarry Smith                                 /* 10*/ 0,
3212cb5b572fSBarry Smith                                         MatLUFactor_SeqAIJ,
3213cb5b572fSBarry Smith                                         0,
321441f059aeSBarry Smith                                         MatSOR_SeqAIJ,
321517ab2063SBarry Smith                                         MatTranspose_SeqAIJ,
321697304618SKris Buschelman                                 /*1 5*/ MatGetInfo_SeqAIJ,
3217cb5b572fSBarry Smith                                         MatEqual_SeqAIJ,
3218cb5b572fSBarry Smith                                         MatGetDiagonal_SeqAIJ,
3219cb5b572fSBarry Smith                                         MatDiagonalScale_SeqAIJ,
3220cb5b572fSBarry Smith                                         MatNorm_SeqAIJ,
322197304618SKris Buschelman                                 /* 20*/ 0,
3222cb5b572fSBarry Smith                                         MatAssemblyEnd_SeqAIJ,
3223cb5b572fSBarry Smith                                         MatSetOption_SeqAIJ,
3224cb5b572fSBarry Smith                                         MatZeroEntries_SeqAIJ,
3225d519adbfSMatthew Knepley                                 /* 24*/ MatZeroRows_SeqAIJ,
3226db4efbfdSBarry Smith                                         0,
3227db4efbfdSBarry Smith                                         0,
3228db4efbfdSBarry Smith                                         0,
3229db4efbfdSBarry Smith                                         0,
32304994cf47SJed Brown                                 /* 29*/ MatSetUp_SeqAIJ,
3231db4efbfdSBarry Smith                                         0,
3232db4efbfdSBarry Smith                                         0,
32338c778c55SBarry Smith                                         0,
32348c778c55SBarry Smith                                         0,
3235d519adbfSMatthew Knepley                                 /* 34*/ MatDuplicate_SeqAIJ,
3236cb5b572fSBarry Smith                                         0,
3237cb5b572fSBarry Smith                                         0,
3238cb5b572fSBarry Smith                                         MatILUFactor_SeqAIJ,
3239cb5b572fSBarry Smith                                         0,
3240d519adbfSMatthew Knepley                                 /* 39*/ MatAXPY_SeqAIJ,
3241cb5b572fSBarry Smith                                         MatGetSubMatrices_SeqAIJ,
3242cb5b572fSBarry Smith                                         MatIncreaseOverlap_SeqAIJ,
3243cb5b572fSBarry Smith                                         MatGetValues_SeqAIJ,
3244cb5b572fSBarry Smith                                         MatCopy_SeqAIJ,
3245d519adbfSMatthew Knepley                                 /* 44*/ MatGetRowMax_SeqAIJ,
3246cb5b572fSBarry Smith                                         MatScale_SeqAIJ,
32477d68702bSBarry Smith                                         MatShift_SeqAIJ,
324879299369SBarry Smith                                         MatDiagonalSet_SeqAIJ,
32496e169961SBarry Smith                                         MatZeroRowsColumns_SeqAIJ,
325073a71a0fSBarry Smith                                 /* 49*/ MatSetRandom_SeqAIJ,
32513b2fbd54SBarry Smith                                         MatGetRowIJ_SeqAIJ,
32523b2fbd54SBarry Smith                                         MatRestoreRowIJ_SeqAIJ,
32533b2fbd54SBarry Smith                                         MatGetColumnIJ_SeqAIJ,
3254a93ec695SBarry Smith                                         MatRestoreColumnIJ_SeqAIJ,
325593dfae19SHong Zhang                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3256b9617806SBarry Smith                                         0,
32570513a670SBarry Smith                                         0,
3258cda55fadSBarry Smith                                         MatPermute_SeqAIJ,
3259cda55fadSBarry Smith                                         0,
3260d519adbfSMatthew Knepley                                 /* 59*/ 0,
3261b9b97703SBarry Smith                                         MatDestroy_SeqAIJ,
3262b9b97703SBarry Smith                                         MatView_SeqAIJ,
3263357abbc8SBarry Smith                                         0,
3264321b30b9SSatish Balay                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3265321b30b9SSatish Balay                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3266321b30b9SSatish Balay                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3267ee4f033dSBarry Smith                                         0,
3268ee4f033dSBarry Smith                                         0,
3269ee4f033dSBarry Smith                                         0,
3270d519adbfSMatthew Knepley                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3271c87e5d42SMatthew Knepley                                         MatGetRowMinAbs_SeqAIJ,
3272ee4f033dSBarry Smith                                         0,
3273ee4f033dSBarry Smith                                         MatSetColoring_SeqAIJ,
3274dcf5cc72SBarry Smith                                         0,
3275d519adbfSMatthew Knepley                                 /* 74*/ MatSetValuesAdifor_SeqAIJ,
32763acb8795SBarry Smith                                         MatFDColoringApply_AIJ,
327797304618SKris Buschelman                                         0,
327897304618SKris Buschelman                                         0,
327997304618SKris Buschelman                                         0,
32806ce1633cSBarry Smith                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
328197304618SKris Buschelman                                         0,
328297304618SKris Buschelman                                         0,
328397304618SKris Buschelman                                         0,
3284bc011b1eSHong Zhang                                         MatLoad_SeqAIJ,
3285d519adbfSMatthew Knepley                                 /* 84*/ MatIsSymmetric_SeqAIJ,
32861cbb95d3SBarry Smith                                         MatIsHermitian_SeqAIJ,
32876284ec50SHong Zhang                                         0,
32886284ec50SHong Zhang                                         0,
3289bc011b1eSHong Zhang                                         0,
3290d519adbfSMatthew Knepley                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
329126be0446SHong Zhang                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
329226be0446SHong Zhang                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
329365e8a0caSHong Zhang                                         MatPtAP_SeqAIJ_SeqAIJ,
32944a1b09b7SHong Zhang                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_DenseAxpy,
329565e8a0caSHong Zhang                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
32966fc122caSHong Zhang                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
32976fc122caSHong Zhang                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
32986fc122caSHong Zhang                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
32992121bac1SHong Zhang                                         0,
33002121bac1SHong Zhang                                 /* 99*/ 0,
3301609c6c4dSKris Buschelman                                         0,
3302609c6c4dSKris Buschelman                                         0,
330387d4246cSBarry Smith                                         MatConjugate_SeqAIJ,
330487d4246cSBarry Smith                                         0,
3305d519adbfSMatthew Knepley                                 /*104*/ MatSetValuesRow_SeqAIJ,
330699cafbc1SBarry Smith                                         MatRealPart_SeqAIJ,
3307f5edf698SHong Zhang                                         MatImaginaryPart_SeqAIJ,
3308f5edf698SHong Zhang                                         0,
33092bebee5dSHong Zhang                                         0,
3310cbd44569SHong Zhang                                 /*109*/ MatMatSolve_SeqAIJ,
3311985db425SBarry Smith                                         0,
33122af78befSBarry Smith                                         MatGetRowMin_SeqAIJ,
33132af78befSBarry Smith                                         0,
3314599ef60dSHong Zhang                                         MatMissingDiagonal_SeqAIJ,
3315d519adbfSMatthew Knepley                                 /*114*/ 0,
3316599ef60dSHong Zhang                                         0,
33173c2a7987SHong Zhang                                         0,
3318fe97e370SBarry Smith                                         0,
3319fbdbba38SShri Abhyankar                                         0,
3320fbdbba38SShri Abhyankar                                 /*119*/ 0,
3321fbdbba38SShri Abhyankar                                         0,
3322fbdbba38SShri Abhyankar                                         0,
332382d44351SHong Zhang                                         0,
3324b3a44c85SBarry Smith                                         MatGetMultiProcBlock_SeqAIJ,
33250716a85fSBarry Smith                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3326bbead8a2SBarry Smith                                         MatGetColumnNorms_SeqAIJ,
332737868618SMatthew G Knepley                                         MatInvertBlockDiagonal_SeqAIJ,
332837868618SMatthew G Knepley                                         0,
332937868618SMatthew G Knepley                                         0,
33305df89d91SHong Zhang                                 /*129*/ 0,
333175648e8dSHong Zhang                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
333275648e8dSHong Zhang                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
333375648e8dSHong Zhang                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3334b9af6bddSHong Zhang                                         MatTransposeColoringCreate_SeqAIJ,
3335b9af6bddSHong Zhang                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
33362b8ad9a3SHong Zhang                                         MatTransColoringApplyDenToSp_SeqAIJ,
33372b8ad9a3SHong Zhang                                         MatRARt_SeqAIJ_SeqAIJ,
33382b8ad9a3SHong Zhang                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
33393964eb88SJed Brown                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
33403964eb88SJed Brown                                  /*139*/0,
3341f9426fe0SMark Adams                                         0,
33421919a2e2SJed Brown                                         0,
33433a062f41SBarry Smith                                         MatFDColoringSetUp_SeqXAIJ,
33449c8f2541SHong Zhang                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
33459c8f2541SHong Zhang                                  /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ
33469e29f15eSvictorle };
334717ab2063SBarry Smith 
33484a2ae208SSatish Balay #undef __FUNCT__
33494a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
33507087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3351bef8e0ddSBarry Smith {
3352bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
335397f1f81fSBarry Smith   PetscInt   i,nz,n;
3354bef8e0ddSBarry Smith 
3355bef8e0ddSBarry Smith   PetscFunctionBegin;
3356bef8e0ddSBarry Smith   nz = aij->maxnz;
3357d0f46423SBarry Smith   n  = mat->rmap->n;
3358bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3359bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3360bef8e0ddSBarry Smith   }
3361bef8e0ddSBarry Smith   aij->nz = nz;
3362bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3363bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3364bef8e0ddSBarry Smith   }
3365bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3366bef8e0ddSBarry Smith }
3367bef8e0ddSBarry Smith 
33684a2ae208SSatish Balay #undef __FUNCT__
33694a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3370bef8e0ddSBarry Smith /*@
3371bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3372bef8e0ddSBarry Smith        in the matrix.
3373bef8e0ddSBarry Smith 
3374bef8e0ddSBarry Smith   Input Parameters:
3375bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3376bef8e0ddSBarry Smith -  indices - the column indices
3377bef8e0ddSBarry Smith 
337815091d37SBarry Smith   Level: advanced
337915091d37SBarry Smith 
3380bef8e0ddSBarry Smith   Notes:
3381bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3382bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3383bef8e0ddSBarry Smith   of the MatSetValues() operation.
3384bef8e0ddSBarry Smith 
3385bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3386d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3387bef8e0ddSBarry Smith 
3388bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3389bef8e0ddSBarry Smith 
3390b9617806SBarry Smith     The indices should start with zero, not one.
3391b9617806SBarry Smith 
3392bef8e0ddSBarry Smith @*/
33937087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3394bef8e0ddSBarry Smith {
33954ac538c5SBarry Smith   PetscErrorCode ierr;
3396bef8e0ddSBarry Smith 
3397bef8e0ddSBarry Smith   PetscFunctionBegin;
33980700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
33994482741eSBarry Smith   PetscValidPointer(indices,2);
34004ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3401bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3402bef8e0ddSBarry Smith }
3403bef8e0ddSBarry Smith 
3404be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3405be6bf707SBarry Smith 
34064a2ae208SSatish Balay #undef __FUNCT__
34074a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
34087087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3409be6bf707SBarry Smith {
3410be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
34116849ba73SBarry Smith   PetscErrorCode ierr;
3412d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3413be6bf707SBarry Smith 
3414be6bf707SBarry Smith   PetscFunctionBegin;
3415169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3416be6bf707SBarry Smith 
3417be6bf707SBarry Smith   /* allocate space for values if not already there */
3418be6bf707SBarry Smith   if (!aij->saved_values) {
3419854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr);
34203bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3421be6bf707SBarry Smith   }
3422be6bf707SBarry Smith 
3423be6bf707SBarry Smith   /* copy values over */
342487828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3425be6bf707SBarry Smith   PetscFunctionReturn(0);
3426be6bf707SBarry Smith }
3427be6bf707SBarry Smith 
34284a2ae208SSatish Balay #undef __FUNCT__
3429b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3430be6bf707SBarry Smith /*@
3431be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3432be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3433be6bf707SBarry Smith        nonlinear portion.
3434be6bf707SBarry Smith 
3435be6bf707SBarry Smith    Collect on Mat
3436be6bf707SBarry Smith 
3437be6bf707SBarry Smith   Input Parameters:
34380e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3439be6bf707SBarry Smith 
344015091d37SBarry Smith   Level: advanced
344115091d37SBarry Smith 
3442be6bf707SBarry Smith   Common Usage, with SNESSolve():
3443be6bf707SBarry Smith $    Create Jacobian matrix
3444be6bf707SBarry Smith $    Set linear terms into matrix
3445be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3446be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3447be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3448512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3449be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3450be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3451be6bf707SBarry Smith $    In your Jacobian routine
3452be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3453be6bf707SBarry Smith $      Set nonlinear terms in matrix
3454be6bf707SBarry Smith 
3455be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3456be6bf707SBarry Smith $    // build linear portion of Jacobian
3457512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3458be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3459be6bf707SBarry Smith $    loop over nonlinear iterations
3460be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3461be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3462be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3463be6bf707SBarry Smith $       Solve linear system with Jacobian
3464be6bf707SBarry Smith $    endloop
3465be6bf707SBarry Smith 
3466be6bf707SBarry Smith   Notes:
3467be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3468512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3469be6bf707SBarry Smith     calling this routine.
3470be6bf707SBarry Smith 
34710c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
34720c468ba9SBarry Smith     and does not allocated additional space.
34730c468ba9SBarry Smith 
3474be6bf707SBarry Smith .seealso: MatRetrieveValues()
3475be6bf707SBarry Smith 
3476be6bf707SBarry Smith @*/
34777087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3478be6bf707SBarry Smith {
34794ac538c5SBarry Smith   PetscErrorCode ierr;
3480be6bf707SBarry Smith 
3481be6bf707SBarry Smith   PetscFunctionBegin;
34820700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3483e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3484e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
34854ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3486be6bf707SBarry Smith   PetscFunctionReturn(0);
3487be6bf707SBarry Smith }
3488be6bf707SBarry Smith 
34894a2ae208SSatish Balay #undef __FUNCT__
34904a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
34917087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3492be6bf707SBarry Smith {
3493be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
34946849ba73SBarry Smith   PetscErrorCode ierr;
3495d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3496be6bf707SBarry Smith 
3497be6bf707SBarry Smith   PetscFunctionBegin;
3498169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3499f23aa3ddSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3500be6bf707SBarry Smith   /* copy values over */
350187828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3502be6bf707SBarry Smith   PetscFunctionReturn(0);
3503be6bf707SBarry Smith }
3504be6bf707SBarry Smith 
35054a2ae208SSatish Balay #undef __FUNCT__
35064a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3507be6bf707SBarry Smith /*@
3508be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3509be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3510be6bf707SBarry Smith        nonlinear portion.
3511be6bf707SBarry Smith 
3512be6bf707SBarry Smith    Collect on Mat
3513be6bf707SBarry Smith 
3514be6bf707SBarry Smith   Input Parameters:
3515be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3516be6bf707SBarry Smith 
351715091d37SBarry Smith   Level: advanced
351815091d37SBarry Smith 
3519be6bf707SBarry Smith .seealso: MatStoreValues()
3520be6bf707SBarry Smith 
3521be6bf707SBarry Smith @*/
35227087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3523be6bf707SBarry Smith {
35244ac538c5SBarry Smith   PetscErrorCode ierr;
3525be6bf707SBarry Smith 
3526be6bf707SBarry Smith   PetscFunctionBegin;
35270700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3528e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3529e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
35304ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3531be6bf707SBarry Smith   PetscFunctionReturn(0);
3532be6bf707SBarry Smith }
3533be6bf707SBarry Smith 
3534f83d6046SBarry Smith 
3535be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
35364a2ae208SSatish Balay #undef __FUNCT__
35374a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
353817ab2063SBarry Smith /*@C
3539682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
35400d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
35416e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
354251c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
35432bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
354417ab2063SBarry Smith 
3545db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3546db81eaa0SLois Curfman McInnes 
354717ab2063SBarry Smith    Input Parameters:
3548db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
354917ab2063SBarry Smith .  m - number of rows
355017ab2063SBarry Smith .  n - number of columns
355117ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
355251c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
35530298fd71SBarry Smith          (possibly different for each row) or NULL
355417ab2063SBarry Smith 
355517ab2063SBarry Smith    Output Parameter:
3556416022c9SBarry Smith .  A - the matrix
355717ab2063SBarry Smith 
3558175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3559ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3560175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3561175b88e8SBarry Smith 
3562b259b22eSLois Curfman McInnes    Notes:
356349a6f317SBarry Smith    If nnz is given then nz is ignored
356449a6f317SBarry Smith 
356517ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
356617ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
35670002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
356844cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
356917ab2063SBarry Smith 
357017ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
35710298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
35723d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
35736da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
357417ab2063SBarry Smith 
3575682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
35764fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3577682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
35786c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
35796c7ebb05SLois Curfman McInnes 
35806c7ebb05SLois Curfman McInnes    Options Database Keys:
3581698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
35829db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
358317ab2063SBarry Smith 
3584027ccd11SLois Curfman McInnes    Level: intermediate
3585027ccd11SLois Curfman McInnes 
358669b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
358736db0b34SBarry Smith 
358817ab2063SBarry Smith @*/
35897087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
359017ab2063SBarry Smith {
3591dfbe8321SBarry Smith   PetscErrorCode ierr;
35926945ee14SBarry Smith 
35933a40ed3dSBarry Smith   PetscFunctionBegin;
3594f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3595117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3596c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3597d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3598273d9f13SBarry Smith   PetscFunctionReturn(0);
3599273d9f13SBarry Smith }
3600273d9f13SBarry Smith 
36014a2ae208SSatish Balay #undef __FUNCT__
36024a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3603273d9f13SBarry Smith /*@C
3604273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3605273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3606273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3607273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3608273d9f13SBarry Smith 
3609273d9f13SBarry Smith    Collective on MPI_Comm
3610273d9f13SBarry Smith 
3611273d9f13SBarry Smith    Input Parameters:
36121c4f3114SJed Brown +  B - The matrix
3613273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3614273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
36150298fd71SBarry Smith          (possibly different for each row) or NULL
3616273d9f13SBarry Smith 
3617273d9f13SBarry Smith    Notes:
361849a6f317SBarry Smith      If nnz is given then nz is ignored
361949a6f317SBarry Smith 
3620273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3621273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3622273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3623273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3624273d9f13SBarry Smith 
3625273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
36260298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3627273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3628273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3629273d9f13SBarry Smith 
3630aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3631aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3632aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3633aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3634aa95bbe8SBarry Smith 
3635a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3636a96a251dSBarry Smith    entries or columns indices
3637a96a251dSBarry Smith 
3638273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3639273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3640273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3641273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3642273d9f13SBarry Smith 
3643273d9f13SBarry Smith    Options Database Keys:
3644698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3645698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3646273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3647273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3648273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3649273d9f13SBarry Smith 
3650273d9f13SBarry Smith    Level: intermediate
3651273d9f13SBarry Smith 
365269b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3653273d9f13SBarry Smith 
3654273d9f13SBarry Smith @*/
36557087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3656273d9f13SBarry Smith {
36574ac538c5SBarry Smith   PetscErrorCode ierr;
3658a23d5eceSKris Buschelman 
3659a23d5eceSKris Buschelman   PetscFunctionBegin;
36606ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
36616ba663aaSJed Brown   PetscValidType(B,1);
36624ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3663a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3664a23d5eceSKris Buschelman }
3665a23d5eceSKris Buschelman 
3666a23d5eceSKris Buschelman #undef __FUNCT__
3667a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
36687087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3669a23d5eceSKris Buschelman {
3670273d9f13SBarry Smith   Mat_SeqAIJ     *b;
36712576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
36726849ba73SBarry Smith   PetscErrorCode ierr;
367397f1f81fSBarry Smith   PetscInt       i;
3674273d9f13SBarry Smith 
3675273d9f13SBarry Smith   PetscFunctionBegin;
36762576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3677a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3678c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3679c461c341SBarry Smith     nz             = 0;
3680c461c341SBarry Smith   }
3681c461c341SBarry Smith 
368226283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
368326283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3684899cda47SBarry Smith 
3685435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
368660e0710aSBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3687b73539f3SBarry Smith   if (nnz) {
3688d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
368960e0710aSBarry 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]);
369060e0710aSBarry 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);
3691b73539f3SBarry Smith     }
3692b73539f3SBarry Smith   }
3693b73539f3SBarry Smith 
3694273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
36952205254eSKarl Rupp 
3696273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3697273d9f13SBarry Smith 
3698ab93d7beSBarry Smith   if (!skipallocation) {
36992ee49352SLisandro Dalcin     if (!b->imax) {
3700dcca6d9dSJed Brown       ierr = PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);CHKERRQ(ierr);
37013bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
37022ee49352SLisandro Dalcin     }
3703273d9f13SBarry Smith     if (!nnz) {
3704435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3705c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3706d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3707d0f46423SBarry Smith       nz = nz*B->rmap->n;
3708273d9f13SBarry Smith     } else {
3709273d9f13SBarry Smith       nz = 0;
3710d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3711273d9f13SBarry Smith     }
3712ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
37132205254eSKarl Rupp     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;
3714ab93d7beSBarry Smith 
3715273d9f13SBarry Smith     /* allocate the matrix space */
3716*53dd7562SDmitry Karpeev     /* FIXME: should B's old memory be unlogged? */
37172ee49352SLisandro Dalcin     ierr    = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3718dcca6d9dSJed Brown     ierr    = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr);
37193bb1ff40SBarry Smith     ierr    = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3720bfeeae90SHong Zhang     b->i[0] = 0;
3721d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
37225da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
37235da197adSKris Buschelman     }
3724273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3725e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3726e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3727b31eba2aSShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
3728b31eba2aSShri Abhyankar     ierr = MatZeroEntries_SeqAIJ(B);CHKERRQ(ierr);
3729b31eba2aSShri Abhyankar #endif
3730c461c341SBarry Smith   } else {
3731e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
3732e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
3733c461c341SBarry Smith   }
3734273d9f13SBarry Smith 
3735273d9f13SBarry Smith   b->nz               = 0;
3736273d9f13SBarry Smith   b->maxnz            = nz;
3737273d9f13SBarry Smith   B->info.nz_unneeded = (double)b->maxnz;
37382205254eSKarl Rupp   if (realalloc) {
37392205254eSKarl Rupp     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
37402205254eSKarl Rupp   }
3741273d9f13SBarry Smith   PetscFunctionReturn(0);
3742273d9f13SBarry Smith }
3743273d9f13SBarry Smith 
3744a1661176SMatthew Knepley #undef  __FUNCT__
3745a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
374658d36128SBarry Smith /*@
3747a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3748a1661176SMatthew Knepley 
3749a1661176SMatthew Knepley    Input Parameters:
3750a1661176SMatthew Knepley +  B - the matrix
3751a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3752a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3753a1661176SMatthew Knepley -  v - optional values in the matrix
3754a1661176SMatthew Knepley 
3755a1661176SMatthew Knepley    Level: developer
3756a1661176SMatthew Knepley 
375758d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
375858d36128SBarry Smith 
3759a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3760a1661176SMatthew Knepley 
3761a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3762a1661176SMatthew Knepley @*/
3763a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3764a1661176SMatthew Knepley {
3765a1661176SMatthew Knepley   PetscErrorCode ierr;
3766a1661176SMatthew Knepley 
3767a1661176SMatthew Knepley   PetscFunctionBegin;
37680700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
37696ba663aaSJed Brown   PetscValidType(B,1);
37704ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3771a1661176SMatthew Knepley   PetscFunctionReturn(0);
3772a1661176SMatthew Knepley }
3773a1661176SMatthew Knepley 
3774a1661176SMatthew Knepley #undef  __FUNCT__
3775a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
37767087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3777a1661176SMatthew Knepley {
3778a1661176SMatthew Knepley   PetscInt       i;
3779a1661176SMatthew Knepley   PetscInt       m,n;
3780a1661176SMatthew Knepley   PetscInt       nz;
3781a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3782a1661176SMatthew Knepley   PetscScalar    *values;
3783a1661176SMatthew Knepley   PetscErrorCode ierr;
3784a1661176SMatthew Knepley 
3785a1661176SMatthew Knepley   PetscFunctionBegin;
378665e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3787779a8d59SSatish Balay 
3788779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3789779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3790779a8d59SSatish Balay 
3791779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3792854ce69bSBarry Smith   ierr = PetscMalloc1(m+1, &nnz);CHKERRQ(ierr);
3793a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3794b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3795a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
379665e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3797a1661176SMatthew Knepley     nnz[i] = nz;
3798a1661176SMatthew Knepley   }
3799a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3800a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3801a1661176SMatthew Knepley 
3802a1661176SMatthew Knepley   if (v) {
3803a1661176SMatthew Knepley     values = (PetscScalar*) v;
3804a1661176SMatthew Knepley   } else {
38051795a4d1SJed Brown     ierr = PetscCalloc1(nz_max, &values);CHKERRQ(ierr);
3806a1661176SMatthew Knepley   }
3807a1661176SMatthew Knepley 
3808a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3809b7940d39SSatish Balay     nz   = Ii[i+1] - Ii[i];
3810b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3811a1661176SMatthew Knepley   }
3812a1661176SMatthew Knepley 
3813a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3814a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3815a1661176SMatthew Knepley 
3816a1661176SMatthew Knepley   if (!v) {
3817a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3818a1661176SMatthew Knepley   }
38197827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3820a1661176SMatthew Knepley   PetscFunctionReturn(0);
3821a1661176SMatthew Knepley }
3822a1661176SMatthew Knepley 
3823c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3824af0996ceSBarry Smith #include <petsc/private/kernels/petscaxpy.h>
3825170fe5c8SBarry Smith 
3826170fe5c8SBarry Smith #undef __FUNCT__
3827170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3828170fe5c8SBarry Smith /*
3829170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3830170fe5c8SBarry Smith 
3831170fe5c8SBarry Smith                n                       p                          p
3832170fe5c8SBarry Smith         (              )       (              )         (                  )
3833170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3834170fe5c8SBarry Smith         (              )       (              )         (                  )
3835170fe5c8SBarry Smith 
3836170fe5c8SBarry Smith */
3837170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3838170fe5c8SBarry Smith {
3839170fe5c8SBarry Smith   PetscErrorCode    ierr;
3840170fe5c8SBarry Smith   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3841170fe5c8SBarry Smith   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3842170fe5c8SBarry Smith   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
38431de00fd4SBarry Smith   PetscInt          i,n,m,q,p;
3844170fe5c8SBarry Smith   const PetscInt    *ii,*idx;
3845170fe5c8SBarry Smith   const PetscScalar *b,*a,*a_q;
3846170fe5c8SBarry Smith   PetscScalar       *c,*c_q;
3847170fe5c8SBarry Smith 
3848170fe5c8SBarry Smith   PetscFunctionBegin;
3849d0f46423SBarry Smith   m    = A->rmap->n;
3850d0f46423SBarry Smith   n    = A->cmap->n;
3851d0f46423SBarry Smith   p    = B->cmap->n;
3852170fe5c8SBarry Smith   a    = sub_a->v;
3853170fe5c8SBarry Smith   b    = sub_b->a;
3854170fe5c8SBarry Smith   c    = sub_c->v;
3855170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3856170fe5c8SBarry Smith 
3857170fe5c8SBarry Smith   ii  = sub_b->i;
3858170fe5c8SBarry Smith   idx = sub_b->j;
3859170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3860170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3861170fe5c8SBarry Smith     while (q-->0) {
3862170fe5c8SBarry Smith       c_q = c + m*(*idx);
3863170fe5c8SBarry Smith       a_q = a + m*i;
3864854c7f52SBarry Smith       PetscKernelAXPY(c_q,*b,a_q,m);
3865170fe5c8SBarry Smith       idx++;
3866170fe5c8SBarry Smith       b++;
3867170fe5c8SBarry Smith     }
3868170fe5c8SBarry Smith   }
3869170fe5c8SBarry Smith   PetscFunctionReturn(0);
3870170fe5c8SBarry Smith }
3871170fe5c8SBarry Smith 
3872170fe5c8SBarry Smith #undef __FUNCT__
3873170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3874170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3875170fe5c8SBarry Smith {
3876170fe5c8SBarry Smith   PetscErrorCode ierr;
3877d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3878170fe5c8SBarry Smith   Mat            Cmat;
3879170fe5c8SBarry Smith 
3880170fe5c8SBarry Smith   PetscFunctionBegin;
388160e0710aSBarry 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);
3882ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
3883170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
388433d57670SJed Brown   ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr);
3885170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
38860298fd71SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
3887d73949e8SHong Zhang 
3888d73949e8SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
38892205254eSKarl Rupp 
3890170fe5c8SBarry Smith   *C = Cmat;
3891170fe5c8SBarry Smith   PetscFunctionReturn(0);
3892170fe5c8SBarry Smith }
3893170fe5c8SBarry Smith 
3894170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3895170fe5c8SBarry Smith #undef __FUNCT__
3896170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3897170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3898170fe5c8SBarry Smith {
3899170fe5c8SBarry Smith   PetscErrorCode ierr;
3900170fe5c8SBarry Smith 
3901170fe5c8SBarry Smith   PetscFunctionBegin;
3902170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
39033ff4c91cSHong Zhang     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3904170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
39053ff4c91cSHong Zhang     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3906170fe5c8SBarry Smith   }
39073ff4c91cSHong Zhang   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3908170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
39093ff4c91cSHong Zhang   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3910170fe5c8SBarry Smith   PetscFunctionReturn(0);
3911170fe5c8SBarry Smith }
3912170fe5c8SBarry Smith 
3913170fe5c8SBarry Smith 
39140bad9183SKris Buschelman /*MC
3915fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
39160bad9183SKris Buschelman    based on compressed sparse row format.
39170bad9183SKris Buschelman 
39180bad9183SKris Buschelman    Options Database Keys:
39190bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
39200bad9183SKris Buschelman 
39210bad9183SKris Buschelman   Level: beginner
39220bad9183SKris Buschelman 
3923f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
39240bad9183SKris Buschelman M*/
39250bad9183SKris Buschelman 
3926ccd284c7SBarry Smith /*MC
3927ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
3928ccd284c7SBarry Smith 
3929ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
3930ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
3931ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
3932ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3933ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3934ccd284c7SBarry Smith 
3935ccd284c7SBarry Smith    Options Database Keys:
3936ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
3937ccd284c7SBarry Smith 
3938ccd284c7SBarry Smith   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
3939ccd284c7SBarry Smith    enough exist.
3940ccd284c7SBarry Smith 
3941ccd284c7SBarry Smith   Level: beginner
3942ccd284c7SBarry Smith 
3943ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3944ccd284c7SBarry Smith M*/
3945ccd284c7SBarry Smith 
3946ccd284c7SBarry Smith /*MC
3947ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
3948ccd284c7SBarry Smith 
3949ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3950ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3951ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3952ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3953ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3954ccd284c7SBarry Smith 
3955ccd284c7SBarry Smith    Options Database Keys:
3956ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3957ccd284c7SBarry Smith 
3958ccd284c7SBarry Smith   Level: beginner
3959ccd284c7SBarry Smith 
3960ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3961ccd284c7SBarry Smith M*/
3962ccd284c7SBarry Smith 
39638cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3964af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
3965af8000cdSHong Zhang PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
3966af8000cdSHong Zhang #endif
3967b49cda9fSStefano Zampini PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);
396842c9c57cSBarry Smith 
3969b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
397029b38603SBarry Smith PETSC_EXTERN PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
397129b38603SBarry Smith PETSC_EXTERN PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3972b3866ffcSBarry Smith #endif
397317667f90SBarry Smith 
3974c0c8ee5eSDmitry Karpeev 
39758c778c55SBarry Smith #undef __FUNCT__
39768c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray"
39778c778c55SBarry Smith /*@C
39788c778c55SBarry Smith    MatSeqAIJGetArray - gives access to the array where the data for a SeqSeqAIJ matrix is stored
39798c778c55SBarry Smith 
39808c778c55SBarry Smith    Not Collective
39818c778c55SBarry Smith 
39828c778c55SBarry Smith    Input Parameter:
3983579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
39848c778c55SBarry Smith 
39858c778c55SBarry Smith    Output Parameter:
39868c778c55SBarry Smith .   array - pointer to the data
39878c778c55SBarry Smith 
39888c778c55SBarry Smith    Level: intermediate
39898c778c55SBarry Smith 
3990774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
39918c778c55SBarry Smith @*/
39928c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
39938c778c55SBarry Smith {
39948c778c55SBarry Smith   PetscErrorCode ierr;
39958c778c55SBarry Smith 
39968c778c55SBarry Smith   PetscFunctionBegin;
39978c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
39988c778c55SBarry Smith   PetscFunctionReturn(0);
39998c778c55SBarry Smith }
40008c778c55SBarry Smith 
40018c778c55SBarry Smith #undef __FUNCT__
400221e72a00SBarry Smith #define __FUNCT__ "MatSeqAIJGetMaxRowNonzeros"
400321e72a00SBarry Smith /*@C
400421e72a00SBarry Smith    MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
400521e72a00SBarry Smith 
400621e72a00SBarry Smith    Not Collective
400721e72a00SBarry Smith 
400821e72a00SBarry Smith    Input Parameter:
4009579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
401021e72a00SBarry Smith 
401121e72a00SBarry Smith    Output Parameter:
401221e72a00SBarry Smith .   nz - the maximum number of nonzeros in any row
401321e72a00SBarry Smith 
401421e72a00SBarry Smith    Level: intermediate
401521e72a00SBarry Smith 
401621e72a00SBarry Smith .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
401721e72a00SBarry Smith @*/
401821e72a00SBarry Smith PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
401921e72a00SBarry Smith {
402021e72a00SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
402121e72a00SBarry Smith 
402221e72a00SBarry Smith   PetscFunctionBegin;
402321e72a00SBarry Smith   *nz = aij->rmax;
402421e72a00SBarry Smith   PetscFunctionReturn(0);
402521e72a00SBarry Smith }
402621e72a00SBarry Smith 
402721e72a00SBarry Smith #undef __FUNCT__
40288c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray"
40298c778c55SBarry Smith /*@C
4030579dbff0SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray()
40318c778c55SBarry Smith 
40328c778c55SBarry Smith    Not Collective
40338c778c55SBarry Smith 
40348c778c55SBarry Smith    Input Parameters:
4035579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
40368c778c55SBarry Smith .  array - pointer to the data
40378c778c55SBarry Smith 
40388c778c55SBarry Smith    Level: intermediate
40398c778c55SBarry Smith 
4040774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
40418c778c55SBarry Smith @*/
40428c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
40438c778c55SBarry Smith {
40448c778c55SBarry Smith   PetscErrorCode ierr;
40458c778c55SBarry Smith 
40468c778c55SBarry Smith   PetscFunctionBegin;
40478c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
40488c778c55SBarry Smith   PetscFunctionReturn(0);
40498c778c55SBarry Smith }
40508c778c55SBarry Smith 
40514a2ae208SSatish Balay #undef __FUNCT__
40524a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
40538cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4054273d9f13SBarry Smith {
4055273d9f13SBarry Smith   Mat_SeqAIJ     *b;
4056dfbe8321SBarry Smith   PetscErrorCode ierr;
405738baddfdSBarry Smith   PetscMPIInt    size;
4058273d9f13SBarry Smith 
4059273d9f13SBarry Smith   PetscFunctionBegin;
4060ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
4061e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
4062273d9f13SBarry Smith 
4063b00a9115SJed Brown   ierr = PetscNewLog(B,&b);CHKERRQ(ierr);
40642205254eSKarl Rupp 
4065b0a32e0cSBarry Smith   B->data = (void*)b;
40662205254eSKarl Rupp 
4067549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
40682205254eSKarl Rupp 
4069416022c9SBarry Smith   b->row                = 0;
4070416022c9SBarry Smith   b->col                = 0;
407182bf6240SBarry Smith   b->icol               = 0;
4072b810aeb4SBarry Smith   b->reallocs           = 0;
407336db0b34SBarry Smith   b->ignorezeroentries  = PETSC_FALSE;
4074f1e2ffcdSBarry Smith   b->roworiented        = PETSC_TRUE;
4075416022c9SBarry Smith   b->nonew              = 0;
4076416022c9SBarry Smith   b->diag               = 0;
4077416022c9SBarry Smith   b->solve_work         = 0;
40782a1b7f2aSHong Zhang   B->spptr              = 0;
4079be6bf707SBarry Smith   b->saved_values       = 0;
4080d7f994e1SBarry Smith   b->idiag              = 0;
408171f1c65dSBarry Smith   b->mdiag              = 0;
408271f1c65dSBarry Smith   b->ssor_work          = 0;
408371f1c65dSBarry Smith   b->omega              = 1.0;
408471f1c65dSBarry Smith   b->fshift             = 0.0;
408571f1c65dSBarry Smith   b->idiagvalid         = PETSC_FALSE;
4086bbead8a2SBarry Smith   b->ibdiagvalid        = PETSC_FALSE;
4087a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
408817ab2063SBarry Smith 
408935d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
4090bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
4091bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
40928c778c55SBarry Smith 
4093b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4094bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
4095bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
4096b3866ffcSBarry Smith #endif
409717f1a0eaSHong Zhang 
4098bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
4099bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
4100bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
4101bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
4102bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
4103bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
4104bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
4105af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
4106af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);CHKERRQ(ierr);
4107af8000cdSHong Zhang #endif
4108b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);CHKERRQ(ierr);
4109bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4110bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4111bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
4112bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
4113bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
4114bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
4115bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
4116bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
41174108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
411817667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
41193a40ed3dSBarry Smith   PetscFunctionReturn(0);
412017ab2063SBarry Smith }
412117ab2063SBarry Smith 
41224a2ae208SSatish Balay #undef __FUNCT__
4123b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
4124b24902e0SBarry Smith /*
4125b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4126b24902e0SBarry Smith */
4127ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
412817ab2063SBarry Smith {
4129416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
41306849ba73SBarry Smith   PetscErrorCode ierr;
4131d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
413217ab2063SBarry Smith 
41333a40ed3dSBarry Smith   PetscFunctionBegin;
4134273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4135273d9f13SBarry Smith 
4136d5f3da31SBarry Smith   C->factortype = A->factortype;
4137416022c9SBarry Smith   c->row        = 0;
4138416022c9SBarry Smith   c->col        = 0;
413982bf6240SBarry Smith   c->icol       = 0;
41406ad4291fSHong Zhang   c->reallocs   = 0;
414117ab2063SBarry Smith 
41426ad4291fSHong Zhang   C->assembled = PETSC_TRUE;
414317ab2063SBarry Smith 
4144aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4145aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4146eec197d1SBarry Smith 
4147dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&c->imax,m,&c->ilen);CHKERRQ(ierr);
41483bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
414917ab2063SBarry Smith   for (i=0; i<m; i++) {
4150416022c9SBarry Smith     c->imax[i] = a->imax[i];
4151416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
415217ab2063SBarry Smith   }
415317ab2063SBarry Smith 
415417ab2063SBarry Smith   /* allocate the matrix space */
4155f77e22a1SHong Zhang   if (mallocmatspace) {
4156dcca6d9dSJed Brown     ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr);
41573bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
41582205254eSKarl Rupp 
4159f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
41602205254eSKarl Rupp 
416197f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
416217ab2063SBarry Smith     if (m > 0) {
416397f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4164be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4165bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4166be6bf707SBarry Smith       } else {
4167bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
416817ab2063SBarry Smith       }
416908480c60SBarry Smith     }
4170f77e22a1SHong Zhang   }
417117ab2063SBarry Smith 
41726ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4173416022c9SBarry Smith   c->roworiented       = a->roworiented;
4174416022c9SBarry Smith   c->nonew             = a->nonew;
4175416022c9SBarry Smith   if (a->diag) {
4176854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&c->diag);CHKERRQ(ierr);
41773bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
417817ab2063SBarry Smith     for (i=0; i<m; i++) {
4179416022c9SBarry Smith       c->diag[i] = a->diag[i];
418017ab2063SBarry Smith     }
41813a40ed3dSBarry Smith   } else c->diag = 0;
41822205254eSKarl Rupp 
41836ad4291fSHong Zhang   c->solve_work         = 0;
41846ad4291fSHong Zhang   c->saved_values       = 0;
41856ad4291fSHong Zhang   c->idiag              = 0;
418671f1c65dSBarry Smith   c->ssor_work          = 0;
4187a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
4188e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
4189e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
41906ad4291fSHong Zhang 
4191893ad86cSHong Zhang   c->rmax         = a->rmax;
4192416022c9SBarry Smith   c->nz           = a->nz;
41938ed568f8SMatthew G Knepley   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4194273d9f13SBarry Smith   C->preallocated = PETSC_TRUE;
4195754ec7b1SSatish Balay 
41966ad4291fSHong Zhang   c->compressedrow.use   = a->compressedrow.use;
41976ad4291fSHong Zhang   c->compressedrow.nrows = a->compressedrow.nrows;
4198cd6b891eSBarry Smith   if (a->compressedrow.use) {
41996ad4291fSHong Zhang     i    = a->compressedrow.nrows;
4200dcca6d9dSJed Brown     ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr);
42016ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
42026ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
420327ea64f8SHong Zhang   } else {
420427ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
42050298fd71SBarry Smith     c->compressedrow.i      = NULL;
42060298fd71SBarry Smith     c->compressedrow.rindex = NULL;
42076ad4291fSHong Zhang   }
4208ea632784SBarry Smith   c->nonzerorowcnt = a->nonzerorowcnt;
4209e56f5c9eSBarry Smith   C->nonzerostate  = A->nonzerostate;
42104846f1f5SKris Buschelman 
42112205254eSKarl Rupp   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4212140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
42133a40ed3dSBarry Smith   PetscFunctionReturn(0);
421417ab2063SBarry Smith }
421517ab2063SBarry Smith 
42164a2ae208SSatish Balay #undef __FUNCT__
4217b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
4218b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4219b24902e0SBarry Smith {
4220b24902e0SBarry Smith   PetscErrorCode ierr;
4221b24902e0SBarry Smith 
4222b24902e0SBarry Smith   PetscFunctionBegin;
4223ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
42244b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4225cfd3f464SBarry Smith   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
422633d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
4227cfd3f464SBarry Smith   }
4228a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4229f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4230b24902e0SBarry Smith   PetscFunctionReturn(0);
4231b24902e0SBarry Smith }
4232b24902e0SBarry Smith 
4233b24902e0SBarry Smith #undef __FUNCT__
42344a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
4235112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4236fbdbba38SShri Abhyankar {
4237fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4238fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4239fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4240fbdbba38SShri Abhyankar   int            fd;
4241fbdbba38SShri Abhyankar   PetscMPIInt    size;
4242fbdbba38SShri Abhyankar   MPI_Comm       comm;
42433059b6faSBarry Smith   PetscInt       bs = newMat->rmap->bs;
4244fbdbba38SShri Abhyankar 
4245fbdbba38SShri Abhyankar   PetscFunctionBegin;
4246c98fd787SBarry Smith   /* force binary viewer to load .info file if it has not yet done so */
4247c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
4248fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4249fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4250fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4251bbead8a2SBarry Smith 
42520298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
42530298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
4254bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
42553059b6faSBarry Smith   if (bs < 0) bs = 1;
42563059b6faSBarry Smith   ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);
4257bbead8a2SBarry Smith 
4258fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4259fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4260fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4261fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4262fbdbba38SShri Abhyankar 
4263bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4264fbdbba38SShri Abhyankar 
4265fbdbba38SShri Abhyankar   /* read in row lengths */
4266785e854fSJed Brown   ierr = PetscMalloc1(M,&rowlengths);CHKERRQ(ierr);
4267fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4268fbdbba38SShri Abhyankar 
4269fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4270fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
427160e0710aSBarry 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);
4272fbdbba38SShri Abhyankar 
4273fbdbba38SShri Abhyankar   /* set global size if not set already*/
4274f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4275fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4276aabbc4fbSShri Abhyankar   } else {
42779d36ed5fSBarry Smith     /* if sizes and type are already set, check if the matrix  global sizes are correct */
4278fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
42794c5b953cSHong Zhang     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
42804c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
42814c5b953cSHong Zhang     }
428260e0710aSBarry 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);
4283aabbc4fbSShri Abhyankar   }
4284fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4285fbdbba38SShri Abhyankar   a    = (Mat_SeqAIJ*)newMat->data;
4286fbdbba38SShri Abhyankar 
4287fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4288fbdbba38SShri Abhyankar 
4289fbdbba38SShri Abhyankar   /* read in nonzero values */
4290fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4291fbdbba38SShri Abhyankar 
4292fbdbba38SShri Abhyankar   /* set matrix "i" values */
4293fbdbba38SShri Abhyankar   a->i[0] = 0;
4294fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4295fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4296fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4297fbdbba38SShri Abhyankar   }
4298fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4299fbdbba38SShri Abhyankar 
4300fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4301fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4302fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4303fbdbba38SShri Abhyankar }
4304fbdbba38SShri Abhyankar 
4305fbdbba38SShri Abhyankar #undef __FUNCT__
4306b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4307ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
43087264ac53SSatish Balay {
43097264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4310dfbe8321SBarry Smith   PetscErrorCode ierr;
4311eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4312eeffb40dSHong Zhang   PetscInt k;
4313eeffb40dSHong Zhang #endif
43147264ac53SSatish Balay 
43153a40ed3dSBarry Smith   PetscFunctionBegin;
4316bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4317d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4318ca44d042SBarry Smith     *flg = PETSC_FALSE;
4319ca44d042SBarry Smith     PetscFunctionReturn(0);
4320bcd2baecSBarry Smith   }
43217264ac53SSatish Balay 
43227264ac53SSatish Balay   /* if the a->i are the same */
4323d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4324abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
43257264ac53SSatish Balay 
43267264ac53SSatish Balay   /* if a->j are the same */
432797f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4328abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4329bcd2baecSBarry Smith 
4330bcd2baecSBarry Smith   /* if a->a are the same */
4331eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4332eeffb40dSHong Zhang   for (k=0; k<a->nz; k++) {
4333eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4334eeffb40dSHong Zhang       *flg = PETSC_FALSE;
43353a40ed3dSBarry Smith       PetscFunctionReturn(0);
4336eeffb40dSHong Zhang     }
4337eeffb40dSHong Zhang   }
4338eeffb40dSHong Zhang #else
4339eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4340eeffb40dSHong Zhang #endif
4341eeffb40dSHong Zhang   PetscFunctionReturn(0);
43427264ac53SSatish Balay }
434336db0b34SBarry Smith 
43444a2ae208SSatish Balay #undef __FUNCT__
43454a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
434605869f15SSatish Balay /*@
434736db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
434836db0b34SBarry Smith               provided by the user.
434936db0b34SBarry Smith 
4350c75a6043SHong Zhang       Collective on MPI_Comm
435136db0b34SBarry Smith 
435236db0b34SBarry Smith    Input Parameters:
435336db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
435436db0b34SBarry Smith .   m - number of rows
435536db0b34SBarry Smith .   n - number of columns
435636db0b34SBarry Smith .   i - row indices
435736db0b34SBarry Smith .   j - column indices
435836db0b34SBarry Smith -   a - matrix values
435936db0b34SBarry Smith 
436036db0b34SBarry Smith    Output Parameter:
436136db0b34SBarry Smith .   mat - the matrix
436236db0b34SBarry Smith 
436336db0b34SBarry Smith    Level: intermediate
436436db0b34SBarry Smith 
436536db0b34SBarry Smith    Notes:
43660551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4367292fb18eSBarry Smith     once the matrix is destroyed and not before
436836db0b34SBarry Smith 
436936db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
437036db0b34SBarry Smith 
4371bfeeae90SHong Zhang        The i and j indices are 0 based
437236db0b34SBarry Smith 
4373a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4374a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
4375a4552177SSatish Balay     as shown:
4376a4552177SSatish Balay 
4377a4552177SSatish Balay         1 0 0
4378a4552177SSatish Balay         2 0 3
4379a4552177SSatish Balay         4 5 6
4380a4552177SSatish Balay 
4381a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
43829985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4383a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
4384a4552177SSatish Balay 
43859985e31cSBarry Smith 
438669b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
438736db0b34SBarry Smith 
438836db0b34SBarry Smith @*/
43897087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
439036db0b34SBarry Smith {
4391dfbe8321SBarry Smith   PetscErrorCode ierr;
4392cbcfb4deSHong Zhang   PetscInt       ii;
439336db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4394cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4395cbcfb4deSHong Zhang   PetscInt jj;
4396cbcfb4deSHong Zhang #endif
439736db0b34SBarry Smith 
439836db0b34SBarry Smith   PetscFunctionBegin;
4399f23aa3ddSBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4400f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4401f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4402a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4403ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4404ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4405ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4406dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&aij->imax,m,&aij->ilen);CHKERRQ(ierr);
4407ab93d7beSBarry Smith 
440836db0b34SBarry Smith   aij->i            = i;
440936db0b34SBarry Smith   aij->j            = j;
441036db0b34SBarry Smith   aij->a            = a;
441136db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
441236db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4413e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4414e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
441536db0b34SBarry Smith 
441636db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
441736db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
44182515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
441960e0710aSBarry 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]);
44209985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4421e32f2f54SBarry 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);
4422e32f2f54SBarry 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);
44239985e31cSBarry Smith     }
442436db0b34SBarry Smith #endif
442536db0b34SBarry Smith   }
44262515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
442736db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
442860e0710aSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
442960e0710aSBarry 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]);
443036db0b34SBarry Smith   }
443136db0b34SBarry Smith #endif
443236db0b34SBarry Smith 
4433b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4434b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
443536db0b34SBarry Smith   PetscFunctionReturn(0);
443636db0b34SBarry Smith }
44378a0b0e6bSVictor Minden #undef __FUNCT__
44388a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
443980ef6e79SMatthew G Knepley /*@C
4440d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
44418a0b0e6bSVictor Minden               provided by the user.
44428a0b0e6bSVictor Minden 
44438a0b0e6bSVictor Minden       Collective on MPI_Comm
44448a0b0e6bSVictor Minden 
44458a0b0e6bSVictor Minden    Input Parameters:
44468a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
44478a0b0e6bSVictor Minden .   m   - number of rows
44488a0b0e6bSVictor Minden .   n   - number of columns
44498a0b0e6bSVictor Minden .   i   - row indices
44508a0b0e6bSVictor Minden .   j   - column indices
44511230e6d1SVictor Minden .   a   - matrix values
44521230e6d1SVictor Minden .   nz  - number of nonzeros
44531230e6d1SVictor Minden -   idx - 0 or 1 based
44548a0b0e6bSVictor Minden 
44558a0b0e6bSVictor Minden    Output Parameter:
44568a0b0e6bSVictor Minden .   mat - the matrix
44578a0b0e6bSVictor Minden 
44588a0b0e6bSVictor Minden    Level: intermediate
44598a0b0e6bSVictor Minden 
44608a0b0e6bSVictor Minden    Notes:
44618a0b0e6bSVictor Minden        The i and j indices are 0 based
44628a0b0e6bSVictor Minden 
44638a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
44648a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
44658a0b0e6bSVictor Minden     as shown:
44668a0b0e6bSVictor Minden 
44678a0b0e6bSVictor Minden         1 0 0
44688a0b0e6bSVictor Minden         2 0 3
44698a0b0e6bSVictor Minden         4 5 6
44708a0b0e6bSVictor Minden 
44718a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
44728a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
44738a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
44748a0b0e6bSVictor Minden 
44758a0b0e6bSVictor Minden 
447669b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
44778a0b0e6bSVictor Minden 
44788a0b0e6bSVictor Minden @*/
44791230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
44808a0b0e6bSVictor Minden {
44818a0b0e6bSVictor Minden   PetscErrorCode ierr;
4482d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
44838a0b0e6bSVictor Minden 
44848a0b0e6bSVictor Minden 
44858a0b0e6bSVictor Minden   PetscFunctionBegin;
44861795a4d1SJed Brown   ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr);
44871230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
4488c8d679ebSHong Zhang     nnz[i[ii] - !!idx] += 1;
44891230e6d1SVictor Minden   }
44908a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
44918a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
44928a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
44931230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
44941230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
44951230e6d1SVictor Minden     if (idx) {
44961230e6d1SVictor Minden       row = i[ii] - 1;
44971230e6d1SVictor Minden       col = j[ii] - 1;
44981230e6d1SVictor Minden     } else {
44991230e6d1SVictor Minden       row = i[ii];
45001230e6d1SVictor Minden       col = j[ii];
45018a0b0e6bSVictor Minden     }
45021230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
45038a0b0e6bSVictor Minden   }
45048a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
45058a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4506d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
45078a0b0e6bSVictor Minden   PetscFunctionReturn(0);
45088a0b0e6bSVictor Minden }
450936db0b34SBarry Smith 
4510cc8ba8e1SBarry Smith #undef __FUNCT__
4511ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4512dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4513cc8ba8e1SBarry Smith {
4514dfbe8321SBarry Smith   PetscErrorCode ierr;
4515cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
451636db0b34SBarry Smith 
4517cc8ba8e1SBarry Smith   PetscFunctionBegin;
45188ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4519cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4520cc8ba8e1SBarry Smith     a->coloring = coloring;
452112c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
452297f1f81fSBarry Smith     PetscInt        i,*larray;
452312c595b3SBarry Smith     ISColoring      ocoloring;
452408b6dcc0SBarry Smith     ISColoringValue *colors;
452512c595b3SBarry Smith 
452612c595b3SBarry Smith     /* set coloring for diagonal portion */
4527785e854fSJed Brown     ierr = PetscMalloc1(A->cmap->n,&larray);CHKERRQ(ierr);
45282205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) larray[i] = i;
45290298fd71SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);CHKERRQ(ierr);
4530785e854fSJed Brown     ierr = PetscMalloc1(A->cmap->n,&colors);CHKERRQ(ierr);
45312205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
453212c595b3SBarry Smith     ierr        = PetscFree(larray);CHKERRQ(ierr);
4533aaf3ff59SMatthew G. Knepley     ierr        = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,PETSC_OWN_POINTER,&ocoloring);CHKERRQ(ierr);
453412c595b3SBarry Smith     a->coloring = ocoloring;
453512c595b3SBarry Smith   }
4536cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4537cc8ba8e1SBarry Smith }
4538cc8ba8e1SBarry Smith 
4539ee4f033dSBarry Smith #undef __FUNCT__
4540ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
454197f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4542ee4f033dSBarry Smith {
4543ee4f033dSBarry Smith   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
4544d0f46423SBarry Smith   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
454554f21887SBarry Smith   MatScalar       *v      = a->a;
454654f21887SBarry Smith   PetscScalar     *values = (PetscScalar*)advalues;
454708b6dcc0SBarry Smith   ISColoringValue *color;
4548ee4f033dSBarry Smith 
4549ee4f033dSBarry Smith   PetscFunctionBegin;
4550e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4551ee4f033dSBarry Smith   color = a->coloring->colors;
4552ee4f033dSBarry Smith   /* loop over rows */
4553ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4554ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4555ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
45562205254eSKarl Rupp     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
4557ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4558cc8ba8e1SBarry Smith   }
4559cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4560cc8ba8e1SBarry Smith }
456136db0b34SBarry Smith 
4562acf2f550SJed Brown #undef __FUNCT__
4563acf2f550SJed Brown #define __FUNCT__ "MatSeqAIJInvalidateDiagonal"
4564acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4565acf2f550SJed Brown {
4566acf2f550SJed Brown   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4567acf2f550SJed Brown   PetscErrorCode ierr;
4568acf2f550SJed Brown 
4569acf2f550SJed Brown   PetscFunctionBegin;
4570acf2f550SJed Brown   a->idiagvalid  = PETSC_FALSE;
4571acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
45722205254eSKarl Rupp 
4573acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4574acf2f550SJed Brown   PetscFunctionReturn(0);
4575acf2f550SJed Brown }
4576acf2f550SJed Brown 
45779c8f2541SHong Zhang #undef __FUNCT__
45789c8f2541SHong Zhang #define __FUNCT__ "MatCreateMPIMatConcatenateSeqMat_SeqAIJ"
45799c8f2541SHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
45809c8f2541SHong Zhang {
45819c8f2541SHong Zhang   PetscErrorCode ierr;
45829c8f2541SHong Zhang 
45839c8f2541SHong Zhang   PetscFunctionBegin;
45849c8f2541SHong Zhang   ierr = MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr);
45859c8f2541SHong Zhang   PetscFunctionReturn(0);
45869c8f2541SHong Zhang }
45879c8f2541SHong Zhang 
458881824310SBarry Smith /*
4589*53dd7562SDmitry Karpeev  Permute A into C's *local* index space using rowemb,colemb.
4590*53dd7562SDmitry Karpeev  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
4591*53dd7562SDmitry Karpeev  of [0,m), colemb is in [0,n).
4592*53dd7562SDmitry Karpeev  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
4593*53dd7562SDmitry Karpeev  */
4594*53dd7562SDmitry Karpeev #undef __FUNCT__
4595*53dd7562SDmitry Karpeev #define __FUNCT__ "MatSetSeqMat_SeqAIJ"
4596*53dd7562SDmitry Karpeev PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
4597*53dd7562SDmitry Karpeev {
4598*53dd7562SDmitry Karpeev   /* If making this function public, change the error returned in this function away from _PLIB. */
4599*53dd7562SDmitry Karpeev   PetscErrorCode ierr;
4600*53dd7562SDmitry Karpeev   Mat_SeqAIJ     *Baij;
4601*53dd7562SDmitry Karpeev   PetscBool      seqaij;
4602*53dd7562SDmitry Karpeev   PetscInt       m,n,*nz,i,j,count;
4603*53dd7562SDmitry Karpeev   PetscScalar    v;
4604*53dd7562SDmitry Karpeev   const PetscInt *rowindices,*colindices;
4605*53dd7562SDmitry Karpeev 
4606*53dd7562SDmitry Karpeev   PetscFunctionBegin;
4607*53dd7562SDmitry Karpeev   if (!B) PetscFunctionReturn(0);
4608*53dd7562SDmitry Karpeev   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
4609*53dd7562SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);CHKERRQ(ierr);
4610*53dd7562SDmitry Karpeev   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
4611*53dd7562SDmitry Karpeev   if (rowemb) {
4612*53dd7562SDmitry Karpeev     ierr = ISGetLocalSize(rowemb,&m);CHKERRQ(ierr);
4613*53dd7562SDmitry 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);
4614*53dd7562SDmitry Karpeev   } else {
4615*53dd7562SDmitry Karpeev     if (C->rmap->n != B->rmap->n) {
4616*53dd7562SDmitry Karpeev       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
4617*53dd7562SDmitry Karpeev     }
4618*53dd7562SDmitry Karpeev   }
4619*53dd7562SDmitry Karpeev   if (colemb) {
4620*53dd7562SDmitry Karpeev     ierr = ISGetLocalSize(colemb,&n);CHKERRQ(ierr);
4621*53dd7562SDmitry 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);
4622*53dd7562SDmitry Karpeev   } else {
4623*53dd7562SDmitry Karpeev     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
4624*53dd7562SDmitry Karpeev   }
4625*53dd7562SDmitry Karpeev 
4626*53dd7562SDmitry Karpeev   Baij = (Mat_SeqAIJ*)(B->data);
4627*53dd7562SDmitry Karpeev   if (pattern == DIFFERENT_NONZERO_PATTERN) {
4628*53dd7562SDmitry Karpeev     ierr = PetscMalloc1(B->rmap->n,&nz);CHKERRQ(ierr);
4629*53dd7562SDmitry Karpeev     for (i=0; i<B->rmap->n; i++) {
4630*53dd7562SDmitry Karpeev       nz[i] = Baij->i[i+1] - Baij->i[i];
4631*53dd7562SDmitry Karpeev     }
4632*53dd7562SDmitry Karpeev     ierr = MatSeqAIJSetPreallocation(C,0,nz);CHKERRQ(ierr);
4633*53dd7562SDmitry Karpeev     ierr = PetscFree(nz);CHKERRQ(ierr);
4634*53dd7562SDmitry Karpeev   }
4635*53dd7562SDmitry Karpeev   if (pattern == SUBSET_NONZERO_PATTERN) {
4636*53dd7562SDmitry Karpeev     ierr = MatZeroEntries(C);CHKERRQ(ierr);
4637*53dd7562SDmitry Karpeev   }
4638*53dd7562SDmitry Karpeev   count = 0;
4639*53dd7562SDmitry Karpeev   rowindices = NULL;
4640*53dd7562SDmitry Karpeev   colindices = NULL;
4641*53dd7562SDmitry Karpeev   if (rowemb) {
4642*53dd7562SDmitry Karpeev     ierr = ISGetIndices(rowemb,&rowindices);CHKERRQ(ierr);
4643*53dd7562SDmitry Karpeev   }
4644*53dd7562SDmitry Karpeev   if (colemb) {
4645*53dd7562SDmitry Karpeev     ierr = ISGetIndices(colemb,&colindices);CHKERRQ(ierr);
4646*53dd7562SDmitry Karpeev   }
4647*53dd7562SDmitry Karpeev   for (i=0; i<B->rmap->n; i++) {
4648*53dd7562SDmitry Karpeev     PetscInt row;
4649*53dd7562SDmitry Karpeev     row = i;
4650*53dd7562SDmitry Karpeev     if (rowindices) row = rowindices[i];
4651*53dd7562SDmitry Karpeev     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
4652*53dd7562SDmitry Karpeev       PetscInt col;
4653*53dd7562SDmitry Karpeev       col  = Baij->j[count];
4654*53dd7562SDmitry Karpeev       if (colindices) col = colindices[col];
4655*53dd7562SDmitry Karpeev       v    = Baij->a[count];
4656*53dd7562SDmitry Karpeev       ierr = MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);CHKERRQ(ierr);
4657*53dd7562SDmitry Karpeev       ++count;
4658*53dd7562SDmitry Karpeev     }
4659*53dd7562SDmitry Karpeev   }
4660*53dd7562SDmitry Karpeev   /* FIXME: set C's nonzerostate correctly. */
4661*53dd7562SDmitry Karpeev   /* Assembly for C is necessary. */
4662*53dd7562SDmitry Karpeev   C->preallocated = PETSC_TRUE;
4663*53dd7562SDmitry Karpeev   C->assembled     = PETSC_TRUE;
4664*53dd7562SDmitry Karpeev   C->was_assembled = PETSC_FALSE;
4665*53dd7562SDmitry Karpeev   PetscFunctionReturn(0);
4666*53dd7562SDmitry Karpeev }
4667*53dd7562SDmitry Karpeev 
4668*53dd7562SDmitry Karpeev 
4669*53dd7562SDmitry Karpeev /*
467081824310SBarry Smith     Special version for direct calls from Fortran
467181824310SBarry Smith */
4672af0996ceSBarry Smith #include <petsc/private/fortranimpl.h>
467381824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
467481824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
467581824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
467681824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
467781824310SBarry Smith #endif
467881824310SBarry Smith 
467981824310SBarry Smith /* Change these macros so can be used in void function */
468081824310SBarry Smith #undef CHKERRQ
4681ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
468281824310SBarry Smith #undef SETERRQ2
4683e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
46844994cf47SJed Brown #undef SETERRQ3
46854994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
468681824310SBarry Smith 
468781824310SBarry Smith #undef __FUNCT__
468881824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
46898cc058d9SJed 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)
469081824310SBarry Smith {
469181824310SBarry Smith   Mat            A  = *AA;
469281824310SBarry Smith   PetscInt       m  = *mm, n = *nn;
469381824310SBarry Smith   InsertMode     is = *isis;
469481824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
469581824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
469681824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
469781824310SBarry Smith   PetscErrorCode ierr;
469881824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
469954f21887SBarry Smith   MatScalar      *ap,value,*aa;
4700ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4701ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
470281824310SBarry Smith 
470381824310SBarry Smith   PetscFunctionBegin;
47044994cf47SJed Brown   MatCheckPreallocated(A,1);
470581824310SBarry Smith   imax  = a->imax;
470681824310SBarry Smith   ai    = a->i;
470781824310SBarry Smith   ailen = a->ilen;
470881824310SBarry Smith   aj    = a->j;
470981824310SBarry Smith   aa    = a->a;
471081824310SBarry Smith 
471181824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
471281824310SBarry Smith     row = im[k];
471381824310SBarry Smith     if (row < 0) continue;
471481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4715ce94432eSBarry Smith     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
471681824310SBarry Smith #endif
471781824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
471881824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
471981824310SBarry Smith     low  = 0;
472081824310SBarry Smith     high = nrow;
472181824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
472281824310SBarry Smith       if (in[l] < 0) continue;
472381824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4724ce94432eSBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
472581824310SBarry Smith #endif
472681824310SBarry Smith       col = in[l];
47272205254eSKarl Rupp       if (roworiented) value = v[l + k*n];
47282205254eSKarl Rupp       else value = v[k + l*m];
47292205254eSKarl Rupp 
473081824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
473181824310SBarry Smith 
47322205254eSKarl Rupp       if (col <= lastcol) low = 0;
47332205254eSKarl Rupp       else high = nrow;
473481824310SBarry Smith       lastcol = col;
473581824310SBarry Smith       while (high-low > 5) {
473681824310SBarry Smith         t = (low+high)/2;
473781824310SBarry Smith         if (rp[t] > col) high = t;
473881824310SBarry Smith         else             low  = t;
473981824310SBarry Smith       }
474081824310SBarry Smith       for (i=low; i<high; i++) {
474181824310SBarry Smith         if (rp[i] > col) break;
474281824310SBarry Smith         if (rp[i] == col) {
474381824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
474481824310SBarry Smith           else                  ap[i] = value;
474581824310SBarry Smith           goto noinsert;
474681824310SBarry Smith         }
474781824310SBarry Smith       }
474881824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
474981824310SBarry Smith       if (nonew == 1) goto noinsert;
4750ce94432eSBarry Smith       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4751fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
475281824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
475381824310SBarry Smith       /* shift up all the later entries in this row */
475481824310SBarry Smith       for (ii=N; ii>=i; ii--) {
475581824310SBarry Smith         rp[ii+1] = rp[ii];
475681824310SBarry Smith         ap[ii+1] = ap[ii];
475781824310SBarry Smith       }
475881824310SBarry Smith       rp[i] = col;
475981824310SBarry Smith       ap[i] = value;
4760e56f5c9eSBarry Smith       A->nonzerostate++;
476181824310SBarry Smith noinsert:;
476281824310SBarry Smith       low = i + 1;
476381824310SBarry Smith     }
476481824310SBarry Smith     ailen[row] = nrow;
476581824310SBarry Smith   }
476681824310SBarry Smith   PetscFunctionReturnVoid();
476781824310SBarry Smith }
47689f7953f8SBarry Smith 
4769