xref: /petsc/src/mat/impls/aij/seq/aij.c (revision af8000cd6f32b2779e6083a43caae2e6d58d87d4)
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>
1106873bf2SBarry 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);
148785e854fSJed Brown   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 */
219785e854fSJed Brown     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;
224785e854fSJed Brown       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);
266785e854fSJed Brown     ierr = PetscMalloc1((n+1),&cia);CHKERRQ(ierr);
267785e854fSJed Brown     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);
326785e854fSJed Brown   ierr = PetscMalloc1((n+1),&cia);CHKERRQ(ierr);
327785e854fSJed Brown   ierr = PetscMalloc1((nz+1),&cja);CHKERRQ(ierr);
328785e854fSJed Brown   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 
390189e4007SBarry 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);
560785e854fSJed Brown   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);
661785e854fSJed Brown     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)
7563c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
7573c215bfdSMatthew Knepley #else
7583c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix 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 
1106a3ca3016SBarry Smith extern PetscErrorCode MatDestroy_Redundant(Mat_Redundant **);
110702b6de14SBarry Smith 
11084a2ae208SSatish Balay #undef __FUNCT__
11094a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
1110dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
111117ab2063SBarry Smith {
1112416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1113dfbe8321SBarry Smith   PetscErrorCode ierr;
1114d5d45c9bSBarry Smith 
11153a40ed3dSBarry Smith   PetscFunctionBegin;
1116aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1117d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
111817ab2063SBarry Smith #endif
1119a3ca3016SBarry Smith   ierr = MatDestroy_Redundant(&a->redundant);CHKERRQ(ierr);
1120e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
11216bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
11226bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
112305b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
1124d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
112505b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
112671f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
112705b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
11286bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
112905b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
11306bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
113105b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
11326bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
1133cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
11340b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
1135a30b2313SHong Zhang 
11364108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
1137bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
1138901853e0SKris Buschelman 
1139dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
1140bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
1141bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1142bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1143bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr);
1144bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr);
1145bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr);
1146*af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
1147*af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);CHKERRQ(ierr);
1148*af8000cdSHong Zhang #endif
1149bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1150bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1151bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1152bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr);
11533a40ed3dSBarry Smith   PetscFunctionReturn(0);
115417ab2063SBarry Smith }
115517ab2063SBarry Smith 
11564a2ae208SSatish Balay #undef __FUNCT__
11574a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
1158ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
115917ab2063SBarry Smith {
1160416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11614846f1f5SKris Buschelman   PetscErrorCode ierr;
11623a40ed3dSBarry Smith 
11633a40ed3dSBarry Smith   PetscFunctionBegin;
1164a65d3064SKris Buschelman   switch (op) {
1165a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
11664e0d8c25SBarry Smith     a->roworiented = flg;
1167a65d3064SKris Buschelman     break;
1168a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1169a9817697SBarry Smith     a->keepnonzeropattern = flg;
1170a65d3064SKris Buschelman     break;
1171512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1172512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1173a65d3064SKris Buschelman     break;
1174a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
11754e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1176a65d3064SKris Buschelman     break;
1177a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
11784e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1179a65d3064SKris Buschelman     break;
118028b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
118128b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
118228b2fa4aSMatthew Knepley     break;
1183a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11844e0d8c25SBarry Smith     a->ignorezeroentries = flg;
11850df259c2SBarry Smith     break;
11863d472b54SHong Zhang   case MAT_SPD:
1187b1646e73SJed Brown   case MAT_SYMMETRIC:
1188b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1189b1646e73SJed Brown   case MAT_HERMITIAN:
1190b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
11915021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
11925021d80fSJed Brown     break;
11934e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1194a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1195a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1196290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1197a65d3064SKris Buschelman     break;
1198b87ac2d8SJed Brown   case MAT_USE_INODES:
1199b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1200b87ac2d8SJed Brown     break;
1201a65d3064SKris Buschelman   default:
1202e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1203a65d3064SKris Buschelman   }
12044108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
12053a40ed3dSBarry Smith   PetscFunctionReturn(0);
120617ab2063SBarry Smith }
120717ab2063SBarry Smith 
12084a2ae208SSatish Balay #undef __FUNCT__
12094a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1210dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
121117ab2063SBarry Smith {
1212416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12136849ba73SBarry Smith   PetscErrorCode ierr;
1214d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
121535e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
121617ab2063SBarry Smith 
12173a40ed3dSBarry Smith   PetscFunctionBegin;
1218d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1219e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
122035e7444dSHong Zhang 
1221d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1222d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
122335e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
12242c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
122535e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
122635e7444dSHong Zhang     PetscFunctionReturn(0);
122735e7444dSHong Zhang   }
122835e7444dSHong Zhang 
12292dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
12301ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
123135e7444dSHong Zhang   for (i=0; i<n; i++) {
123235e7444dSHong Zhang     nz = ai[i+1] - ai[i];
12332f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
123435e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++) {
123535e7444dSHong Zhang       if (aj[j] == i) {
123635e7444dSHong Zhang         x[i] = aa[j];
123717ab2063SBarry Smith         break;
123817ab2063SBarry Smith       }
123917ab2063SBarry Smith     }
124017ab2063SBarry Smith   }
12411ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
12423a40ed3dSBarry Smith   PetscFunctionReturn(0);
124317ab2063SBarry Smith }
124417ab2063SBarry Smith 
1245c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
12464a2ae208SSatish Balay #undef __FUNCT__
12474a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1248dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
124917ab2063SBarry Smith {
1250416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12515c897100SBarry Smith   PetscScalar    *x,*y;
1252dfbe8321SBarry Smith   PetscErrorCode ierr;
1253d0f46423SBarry Smith   PetscInt       m = A->rmap->n;
12545c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1255a77337e4SBarry Smith   MatScalar         *v;
1256a77337e4SBarry Smith   PetscScalar       alpha;
12570298fd71SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=NULL;
12583447b6efSHong Zhang   Mat_CompressedRow cprow    = a->compressedrow;
1259ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
12605c897100SBarry Smith #endif
126117ab2063SBarry Smith 
12623a40ed3dSBarry Smith   PetscFunctionBegin;
12632e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
12641ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
12651ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
12665c897100SBarry Smith 
12675c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1268bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
12695c897100SBarry Smith #else
12703447b6efSHong Zhang   if (usecprow) {
12713447b6efSHong Zhang     m    = cprow.nrows;
12723447b6efSHong Zhang     ii   = cprow.i;
12737b2bb3b9SHong Zhang     ridx = cprow.rindex;
12743447b6efSHong Zhang   } else {
12753447b6efSHong Zhang     ii = a->i;
12763447b6efSHong Zhang   }
127717ab2063SBarry Smith   for (i=0; i<m; i++) {
12783447b6efSHong Zhang     idx = a->j + ii[i];
12793447b6efSHong Zhang     v   = a->a + ii[i];
12803447b6efSHong Zhang     n   = ii[i+1] - ii[i];
12813447b6efSHong Zhang     if (usecprow) {
12827b2bb3b9SHong Zhang       alpha = x[ridx[i]];
12833447b6efSHong Zhang     } else {
128417ab2063SBarry Smith       alpha = x[i];
12853447b6efSHong Zhang     }
128604fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
128717ab2063SBarry Smith   }
12885c897100SBarry Smith #endif
1289dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
12901ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12911ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12923a40ed3dSBarry Smith   PetscFunctionReturn(0);
129317ab2063SBarry Smith }
129417ab2063SBarry Smith 
12954a2ae208SSatish Balay #undef __FUNCT__
12965c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1297dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
12985c897100SBarry Smith {
1299dfbe8321SBarry Smith   PetscErrorCode ierr;
13005c897100SBarry Smith 
13015c897100SBarry Smith   PetscFunctionBegin;
1302170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
13035c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
13045c897100SBarry Smith   PetscFunctionReturn(0);
13055c897100SBarry Smith }
13065c897100SBarry Smith 
1307c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
130878b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
130978b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ_Kernel(PetscInt thread_id,Mat A,Vec xx,Vec yy)
131078b84d54SShri Abhyankar {
131178b84d54SShri Abhyankar   PetscErrorCode    ierr;
131278b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
131378b84d54SShri Abhyankar   PetscScalar       *y;
131478b84d54SShri Abhyankar   const PetscScalar *x;
131578b84d54SShri Abhyankar   const MatScalar   *aa;
131678b84d54SShri Abhyankar   PetscInt          *trstarts=A->rmap->trstarts;
131778b84d54SShri Abhyankar   PetscInt          n,start,end,i;
131878b84d54SShri Abhyankar   const PetscInt    *aj,*ai;
131978b84d54SShri Abhyankar   PetscScalar       sum;
132078b84d54SShri Abhyankar 
132178b84d54SShri Abhyankar   ierr  = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
132278b84d54SShri Abhyankar   ierr  = VecGetArray(yy,&y);CHKERRQ(ierr);
132378b84d54SShri Abhyankar   start = trstarts[thread_id];
132478b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
132578b84d54SShri Abhyankar   aj    = a->j;
132678b84d54SShri Abhyankar   aa    = a->a;
132778b84d54SShri Abhyankar   ai    = a->i;
132878b84d54SShri Abhyankar   for (i=start; i<end; i++) {
132978b84d54SShri Abhyankar     n   = ai[i+1] - ai[i];
133078b84d54SShri Abhyankar     aj  = a->j + ai[i];
133178b84d54SShri Abhyankar     aa  = a->a + ai[i];
133278b84d54SShri Abhyankar     sum = 0.0;
133378b84d54SShri Abhyankar     PetscSparseDensePlusDot(sum,x,aa,aj,n);
133478b84d54SShri Abhyankar     y[i] = sum;
133578b84d54SShri Abhyankar   }
133678b84d54SShri Abhyankar   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
133778b84d54SShri Abhyankar   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
133878b84d54SShri Abhyankar   return 0;
133978b84d54SShri Abhyankar }
134078b84d54SShri Abhyankar 
134178b84d54SShri Abhyankar #undef __FUNCT__
134278b84d54SShri Abhyankar #define __FUNCT__ "MatMult_SeqAIJ"
134378b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
134478b84d54SShri Abhyankar {
134578b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
134678b84d54SShri Abhyankar   PetscScalar       *y;
134778b84d54SShri Abhyankar   const PetscScalar *x;
134878b84d54SShri Abhyankar   const MatScalar   *aa;
134978b84d54SShri Abhyankar   PetscErrorCode    ierr;
135078b84d54SShri Abhyankar   PetscInt          m=A->rmap->n;
13510298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
13527b083b7cSBarry Smith   PetscInt          n,i;
135378b84d54SShri Abhyankar   PetscScalar       sum;
135478b84d54SShri Abhyankar   PetscBool         usecprow=a->compressedrow.use;
135578b84d54SShri Abhyankar 
135678b84d54SShri Abhyankar #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
135778b84d54SShri Abhyankar #pragma disjoint(*x,*y,*aa)
135878b84d54SShri Abhyankar #endif
135978b84d54SShri Abhyankar 
136078b84d54SShri Abhyankar   PetscFunctionBegin;
136178b84d54SShri Abhyankar   aj = a->j;
136278b84d54SShri Abhyankar   aa = a->a;
136378b84d54SShri Abhyankar   ii = a->i;
136478b84d54SShri Abhyankar   if (usecprow) { /* use compressed row format */
136578b84d54SShri Abhyankar     ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
136678b84d54SShri Abhyankar     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
13674f390cb1SBarry Smith     ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr);
136878b84d54SShri Abhyankar     m    = a->compressedrow.nrows;
136978b84d54SShri Abhyankar     ii   = a->compressedrow.i;
137078b84d54SShri Abhyankar     ridx = a->compressedrow.rindex;
137178b84d54SShri Abhyankar     for (i=0; i<m; i++) {
137278b84d54SShri Abhyankar       n           = ii[i+1] - ii[i];
137378b84d54SShri Abhyankar       aj          = a->j + ii[i];
137478b84d54SShri Abhyankar       aa          = a->a + ii[i];
137578b84d54SShri Abhyankar       sum         = 0.0;
137678b84d54SShri Abhyankar       PetscSparseDensePlusDot(sum,x,aa,aj,n);
137778b84d54SShri Abhyankar       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
137878b84d54SShri Abhyankar       y[*ridx++] = sum;
137978b84d54SShri Abhyankar     }
138078b84d54SShri Abhyankar     ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
138178b84d54SShri Abhyankar     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
138278b84d54SShri Abhyankar   } else { /* do not use compressed row format */
138378b84d54SShri Abhyankar #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
138478b84d54SShri Abhyankar     fortranmultaij_(&m,x,ii,aj,aa,y);
138578b84d54SShri Abhyankar #else
1386ce94432eSBarry Smith     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
138778b84d54SShri Abhyankar #endif
138878b84d54SShri Abhyankar   }
13897b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
139078b84d54SShri Abhyankar   PetscFunctionReturn(0);
139178b84d54SShri Abhyankar }
139278b84d54SShri Abhyankar #else
13935c897100SBarry Smith #undef __FUNCT__
13944a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1395dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
139617ab2063SBarry Smith {
1397416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1398d9fead3dSBarry Smith   PetscScalar       *y;
139954f21887SBarry Smith   const PetscScalar *x;
140054f21887SBarry Smith   const MatScalar   *aa;
1401dfbe8321SBarry Smith   PetscErrorCode    ierr;
1402003131ecSBarry Smith   PetscInt          m=A->rmap->n;
14030298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
14047b083b7cSBarry Smith   PetscInt          n,i;
1405362ced78SSatish Balay   PetscScalar       sum;
1406ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
140717ab2063SBarry Smith 
1408b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
140997952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1410fee21e36SBarry Smith #endif
1411fee21e36SBarry Smith 
14123a40ed3dSBarry Smith   PetscFunctionBegin;
14133649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
14141ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
141597952fefSHong Zhang   aj   = a->j;
141697952fefSHong Zhang   aa   = a->a;
1417416022c9SBarry Smith   ii   = a->i;
14184eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
14194f390cb1SBarry Smith     ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr);
142097952fefSHong Zhang     m    = a->compressedrow.nrows;
142197952fefSHong Zhang     ii   = a->compressedrow.i;
142297952fefSHong Zhang     ridx = a->compressedrow.rindex;
142397952fefSHong Zhang     for (i=0; i<m; i++) {
142497952fefSHong Zhang       n           = ii[i+1] - ii[i];
142597952fefSHong Zhang       aj          = a->j + ii[i];
142697952fefSHong Zhang       aa          = a->a + ii[i];
142797952fefSHong Zhang       sum         = 0.0;
1428003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1429003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
143097952fefSHong Zhang       y[*ridx++] = sum;
143197952fefSHong Zhang     }
143297952fefSHong Zhang   } else { /* do not use compressed row format */
1433b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1434b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1435b05257ddSBarry Smith #else
143678b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
1437ce94432eSBarry Smith     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
143878b84d54SShri Abhyankar #else
143917ab2063SBarry Smith     for (i=0; i<m; i++) {
1440003131ecSBarry Smith       n           = ii[i+1] - ii[i];
1441003131ecSBarry Smith       aj          = a->j + ii[i];
1442003131ecSBarry Smith       aa          = a->a + ii[i];
144317ab2063SBarry Smith       sum         = 0.0;
1444003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
144517ab2063SBarry Smith       y[i] = sum;
144617ab2063SBarry Smith     }
14478d195f9aSBarry Smith #endif
144878b84d54SShri Abhyankar #endif
1449b05257ddSBarry Smith   }
14507b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
14513649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
14521ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
14533a40ed3dSBarry Smith   PetscFunctionReturn(0);
145417ab2063SBarry Smith }
145578b84d54SShri Abhyankar #endif
145617ab2063SBarry Smith 
1457b434eb95SMatthew G. Knepley #undef __FUNCT__
1458b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultMax_SeqAIJ"
1459b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1460b434eb95SMatthew G. Knepley {
1461b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1462b434eb95SMatthew G. Knepley   PetscScalar       *y;
1463b434eb95SMatthew G. Knepley   const PetscScalar *x;
1464b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1465b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1466b434eb95SMatthew G. Knepley   PetscInt          m=A->rmap->n;
1467b434eb95SMatthew G. Knepley   const PetscInt    *aj,*ii,*ridx=NULL;
1468b434eb95SMatthew G. Knepley   PetscInt          n,i,nonzerorow=0;
1469b434eb95SMatthew G. Knepley   PetscScalar       sum;
1470b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1471b434eb95SMatthew G. Knepley 
1472b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1473b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa)
1474b434eb95SMatthew G. Knepley #endif
1475b434eb95SMatthew G. Knepley 
1476b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1477b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1478b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1479b434eb95SMatthew G. Knepley   aj   = a->j;
1480b434eb95SMatthew G. Knepley   aa   = a->a;
1481b434eb95SMatthew G. Knepley   ii   = a->i;
1482b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1483b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1484b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1485b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1486b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1487b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1488b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1489b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1490b434eb95SMatthew G. Knepley       sum         = 0.0;
1491b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1492b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1493b434eb95SMatthew G. Knepley       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1494b434eb95SMatthew G. Knepley       y[*ridx++] = sum;
1495b434eb95SMatthew G. Knepley     }
1496b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
1497b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1498b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1499b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1500b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1501b434eb95SMatthew G. Knepley       sum         = 0.0;
1502b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1503b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1504b434eb95SMatthew G. Knepley       y[i] = sum;
1505b434eb95SMatthew G. Knepley     }
1506b434eb95SMatthew G. Knepley   }
1507b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1508b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1509b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1510b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1511b434eb95SMatthew G. Knepley }
1512b434eb95SMatthew G. Knepley 
1513b434eb95SMatthew G. Knepley #undef __FUNCT__
1514b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultAddMax_SeqAIJ"
1515b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1516b434eb95SMatthew G. Knepley {
1517b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1518b434eb95SMatthew G. Knepley   PetscScalar       *y,*z;
1519b434eb95SMatthew G. Knepley   const PetscScalar *x;
1520b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1521b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1522b434eb95SMatthew G. Knepley   PetscInt          m = A->rmap->n,*aj,*ii;
1523b434eb95SMatthew G. Knepley   PetscInt          n,i,*ridx=NULL;
1524b434eb95SMatthew G. Knepley   PetscScalar       sum;
1525b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1526b434eb95SMatthew G. Knepley 
1527b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1528b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1529b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1530b434eb95SMatthew G. Knepley   if (zz != yy) {
1531b434eb95SMatthew G. Knepley     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
1532b434eb95SMatthew G. Knepley   } else {
1533b434eb95SMatthew G. Knepley     z = y;
1534b434eb95SMatthew G. Knepley   }
1535b434eb95SMatthew G. Knepley 
1536b434eb95SMatthew G. Knepley   aj = a->j;
1537b434eb95SMatthew G. Knepley   aa = a->a;
1538b434eb95SMatthew G. Knepley   ii = a->i;
1539b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1540b434eb95SMatthew G. Knepley     if (zz != yy) {
1541b434eb95SMatthew G. Knepley       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1542b434eb95SMatthew G. Knepley     }
1543b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1544b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1545b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1546b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1547b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1548b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1549b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1550b434eb95SMatthew G. Knepley       sum = y[*ridx];
1551b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1552b434eb95SMatthew G. Knepley       z[*ridx++] = sum;
1553b434eb95SMatthew G. Knepley     }
1554b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
1555b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1556b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1557b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1558b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1559b434eb95SMatthew G. Knepley       sum = y[i];
1560b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1561b434eb95SMatthew G. Knepley       z[i] = sum;
1562b434eb95SMatthew G. Knepley     }
1563b434eb95SMatthew G. Knepley   }
1564b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1565b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1566b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1567b434eb95SMatthew G. Knepley   if (zz != yy) {
1568b434eb95SMatthew G. Knepley     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
1569b434eb95SMatthew G. Knepley   }
1570b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1571b434eb95SMatthew G. Knepley }
1572b434eb95SMatthew G. Knepley 
1573c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
15744a2ae208SSatish Balay #undef __FUNCT__
15754a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1576dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
157717ab2063SBarry Smith {
1578416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1579f15663dcSBarry Smith   PetscScalar       *y,*z;
1580f15663dcSBarry Smith   const PetscScalar *x;
158154f21887SBarry Smith   const MatScalar   *aa;
1582dfbe8321SBarry Smith   PetscErrorCode    ierr;
1583d0f46423SBarry Smith   PetscInt          m = A->rmap->n,*aj,*ii;
15840298fd71SBarry Smith   PetscInt          n,i,*ridx=NULL;
1585362ced78SSatish Balay   PetscScalar       sum;
1586ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
15879ea0dfa2SSatish Balay 
15883a40ed3dSBarry Smith   PetscFunctionBegin;
1589f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
15901ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
15912e8a6d31SBarry Smith   if (zz != yy) {
15921ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
15932e8a6d31SBarry Smith   } else {
15942e8a6d31SBarry Smith     z = y;
15952e8a6d31SBarry Smith   }
1596bfeeae90SHong Zhang 
159797952fefSHong Zhang   aj = a->j;
159897952fefSHong Zhang   aa = a->a;
1599cddf8d76SBarry Smith   ii = a->i;
16004eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
16014eb6d288SHong Zhang     if (zz != yy) {
16024eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
16034eb6d288SHong Zhang     }
160497952fefSHong Zhang     m    = a->compressedrow.nrows;
160597952fefSHong Zhang     ii   = a->compressedrow.i;
160697952fefSHong Zhang     ridx = a->compressedrow.rindex;
160797952fefSHong Zhang     for (i=0; i<m; i++) {
160897952fefSHong Zhang       n   = ii[i+1] - ii[i];
160997952fefSHong Zhang       aj  = a->j + ii[i];
161097952fefSHong Zhang       aa  = a->a + ii[i];
161197952fefSHong Zhang       sum = y[*ridx];
1612f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
161397952fefSHong Zhang       z[*ridx++] = sum;
161497952fefSHong Zhang     }
161597952fefSHong Zhang   } else { /* do not use compressed row format */
1616f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1617f15663dcSBarry Smith     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1618f15663dcSBarry Smith #else
161917ab2063SBarry Smith     for (i=0; i<m; i++) {
1620f15663dcSBarry Smith       n   = ii[i+1] - ii[i];
1621f15663dcSBarry Smith       aj  = a->j + ii[i];
1622f15663dcSBarry Smith       aa  = a->a + ii[i];
162317ab2063SBarry Smith       sum = y[i];
1624f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
162517ab2063SBarry Smith       z[i] = sum;
162617ab2063SBarry Smith     }
162702ab625aSSatish Balay #endif
1628f15663dcSBarry Smith   }
1629dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1630f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
16311ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
16322e8a6d31SBarry Smith   if (zz != yy) {
16331ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
16342e8a6d31SBarry Smith   }
16358154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
16366b375ea7SVictor Minden   /*
1637918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1638918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1639918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
16406b375ea7SVictor Minden   */
1641918e98c3SVictor Minden #endif
16423a40ed3dSBarry Smith   PetscFunctionReturn(0);
164317ab2063SBarry Smith }
164417ab2063SBarry Smith 
164517ab2063SBarry Smith /*
164617ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
164717ab2063SBarry Smith */
16484a2ae208SSatish Balay #undef __FUNCT__
16494a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1650dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
165117ab2063SBarry Smith {
1652416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
16536849ba73SBarry Smith   PetscErrorCode ierr;
1654d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
165517ab2063SBarry Smith 
16563a40ed3dSBarry Smith   PetscFunctionBegin;
165709f38230SBarry Smith   if (!a->diag) {
1658785e854fSJed Brown     ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr);
16593bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr);
166009f38230SBarry Smith   }
1661d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
166209f38230SBarry Smith     a->diag[i] = a->i[i+1];
1663bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1664bfeeae90SHong Zhang       if (a->j[j] == i) {
166509f38230SBarry Smith         a->diag[i] = j;
166617ab2063SBarry Smith         break;
166717ab2063SBarry Smith       }
166817ab2063SBarry Smith     }
166917ab2063SBarry Smith   }
16703a40ed3dSBarry Smith   PetscFunctionReturn(0);
167117ab2063SBarry Smith }
167217ab2063SBarry Smith 
1673be5855fcSBarry Smith /*
1674be5855fcSBarry Smith      Checks for missing diagonals
1675be5855fcSBarry Smith */
16764a2ae208SSatish Balay #undef __FUNCT__
16774a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1678ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1679be5855fcSBarry Smith {
1680be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
16817734d3b5SMatthew G. Knepley   PetscInt   *diag,*ii = a->i,i;
1682be5855fcSBarry Smith 
1683be5855fcSBarry Smith   PetscFunctionBegin;
168409f38230SBarry Smith   *missing = PETSC_FALSE;
16857734d3b5SMatthew G. Knepley   if (A->rmap->n > 0 && !ii) {
168609f38230SBarry Smith     *missing = PETSC_TRUE;
168709f38230SBarry Smith     if (d) *d = 0;
1688358d2f5dSShri Abhyankar     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
168909f38230SBarry Smith   } else {
1690f1e2ffcdSBarry Smith     diag = a->diag;
1691d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
16927734d3b5SMatthew G. Knepley       if (diag[i] >= ii[i+1]) {
169309f38230SBarry Smith         *missing = PETSC_TRUE;
169409f38230SBarry Smith         if (d) *d = i;
169509f38230SBarry Smith         PetscInfo1(A,"Matrix is missing diagonal number %D",i);
1696358d2f5dSShri Abhyankar         break;
169709f38230SBarry Smith       }
1698be5855fcSBarry Smith     }
1699be5855fcSBarry Smith   }
1700be5855fcSBarry Smith   PetscFunctionReturn(0);
1701be5855fcSBarry Smith }
1702be5855fcSBarry Smith 
170371f1c65dSBarry Smith #undef __FUNCT__
170471f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
17057087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
170671f1c65dSBarry Smith {
170771f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
170871f1c65dSBarry Smith   PetscErrorCode ierr;
1709d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
171054f21887SBarry Smith   MatScalar      *v = a->a;
171154f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
171271f1c65dSBarry Smith 
171371f1c65dSBarry Smith   PetscFunctionBegin;
171471f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
171571f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
171671f1c65dSBarry Smith   diag = a->diag;
171771f1c65dSBarry Smith   if (!a->idiag) {
1718dcca6d9dSJed Brown     ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr);
17193bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
172071f1c65dSBarry Smith     v    = a->a;
172171f1c65dSBarry Smith   }
172271f1c65dSBarry Smith   mdiag = a->mdiag;
172371f1c65dSBarry Smith   idiag = a->idiag;
172471f1c65dSBarry Smith 
1725028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
172671f1c65dSBarry Smith     for (i=0; i<m; i++) {
172771f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1728e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
172971f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
173071f1c65dSBarry Smith     }
173171f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
173271f1c65dSBarry Smith   } else {
173371f1c65dSBarry Smith     for (i=0; i<m; i++) {
173471f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
173571f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
173671f1c65dSBarry Smith     }
1737dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
173871f1c65dSBarry Smith   }
173971f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
174071f1c65dSBarry Smith   PetscFunctionReturn(0);
174171f1c65dSBarry Smith }
174271f1c65dSBarry Smith 
1743c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
17444a2ae208SSatish Balay #undef __FUNCT__
174541f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
174641f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
174717ab2063SBarry Smith {
1748416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1749e6d1f457SBarry Smith   PetscScalar       *x,d,sum,*t,scale;
1750e6d1f457SBarry Smith   const MatScalar   *v = a->a,*idiag=0,*mdiag;
175154f21887SBarry Smith   const PetscScalar *b, *bs,*xb, *ts;
1752dfbe8321SBarry Smith   PetscErrorCode    ierr;
1753d0f46423SBarry Smith   PetscInt          n = A->cmap->n,m = A->rmap->n,i;
175497f1f81fSBarry Smith   const PetscInt    *idx,*diag;
175517ab2063SBarry Smith 
17563a40ed3dSBarry Smith   PetscFunctionBegin;
1757b965ef7fSBarry Smith   its = its*lits;
175891723122SBarry Smith 
175971f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
176071f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
176171f1c65dSBarry Smith   a->fshift = fshift;
176271f1c65dSBarry Smith   a->omega  = omega;
1763ed480e8bSBarry Smith 
176471f1c65dSBarry Smith   diag  = a->diag;
176571f1c65dSBarry Smith   t     = a->ssor_work;
1766ed480e8bSBarry Smith   idiag = a->idiag;
176771f1c65dSBarry Smith   mdiag = a->mdiag;
1768ed480e8bSBarry Smith 
17691ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
17703649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1771ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
177217ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
177317ab2063SBarry Smith     /* apply (U + D/omega) to the vector */
1774ed480e8bSBarry Smith     bs = b;
177517ab2063SBarry Smith     for (i=0; i<m; i++) {
177671f1c65dSBarry Smith       d   = fshift + mdiag[i];
1777416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1778ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1779ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
178017ab2063SBarry Smith       sum = b[i]*d/omega;
1781003131ecSBarry Smith       PetscSparseDensePlusDot(sum,bs,v,idx,n);
178217ab2063SBarry Smith       x[i] = sum;
178317ab2063SBarry Smith     }
17841ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
17853649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1786efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17873a40ed3dSBarry Smith     PetscFunctionReturn(0);
178817ab2063SBarry Smith   }
1789c783ea89SBarry Smith 
17902205254eSKarl Rupp   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
17912205254eSKarl Rupp   else if (flag & SOR_EISENSTAT) {
179217ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1793887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
179417ab2063SBarry Smith 
179517ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
179617ab2063SBarry Smith 
1797887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
179817ab2063SBarry Smith     */
179917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
180017ab2063SBarry Smith 
180117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
180217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1803416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1804ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1805ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
180617ab2063SBarry Smith       sum = b[i];
1807e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1808ed480e8bSBarry Smith       x[i] = sum*idiag[i];
180917ab2063SBarry Smith     }
181017ab2063SBarry Smith 
181117ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1812416022c9SBarry Smith     v = a->a;
18132205254eSKarl Rupp     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
181417ab2063SBarry Smith 
181517ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1816ed480e8bSBarry Smith     ts   = t;
1817416022c9SBarry Smith     diag = a->diag;
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 = t[i];
1823003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1824ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1825733d66baSBarry Smith       /*  x = x + t */
1826733d66baSBarry Smith       x[i] += t[i];
182717ab2063SBarry Smith     }
182817ab2063SBarry Smith 
1829dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
18301ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
18313649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
18323a40ed3dSBarry Smith     PetscFunctionReturn(0);
183317ab2063SBarry Smith   }
183417ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
183517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
183617ab2063SBarry Smith       for (i=0; i<m; i++) {
1837416022c9SBarry Smith         n   = diag[i] - a->i[i];
1838ed480e8bSBarry Smith         idx = a->j + a->i[i];
1839ed480e8bSBarry Smith         v   = a->a + a->i[i];
184017ab2063SBarry Smith         sum = b[i];
1841e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
18425c99c7daSBarry Smith         t[i] = sum;
1843ed480e8bSBarry Smith         x[i] = sum*idiag[i];
184417ab2063SBarry Smith       }
18455c99c7daSBarry Smith       xb   = t;
1846efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18473a40ed3dSBarry Smith     } else xb = b;
184817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
184917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1850416022c9SBarry Smith         n   = a->i[i+1] - diag[i] - 1;
1851ed480e8bSBarry Smith         idx = a->j + diag[i] + 1;
1852ed480e8bSBarry Smith         v   = a->a + diag[i] + 1;
185317ab2063SBarry Smith         sum = xb[i];
1854e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
18555c99c7daSBarry Smith         if (xb == b) {
1856ed480e8bSBarry Smith           x[i] = sum*idiag[i];
18575c99c7daSBarry Smith         } else {
1858b19a5dc2SMark Adams           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
185917ab2063SBarry Smith         }
18605c99c7daSBarry Smith       }
1861b19a5dc2SMark Adams       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
186217ab2063SBarry Smith     }
186317ab2063SBarry Smith     its--;
186417ab2063SBarry Smith   }
186517ab2063SBarry Smith   while (its--) {
186617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
186717ab2063SBarry Smith       for (i=0; i<m; i++) {
1868b19a5dc2SMark Adams         /* lower */
1869b19a5dc2SMark Adams         n   = diag[i] - a->i[i];
1870ed480e8bSBarry Smith         idx = a->j + a->i[i];
1871ed480e8bSBarry Smith         v   = a->a + a->i[i];
187217ab2063SBarry Smith         sum = b[i];
1873e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1874b19a5dc2SMark Adams         t[i] = sum;             /* save application of the lower-triangular part */
1875b19a5dc2SMark Adams         /* upper */
1876b19a5dc2SMark Adams         n   = a->i[i+1] - diag[i] - 1;
1877b19a5dc2SMark Adams         idx = a->j + diag[i] + 1;
1878b19a5dc2SMark Adams         v   = a->a + diag[i] + 1;
1879b19a5dc2SMark Adams         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1880b19a5dc2SMark Adams         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
188117ab2063SBarry Smith       }
1882b19a5dc2SMark Adams       xb   = t;
18839f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1884b19a5dc2SMark Adams     } else xb = b;
188517ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
188617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1887b19a5dc2SMark Adams         sum = xb[i];
1888b19a5dc2SMark Adams         if (xb == b) {
1889b19a5dc2SMark Adams           /* whole matrix (no checkpointing available) */
1890416022c9SBarry Smith           n   = a->i[i+1] - a->i[i];
1891ed480e8bSBarry Smith           idx = a->j + a->i[i];
1892ed480e8bSBarry Smith           v   = a->a + a->i[i];
1893e6d1f457SBarry Smith           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1894ed480e8bSBarry Smith           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1895b19a5dc2SMark Adams         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1896b19a5dc2SMark Adams           n   = a->i[i+1] - diag[i] - 1;
1897b19a5dc2SMark Adams           idx = a->j + diag[i] + 1;
1898b19a5dc2SMark Adams           v   = a->a + diag[i] + 1;
1899b19a5dc2SMark Adams           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1900b19a5dc2SMark Adams           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
190117ab2063SBarry Smith         }
1902b19a5dc2SMark Adams       }
1903b19a5dc2SMark Adams       if (xb == b) {
19049f863219SBarry Smith         ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1905b19a5dc2SMark Adams       } else {
1906b19a5dc2SMark Adams         ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1907b19a5dc2SMark Adams       }
190817ab2063SBarry Smith     }
190917ab2063SBarry Smith   }
19101ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
19113649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1912365a8a9eSBarry Smith   PetscFunctionReturn(0);
191317ab2063SBarry Smith }
191417ab2063SBarry Smith 
19152af78befSBarry Smith 
19164a2ae208SSatish Balay #undef __FUNCT__
19174a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1918dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
191917ab2063SBarry Smith {
1920416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
19214e220ebcSLois Curfman McInnes 
19223a40ed3dSBarry Smith   PetscFunctionBegin;
19234e220ebcSLois Curfman McInnes   info->block_size   = 1.0;
19244e220ebcSLois Curfman McInnes   info->nz_allocated = (double)a->maxnz;
19254e220ebcSLois Curfman McInnes   info->nz_used      = (double)a->nz;
19264e220ebcSLois Curfman McInnes   info->nz_unneeded  = (double)(a->maxnz - a->nz);
19274e220ebcSLois Curfman McInnes   info->assemblies   = (double)A->num_ass;
19288e58a170SBarry Smith   info->mallocs      = (double)A->info.mallocs;
19297adad957SLisandro Dalcin   info->memory       = ((PetscObject)A)->mem;
1930d5f3da31SBarry Smith   if (A->factortype) {
19314e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
19324e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
19334e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
19344e220ebcSLois Curfman McInnes   } else {
19354e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
19364e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
19374e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
19384e220ebcSLois Curfman McInnes   }
19393a40ed3dSBarry Smith   PetscFunctionReturn(0);
194017ab2063SBarry Smith }
194117ab2063SBarry Smith 
19424a2ae208SSatish Balay #undef __FUNCT__
19434a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
19442b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
194517ab2063SBarry Smith {
1946416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
19473b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
19486849ba73SBarry Smith   PetscErrorCode    ierr;
194997b48c8fSBarry Smith   const PetscScalar *xx;
195097b48c8fSBarry Smith   PetscScalar       *bb;
1951ace3abfcSBarry Smith   PetscBool         missing;
195217ab2063SBarry Smith 
19533a40ed3dSBarry Smith   PetscFunctionBegin;
195497b48c8fSBarry Smith   if (x && b) {
195597b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
195697b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
195797b48c8fSBarry Smith     for (i=0; i<N; i++) {
195897b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
195997b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
196097b48c8fSBarry Smith     }
196197b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
196297b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
196397b48c8fSBarry Smith   }
196497b48c8fSBarry Smith 
1965a9817697SBarry Smith   if (a->keepnonzeropattern) {
1966f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1967e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1968bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1969f1e2ffcdSBarry Smith     }
1970f4df32b1SMatthew Knepley     if (diag != 0.0) {
197109f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1972e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1973f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1974f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1975f1e2ffcdSBarry Smith       }
1976f1e2ffcdSBarry Smith     }
1977f1e2ffcdSBarry Smith   } else {
1978f4df32b1SMatthew Knepley     if (diag != 0.0) {
197917ab2063SBarry Smith       for (i=0; i<N; i++) {
1980e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
19817ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1982416022c9SBarry Smith           a->ilen[rows[i]]    = 1;
1983f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1984bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
19857ae801bdSBarry Smith         } else { /* in case row was completely empty */
1986f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
198717ab2063SBarry Smith         }
198817ab2063SBarry Smith       }
19893a40ed3dSBarry Smith     } else {
199017ab2063SBarry Smith       for (i=0; i<N; i++) {
1991e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1992416022c9SBarry Smith         a->ilen[rows[i]] = 0;
199317ab2063SBarry Smith       }
199417ab2063SBarry Smith     }
1995e56f5c9eSBarry Smith     A->nonzerostate++;
1996f1e2ffcdSBarry Smith   }
199743a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19983a40ed3dSBarry Smith   PetscFunctionReturn(0);
199917ab2063SBarry Smith }
200017ab2063SBarry Smith 
20014a2ae208SSatish Balay #undef __FUNCT__
20026e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
20036e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
20046e169961SBarry Smith {
20056e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
20066e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
20076e169961SBarry Smith   PetscErrorCode    ierr;
20082b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
20096e169961SBarry Smith   const PetscScalar *xx;
20106e169961SBarry Smith   PetscScalar       *bb;
20116e169961SBarry Smith 
20126e169961SBarry Smith   PetscFunctionBegin;
20136e169961SBarry Smith   if (x && b) {
20146e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
20156e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
20162b40b63fSBarry Smith     vecs = PETSC_TRUE;
20176e169961SBarry Smith   }
20181795a4d1SJed Brown   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
20196e169961SBarry Smith   for (i=0; i<N; i++) {
20206e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
20216e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
20222205254eSKarl Rupp 
20236e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
20246e169961SBarry Smith   }
20256e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
20266e169961SBarry Smith     if (!zeroed[i]) {
20276e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
20286e169961SBarry Smith         if (zeroed[a->j[j]]) {
20292b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
20306e169961SBarry Smith           a->a[j] = 0.0;
20316e169961SBarry Smith         }
20326e169961SBarry Smith       }
20332b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
20346e169961SBarry Smith   }
20356e169961SBarry Smith   if (x && b) {
20366e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
20376e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
20386e169961SBarry Smith   }
20396e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
20406e169961SBarry Smith   if (diag != 0.0) {
20416e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
20426e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
20436e169961SBarry Smith     for (i=0; i<N; i++) {
20446e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
20456e169961SBarry Smith     }
20466e169961SBarry Smith   }
20476e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20486e169961SBarry Smith   PetscFunctionReturn(0);
20496e169961SBarry Smith }
20506e169961SBarry Smith 
20516e169961SBarry Smith #undef __FUNCT__
20524a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
2053a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
205417ab2063SBarry Smith {
2055416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
205697f1f81fSBarry Smith   PetscInt   *itmp;
205717ab2063SBarry Smith 
20583a40ed3dSBarry Smith   PetscFunctionBegin;
2059e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
206017ab2063SBarry Smith 
2061416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
2062bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
206317ab2063SBarry Smith   if (idx) {
2064bfeeae90SHong Zhang     itmp = a->j + a->i[row];
206526fbe8dcSKarl Rupp     if (*nz) *idx = itmp;
206617ab2063SBarry Smith     else *idx = 0;
206717ab2063SBarry Smith   }
20683a40ed3dSBarry Smith   PetscFunctionReturn(0);
206917ab2063SBarry Smith }
207017ab2063SBarry Smith 
2071bfeeae90SHong Zhang /* remove this function? */
20724a2ae208SSatish Balay #undef __FUNCT__
20734a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
2074a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
207517ab2063SBarry Smith {
20763a40ed3dSBarry Smith   PetscFunctionBegin;
20773a40ed3dSBarry Smith   PetscFunctionReturn(0);
207817ab2063SBarry Smith }
207917ab2063SBarry Smith 
20804a2ae208SSatish Balay #undef __FUNCT__
20814a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
2082dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
208317ab2063SBarry Smith {
2084416022c9SBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
208554f21887SBarry Smith   MatScalar      *v  = a->a;
208636db0b34SBarry Smith   PetscReal      sum = 0.0;
20876849ba73SBarry Smith   PetscErrorCode ierr;
208897f1f81fSBarry Smith   PetscInt       i,j;
208917ab2063SBarry Smith 
20903a40ed3dSBarry Smith   PetscFunctionBegin;
209117ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
2092416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
209336db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
209417ab2063SBarry Smith     }
20958f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
20963a40ed3dSBarry Smith   } else if (type == NORM_1) {
209736db0b34SBarry Smith     PetscReal *tmp;
209897f1f81fSBarry Smith     PetscInt  *jj = a->j;
20991795a4d1SJed Brown     ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr);
2100064f8208SBarry Smith     *nrm = 0.0;
2101416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
2102bfeeae90SHong Zhang       tmp[*jj++] += PetscAbsScalar(*v);  v++;
210317ab2063SBarry Smith     }
2104d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
2105064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
210617ab2063SBarry Smith     }
2107606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
21083a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
2109064f8208SBarry Smith     *nrm = 0.0;
2110d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
2111bfeeae90SHong Zhang       v   = a->a + a->i[j];
211217ab2063SBarry Smith       sum = 0.0;
2113416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2114cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
211517ab2063SBarry Smith       }
2116064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
211717ab2063SBarry Smith     }
2118f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
21193a40ed3dSBarry Smith   PetscFunctionReturn(0);
212017ab2063SBarry Smith }
212117ab2063SBarry Smith 
21224e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
21234e938277SHong Zhang #undef __FUNCT__
21244e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
21254e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
21264e938277SHong Zhang {
21274e938277SHong Zhang   PetscErrorCode ierr;
21284e938277SHong Zhang   PetscInt       i,j,anzj;
21294e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
21304e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
21314e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
21324e938277SHong Zhang 
21334e938277SHong Zhang   PetscFunctionBegin;
21344e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
21351795a4d1SJed Brown   ierr = PetscCalloc1((an+1),&ati);CHKERRQ(ierr);
2136785e854fSJed Brown   ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr);
2137785e854fSJed Brown   ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr);
21384e938277SHong Zhang 
21394e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
21404e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
214126fbe8dcSKarl Rupp   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
21424e938277SHong Zhang   /* Form ati for csr format of A^T. */
214326fbe8dcSKarl Rupp   for (i=0;i<an;i++) ati[i+1] += ati[i];
21444e938277SHong Zhang 
21454e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
21464e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
21474e938277SHong Zhang 
21484e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
21494e938277SHong Zhang   for (i=0;i<am;i++) {
21504e938277SHong Zhang     anzj = ai[i+1] - ai[i];
21514e938277SHong Zhang     for (j=0;j<anzj;j++) {
21524e938277SHong Zhang       atj[atfill[*aj]] = i;
21534e938277SHong Zhang       atfill[*aj++]   += 1;
21544e938277SHong Zhang     }
21554e938277SHong Zhang   }
21564e938277SHong Zhang 
21574e938277SHong Zhang   /* Clean up temporary space and complete requests. */
21584e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
2159ce94432eSBarry Smith   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
216033d57670SJed Brown   ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
2161a2f3521dSMark F. Adams 
21624e938277SHong Zhang   b          = (Mat_SeqAIJ*)((*B)->data);
21634e938277SHong Zhang   b->free_a  = PETSC_FALSE;
21644e938277SHong Zhang   b->free_ij = PETSC_TRUE;
21654e938277SHong Zhang   b->nonew   = 0;
21664e938277SHong Zhang   PetscFunctionReturn(0);
21674e938277SHong Zhang }
21684e938277SHong Zhang 
21694a2ae208SSatish Balay #undef __FUNCT__
21704a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
2171fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
217217ab2063SBarry Smith {
2173416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2174416022c9SBarry Smith   Mat            C;
21756849ba73SBarry Smith   PetscErrorCode ierr;
2176d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
217754f21887SBarry Smith   MatScalar      *array = a->a;
217817ab2063SBarry Smith 
21793a40ed3dSBarry Smith   PetscFunctionBegin;
2180e32f2f54SBarry 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");
2181fc4dec0aSBarry Smith 
2182fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
21831795a4d1SJed Brown     ierr = PetscCalloc1((1+A->cmap->n),&col);CHKERRQ(ierr);
2184bfeeae90SHong Zhang 
2185bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
2186ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2187d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
218833d57670SJed Brown     ierr = MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
21897adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2190ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
2191606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
2192a541d17aSBarry Smith   } else {
2193a541d17aSBarry Smith     C = *B;
2194a541d17aSBarry Smith   }
2195a541d17aSBarry Smith 
219617ab2063SBarry Smith   for (i=0; i<m; i++) {
219717ab2063SBarry Smith     len    = ai[i+1]-ai[i];
219887d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
2199b9b97703SBarry Smith     array += len;
2200b9b97703SBarry Smith     aj    += len;
220117ab2063SBarry Smith   }
22026d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22036d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
220417ab2063SBarry Smith 
2205815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
2206416022c9SBarry Smith     *B = C;
220717ab2063SBarry Smith   } else {
2208eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
220917ab2063SBarry Smith   }
22103a40ed3dSBarry Smith   PetscFunctionReturn(0);
221117ab2063SBarry Smith }
221217ab2063SBarry Smith 
2213cd0d46ebSvictorle #undef __FUNCT__
22145fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
22157087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2216cd0d46ebSvictorle {
2217cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
221854f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
221954f21887SBarry Smith   MatScalar      *va,*vb;
22206849ba73SBarry Smith   PetscErrorCode ierr;
222197f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
2222cd0d46ebSvictorle 
2223cd0d46ebSvictorle   PetscFunctionBegin;
2224cd0d46ebSvictorle   bij = (Mat_SeqAIJ*) B->data;
2225cd0d46ebSvictorle 
2226cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2227cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
22285485867bSBarry Smith   if (ma!=nb || na!=mb) {
22295485867bSBarry Smith     *f = PETSC_FALSE;
22305485867bSBarry Smith     PetscFunctionReturn(0);
22315485867bSBarry Smith   }
2232cd0d46ebSvictorle   aii  = aij->i; bii = bij->i;
2233cd0d46ebSvictorle   adx  = aij->j; bdx = bij->j;
2234cd0d46ebSvictorle   va   = aij->a; vb = bij->a;
2235785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2236785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
2237cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
2238cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
2239cd0d46ebSvictorle 
2240cd0d46ebSvictorle   *f = PETSC_TRUE;
2241cd0d46ebSvictorle   for (i=0; i<ma; i++) {
2242cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
224397f1f81fSBarry Smith       PetscInt    idc,idr;
22445485867bSBarry Smith       PetscScalar vc,vr;
2245cd0d46ebSvictorle       /* column/row index/value */
22465485867bSBarry Smith       idc = adx[aptr[i]];
22475485867bSBarry Smith       idr = bdx[bptr[idc]];
22485485867bSBarry Smith       vc  = va[aptr[i]];
22495485867bSBarry Smith       vr  = vb[bptr[idc]];
22505485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
22515485867bSBarry Smith         *f = PETSC_FALSE;
22525485867bSBarry Smith         goto done;
2253cd0d46ebSvictorle       } else {
22545485867bSBarry Smith         aptr[i]++;
22555485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
2256cd0d46ebSvictorle       }
2257cd0d46ebSvictorle     }
2258cd0d46ebSvictorle   }
2259cd0d46ebSvictorle done:
2260cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
22613aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
2262cd0d46ebSvictorle   PetscFunctionReturn(0);
2263cd0d46ebSvictorle }
2264cd0d46ebSvictorle 
22651cbb95d3SBarry Smith #undef __FUNCT__
22661cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
22677087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
22681cbb95d3SBarry Smith {
22691cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
227054f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
227154f21887SBarry Smith   MatScalar      *va,*vb;
22721cbb95d3SBarry Smith   PetscErrorCode ierr;
22731cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
22741cbb95d3SBarry Smith 
22751cbb95d3SBarry Smith   PetscFunctionBegin;
22761cbb95d3SBarry Smith   bij = (Mat_SeqAIJ*) B->data;
22771cbb95d3SBarry Smith 
22781cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
22791cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
22801cbb95d3SBarry Smith   if (ma!=nb || na!=mb) {
22811cbb95d3SBarry Smith     *f = PETSC_FALSE;
22821cbb95d3SBarry Smith     PetscFunctionReturn(0);
22831cbb95d3SBarry Smith   }
22841cbb95d3SBarry Smith   aii  = aij->i; bii = bij->i;
22851cbb95d3SBarry Smith   adx  = aij->j; bdx = bij->j;
22861cbb95d3SBarry Smith   va   = aij->a; vb = bij->a;
2287785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2288785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
22891cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
22901cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
22911cbb95d3SBarry Smith 
22921cbb95d3SBarry Smith   *f = PETSC_TRUE;
22931cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
22941cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
22951cbb95d3SBarry Smith       PetscInt    idc,idr;
22961cbb95d3SBarry Smith       PetscScalar vc,vr;
22971cbb95d3SBarry Smith       /* column/row index/value */
22981cbb95d3SBarry Smith       idc = adx[aptr[i]];
22991cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
23001cbb95d3SBarry Smith       vc  = va[aptr[i]];
23011cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
23021cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
23031cbb95d3SBarry Smith         *f = PETSC_FALSE;
23041cbb95d3SBarry Smith         goto done;
23051cbb95d3SBarry Smith       } else {
23061cbb95d3SBarry Smith         aptr[i]++;
23071cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
23081cbb95d3SBarry Smith       }
23091cbb95d3SBarry Smith     }
23101cbb95d3SBarry Smith   }
23111cbb95d3SBarry Smith done:
23121cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
23131cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
23141cbb95d3SBarry Smith   PetscFunctionReturn(0);
23151cbb95d3SBarry Smith }
23161cbb95d3SBarry Smith 
23179e29f15eSvictorle #undef __FUNCT__
23189e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2319ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
23209e29f15eSvictorle {
2321dfbe8321SBarry Smith   PetscErrorCode ierr;
23226e111a19SKarl Rupp 
23239e29f15eSvictorle   PetscFunctionBegin;
23245485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
23259e29f15eSvictorle   PetscFunctionReturn(0);
23269e29f15eSvictorle }
23279e29f15eSvictorle 
23284a2ae208SSatish Balay #undef __FUNCT__
23291cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2330ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
23311cbb95d3SBarry Smith {
23321cbb95d3SBarry Smith   PetscErrorCode ierr;
23336e111a19SKarl Rupp 
23341cbb95d3SBarry Smith   PetscFunctionBegin;
23351cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
23361cbb95d3SBarry Smith   PetscFunctionReturn(0);
23371cbb95d3SBarry Smith }
23381cbb95d3SBarry Smith 
23391cbb95d3SBarry Smith #undef __FUNCT__
23404a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2341dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
234217ab2063SBarry Smith {
2343416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
234454f21887SBarry Smith   PetscScalar    *l,*r,x;
234554f21887SBarry Smith   MatScalar      *v;
2346dfbe8321SBarry Smith   PetscErrorCode ierr;
2347d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
234817ab2063SBarry Smith 
23493a40ed3dSBarry Smith   PetscFunctionBegin;
235017ab2063SBarry Smith   if (ll) {
23513ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
23523ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2353e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2354e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
23551ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2356416022c9SBarry Smith     v    = a->a;
235717ab2063SBarry Smith     for (i=0; i<m; i++) {
235817ab2063SBarry Smith       x = l[i];
2359416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
23602205254eSKarl Rupp       for (j=0; j<M; j++) (*v++) *= x;
236117ab2063SBarry Smith     }
23621ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2363efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
236417ab2063SBarry Smith   }
236517ab2063SBarry Smith   if (rr) {
2366e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2367e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
23681ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2369416022c9SBarry Smith     v    = a->a; jj = a->j;
23702205254eSKarl Rupp     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
23711ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2372efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
237317ab2063SBarry Smith   }
2374acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
23753a40ed3dSBarry Smith   PetscFunctionReturn(0);
237617ab2063SBarry Smith }
237717ab2063SBarry Smith 
23784a2ae208SSatish Balay #undef __FUNCT__
23794a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
238097f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
238117ab2063SBarry Smith {
2382db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
23836849ba73SBarry Smith   PetscErrorCode ierr;
2384d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
238597f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
23865d0c19d7SBarry Smith   const PetscInt *irow,*icol;
23875d0c19d7SBarry Smith   PetscInt       nrows,ncols;
238897f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
238954f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2390416022c9SBarry Smith   Mat            C;
2391ace3abfcSBarry Smith   PetscBool      stride,sorted;
239217ab2063SBarry Smith 
23933a40ed3dSBarry Smith   PetscFunctionBegin;
239414ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
2395e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
239614ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
2397e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
239899141d43SSatish Balay 
239917ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2400b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2401b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
240217ab2063SBarry Smith 
2403251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2404ff718158SBarry Smith   if (stride) {
2405ff718158SBarry Smith     ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2406ff718158SBarry Smith   } else {
2407ff718158SBarry Smith     first = 0;
2408ff718158SBarry Smith     step  = 0;
2409ff718158SBarry Smith   }
2410fee21e36SBarry Smith   if (stride && step == 1) {
241102834360SBarry Smith     /* special case of contiguous rows */
2412dcca6d9dSJed Brown     ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr);
241302834360SBarry Smith     /* loop over new rows determining lens and starting points */
241402834360SBarry Smith     for (i=0; i<nrows; i++) {
2415bfeeae90SHong Zhang       kstart = ai[irow[i]];
2416a2744918SBarry Smith       kend   = kstart + ailen[irow[i]];
241702834360SBarry Smith       for (k=kstart; k<kend; k++) {
2418bfeeae90SHong Zhang         if (aj[k] >= first) {
241902834360SBarry Smith           starts[i] = k;
242002834360SBarry Smith           break;
242102834360SBarry Smith         }
242202834360SBarry Smith       }
2423a2744918SBarry Smith       sum = 0;
242402834360SBarry Smith       while (k < kend) {
2425bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2426a2744918SBarry Smith         sum++;
242702834360SBarry Smith       }
2428a2744918SBarry Smith       lens[i] = sum;
242902834360SBarry Smith     }
243002834360SBarry Smith     /* create submatrix */
2431cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
243297f1f81fSBarry Smith       PetscInt n_cols,n_rows;
243308480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2434e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2435d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
243608480c60SBarry Smith       C    = *B;
24373a40ed3dSBarry Smith     } else {
24383bef6203SJed Brown       PetscInt rbs,cbs;
2439ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2440f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
24413bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
24423bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
24433bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
24447adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2445ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
244608480c60SBarry Smith     }
2447db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2448db02288aSLois Curfman McInnes 
244902834360SBarry Smith     /* loop over rows inserting into submatrix */
2450db02288aSLois Curfman McInnes     a_new = c->a;
2451db02288aSLois Curfman McInnes     j_new = c->j;
2452db02288aSLois Curfman McInnes     i_new = c->i;
2453bfeeae90SHong Zhang 
245402834360SBarry Smith     for (i=0; i<nrows; i++) {
2455a2744918SBarry Smith       ii    = starts[i];
2456a2744918SBarry Smith       lensi = lens[i];
2457a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2458a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
245902834360SBarry Smith       }
246087828ca2SBarry Smith       ierr       = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2461a2744918SBarry Smith       a_new     += lensi;
2462a2744918SBarry Smith       i_new[i+1] = i_new[i] + lensi;
2463a2744918SBarry Smith       c->ilen[i] = lensi;
246402834360SBarry Smith     }
24650e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
24663a40ed3dSBarry Smith   } else {
246702834360SBarry Smith     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
24681795a4d1SJed Brown     ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr);
2469785e854fSJed Brown     ierr = PetscMalloc1((1+nrows),&lens);CHKERRQ(ierr);
24704dcab191SBarry Smith     for (i=0; i<ncols; i++) {
24714dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
24724dcab191SBarry 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);
24734dcab191SBarry Smith #endif
24744dcab191SBarry Smith       smap[icol[i]] = i+1;
24754dcab191SBarry Smith     }
24764dcab191SBarry Smith 
247702834360SBarry Smith     /* determine lens of each row */
247802834360SBarry Smith     for (i=0; i<nrows; i++) {
2479bfeeae90SHong Zhang       kstart  = ai[irow[i]];
248002834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
248102834360SBarry Smith       lens[i] = 0;
248202834360SBarry Smith       for (k=kstart; k<kend; k++) {
2483bfeeae90SHong Zhang         if (smap[aj[k]]) {
248402834360SBarry Smith           lens[i]++;
248502834360SBarry Smith         }
248602834360SBarry Smith       }
248702834360SBarry Smith     }
248817ab2063SBarry Smith     /* Create and fill new matrix */
2489a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2490ace3abfcSBarry Smith       PetscBool equal;
24910f5bd95cSBarry Smith 
249299141d43SSatish Balay       c = (Mat_SeqAIJ*)((*B)->data);
2493e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2494d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
2495f23aa3ddSBarry Smith       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2496d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
249708480c60SBarry Smith       C    = *B;
24983a40ed3dSBarry Smith     } else {
24993bef6203SJed Brown       PetscInt rbs,cbs;
2500ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2501f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
25023bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
25033bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
25043bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
25057adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2506ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
250708480c60SBarry Smith     }
250899141d43SSatish Balay     c = (Mat_SeqAIJ*)(C->data);
250917ab2063SBarry Smith     for (i=0; i<nrows; i++) {
251099141d43SSatish Balay       row      = irow[i];
2511bfeeae90SHong Zhang       kstart   = ai[row];
251299141d43SSatish Balay       kend     = kstart + a->ilen[row];
2513bfeeae90SHong Zhang       mat_i    = c->i[i];
251499141d43SSatish Balay       mat_j    = c->j + mat_i;
251599141d43SSatish Balay       mat_a    = c->a + mat_i;
251699141d43SSatish Balay       mat_ilen = c->ilen + i;
251717ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2518bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2519ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
252099141d43SSatish Balay           *mat_a++ = a->a[k];
252199141d43SSatish Balay           (*mat_ilen)++;
252299141d43SSatish Balay 
252317ab2063SBarry Smith         }
252417ab2063SBarry Smith       }
252517ab2063SBarry Smith     }
252602834360SBarry Smith     /* Free work space */
252702834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2528606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2529606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
253002834360SBarry Smith   }
25316d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
25326d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
253317ab2063SBarry Smith 
253417ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2535416022c9SBarry Smith   *B   = C;
25363a40ed3dSBarry Smith   PetscFunctionReturn(0);
253717ab2063SBarry Smith }
253817ab2063SBarry Smith 
25391df811f5SHong Zhang #undef __FUNCT__
254082d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2541fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
254282d44351SHong Zhang {
254382d44351SHong Zhang   PetscErrorCode ierr;
254482d44351SHong Zhang   Mat            B;
254582d44351SHong Zhang 
254682d44351SHong Zhang   PetscFunctionBegin;
2547c2d650bdSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
254882d44351SHong Zhang     ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
254982d44351SHong Zhang     ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
255033d57670SJed Brown     ierr    = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr);
255182d44351SHong Zhang     ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
255282d44351SHong Zhang     ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
255382d44351SHong Zhang     *subMat = B;
2554c2d650bdSHong Zhang   } else {
2555c2d650bdSHong Zhang     ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
2556c2d650bdSHong Zhang   }
255782d44351SHong Zhang   PetscFunctionReturn(0);
255882d44351SHong Zhang }
255982d44351SHong Zhang 
256082d44351SHong Zhang #undef __FUNCT__
25614a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
25620481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2563a871dcd8SBarry Smith {
256463b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2565dfbe8321SBarry Smith   PetscErrorCode ierr;
256663b91edcSBarry Smith   Mat            outA;
2567ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
256863b91edcSBarry Smith 
25693a40ed3dSBarry Smith   PetscFunctionBegin;
2570e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
25711df811f5SHong Zhang 
2572b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2573b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2574a871dcd8SBarry Smith 
257563b91edcSBarry Smith   outA             = inA;
2576d5f3da31SBarry Smith   outA->factortype = MAT_FACTOR_LU;
25772205254eSKarl Rupp 
2578c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
25796bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
25802205254eSKarl Rupp 
2581c3122656SLisandro Dalcin   a->row = row;
25822205254eSKarl Rupp 
2583c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
25846bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
25852205254eSKarl Rupp 
2586c3122656SLisandro Dalcin   a->col = col;
258763b91edcSBarry Smith 
258836db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
25896bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
25904c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
25913bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
2592f0ec6fceSSatish Balay 
259394a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2594785e854fSJed Brown     ierr = PetscMalloc1((inA->rmap->n+1),&a->solve_work);CHKERRQ(ierr);
25953bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
259694a9d846SBarry Smith   }
259763b91edcSBarry Smith 
2598f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2599137fb511SHong Zhang   if (row_identity && col_identity) {
2600ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2601137fb511SHong Zhang   } else {
2602719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2603137fb511SHong Zhang   }
26043a40ed3dSBarry Smith   PetscFunctionReturn(0);
2605a871dcd8SBarry Smith }
2606a871dcd8SBarry Smith 
26074a2ae208SSatish Balay #undef __FUNCT__
26084a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2609f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2610f0b747eeSBarry Smith {
2611f0b747eeSBarry Smith   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2612f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2613efee365bSSatish Balay   PetscErrorCode ierr;
2614c5df96a5SBarry Smith   PetscBLASInt   one = 1,bnz;
26153a40ed3dSBarry Smith 
26163a40ed3dSBarry Smith   PetscFunctionBegin;
2617c5df96a5SBarry Smith   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
26188b83055fSJed Brown   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2619efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2620acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
26213a40ed3dSBarry Smith   PetscFunctionReturn(0);
2622f0b747eeSBarry Smith }
2623f0b747eeSBarry Smith 
26244a2ae208SSatish Balay #undef __FUNCT__
26254a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
262697f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2627cddf8d76SBarry Smith {
2628dfbe8321SBarry Smith   PetscErrorCode ierr;
262997f1f81fSBarry Smith   PetscInt       i;
2630cddf8d76SBarry Smith 
26313a40ed3dSBarry Smith   PetscFunctionBegin;
2632cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2633785e854fSJed Brown     ierr = PetscMalloc1((n+1),B);CHKERRQ(ierr);
2634cddf8d76SBarry Smith   }
2635cddf8d76SBarry Smith 
2636cddf8d76SBarry Smith   for (i=0; i<n; i++) {
26376a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2638cddf8d76SBarry Smith   }
26393a40ed3dSBarry Smith   PetscFunctionReturn(0);
2640cddf8d76SBarry Smith }
2641cddf8d76SBarry Smith 
26424a2ae208SSatish Balay #undef __FUNCT__
26434a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
264497f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
26454dcbc457SBarry Smith {
2646e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
26476849ba73SBarry Smith   PetscErrorCode ierr;
26485d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
26495d0c19d7SBarry Smith   const PetscInt *idx;
265097f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2651f1af5d2fSBarry Smith   PetscBT        table;
2652bbd702dbSSatish Balay 
26533a40ed3dSBarry Smith   PetscFunctionBegin;
2654d0f46423SBarry Smith   m  = A->rmap->n;
2655e4d965acSSatish Balay   ai = a->i;
2656bfeeae90SHong Zhang   aj = a->j;
26578a047759SSatish Balay 
2658e32f2f54SBarry Smith   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
265906763907SSatish Balay 
2660785e854fSJed Brown   ierr = PetscMalloc1((m+1),&nidx);CHKERRQ(ierr);
266153b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
266206763907SSatish Balay 
2663e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2664b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2665e4d965acSSatish Balay     isz  = 0;
26666831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2667e4d965acSSatish Balay 
2668e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
26694dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2670b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2671e4d965acSSatish Balay 
2672dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2673e4d965acSSatish Balay     for (j=0; j<n; ++j) {
26742205254eSKarl Rupp       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
26754dcbc457SBarry Smith     }
267606763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
26776bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2678e4d965acSSatish Balay 
267904a348a9SBarry Smith     k = 0;
268004a348a9SBarry Smith     for (j=0; j<ov; j++) { /* for each overlap */
268104a348a9SBarry Smith       n = isz;
268206763907SSatish Balay       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2683e4d965acSSatish Balay         row   = nidx[k];
2684e4d965acSSatish Balay         start = ai[row];
2685e4d965acSSatish Balay         end   = ai[row+1];
268604a348a9SBarry Smith         for (l = start; l<end; l++) {
2687efb16452SHong Zhang           val = aj[l];
26882205254eSKarl Rupp           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2689e4d965acSSatish Balay         }
2690e4d965acSSatish Balay       }
2691e4d965acSSatish Balay     }
269270b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2693e4d965acSSatish Balay   }
269494bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2695606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
26963a40ed3dSBarry Smith   PetscFunctionReturn(0);
26974dcbc457SBarry Smith }
269817ab2063SBarry Smith 
26990513a670SBarry Smith /* -------------------------------------------------------------- */
27004a2ae208SSatish Balay #undef __FUNCT__
27014a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2702dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
27030513a670SBarry Smith {
27040513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
27056849ba73SBarry Smith   PetscErrorCode ierr;
27063b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
27075d0c19d7SBarry Smith   const PetscInt *row,*col;
27085d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
270956cd22aeSBarry Smith   IS             icolp,irowp;
27100298fd71SBarry Smith   PetscInt       *cwork = NULL;
27110298fd71SBarry Smith   PetscScalar    *vwork = NULL;
27120513a670SBarry Smith 
27133a40ed3dSBarry Smith   PetscFunctionBegin;
27144c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
271556cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
27164c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
271756cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
27180513a670SBarry Smith 
27190513a670SBarry Smith   /* determine lengths of permuted rows */
2720785e854fSJed Brown   ierr = PetscMalloc1((m+1),&lens);CHKERRQ(ierr);
27212205254eSKarl Rupp   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2722ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2723f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
272433d57670SJed Brown   ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
27257adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2726ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2727606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
27280513a670SBarry Smith 
2729785e854fSJed Brown   ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr);
27300513a670SBarry Smith   for (i=0; i<m; i++) {
273132ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
27322205254eSKarl Rupp     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2733cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
273432ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
27350513a670SBarry Smith   }
2736606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
27372205254eSKarl Rupp 
27383c7d62e4SBarry Smith   (*B)->assembled = PETSC_FALSE;
27392205254eSKarl Rupp 
27400513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27410513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
274256cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
274356cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
27446bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
27456bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
27463a40ed3dSBarry Smith   PetscFunctionReturn(0);
27470513a670SBarry Smith }
27480513a670SBarry Smith 
27494a2ae208SSatish Balay #undef __FUNCT__
27504a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2751dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2752cb5b572fSBarry Smith {
2753dfbe8321SBarry Smith   PetscErrorCode ierr;
2754cb5b572fSBarry Smith 
2755cb5b572fSBarry Smith   PetscFunctionBegin;
275633f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
275733f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2758be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2759be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2760be6bf707SBarry Smith 
2761700c5bfcSBarry 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");
2762d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2763cb5b572fSBarry Smith   } else {
2764cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2765cb5b572fSBarry Smith   }
2766cb5b572fSBarry Smith   PetscFunctionReturn(0);
2767cb5b572fSBarry Smith }
2768cb5b572fSBarry Smith 
27694a2ae208SSatish Balay #undef __FUNCT__
27704994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
27714994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2772273d9f13SBarry Smith {
2773dfbe8321SBarry Smith   PetscErrorCode ierr;
2774273d9f13SBarry Smith 
2775273d9f13SBarry Smith   PetscFunctionBegin;
2776ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2777273d9f13SBarry Smith   PetscFunctionReturn(0);
2778273d9f13SBarry Smith }
2779273d9f13SBarry Smith 
27804a2ae208SSatish Balay #undef __FUNCT__
27818c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray_SeqAIJ"
27828c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
27836c0721eeSBarry Smith {
27846c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
27856e111a19SKarl Rupp 
27866c0721eeSBarry Smith   PetscFunctionBegin;
27876c0721eeSBarry Smith   *array = a->a;
27886c0721eeSBarry Smith   PetscFunctionReturn(0);
27896c0721eeSBarry Smith }
27906c0721eeSBarry Smith 
27914a2ae208SSatish Balay #undef __FUNCT__
27928c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray_SeqAIJ"
27938c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
27946c0721eeSBarry Smith {
27956c0721eeSBarry Smith   PetscFunctionBegin;
27966c0721eeSBarry Smith   PetscFunctionReturn(0);
27976c0721eeSBarry Smith }
2798273d9f13SBarry Smith 
27998229c054SShri Abhyankar /*
28008229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
28018229c054SShri Abhyankar    have different nonzero structure.
28028229c054SShri Abhyankar */
2803ac90fabeSBarry Smith #undef __FUNCT__
2804b264fe52SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqX_private"
2805b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2806ec7775f6SShri Abhyankar {
2807b264fe52SHong Zhang   PetscInt       i,j,k,nzx,nzy;
2808ec7775f6SShri Abhyankar 
2809ec7775f6SShri Abhyankar   PetscFunctionBegin;
2810ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2811ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
2812b264fe52SHong Zhang     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2813b264fe52SHong Zhang     nzx = xi[i+1] - xi[i];
2814b264fe52SHong Zhang     nzy = yi[i+1] - yi[i];
28158af7cee1SJed Brown     nnz[i] = 0;
28168af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2817b264fe52SHong Zhang       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2818b264fe52SHong Zhang       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
28198af7cee1SJed Brown       nnz[i]++;
28208af7cee1SJed Brown     }
28218af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2822ec7775f6SShri Abhyankar   }
2823ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2824ec7775f6SShri Abhyankar }
2825ec7775f6SShri Abhyankar 
2826ec7775f6SShri Abhyankar #undef __FUNCT__
2827b264fe52SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
2828b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2829b264fe52SHong Zhang {
2830b264fe52SHong Zhang   PetscInt       m = Y->rmap->N;
2831b264fe52SHong Zhang   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2832b264fe52SHong Zhang   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;
2833b264fe52SHong Zhang   PetscErrorCode ierr;
2834b264fe52SHong Zhang 
2835b264fe52SHong Zhang   PetscFunctionBegin;
2836b264fe52SHong Zhang   /* Set the number of nonzeros in the new matrix */
2837b264fe52SHong Zhang   ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr);
2838b264fe52SHong Zhang   PetscFunctionReturn(0);
2839b264fe52SHong Zhang }
2840b264fe52SHong Zhang 
2841b264fe52SHong Zhang #undef __FUNCT__
2842ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2843f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2844ac90fabeSBarry Smith {
2845dfbe8321SBarry Smith   PetscErrorCode ierr;
284697f1f81fSBarry Smith   PetscInt       i;
2847ac90fabeSBarry Smith   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2848c5df96a5SBarry Smith   PetscBLASInt   one=1,bnz;
2849ac90fabeSBarry Smith 
2850ac90fabeSBarry Smith   PetscFunctionBegin;
2851c5df96a5SBarry Smith   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2852ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2853f4df32b1SMatthew Knepley     PetscScalar alpha = a;
28548b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2855acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2856a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2857c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2858a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2859a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
28606bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2861a30b2313SHong Zhang     }
2862a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
28630298fd71SBarry Smith       ierr    = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,NULL, y->i,y->j,NULL, &y->xtoy);CHKERRQ(ierr);
2864a30b2313SHong Zhang       y->XtoY = X;
2865407f6b05SHong Zhang       ierr    = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2866c537a176SHong Zhang     }
2867f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2868a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
28696712e2f1SBarry Smith     ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %D/%D = %g\n",x->nz,y->nz,(double)((PetscReal)(x->nz)/(y->nz+1)));CHKERRQ(ierr);
2870ac90fabeSBarry Smith   } else {
28718229c054SShri Abhyankar     Mat      B;
28728229c054SShri Abhyankar     PetscInt *nnz;
2873785e854fSJed Brown     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
2874ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2875bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
28764aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
287733d57670SJed Brown     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
2878176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
28798229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2880ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2881ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2882ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
28838229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2884ac90fabeSBarry Smith   }
2885ac90fabeSBarry Smith   PetscFunctionReturn(0);
2886ac90fabeSBarry Smith }
2887ac90fabeSBarry Smith 
2888521d7252SBarry Smith #undef __FUNCT__
2889354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
28907087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2891354c94deSBarry Smith {
2892354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2893354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2894354c94deSBarry Smith   PetscInt    i,nz;
2895354c94deSBarry Smith   PetscScalar *a;
2896354c94deSBarry Smith 
2897354c94deSBarry Smith   PetscFunctionBegin;
2898354c94deSBarry Smith   nz = aij->nz;
2899354c94deSBarry Smith   a  = aij->a;
29002205254eSKarl Rupp   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2901354c94deSBarry Smith #else
2902354c94deSBarry Smith   PetscFunctionBegin;
2903354c94deSBarry Smith #endif
2904354c94deSBarry Smith   PetscFunctionReturn(0);
2905354c94deSBarry Smith }
2906354c94deSBarry Smith 
2907e34fafa9SBarry Smith #undef __FUNCT__
2908985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2909985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2910e34fafa9SBarry Smith {
2911e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2912e34fafa9SBarry Smith   PetscErrorCode ierr;
2913d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2914e34fafa9SBarry Smith   PetscReal      atmp;
2915985db425SBarry Smith   PetscScalar    *x;
2916e34fafa9SBarry Smith   MatScalar      *aa;
2917e34fafa9SBarry Smith 
2918e34fafa9SBarry Smith   PetscFunctionBegin;
2919e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2920e34fafa9SBarry Smith   aa = a->a;
2921e34fafa9SBarry Smith   ai = a->i;
2922e34fafa9SBarry Smith   aj = a->j;
2923e34fafa9SBarry Smith 
2924985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2925e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2926e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2927e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2928e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2929e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
29309189402eSHong Zhang     x[i]  = 0.0;
2931e34fafa9SBarry Smith     for (j=0; j<ncols; j++) {
2932985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2933985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2934985db425SBarry Smith       aa++; aj++;
2935985db425SBarry Smith     }
2936985db425SBarry Smith   }
2937985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2938985db425SBarry Smith   PetscFunctionReturn(0);
2939985db425SBarry Smith }
2940985db425SBarry Smith 
2941985db425SBarry Smith #undef __FUNCT__
2942985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2943985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2944985db425SBarry Smith {
2945985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2946985db425SBarry Smith   PetscErrorCode ierr;
2947d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2948985db425SBarry Smith   PetscScalar    *x;
2949985db425SBarry Smith   MatScalar      *aa;
2950985db425SBarry Smith 
2951985db425SBarry Smith   PetscFunctionBegin;
2952e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2953985db425SBarry Smith   aa = a->a;
2954985db425SBarry Smith   ai = a->i;
2955985db425SBarry Smith   aj = a->j;
2956985db425SBarry Smith 
2957985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2958985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2959985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2960e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2961985db425SBarry Smith   for (i=0; i<m; i++) {
2962985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2963d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2964985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2965985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2966985db425SBarry Smith       x[i] = 0.0;
2967985db425SBarry Smith       if (idx) {
2968985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2969985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2970985db425SBarry Smith           if (aj[j] > j) {
2971985db425SBarry Smith             idx[i] = j;
2972985db425SBarry Smith             break;
2973985db425SBarry Smith           }
2974985db425SBarry Smith         }
2975985db425SBarry Smith       }
2976985db425SBarry Smith     }
2977985db425SBarry Smith     for (j=0; j<ncols; j++) {
2978985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2979985db425SBarry Smith       aa++; aj++;
2980985db425SBarry Smith     }
2981985db425SBarry Smith   }
2982985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2983985db425SBarry Smith   PetscFunctionReturn(0);
2984985db425SBarry Smith }
2985985db425SBarry Smith 
2986985db425SBarry Smith #undef __FUNCT__
2987c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2988c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2989c87e5d42SMatthew Knepley {
2990c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2991c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2992c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2993c87e5d42SMatthew Knepley   PetscReal      atmp;
2994c87e5d42SMatthew Knepley   PetscScalar    *x;
2995c87e5d42SMatthew Knepley   MatScalar      *aa;
2996c87e5d42SMatthew Knepley 
2997c87e5d42SMatthew Knepley   PetscFunctionBegin;
2998e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2999c87e5d42SMatthew Knepley   aa = a->a;
3000c87e5d42SMatthew Knepley   ai = a->i;
3001c87e5d42SMatthew Knepley   aj = a->j;
3002c87e5d42SMatthew Knepley 
3003c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3004c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3005c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
300660e0710aSBarry 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);
3007c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
3008c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
3009289a08f5SMatthew Knepley     if (ncols) {
3010289a08f5SMatthew Knepley       /* Get first nonzero */
3011289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
3012289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
30132205254eSKarl Rupp         if (atmp > 1.0e-12) {
30142205254eSKarl Rupp           x[i] = atmp;
30152205254eSKarl Rupp           if (idx) idx[i] = aj[j];
30162205254eSKarl Rupp           break;
30172205254eSKarl Rupp         }
3018289a08f5SMatthew Knepley       }
301912431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
3020289a08f5SMatthew Knepley     } else {
3021289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
3022289a08f5SMatthew Knepley     }
3023c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
3024c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
3025289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3026c87e5d42SMatthew Knepley       aa++; aj++;
3027c87e5d42SMatthew Knepley     }
3028c87e5d42SMatthew Knepley   }
3029c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3030c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
3031c87e5d42SMatthew Knepley }
3032c87e5d42SMatthew Knepley 
3033c87e5d42SMatthew Knepley #undef __FUNCT__
3034985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
3035985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3036985db425SBarry Smith {
3037985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3038985db425SBarry Smith   PetscErrorCode ierr;
3039d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3040985db425SBarry Smith   PetscScalar    *x;
3041985db425SBarry Smith   MatScalar      *aa;
3042985db425SBarry Smith 
3043985db425SBarry Smith   PetscFunctionBegin;
3044e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3045985db425SBarry Smith   aa = a->a;
3046985db425SBarry Smith   ai = a->i;
3047985db425SBarry Smith   aj = a->j;
3048985db425SBarry Smith 
3049985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3050985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3051985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
3052e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3053985db425SBarry Smith   for (i=0; i<m; i++) {
3054985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
3055d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
3056985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
3057985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3058985db425SBarry Smith       x[i] = 0.0;
3059985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
3060985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
3061985db425SBarry Smith         for (j=0; j<ncols; j++) {
3062985db425SBarry Smith           if (aj[j] > j) {
3063985db425SBarry Smith             idx[i] = j;
3064985db425SBarry Smith             break;
3065985db425SBarry Smith           }
3066985db425SBarry Smith         }
3067985db425SBarry Smith       }
3068985db425SBarry Smith     }
3069985db425SBarry Smith     for (j=0; j<ncols; j++) {
3070985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3071985db425SBarry Smith       aa++; aj++;
3072e34fafa9SBarry Smith     }
3073e34fafa9SBarry Smith   }
3074e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3075e34fafa9SBarry Smith   PetscFunctionReturn(0);
3076e34fafa9SBarry Smith }
3077bbead8a2SBarry Smith 
3078bbead8a2SBarry Smith #include <petscblaslapack.h>
307906873bf2SBarry Smith #include <petsc-private/kernels/blockinvert.h>
3080bbead8a2SBarry Smith 
3081bbead8a2SBarry Smith #undef __FUNCT__
3082bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
3083713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3084bbead8a2SBarry Smith {
3085bbead8a2SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
3086bbead8a2SBarry Smith   PetscErrorCode ierr;
308733d57670SJed Brown   PetscInt       i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3088bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
3089bbead8a2SBarry Smith   PetscReal      shift = 0.0;
3090bbead8a2SBarry Smith 
3091bbead8a2SBarry Smith   PetscFunctionBegin;
30924a0d0026SBarry Smith   if (a->ibdiagvalid) {
30934a0d0026SBarry Smith     if (values) *values = a->ibdiag;
30944a0d0026SBarry Smith     PetscFunctionReturn(0);
30954a0d0026SBarry Smith   }
3096bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
3097bbead8a2SBarry Smith   if (!a->ibdiag) {
3098785e854fSJed Brown     ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr);
30993bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
3100bbead8a2SBarry Smith   }
3101bbead8a2SBarry Smith   diag = a->ibdiag;
3102bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
3103bbead8a2SBarry Smith   /* factor and invert each block */
3104bbead8a2SBarry Smith   switch (bs) {
3105bbead8a2SBarry Smith   case 1:
3106bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3107bbead8a2SBarry Smith       ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
3108bbead8a2SBarry Smith       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3109bbead8a2SBarry Smith     }
3110bbead8a2SBarry Smith     break;
3111bbead8a2SBarry Smith   case 2:
3112bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3113bbead8a2SBarry Smith       ij[0] = 2*i; ij[1] = 2*i + 1;
3114bbead8a2SBarry Smith       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
311596b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
311696b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
3117bbead8a2SBarry Smith       diag += 4;
3118bbead8a2SBarry Smith     }
3119bbead8a2SBarry Smith     break;
3120bbead8a2SBarry Smith   case 3:
3121bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3122bbead8a2SBarry Smith       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3123bbead8a2SBarry Smith       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
312496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
312596b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
3126bbead8a2SBarry Smith       diag += 9;
3127bbead8a2SBarry Smith     }
3128bbead8a2SBarry Smith     break;
3129bbead8a2SBarry Smith   case 4:
3130bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3131bbead8a2SBarry Smith       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3132bbead8a2SBarry Smith       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
313396b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
313496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
3135bbead8a2SBarry Smith       diag += 16;
3136bbead8a2SBarry Smith     }
3137bbead8a2SBarry Smith     break;
3138bbead8a2SBarry Smith   case 5:
3139bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3140bbead8a2SBarry 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;
3141bbead8a2SBarry Smith       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
314296b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
314396b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
3144bbead8a2SBarry Smith       diag += 25;
3145bbead8a2SBarry Smith     }
3146bbead8a2SBarry Smith     break;
3147bbead8a2SBarry Smith   case 6:
3148bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3149bbead8a2SBarry 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;
3150bbead8a2SBarry Smith       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
315196b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
315296b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3153bbead8a2SBarry Smith       diag += 36;
3154bbead8a2SBarry Smith     }
3155bbead8a2SBarry Smith     break;
3156bbead8a2SBarry Smith   case 7:
3157bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3158bbead8a2SBarry 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;
3159bbead8a2SBarry Smith       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
316096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
316196b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3162bbead8a2SBarry Smith       diag += 49;
3163bbead8a2SBarry Smith     }
3164bbead8a2SBarry Smith     break;
3165bbead8a2SBarry Smith   default:
3166dcca6d9dSJed Brown     ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr);
3167bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3168bbead8a2SBarry Smith       for (j=0; j<bs; j++) {
3169bbead8a2SBarry Smith         IJ[j] = bs*i + j;
3170bbead8a2SBarry Smith       }
3171bbead8a2SBarry Smith       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
317296b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
317396b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3174bbead8a2SBarry Smith       diag += bs2;
3175bbead8a2SBarry Smith     }
3176bbead8a2SBarry Smith     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3177bbead8a2SBarry Smith   }
3178bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3179bbead8a2SBarry Smith   PetscFunctionReturn(0);
3180bbead8a2SBarry Smith }
3181bbead8a2SBarry Smith 
318273a71a0fSBarry Smith #undef __FUNCT__
318373a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_SeqAIJ"
318473a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
318573a71a0fSBarry Smith {
318673a71a0fSBarry Smith   PetscErrorCode ierr;
318773a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
318873a71a0fSBarry Smith   PetscScalar    a;
318973a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
319073a71a0fSBarry Smith 
319173a71a0fSBarry Smith   PetscFunctionBegin;
319273a71a0fSBarry Smith   if (!x->assembled) {
319373a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
319473a71a0fSBarry Smith     for (i=0; i<m; i++) {
319573a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
319673a71a0fSBarry Smith         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
319773a71a0fSBarry Smith         col  = (PetscInt)(n*PetscRealPart(a));
319873a71a0fSBarry Smith         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
319973a71a0fSBarry Smith       }
320073a71a0fSBarry Smith     }
320173a71a0fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
320273a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
320373a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
320473a71a0fSBarry Smith   PetscFunctionReturn(0);
320573a71a0fSBarry Smith }
320673a71a0fSBarry Smith 
3207682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
32080a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3209cb5b572fSBarry Smith                                         MatGetRow_SeqAIJ,
3210cb5b572fSBarry Smith                                         MatRestoreRow_SeqAIJ,
3211cb5b572fSBarry Smith                                         MatMult_SeqAIJ,
321297304618SKris Buschelman                                 /*  4*/ MatMultAdd_SeqAIJ,
32137c922b88SBarry Smith                                         MatMultTranspose_SeqAIJ,
32147c922b88SBarry Smith                                         MatMultTransposeAdd_SeqAIJ,
3215db4efbfdSBarry Smith                                         0,
3216db4efbfdSBarry Smith                                         0,
3217db4efbfdSBarry Smith                                         0,
3218db4efbfdSBarry Smith                                 /* 10*/ 0,
3219cb5b572fSBarry Smith                                         MatLUFactor_SeqAIJ,
3220cb5b572fSBarry Smith                                         0,
322141f059aeSBarry Smith                                         MatSOR_SeqAIJ,
322217ab2063SBarry Smith                                         MatTranspose_SeqAIJ,
322397304618SKris Buschelman                                 /*1 5*/ MatGetInfo_SeqAIJ,
3224cb5b572fSBarry Smith                                         MatEqual_SeqAIJ,
3225cb5b572fSBarry Smith                                         MatGetDiagonal_SeqAIJ,
3226cb5b572fSBarry Smith                                         MatDiagonalScale_SeqAIJ,
3227cb5b572fSBarry Smith                                         MatNorm_SeqAIJ,
322897304618SKris Buschelman                                 /* 20*/ 0,
3229cb5b572fSBarry Smith                                         MatAssemblyEnd_SeqAIJ,
3230cb5b572fSBarry Smith                                         MatSetOption_SeqAIJ,
3231cb5b572fSBarry Smith                                         MatZeroEntries_SeqAIJ,
3232d519adbfSMatthew Knepley                                 /* 24*/ MatZeroRows_SeqAIJ,
3233db4efbfdSBarry Smith                                         0,
3234db4efbfdSBarry Smith                                         0,
3235db4efbfdSBarry Smith                                         0,
3236db4efbfdSBarry Smith                                         0,
32374994cf47SJed Brown                                 /* 29*/ MatSetUp_SeqAIJ,
3238db4efbfdSBarry Smith                                         0,
3239db4efbfdSBarry Smith                                         0,
32408c778c55SBarry Smith                                         0,
32418c778c55SBarry Smith                                         0,
3242d519adbfSMatthew Knepley                                 /* 34*/ MatDuplicate_SeqAIJ,
3243cb5b572fSBarry Smith                                         0,
3244cb5b572fSBarry Smith                                         0,
3245cb5b572fSBarry Smith                                         MatILUFactor_SeqAIJ,
3246cb5b572fSBarry Smith                                         0,
3247d519adbfSMatthew Knepley                                 /* 39*/ MatAXPY_SeqAIJ,
3248cb5b572fSBarry Smith                                         MatGetSubMatrices_SeqAIJ,
3249cb5b572fSBarry Smith                                         MatIncreaseOverlap_SeqAIJ,
3250cb5b572fSBarry Smith                                         MatGetValues_SeqAIJ,
3251cb5b572fSBarry Smith                                         MatCopy_SeqAIJ,
3252d519adbfSMatthew Knepley                                 /* 44*/ MatGetRowMax_SeqAIJ,
3253cb5b572fSBarry Smith                                         MatScale_SeqAIJ,
3254cb5b572fSBarry Smith                                         0,
325579299369SBarry Smith                                         MatDiagonalSet_SeqAIJ,
32566e169961SBarry Smith                                         MatZeroRowsColumns_SeqAIJ,
325773a71a0fSBarry Smith                                 /* 49*/ MatSetRandom_SeqAIJ,
32583b2fbd54SBarry Smith                                         MatGetRowIJ_SeqAIJ,
32593b2fbd54SBarry Smith                                         MatRestoreRowIJ_SeqAIJ,
32603b2fbd54SBarry Smith                                         MatGetColumnIJ_SeqAIJ,
3261a93ec695SBarry Smith                                         MatRestoreColumnIJ_SeqAIJ,
326293dfae19SHong Zhang                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3263b9617806SBarry Smith                                         0,
32640513a670SBarry Smith                                         0,
3265cda55fadSBarry Smith                                         MatPermute_SeqAIJ,
3266cda55fadSBarry Smith                                         0,
3267d519adbfSMatthew Knepley                                 /* 59*/ 0,
3268b9b97703SBarry Smith                                         MatDestroy_SeqAIJ,
3269b9b97703SBarry Smith                                         MatView_SeqAIJ,
3270357abbc8SBarry Smith                                         0,
3271321b30b9SSatish Balay                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3272321b30b9SSatish Balay                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3273321b30b9SSatish Balay                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3274ee4f033dSBarry Smith                                         0,
3275ee4f033dSBarry Smith                                         0,
3276ee4f033dSBarry Smith                                         0,
3277d519adbfSMatthew Knepley                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3278c87e5d42SMatthew Knepley                                         MatGetRowMinAbs_SeqAIJ,
3279ee4f033dSBarry Smith                                         0,
3280ee4f033dSBarry Smith                                         MatSetColoring_SeqAIJ,
3281dcf5cc72SBarry Smith                                         0,
3282d519adbfSMatthew Knepley                                 /* 74*/ MatSetValuesAdifor_SeqAIJ,
32833acb8795SBarry Smith                                         MatFDColoringApply_AIJ,
328497304618SKris Buschelman                                         0,
328597304618SKris Buschelman                                         0,
328697304618SKris Buschelman                                         0,
32876ce1633cSBarry Smith                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
328897304618SKris Buschelman                                         0,
328997304618SKris Buschelman                                         0,
329097304618SKris Buschelman                                         0,
3291bc011b1eSHong Zhang                                         MatLoad_SeqAIJ,
3292d519adbfSMatthew Knepley                                 /* 84*/ MatIsSymmetric_SeqAIJ,
32931cbb95d3SBarry Smith                                         MatIsHermitian_SeqAIJ,
32946284ec50SHong Zhang                                         0,
32956284ec50SHong Zhang                                         0,
3296bc011b1eSHong Zhang                                         0,
3297d519adbfSMatthew Knepley                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
329826be0446SHong Zhang                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
329926be0446SHong Zhang                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
330065e8a0caSHong Zhang                                         MatPtAP_SeqAIJ_SeqAIJ,
33014a1b09b7SHong Zhang                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_DenseAxpy,
330265e8a0caSHong Zhang                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
33036fc122caSHong Zhang                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
33046fc122caSHong Zhang                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
33056fc122caSHong Zhang                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
33062121bac1SHong Zhang                                         0,
33072121bac1SHong Zhang                                 /* 99*/ 0,
3308609c6c4dSKris Buschelman                                         0,
3309609c6c4dSKris Buschelman                                         0,
331087d4246cSBarry Smith                                         MatConjugate_SeqAIJ,
331187d4246cSBarry Smith                                         0,
3312d519adbfSMatthew Knepley                                 /*104*/ MatSetValuesRow_SeqAIJ,
331399cafbc1SBarry Smith                                         MatRealPart_SeqAIJ,
3314f5edf698SHong Zhang                                         MatImaginaryPart_SeqAIJ,
3315f5edf698SHong Zhang                                         0,
33162bebee5dSHong Zhang                                         0,
3317cbd44569SHong Zhang                                 /*109*/ MatMatSolve_SeqAIJ,
3318985db425SBarry Smith                                         0,
33192af78befSBarry Smith                                         MatGetRowMin_SeqAIJ,
33202af78befSBarry Smith                                         0,
3321599ef60dSHong Zhang                                         MatMissingDiagonal_SeqAIJ,
3322d519adbfSMatthew Knepley                                 /*114*/ 0,
3323599ef60dSHong Zhang                                         0,
33243c2a7987SHong Zhang                                         0,
3325fe97e370SBarry Smith                                         0,
3326fbdbba38SShri Abhyankar                                         0,
3327fbdbba38SShri Abhyankar                                 /*119*/ 0,
3328fbdbba38SShri Abhyankar                                         0,
3329fbdbba38SShri Abhyankar                                         0,
333082d44351SHong Zhang                                         0,
3331b3a44c85SBarry Smith                                         MatGetMultiProcBlock_SeqAIJ,
33320716a85fSBarry Smith                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3333bbead8a2SBarry Smith                                         MatGetColumnNorms_SeqAIJ,
333437868618SMatthew G Knepley                                         MatInvertBlockDiagonal_SeqAIJ,
333537868618SMatthew G Knepley                                         0,
333637868618SMatthew G Knepley                                         0,
33375df89d91SHong Zhang                                 /*129*/ 0,
333875648e8dSHong Zhang                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
333975648e8dSHong Zhang                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
334075648e8dSHong Zhang                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3341b9af6bddSHong Zhang                                         MatTransposeColoringCreate_SeqAIJ,
3342b9af6bddSHong Zhang                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
33432b8ad9a3SHong Zhang                                         MatTransColoringApplyDenToSp_SeqAIJ,
33442b8ad9a3SHong Zhang                                         MatRARt_SeqAIJ_SeqAIJ,
33452b8ad9a3SHong Zhang                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
33463964eb88SJed Brown                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
33473964eb88SJed Brown                                  /*139*/0,
3348f9426fe0SMark Adams                                         0,
33491919a2e2SJed Brown                                         0,
33503a062f41SBarry Smith                                         MatFDColoringSetUp_SeqXAIJ,
33513a062f41SBarry Smith                                         MatFindOffBlockDiagonalEntries_SeqAIJ
33529e29f15eSvictorle };
335317ab2063SBarry Smith 
33544a2ae208SSatish Balay #undef __FUNCT__
33554a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
33567087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3357bef8e0ddSBarry Smith {
3358bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
335997f1f81fSBarry Smith   PetscInt   i,nz,n;
3360bef8e0ddSBarry Smith 
3361bef8e0ddSBarry Smith   PetscFunctionBegin;
3362bef8e0ddSBarry Smith   nz = aij->maxnz;
3363d0f46423SBarry Smith   n  = mat->rmap->n;
3364bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3365bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3366bef8e0ddSBarry Smith   }
3367bef8e0ddSBarry Smith   aij->nz = nz;
3368bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3369bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3370bef8e0ddSBarry Smith   }
3371bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3372bef8e0ddSBarry Smith }
3373bef8e0ddSBarry Smith 
33744a2ae208SSatish Balay #undef __FUNCT__
33754a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3376bef8e0ddSBarry Smith /*@
3377bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3378bef8e0ddSBarry Smith        in the matrix.
3379bef8e0ddSBarry Smith 
3380bef8e0ddSBarry Smith   Input Parameters:
3381bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3382bef8e0ddSBarry Smith -  indices - the column indices
3383bef8e0ddSBarry Smith 
338415091d37SBarry Smith   Level: advanced
338515091d37SBarry Smith 
3386bef8e0ddSBarry Smith   Notes:
3387bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3388bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3389bef8e0ddSBarry Smith   of the MatSetValues() operation.
3390bef8e0ddSBarry Smith 
3391bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3392d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3393bef8e0ddSBarry Smith 
3394bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3395bef8e0ddSBarry Smith 
3396b9617806SBarry Smith     The indices should start with zero, not one.
3397b9617806SBarry Smith 
3398bef8e0ddSBarry Smith @*/
33997087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3400bef8e0ddSBarry Smith {
34014ac538c5SBarry Smith   PetscErrorCode ierr;
3402bef8e0ddSBarry Smith 
3403bef8e0ddSBarry Smith   PetscFunctionBegin;
34040700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
34054482741eSBarry Smith   PetscValidPointer(indices,2);
34064ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3407bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3408bef8e0ddSBarry Smith }
3409bef8e0ddSBarry Smith 
3410be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3411be6bf707SBarry Smith 
34124a2ae208SSatish Balay #undef __FUNCT__
34134a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
34147087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3415be6bf707SBarry Smith {
3416be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
34176849ba73SBarry Smith   PetscErrorCode ierr;
3418d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3419be6bf707SBarry Smith 
3420be6bf707SBarry Smith   PetscFunctionBegin;
3421169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3422be6bf707SBarry Smith 
3423be6bf707SBarry Smith   /* allocate space for values if not already there */
3424be6bf707SBarry Smith   if (!aij->saved_values) {
3425785e854fSJed Brown     ierr = PetscMalloc1((nz+1),&aij->saved_values);CHKERRQ(ierr);
34263bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3427be6bf707SBarry Smith   }
3428be6bf707SBarry Smith 
3429be6bf707SBarry Smith   /* copy values over */
343087828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3431be6bf707SBarry Smith   PetscFunctionReturn(0);
3432be6bf707SBarry Smith }
3433be6bf707SBarry Smith 
34344a2ae208SSatish Balay #undef __FUNCT__
3435b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3436be6bf707SBarry Smith /*@
3437be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3438be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3439be6bf707SBarry Smith        nonlinear portion.
3440be6bf707SBarry Smith 
3441be6bf707SBarry Smith    Collect on Mat
3442be6bf707SBarry Smith 
3443be6bf707SBarry Smith   Input Parameters:
34440e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3445be6bf707SBarry Smith 
344615091d37SBarry Smith   Level: advanced
344715091d37SBarry Smith 
3448be6bf707SBarry Smith   Common Usage, with SNESSolve():
3449be6bf707SBarry Smith $    Create Jacobian matrix
3450be6bf707SBarry Smith $    Set linear terms into matrix
3451be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3452be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3453be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3454512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3455be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3456be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3457be6bf707SBarry Smith $    In your Jacobian routine
3458be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3459be6bf707SBarry Smith $      Set nonlinear terms in matrix
3460be6bf707SBarry Smith 
3461be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3462be6bf707SBarry Smith $    // build linear portion of Jacobian
3463512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3464be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3465be6bf707SBarry Smith $    loop over nonlinear iterations
3466be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3467be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3468be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3469be6bf707SBarry Smith $       Solve linear system with Jacobian
3470be6bf707SBarry Smith $    endloop
3471be6bf707SBarry Smith 
3472be6bf707SBarry Smith   Notes:
3473be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3474512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3475be6bf707SBarry Smith     calling this routine.
3476be6bf707SBarry Smith 
34770c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
34780c468ba9SBarry Smith     and does not allocated additional space.
34790c468ba9SBarry Smith 
3480be6bf707SBarry Smith .seealso: MatRetrieveValues()
3481be6bf707SBarry Smith 
3482be6bf707SBarry Smith @*/
34837087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3484be6bf707SBarry Smith {
34854ac538c5SBarry Smith   PetscErrorCode ierr;
3486be6bf707SBarry Smith 
3487be6bf707SBarry Smith   PetscFunctionBegin;
34880700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3489e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3490e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
34914ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3492be6bf707SBarry Smith   PetscFunctionReturn(0);
3493be6bf707SBarry Smith }
3494be6bf707SBarry Smith 
34954a2ae208SSatish Balay #undef __FUNCT__
34964a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
34977087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3498be6bf707SBarry Smith {
3499be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
35006849ba73SBarry Smith   PetscErrorCode ierr;
3501d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3502be6bf707SBarry Smith 
3503be6bf707SBarry Smith   PetscFunctionBegin;
3504169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3505f23aa3ddSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3506be6bf707SBarry Smith   /* copy values over */
350787828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3508be6bf707SBarry Smith   PetscFunctionReturn(0);
3509be6bf707SBarry Smith }
3510be6bf707SBarry Smith 
35114a2ae208SSatish Balay #undef __FUNCT__
35124a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3513be6bf707SBarry Smith /*@
3514be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3515be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3516be6bf707SBarry Smith        nonlinear portion.
3517be6bf707SBarry Smith 
3518be6bf707SBarry Smith    Collect on Mat
3519be6bf707SBarry Smith 
3520be6bf707SBarry Smith   Input Parameters:
3521be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3522be6bf707SBarry Smith 
352315091d37SBarry Smith   Level: advanced
352415091d37SBarry Smith 
3525be6bf707SBarry Smith .seealso: MatStoreValues()
3526be6bf707SBarry Smith 
3527be6bf707SBarry Smith @*/
35287087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3529be6bf707SBarry Smith {
35304ac538c5SBarry Smith   PetscErrorCode ierr;
3531be6bf707SBarry Smith 
3532be6bf707SBarry Smith   PetscFunctionBegin;
35330700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3534e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3535e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
35364ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3537be6bf707SBarry Smith   PetscFunctionReturn(0);
3538be6bf707SBarry Smith }
3539be6bf707SBarry Smith 
3540f83d6046SBarry Smith 
3541be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
35424a2ae208SSatish Balay #undef __FUNCT__
35434a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
354417ab2063SBarry Smith /*@C
3545682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
35460d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
35476e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
354851c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
35492bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
355017ab2063SBarry Smith 
3551db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3552db81eaa0SLois Curfman McInnes 
355317ab2063SBarry Smith    Input Parameters:
3554db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
355517ab2063SBarry Smith .  m - number of rows
355617ab2063SBarry Smith .  n - number of columns
355717ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
355851c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
35590298fd71SBarry Smith          (possibly different for each row) or NULL
356017ab2063SBarry Smith 
356117ab2063SBarry Smith    Output Parameter:
3562416022c9SBarry Smith .  A - the matrix
356317ab2063SBarry Smith 
3564175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3565ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3566175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3567175b88e8SBarry Smith 
3568b259b22eSLois Curfman McInnes    Notes:
356949a6f317SBarry Smith    If nnz is given then nz is ignored
357049a6f317SBarry Smith 
357117ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
357217ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
35730002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
357444cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
357517ab2063SBarry Smith 
357617ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
35770298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
35783d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
35796da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
358017ab2063SBarry Smith 
3581682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
35824fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3583682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
35846c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
35856c7ebb05SLois Curfman McInnes 
35866c7ebb05SLois Curfman McInnes    Options Database Keys:
3587698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
35889db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
358917ab2063SBarry Smith 
3590027ccd11SLois Curfman McInnes    Level: intermediate
3591027ccd11SLois Curfman McInnes 
359269b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
359336db0b34SBarry Smith 
359417ab2063SBarry Smith @*/
35957087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
359617ab2063SBarry Smith {
3597dfbe8321SBarry Smith   PetscErrorCode ierr;
35986945ee14SBarry Smith 
35993a40ed3dSBarry Smith   PetscFunctionBegin;
3600f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3601117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3602c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3603d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3604273d9f13SBarry Smith   PetscFunctionReturn(0);
3605273d9f13SBarry Smith }
3606273d9f13SBarry Smith 
36074a2ae208SSatish Balay #undef __FUNCT__
36084a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3609273d9f13SBarry Smith /*@C
3610273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3611273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3612273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3613273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3614273d9f13SBarry Smith 
3615273d9f13SBarry Smith    Collective on MPI_Comm
3616273d9f13SBarry Smith 
3617273d9f13SBarry Smith    Input Parameters:
36181c4f3114SJed Brown +  B - The matrix
3619273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3620273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
36210298fd71SBarry Smith          (possibly different for each row) or NULL
3622273d9f13SBarry Smith 
3623273d9f13SBarry Smith    Notes:
362449a6f317SBarry Smith      If nnz is given then nz is ignored
362549a6f317SBarry Smith 
3626273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3627273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3628273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3629273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3630273d9f13SBarry Smith 
3631273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
36320298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3633273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3634273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3635273d9f13SBarry Smith 
3636aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3637aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3638aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3639aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3640aa95bbe8SBarry Smith 
3641a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3642a96a251dSBarry Smith    entries or columns indices
3643a96a251dSBarry Smith 
3644273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3645273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3646273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3647273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3648273d9f13SBarry Smith 
3649273d9f13SBarry Smith    Options Database Keys:
3650698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3651698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3652273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3653273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3654273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3655273d9f13SBarry Smith 
3656273d9f13SBarry Smith    Level: intermediate
3657273d9f13SBarry Smith 
365869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3659273d9f13SBarry Smith 
3660273d9f13SBarry Smith @*/
36617087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3662273d9f13SBarry Smith {
36634ac538c5SBarry Smith   PetscErrorCode ierr;
3664a23d5eceSKris Buschelman 
3665a23d5eceSKris Buschelman   PetscFunctionBegin;
36666ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
36676ba663aaSJed Brown   PetscValidType(B,1);
36684ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3669a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3670a23d5eceSKris Buschelman }
3671a23d5eceSKris Buschelman 
3672a23d5eceSKris Buschelman #undef __FUNCT__
3673a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
36747087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3675a23d5eceSKris Buschelman {
3676273d9f13SBarry Smith   Mat_SeqAIJ     *b;
36772576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
36786849ba73SBarry Smith   PetscErrorCode ierr;
367997f1f81fSBarry Smith   PetscInt       i;
3680273d9f13SBarry Smith 
3681273d9f13SBarry Smith   PetscFunctionBegin;
36822576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3683a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3684c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3685c461c341SBarry Smith     nz             = 0;
3686c461c341SBarry Smith   }
3687c461c341SBarry Smith 
368826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
368926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3690899cda47SBarry Smith 
3691435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
369260e0710aSBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3693b73539f3SBarry Smith   if (nnz) {
3694d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
369560e0710aSBarry 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]);
369660e0710aSBarry 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);
3697b73539f3SBarry Smith     }
3698b73539f3SBarry Smith   }
3699b73539f3SBarry Smith 
3700273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
37012205254eSKarl Rupp 
3702273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3703273d9f13SBarry Smith 
3704ab93d7beSBarry Smith   if (!skipallocation) {
37052ee49352SLisandro Dalcin     if (!b->imax) {
3706dcca6d9dSJed Brown       ierr = PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);CHKERRQ(ierr);
37073bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
37082ee49352SLisandro Dalcin     }
3709273d9f13SBarry Smith     if (!nnz) {
3710435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3711c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3712d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3713d0f46423SBarry Smith       nz = nz*B->rmap->n;
3714273d9f13SBarry Smith     } else {
3715273d9f13SBarry Smith       nz = 0;
3716d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3717273d9f13SBarry Smith     }
3718ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
37192205254eSKarl Rupp     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;
3720ab93d7beSBarry Smith 
3721273d9f13SBarry Smith     /* allocate the matrix space */
37222ee49352SLisandro Dalcin     ierr    = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3723dcca6d9dSJed Brown     ierr    = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr);
37243bb1ff40SBarry Smith     ierr    = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3725bfeeae90SHong Zhang     b->i[0] = 0;
3726d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
37275da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
37285da197adSKris Buschelman     }
3729273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3730e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3731e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3732b31eba2aSShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
3733b31eba2aSShri Abhyankar     ierr = MatZeroEntries_SeqAIJ(B);CHKERRQ(ierr);
3734b31eba2aSShri Abhyankar #endif
3735c461c341SBarry Smith   } else {
3736e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
3737e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
3738c461c341SBarry Smith   }
3739273d9f13SBarry Smith 
3740273d9f13SBarry Smith   b->nz               = 0;
3741273d9f13SBarry Smith   b->maxnz            = nz;
3742273d9f13SBarry Smith   B->info.nz_unneeded = (double)b->maxnz;
37432205254eSKarl Rupp   if (realalloc) {
37442205254eSKarl Rupp     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
37452205254eSKarl Rupp   }
3746273d9f13SBarry Smith   PetscFunctionReturn(0);
3747273d9f13SBarry Smith }
3748273d9f13SBarry Smith 
3749a1661176SMatthew Knepley #undef  __FUNCT__
3750a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
375158d36128SBarry Smith /*@
3752a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3753a1661176SMatthew Knepley 
3754a1661176SMatthew Knepley    Input Parameters:
3755a1661176SMatthew Knepley +  B - the matrix
3756a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3757a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3758a1661176SMatthew Knepley -  v - optional values in the matrix
3759a1661176SMatthew Knepley 
3760a1661176SMatthew Knepley    Level: developer
3761a1661176SMatthew Knepley 
376258d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
376358d36128SBarry Smith 
3764a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3765a1661176SMatthew Knepley 
3766a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3767a1661176SMatthew Knepley @*/
3768a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3769a1661176SMatthew Knepley {
3770a1661176SMatthew Knepley   PetscErrorCode ierr;
3771a1661176SMatthew Knepley 
3772a1661176SMatthew Knepley   PetscFunctionBegin;
37730700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
37746ba663aaSJed Brown   PetscValidType(B,1);
37754ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3776a1661176SMatthew Knepley   PetscFunctionReturn(0);
3777a1661176SMatthew Knepley }
3778a1661176SMatthew Knepley 
3779a1661176SMatthew Knepley #undef  __FUNCT__
3780a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
37817087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3782a1661176SMatthew Knepley {
3783a1661176SMatthew Knepley   PetscInt       i;
3784a1661176SMatthew Knepley   PetscInt       m,n;
3785a1661176SMatthew Knepley   PetscInt       nz;
3786a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3787a1661176SMatthew Knepley   PetscScalar    *values;
3788a1661176SMatthew Knepley   PetscErrorCode ierr;
3789a1661176SMatthew Knepley 
3790a1661176SMatthew Knepley   PetscFunctionBegin;
379165e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3792779a8d59SSatish Balay 
3793779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3794779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3795779a8d59SSatish Balay 
3796779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3797785e854fSJed Brown   ierr = PetscMalloc1((m+1), &nnz);CHKERRQ(ierr);
3798a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3799b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3800a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
380165e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3802a1661176SMatthew Knepley     nnz[i] = nz;
3803a1661176SMatthew Knepley   }
3804a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3805a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3806a1661176SMatthew Knepley 
3807a1661176SMatthew Knepley   if (v) {
3808a1661176SMatthew Knepley     values = (PetscScalar*) v;
3809a1661176SMatthew Knepley   } else {
38101795a4d1SJed Brown     ierr = PetscCalloc1(nz_max, &values);CHKERRQ(ierr);
3811a1661176SMatthew Knepley   }
3812a1661176SMatthew Knepley 
3813a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3814b7940d39SSatish Balay     nz   = Ii[i+1] - Ii[i];
3815b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3816a1661176SMatthew Knepley   }
3817a1661176SMatthew Knepley 
3818a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3819a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3820a1661176SMatthew Knepley 
3821a1661176SMatthew Knepley   if (!v) {
3822a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3823a1661176SMatthew Knepley   }
38247827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3825a1661176SMatthew Knepley   PetscFunctionReturn(0);
3826a1661176SMatthew Knepley }
3827a1661176SMatthew Knepley 
3828c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
382906873bf2SBarry Smith #include <petsc-private/kernels/petscaxpy.h>
3830170fe5c8SBarry Smith 
3831170fe5c8SBarry Smith #undef __FUNCT__
3832170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3833170fe5c8SBarry Smith /*
3834170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3835170fe5c8SBarry Smith 
3836170fe5c8SBarry Smith                n                       p                          p
3837170fe5c8SBarry Smith         (              )       (              )         (                  )
3838170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3839170fe5c8SBarry Smith         (              )       (              )         (                  )
3840170fe5c8SBarry Smith 
3841170fe5c8SBarry Smith */
3842170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3843170fe5c8SBarry Smith {
3844170fe5c8SBarry Smith   PetscErrorCode    ierr;
3845170fe5c8SBarry Smith   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3846170fe5c8SBarry Smith   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3847170fe5c8SBarry Smith   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
38481de00fd4SBarry Smith   PetscInt          i,n,m,q,p;
3849170fe5c8SBarry Smith   const PetscInt    *ii,*idx;
3850170fe5c8SBarry Smith   const PetscScalar *b,*a,*a_q;
3851170fe5c8SBarry Smith   PetscScalar       *c,*c_q;
3852170fe5c8SBarry Smith 
3853170fe5c8SBarry Smith   PetscFunctionBegin;
3854d0f46423SBarry Smith   m    = A->rmap->n;
3855d0f46423SBarry Smith   n    = A->cmap->n;
3856d0f46423SBarry Smith   p    = B->cmap->n;
3857170fe5c8SBarry Smith   a    = sub_a->v;
3858170fe5c8SBarry Smith   b    = sub_b->a;
3859170fe5c8SBarry Smith   c    = sub_c->v;
3860170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3861170fe5c8SBarry Smith 
3862170fe5c8SBarry Smith   ii  = sub_b->i;
3863170fe5c8SBarry Smith   idx = sub_b->j;
3864170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3865170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3866170fe5c8SBarry Smith     while (q-->0) {
3867170fe5c8SBarry Smith       c_q = c + m*(*idx);
3868170fe5c8SBarry Smith       a_q = a + m*i;
3869854c7f52SBarry Smith       PetscKernelAXPY(c_q,*b,a_q,m);
3870170fe5c8SBarry Smith       idx++;
3871170fe5c8SBarry Smith       b++;
3872170fe5c8SBarry Smith     }
3873170fe5c8SBarry Smith   }
3874170fe5c8SBarry Smith   PetscFunctionReturn(0);
3875170fe5c8SBarry Smith }
3876170fe5c8SBarry Smith 
3877170fe5c8SBarry Smith #undef __FUNCT__
3878170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3879170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3880170fe5c8SBarry Smith {
3881170fe5c8SBarry Smith   PetscErrorCode ierr;
3882d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3883170fe5c8SBarry Smith   Mat            Cmat;
3884170fe5c8SBarry Smith 
3885170fe5c8SBarry Smith   PetscFunctionBegin;
388660e0710aSBarry 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);
3887ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
3888170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
388933d57670SJed Brown   ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr);
3890170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
38910298fd71SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
3892d73949e8SHong Zhang 
3893d73949e8SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
38942205254eSKarl Rupp 
3895170fe5c8SBarry Smith   *C = Cmat;
3896170fe5c8SBarry Smith   PetscFunctionReturn(0);
3897170fe5c8SBarry Smith }
3898170fe5c8SBarry Smith 
3899170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3900170fe5c8SBarry Smith #undef __FUNCT__
3901170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3902170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3903170fe5c8SBarry Smith {
3904170fe5c8SBarry Smith   PetscErrorCode ierr;
3905170fe5c8SBarry Smith 
3906170fe5c8SBarry Smith   PetscFunctionBegin;
3907170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
39083ff4c91cSHong Zhang     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3909170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
39103ff4c91cSHong Zhang     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3911170fe5c8SBarry Smith   }
39123ff4c91cSHong Zhang   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3913170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
39143ff4c91cSHong Zhang   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3915170fe5c8SBarry Smith   PetscFunctionReturn(0);
3916170fe5c8SBarry Smith }
3917170fe5c8SBarry Smith 
3918170fe5c8SBarry Smith 
39190bad9183SKris Buschelman /*MC
3920fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
39210bad9183SKris Buschelman    based on compressed sparse row format.
39220bad9183SKris Buschelman 
39230bad9183SKris Buschelman    Options Database Keys:
39240bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
39250bad9183SKris Buschelman 
39260bad9183SKris Buschelman   Level: beginner
39270bad9183SKris Buschelman 
3928f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
39290bad9183SKris Buschelman M*/
39300bad9183SKris Buschelman 
3931ccd284c7SBarry Smith /*MC
3932ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
3933ccd284c7SBarry Smith 
3934ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
3935ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
3936ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
3937ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3938ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3939ccd284c7SBarry Smith 
3940ccd284c7SBarry Smith    Options Database Keys:
3941ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
3942ccd284c7SBarry Smith 
3943ccd284c7SBarry Smith   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
3944ccd284c7SBarry Smith    enough exist.
3945ccd284c7SBarry Smith 
3946ccd284c7SBarry Smith   Level: beginner
3947ccd284c7SBarry Smith 
3948ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3949ccd284c7SBarry Smith M*/
3950ccd284c7SBarry Smith 
3951ccd284c7SBarry Smith /*MC
3952ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
3953ccd284c7SBarry Smith 
3954ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3955ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3956ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3957ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3958ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3959ccd284c7SBarry Smith 
3960ccd284c7SBarry Smith    Options Database Keys:
3961ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3962ccd284c7SBarry Smith 
3963ccd284c7SBarry Smith   Level: beginner
3964ccd284c7SBarry Smith 
3965ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3966ccd284c7SBarry Smith M*/
3967ccd284c7SBarry Smith 
3968b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
39698cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3970b5e56a35SBarry Smith #endif
3971ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
39728cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat*);
3973af1023dbSSatish Balay #endif
39748cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3975*af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
3976*af8000cdSHong Zhang PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
3977*af8000cdSHong Zhang #endif
39788cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
39798cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
39807087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool*);
3981611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
39828cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3983611f576cSBarry Smith #endif
3984611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
39858cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3986611f576cSBarry Smith #endif
3987b500a6b7SJose David Bermeol #if defined(PETSC_HAVE_MKL_PARDISO)
3988d615f992SSatish Balay PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mkl_pardiso(Mat,MatFactorType,Mat*);
3989b500a6b7SJose David Bermeol #endif
3990f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
39918cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3992f3c0ef26SHong Zhang #endif
3993913c410bSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
39948cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3995eb3b5408SSatish Balay #endif
3996913c410bSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
39978cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3998586621ddSJed Brown #endif
39997087e0deSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
400069e15a41SShri Abhyankar PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_klu(Mat,MatFactorType,Mat*);
400169e15a41SShri Abhyankar #endif
4002719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
40038cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
4004719d5645SBarry Smith #endif
4005b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
40068cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
40077087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
40087087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
4009b3866ffcSBarry Smith #endif
401017f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
40118cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
401217f1a0eaSHong Zhang #endif
401317667f90SBarry Smith 
4014c0c8ee5eSDmitry Karpeev 
40158c778c55SBarry Smith #undef __FUNCT__
40168c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray"
40178c778c55SBarry Smith /*@C
40188c778c55SBarry Smith    MatSeqAIJGetArray - gives access to the array where the data for a SeqSeqAIJ matrix is stored
40198c778c55SBarry Smith 
40208c778c55SBarry Smith    Not Collective
40218c778c55SBarry Smith 
40228c778c55SBarry Smith    Input Parameter:
4023579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
40248c778c55SBarry Smith 
40258c778c55SBarry Smith    Output Parameter:
40268c778c55SBarry Smith .   array - pointer to the data
40278c778c55SBarry Smith 
40288c778c55SBarry Smith    Level: intermediate
40298c778c55SBarry Smith 
4030774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
40318c778c55SBarry Smith @*/
40328c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
40338c778c55SBarry Smith {
40348c778c55SBarry Smith   PetscErrorCode ierr;
40358c778c55SBarry Smith 
40368c778c55SBarry Smith   PetscFunctionBegin;
40378c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
40388c778c55SBarry Smith   PetscFunctionReturn(0);
40398c778c55SBarry Smith }
40408c778c55SBarry Smith 
40418c778c55SBarry Smith #undef __FUNCT__
404221e72a00SBarry Smith #define __FUNCT__ "MatSeqAIJGetMaxRowNonzeros"
404321e72a00SBarry Smith /*@C
404421e72a00SBarry Smith    MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
404521e72a00SBarry Smith 
404621e72a00SBarry Smith    Not Collective
404721e72a00SBarry Smith 
404821e72a00SBarry Smith    Input Parameter:
4049579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
405021e72a00SBarry Smith 
405121e72a00SBarry Smith    Output Parameter:
405221e72a00SBarry Smith .   nz - the maximum number of nonzeros in any row
405321e72a00SBarry Smith 
405421e72a00SBarry Smith    Level: intermediate
405521e72a00SBarry Smith 
405621e72a00SBarry Smith .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
405721e72a00SBarry Smith @*/
405821e72a00SBarry Smith PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
405921e72a00SBarry Smith {
406021e72a00SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
406121e72a00SBarry Smith 
406221e72a00SBarry Smith   PetscFunctionBegin;
406321e72a00SBarry Smith   *nz = aij->rmax;
406421e72a00SBarry Smith   PetscFunctionReturn(0);
406521e72a00SBarry Smith }
406621e72a00SBarry Smith 
406721e72a00SBarry Smith #undef __FUNCT__
40688c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray"
40698c778c55SBarry Smith /*@C
4070579dbff0SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray()
40718c778c55SBarry Smith 
40728c778c55SBarry Smith    Not Collective
40738c778c55SBarry Smith 
40748c778c55SBarry Smith    Input Parameters:
4075579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
40768c778c55SBarry Smith .  array - pointer to the data
40778c778c55SBarry Smith 
40788c778c55SBarry Smith    Level: intermediate
40798c778c55SBarry Smith 
4080774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
40818c778c55SBarry Smith @*/
40828c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
40838c778c55SBarry Smith {
40848c778c55SBarry Smith   PetscErrorCode ierr;
40858c778c55SBarry Smith 
40868c778c55SBarry Smith   PetscFunctionBegin;
40878c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
40888c778c55SBarry Smith   PetscFunctionReturn(0);
40898c778c55SBarry Smith }
40908c778c55SBarry Smith 
40914a2ae208SSatish Balay #undef __FUNCT__
40924a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
40938cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4094273d9f13SBarry Smith {
4095273d9f13SBarry Smith   Mat_SeqAIJ     *b;
4096dfbe8321SBarry Smith   PetscErrorCode ierr;
409738baddfdSBarry Smith   PetscMPIInt    size;
4098273d9f13SBarry Smith 
4099273d9f13SBarry Smith   PetscFunctionBegin;
4100ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
4101e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
4102273d9f13SBarry Smith 
4103b00a9115SJed Brown   ierr = PetscNewLog(B,&b);CHKERRQ(ierr);
41042205254eSKarl Rupp 
4105b0a32e0cSBarry Smith   B->data = (void*)b;
41062205254eSKarl Rupp 
4107549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
41082205254eSKarl Rupp 
4109416022c9SBarry Smith   b->row                = 0;
4110416022c9SBarry Smith   b->col                = 0;
411182bf6240SBarry Smith   b->icol               = 0;
4112b810aeb4SBarry Smith   b->reallocs           = 0;
411336db0b34SBarry Smith   b->ignorezeroentries  = PETSC_FALSE;
4114f1e2ffcdSBarry Smith   b->roworiented        = PETSC_TRUE;
4115416022c9SBarry Smith   b->nonew              = 0;
4116416022c9SBarry Smith   b->diag               = 0;
4117416022c9SBarry Smith   b->solve_work         = 0;
41182a1b7f2aSHong Zhang   B->spptr              = 0;
4119be6bf707SBarry Smith   b->saved_values       = 0;
4120d7f994e1SBarry Smith   b->idiag              = 0;
412171f1c65dSBarry Smith   b->mdiag              = 0;
412271f1c65dSBarry Smith   b->ssor_work          = 0;
412371f1c65dSBarry Smith   b->omega              = 1.0;
412471f1c65dSBarry Smith   b->fshift             = 0.0;
412571f1c65dSBarry Smith   b->idiagvalid         = PETSC_FALSE;
4126bbead8a2SBarry Smith   b->ibdiagvalid        = PETSC_FALSE;
4127a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
4128a30b2313SHong Zhang   b->xtoy               = 0;
4129a30b2313SHong Zhang   b->XtoY               = 0;
413017ab2063SBarry Smith 
413135d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
4132bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
4133bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
41348c778c55SBarry Smith 
4135b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4136bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_matlab_C",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
4137bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
4138bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
4139b3866ffcSBarry Smith #endif
4140b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
4141bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
4142b5e56a35SBarry Smith #endif
4143ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
4144bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_essl_C",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
4145719d5645SBarry Smith #endif
4146611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
4147bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_C",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
4148611f576cSBarry Smith #endif
4149b500a6b7SJose David Bermeol #if defined(PETSC_HAVE_MKL_PARDISO)
4150d615f992SSatish Balay   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mkl_pardiso_C",MatGetFactor_aij_mkl_pardiso);CHKERRQ(ierr);
4151b500a6b7SJose David Bermeol #endif
4152f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
4153bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_dist_C",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
4154f3c0ef26SHong Zhang #endif
4155611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
4156bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_aij_mumps);CHKERRQ(ierr);
4157611f576cSBarry Smith #endif
4158913c410bSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
4159bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_umfpack_C",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
4160eb3b5408SSatish Balay #endif
4161913c410bSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
4162bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_cholmod_C",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
4163586621ddSJed Brown #endif
41647087e0deSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
416569e15a41SShri Abhyankar   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_klu_C",MatGetFactor_seqaij_klu);CHKERRQ(ierr);
416669e15a41SShri Abhyankar #endif
4167719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
4168bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_lusol_C",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
4169719d5645SBarry Smith #endif
417017f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
4171bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_clique_C",MatGetFactor_aij_clique);CHKERRQ(ierr);
417217f1a0eaSHong Zhang #endif
417317f1a0eaSHong Zhang 
4174bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_petsc_C",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
4175bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactorAvailable_petsc_C",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
4176bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_bas_C",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
4177bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
4178bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
4179bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
4180bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
4181bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
4182bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
4183bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
4184*af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
4185*af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);CHKERRQ(ierr);
4186*af8000cdSHong Zhang #endif
4187bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4188bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4189bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
4190bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
4191bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
4192bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
4193bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
4194bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
41954108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
419617667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
41973a40ed3dSBarry Smith   PetscFunctionReturn(0);
419817ab2063SBarry Smith }
419917ab2063SBarry Smith 
42004a2ae208SSatish Balay #undef __FUNCT__
4201b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
4202b24902e0SBarry Smith /*
4203b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4204b24902e0SBarry Smith */
4205ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
420617ab2063SBarry Smith {
4207416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
42086849ba73SBarry Smith   PetscErrorCode ierr;
4209d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
421017ab2063SBarry Smith 
42113a40ed3dSBarry Smith   PetscFunctionBegin;
4212273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4213273d9f13SBarry Smith 
4214d5f3da31SBarry Smith   C->factortype = A->factortype;
4215416022c9SBarry Smith   c->row        = 0;
4216416022c9SBarry Smith   c->col        = 0;
421782bf6240SBarry Smith   c->icol       = 0;
42186ad4291fSHong Zhang   c->reallocs   = 0;
421917ab2063SBarry Smith 
42206ad4291fSHong Zhang   C->assembled = PETSC_TRUE;
422117ab2063SBarry Smith 
4222aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4223aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4224eec197d1SBarry Smith 
4225dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&c->imax,m,&c->ilen);CHKERRQ(ierr);
42263bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
422717ab2063SBarry Smith   for (i=0; i<m; i++) {
4228416022c9SBarry Smith     c->imax[i] = a->imax[i];
4229416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
423017ab2063SBarry Smith   }
423117ab2063SBarry Smith 
423217ab2063SBarry Smith   /* allocate the matrix space */
4233f77e22a1SHong Zhang   if (mallocmatspace) {
4234dcca6d9dSJed Brown     ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr);
42353bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
42362205254eSKarl Rupp 
4237f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
42382205254eSKarl Rupp 
423997f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
424017ab2063SBarry Smith     if (m > 0) {
424197f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4242be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4243bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4244be6bf707SBarry Smith       } else {
4245bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
424617ab2063SBarry Smith       }
424708480c60SBarry Smith     }
4248f77e22a1SHong Zhang   }
424917ab2063SBarry Smith 
42506ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4251416022c9SBarry Smith   c->roworiented       = a->roworiented;
4252416022c9SBarry Smith   c->nonew             = a->nonew;
4253416022c9SBarry Smith   if (a->diag) {
4254785e854fSJed Brown     ierr = PetscMalloc1((m+1),&c->diag);CHKERRQ(ierr);
42553bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
425617ab2063SBarry Smith     for (i=0; i<m; i++) {
4257416022c9SBarry Smith       c->diag[i] = a->diag[i];
425817ab2063SBarry Smith     }
42593a40ed3dSBarry Smith   } else c->diag = 0;
42602205254eSKarl Rupp 
42616ad4291fSHong Zhang   c->solve_work         = 0;
42626ad4291fSHong Zhang   c->saved_values       = 0;
42636ad4291fSHong Zhang   c->idiag              = 0;
426471f1c65dSBarry Smith   c->ssor_work          = 0;
4265a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
4266e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
4267e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
42686ad4291fSHong Zhang   c->xtoy               = 0;
42696ad4291fSHong Zhang   c->XtoY               = 0;
42706ad4291fSHong Zhang 
4271893ad86cSHong Zhang   c->rmax         = a->rmax;
4272416022c9SBarry Smith   c->nz           = a->nz;
42738ed568f8SMatthew G Knepley   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4274273d9f13SBarry Smith   C->preallocated = PETSC_TRUE;
4275754ec7b1SSatish Balay 
42766ad4291fSHong Zhang   c->compressedrow.use   = a->compressedrow.use;
42776ad4291fSHong Zhang   c->compressedrow.nrows = a->compressedrow.nrows;
4278cd6b891eSBarry Smith   if (a->compressedrow.use) {
42796ad4291fSHong Zhang     i    = a->compressedrow.nrows;
4280dcca6d9dSJed Brown     ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr);
42816ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
42826ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
428327ea64f8SHong Zhang   } else {
428427ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
42850298fd71SBarry Smith     c->compressedrow.i      = NULL;
42860298fd71SBarry Smith     c->compressedrow.rindex = NULL;
42876ad4291fSHong Zhang   }
4288e56f5c9eSBarry Smith   C->nonzerostate = A->nonzerostate;
42894846f1f5SKris Buschelman 
42902205254eSKarl Rupp   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4291140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
42923a40ed3dSBarry Smith   PetscFunctionReturn(0);
429317ab2063SBarry Smith }
429417ab2063SBarry Smith 
42954a2ae208SSatish Balay #undef __FUNCT__
4296b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
4297b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4298b24902e0SBarry Smith {
4299b24902e0SBarry Smith   PetscErrorCode ierr;
4300b24902e0SBarry Smith 
4301b24902e0SBarry Smith   PetscFunctionBegin;
4302ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
43034b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4304cfd3f464SBarry Smith   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
430533d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
4306cfd3f464SBarry Smith   }
4307a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4308f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4309b24902e0SBarry Smith   PetscFunctionReturn(0);
4310b24902e0SBarry Smith }
4311b24902e0SBarry Smith 
4312b24902e0SBarry Smith #undef __FUNCT__
43134a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
4314112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4315fbdbba38SShri Abhyankar {
4316fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4317fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4318fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4319fbdbba38SShri Abhyankar   int            fd;
4320fbdbba38SShri Abhyankar   PetscMPIInt    size;
4321fbdbba38SShri Abhyankar   MPI_Comm       comm;
4322bbead8a2SBarry Smith   PetscInt       bs = 1;
4323fbdbba38SShri Abhyankar 
4324fbdbba38SShri Abhyankar   PetscFunctionBegin;
4325fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4326fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4327fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4328bbead8a2SBarry Smith 
43290298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
43300298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
4331bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
43321814747fSJed Brown   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
4333bbead8a2SBarry Smith 
4334fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4335fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4336fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4337fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4338fbdbba38SShri Abhyankar 
4339bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4340fbdbba38SShri Abhyankar 
4341fbdbba38SShri Abhyankar   /* read in row lengths */
4342785e854fSJed Brown   ierr = PetscMalloc1(M,&rowlengths);CHKERRQ(ierr);
4343fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4344fbdbba38SShri Abhyankar 
4345fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4346fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
434760e0710aSBarry 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);
4348fbdbba38SShri Abhyankar 
4349fbdbba38SShri Abhyankar   /* set global size if not set already*/
4350f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4351fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4352aabbc4fbSShri Abhyankar   } else {
4353fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
4354fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
43554c5b953cSHong Zhang     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
43564c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
43574c5b953cSHong Zhang     }
435860e0710aSBarry 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);
4359aabbc4fbSShri Abhyankar   }
4360fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4361fbdbba38SShri Abhyankar   a    = (Mat_SeqAIJ*)newMat->data;
4362fbdbba38SShri Abhyankar 
4363fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4364fbdbba38SShri Abhyankar 
4365fbdbba38SShri Abhyankar   /* read in nonzero values */
4366fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4367fbdbba38SShri Abhyankar 
4368fbdbba38SShri Abhyankar   /* set matrix "i" values */
4369fbdbba38SShri Abhyankar   a->i[0] = 0;
4370fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4371fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4372fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4373fbdbba38SShri Abhyankar   }
4374fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4375fbdbba38SShri Abhyankar 
4376fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4377fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4378fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4379fbdbba38SShri Abhyankar }
4380fbdbba38SShri Abhyankar 
4381fbdbba38SShri Abhyankar #undef __FUNCT__
4382b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4383ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
43847264ac53SSatish Balay {
43857264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4386dfbe8321SBarry Smith   PetscErrorCode ierr;
4387eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4388eeffb40dSHong Zhang   PetscInt k;
4389eeffb40dSHong Zhang #endif
43907264ac53SSatish Balay 
43913a40ed3dSBarry Smith   PetscFunctionBegin;
4392bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4393d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4394ca44d042SBarry Smith     *flg = PETSC_FALSE;
4395ca44d042SBarry Smith     PetscFunctionReturn(0);
4396bcd2baecSBarry Smith   }
43977264ac53SSatish Balay 
43987264ac53SSatish Balay   /* if the a->i are the same */
4399d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4400abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
44017264ac53SSatish Balay 
44027264ac53SSatish Balay   /* if a->j are the same */
440397f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4404abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4405bcd2baecSBarry Smith 
4406bcd2baecSBarry Smith   /* if a->a are the same */
4407eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4408eeffb40dSHong Zhang   for (k=0; k<a->nz; k++) {
4409eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4410eeffb40dSHong Zhang       *flg = PETSC_FALSE;
44113a40ed3dSBarry Smith       PetscFunctionReturn(0);
4412eeffb40dSHong Zhang     }
4413eeffb40dSHong Zhang   }
4414eeffb40dSHong Zhang #else
4415eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4416eeffb40dSHong Zhang #endif
4417eeffb40dSHong Zhang   PetscFunctionReturn(0);
44187264ac53SSatish Balay }
441936db0b34SBarry Smith 
44204a2ae208SSatish Balay #undef __FUNCT__
44214a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
442205869f15SSatish Balay /*@
442336db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
442436db0b34SBarry Smith               provided by the user.
442536db0b34SBarry Smith 
4426c75a6043SHong Zhang       Collective on MPI_Comm
442736db0b34SBarry Smith 
442836db0b34SBarry Smith    Input Parameters:
442936db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
443036db0b34SBarry Smith .   m - number of rows
443136db0b34SBarry Smith .   n - number of columns
443236db0b34SBarry Smith .   i - row indices
443336db0b34SBarry Smith .   j - column indices
443436db0b34SBarry Smith -   a - matrix values
443536db0b34SBarry Smith 
443636db0b34SBarry Smith    Output Parameter:
443736db0b34SBarry Smith .   mat - the matrix
443836db0b34SBarry Smith 
443936db0b34SBarry Smith    Level: intermediate
444036db0b34SBarry Smith 
444136db0b34SBarry Smith    Notes:
44420551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4443292fb18eSBarry Smith     once the matrix is destroyed and not before
444436db0b34SBarry Smith 
444536db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
444636db0b34SBarry Smith 
4447bfeeae90SHong Zhang        The i and j indices are 0 based
444836db0b34SBarry Smith 
4449a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4450a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
4451a4552177SSatish Balay     as shown:
4452a4552177SSatish Balay 
4453a4552177SSatish Balay         1 0 0
4454a4552177SSatish Balay         2 0 3
4455a4552177SSatish Balay         4 5 6
4456a4552177SSatish Balay 
4457a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
44589985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4459a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
4460a4552177SSatish Balay 
44619985e31cSBarry Smith 
446269b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
446336db0b34SBarry Smith 
446436db0b34SBarry Smith @*/
44657087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
446636db0b34SBarry Smith {
4467dfbe8321SBarry Smith   PetscErrorCode ierr;
4468cbcfb4deSHong Zhang   PetscInt       ii;
446936db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4470cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4471cbcfb4deSHong Zhang   PetscInt jj;
4472cbcfb4deSHong Zhang #endif
447336db0b34SBarry Smith 
447436db0b34SBarry Smith   PetscFunctionBegin;
4475f23aa3ddSBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4476f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4477f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4478a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4479ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4480ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4481ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4482dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&aij->imax,m,&aij->ilen);CHKERRQ(ierr);
4483ab93d7beSBarry Smith 
448436db0b34SBarry Smith   aij->i            = i;
448536db0b34SBarry Smith   aij->j            = j;
448636db0b34SBarry Smith   aij->a            = a;
448736db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
448836db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4489e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4490e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
449136db0b34SBarry Smith 
449236db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
449336db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
44942515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
449560e0710aSBarry 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]);
44969985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4497e32f2f54SBarry 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);
4498e32f2f54SBarry 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);
44999985e31cSBarry Smith     }
450036db0b34SBarry Smith #endif
450136db0b34SBarry Smith   }
45022515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
450336db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
450460e0710aSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
450560e0710aSBarry 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]);
450636db0b34SBarry Smith   }
450736db0b34SBarry Smith #endif
450836db0b34SBarry Smith 
4509b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4510b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
451136db0b34SBarry Smith   PetscFunctionReturn(0);
451236db0b34SBarry Smith }
45138a0b0e6bSVictor Minden #undef __FUNCT__
45148a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
451580ef6e79SMatthew G Knepley /*@C
4516d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
45178a0b0e6bSVictor Minden               provided by the user.
45188a0b0e6bSVictor Minden 
45198a0b0e6bSVictor Minden       Collective on MPI_Comm
45208a0b0e6bSVictor Minden 
45218a0b0e6bSVictor Minden    Input Parameters:
45228a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
45238a0b0e6bSVictor Minden .   m   - number of rows
45248a0b0e6bSVictor Minden .   n   - number of columns
45258a0b0e6bSVictor Minden .   i   - row indices
45268a0b0e6bSVictor Minden .   j   - column indices
45271230e6d1SVictor Minden .   a   - matrix values
45281230e6d1SVictor Minden .   nz  - number of nonzeros
45291230e6d1SVictor Minden -   idx - 0 or 1 based
45308a0b0e6bSVictor Minden 
45318a0b0e6bSVictor Minden    Output Parameter:
45328a0b0e6bSVictor Minden .   mat - the matrix
45338a0b0e6bSVictor Minden 
45348a0b0e6bSVictor Minden    Level: intermediate
45358a0b0e6bSVictor Minden 
45368a0b0e6bSVictor Minden    Notes:
45378a0b0e6bSVictor Minden        The i and j indices are 0 based
45388a0b0e6bSVictor Minden 
45398a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
45408a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
45418a0b0e6bSVictor Minden     as shown:
45428a0b0e6bSVictor Minden 
45438a0b0e6bSVictor Minden         1 0 0
45448a0b0e6bSVictor Minden         2 0 3
45458a0b0e6bSVictor Minden         4 5 6
45468a0b0e6bSVictor Minden 
45478a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
45488a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
45498a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
45508a0b0e6bSVictor Minden 
45518a0b0e6bSVictor Minden 
455269b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
45538a0b0e6bSVictor Minden 
45548a0b0e6bSVictor Minden @*/
45551230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
45568a0b0e6bSVictor Minden {
45578a0b0e6bSVictor Minden   PetscErrorCode ierr;
4558d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
45598a0b0e6bSVictor Minden 
45608a0b0e6bSVictor Minden 
45618a0b0e6bSVictor Minden   PetscFunctionBegin;
45621795a4d1SJed Brown   ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr);
45631230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
4564c8d679ebSHong Zhang     nnz[i[ii] - !!idx] += 1;
45651230e6d1SVictor Minden   }
45668a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
45678a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
45688a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
45691230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
45701230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
45711230e6d1SVictor Minden     if (idx) {
45721230e6d1SVictor Minden       row = i[ii] - 1;
45731230e6d1SVictor Minden       col = j[ii] - 1;
45741230e6d1SVictor Minden     } else {
45751230e6d1SVictor Minden       row = i[ii];
45761230e6d1SVictor Minden       col = j[ii];
45778a0b0e6bSVictor Minden     }
45781230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
45798a0b0e6bSVictor Minden   }
45808a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
45818a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4582d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
45838a0b0e6bSVictor Minden   PetscFunctionReturn(0);
45848a0b0e6bSVictor Minden }
458536db0b34SBarry Smith 
4586cc8ba8e1SBarry Smith #undef __FUNCT__
4587ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4588dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4589cc8ba8e1SBarry Smith {
4590dfbe8321SBarry Smith   PetscErrorCode ierr;
4591cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
459236db0b34SBarry Smith 
4593cc8ba8e1SBarry Smith   PetscFunctionBegin;
45948ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4595cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4596cc8ba8e1SBarry Smith     a->coloring = coloring;
459712c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
459897f1f81fSBarry Smith     PetscInt        i,*larray;
459912c595b3SBarry Smith     ISColoring      ocoloring;
460008b6dcc0SBarry Smith     ISColoringValue *colors;
460112c595b3SBarry Smith 
460212c595b3SBarry Smith     /* set coloring for diagonal portion */
4603785e854fSJed Brown     ierr = PetscMalloc1(A->cmap->n,&larray);CHKERRQ(ierr);
46042205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) larray[i] = i;
46050298fd71SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);CHKERRQ(ierr);
4606785e854fSJed Brown     ierr = PetscMalloc1(A->cmap->n,&colors);CHKERRQ(ierr);
46072205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
460812c595b3SBarry Smith     ierr        = PetscFree(larray);CHKERRQ(ierr);
4609d0f46423SBarry Smith     ierr        = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
461012c595b3SBarry Smith     a->coloring = ocoloring;
461112c595b3SBarry Smith   }
4612cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4613cc8ba8e1SBarry Smith }
4614cc8ba8e1SBarry Smith 
4615ee4f033dSBarry Smith #undef __FUNCT__
4616ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
461797f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4618ee4f033dSBarry Smith {
4619ee4f033dSBarry Smith   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
4620d0f46423SBarry Smith   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
462154f21887SBarry Smith   MatScalar       *v      = a->a;
462254f21887SBarry Smith   PetscScalar     *values = (PetscScalar*)advalues;
462308b6dcc0SBarry Smith   ISColoringValue *color;
4624ee4f033dSBarry Smith 
4625ee4f033dSBarry Smith   PetscFunctionBegin;
4626e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4627ee4f033dSBarry Smith   color = a->coloring->colors;
4628ee4f033dSBarry Smith   /* loop over rows */
4629ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4630ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4631ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
46322205254eSKarl Rupp     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
4633ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4634cc8ba8e1SBarry Smith   }
4635cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4636cc8ba8e1SBarry Smith }
463736db0b34SBarry Smith 
4638acf2f550SJed Brown #undef __FUNCT__
4639acf2f550SJed Brown #define __FUNCT__ "MatSeqAIJInvalidateDiagonal"
4640acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4641acf2f550SJed Brown {
4642acf2f550SJed Brown   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4643acf2f550SJed Brown   PetscErrorCode ierr;
4644acf2f550SJed Brown 
4645acf2f550SJed Brown   PetscFunctionBegin;
4646acf2f550SJed Brown   a->idiagvalid  = PETSC_FALSE;
4647acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
46482205254eSKarl Rupp 
4649acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4650acf2f550SJed Brown   PetscFunctionReturn(0);
4651acf2f550SJed Brown }
4652acf2f550SJed Brown 
465381824310SBarry Smith /*
465481824310SBarry Smith     Special version for direct calls from Fortran
465581824310SBarry Smith */
4656b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
465781824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
465881824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
465981824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
466081824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
466181824310SBarry Smith #endif
466281824310SBarry Smith 
466381824310SBarry Smith /* Change these macros so can be used in void function */
466481824310SBarry Smith #undef CHKERRQ
4665ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
466681824310SBarry Smith #undef SETERRQ2
4667e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
46684994cf47SJed Brown #undef SETERRQ3
46694994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
467081824310SBarry Smith 
467181824310SBarry Smith #undef __FUNCT__
467281824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
46738cc058d9SJed 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)
467481824310SBarry Smith {
467581824310SBarry Smith   Mat            A  = *AA;
467681824310SBarry Smith   PetscInt       m  = *mm, n = *nn;
467781824310SBarry Smith   InsertMode     is = *isis;
467881824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
467981824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
468081824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
468181824310SBarry Smith   PetscErrorCode ierr;
468281824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
468354f21887SBarry Smith   MatScalar      *ap,value,*aa;
4684ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4685ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
468681824310SBarry Smith 
468781824310SBarry Smith   PetscFunctionBegin;
46884994cf47SJed Brown   MatCheckPreallocated(A,1);
468981824310SBarry Smith   imax  = a->imax;
469081824310SBarry Smith   ai    = a->i;
469181824310SBarry Smith   ailen = a->ilen;
469281824310SBarry Smith   aj    = a->j;
469381824310SBarry Smith   aa    = a->a;
469481824310SBarry Smith 
469581824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
469681824310SBarry Smith     row = im[k];
469781824310SBarry Smith     if (row < 0) continue;
469881824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4699ce94432eSBarry Smith     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
470081824310SBarry Smith #endif
470181824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
470281824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
470381824310SBarry Smith     low  = 0;
470481824310SBarry Smith     high = nrow;
470581824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
470681824310SBarry Smith       if (in[l] < 0) continue;
470781824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4708ce94432eSBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
470981824310SBarry Smith #endif
471081824310SBarry Smith       col = in[l];
47112205254eSKarl Rupp       if (roworiented) value = v[l + k*n];
47122205254eSKarl Rupp       else value = v[k + l*m];
47132205254eSKarl Rupp 
471481824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
471581824310SBarry Smith 
47162205254eSKarl Rupp       if (col <= lastcol) low = 0;
47172205254eSKarl Rupp       else high = nrow;
471881824310SBarry Smith       lastcol = col;
471981824310SBarry Smith       while (high-low > 5) {
472081824310SBarry Smith         t = (low+high)/2;
472181824310SBarry Smith         if (rp[t] > col) high = t;
472281824310SBarry Smith         else             low  = t;
472381824310SBarry Smith       }
472481824310SBarry Smith       for (i=low; i<high; i++) {
472581824310SBarry Smith         if (rp[i] > col) break;
472681824310SBarry Smith         if (rp[i] == col) {
472781824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
472881824310SBarry Smith           else                  ap[i] = value;
472981824310SBarry Smith           goto noinsert;
473081824310SBarry Smith         }
473181824310SBarry Smith       }
473281824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
473381824310SBarry Smith       if (nonew == 1) goto noinsert;
4734ce94432eSBarry Smith       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4735fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
473681824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
473781824310SBarry Smith       /* shift up all the later entries in this row */
473881824310SBarry Smith       for (ii=N; ii>=i; ii--) {
473981824310SBarry Smith         rp[ii+1] = rp[ii];
474081824310SBarry Smith         ap[ii+1] = ap[ii];
474181824310SBarry Smith       }
474281824310SBarry Smith       rp[i] = col;
474381824310SBarry Smith       ap[i] = value;
4744e56f5c9eSBarry Smith       A->nonzerostate++;
474581824310SBarry Smith noinsert:;
474681824310SBarry Smith       low = i + 1;
474781824310SBarry Smith     }
474881824310SBarry Smith     ailen[row] = nrow;
474981824310SBarry Smith   }
475081824310SBarry Smith   PetscFunctionReturnVoid();
475181824310SBarry Smith }
47529f7953f8SBarry Smith 
475362298a1eSBarry Smith 
4754