xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 7734d3b51de2654fbe25805dd8188ffdf802e863)
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;
171d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
17254f21887SBarry Smith   MatScalar      *aa = aij->a;
17354f21887SBarry Smith   PetscScalar    *v;
174ace3abfcSBarry Smith   PetscBool      missing;
17579299369SBarry Smith 
17679299369SBarry Smith   PetscFunctionBegin;
17709f38230SBarry Smith   if (Y->assembled) {
1780298fd71SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);CHKERRQ(ierr);
17909f38230SBarry Smith     if (!missing) {
18079299369SBarry Smith       diag = aij->diag;
18179299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
18279299369SBarry Smith       if (is == INSERT_VALUES) {
18379299369SBarry Smith         for (i=0; i<m; i++) {
18479299369SBarry Smith           aa[diag[i]] = v[i];
18579299369SBarry Smith         }
18679299369SBarry Smith       } else {
18779299369SBarry Smith         for (i=0; i<m; i++) {
18879299369SBarry Smith           aa[diag[i]] += v[i];
18979299369SBarry Smith         }
19079299369SBarry Smith       }
19179299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
19279299369SBarry Smith       PetscFunctionReturn(0);
19379299369SBarry Smith     }
194acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
19509f38230SBarry Smith   }
19609f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
19709f38230SBarry Smith   PetscFunctionReturn(0);
19809f38230SBarry Smith }
19979299369SBarry Smith 
20079299369SBarry Smith #undef __FUNCT__
2014a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
2021a83f524SJed Brown PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
20317ab2063SBarry Smith {
204416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
205dfbe8321SBarry Smith   PetscErrorCode ierr;
20697f1f81fSBarry Smith   PetscInt       i,ishift;
20717ab2063SBarry Smith 
2083a40ed3dSBarry Smith   PetscFunctionBegin;
209d0f46423SBarry Smith   *m = A->rmap->n;
2103a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
211bfeeae90SHong Zhang   ishift = 0;
21253e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
2131a83f524SJed Brown     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
214bfeeae90SHong Zhang   } else if (oshift == 1) {
2151a83f524SJed Brown     PetscInt *tia;
216d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
2173b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
218785e854fSJed Brown     ierr = PetscMalloc1((A->rmap->n+1),&tia);CHKERRQ(ierr);
2191a83f524SJed Brown     for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1;
2201a83f524SJed Brown     *ia = tia;
221ecc77c7aSBarry Smith     if (ja) {
2221a83f524SJed Brown       PetscInt *tja;
223785e854fSJed Brown       ierr = PetscMalloc1((nz+1),&tja);CHKERRQ(ierr);
2241a83f524SJed Brown       for (i=0; i<nz; i++) tja[i] = a->j[i] + 1;
2251a83f524SJed Brown       *ja = tja;
226ecc77c7aSBarry Smith     }
2276945ee14SBarry Smith   } else {
228ecc77c7aSBarry Smith     *ia = a->i;
229ecc77c7aSBarry Smith     if (ja) *ja = a->j;
230a2ce50c7SBarry Smith   }
2313a40ed3dSBarry Smith   PetscFunctionReturn(0);
232a2744918SBarry Smith }
233a2744918SBarry Smith 
2344a2ae208SSatish Balay #undef __FUNCT__
2354a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
2361a83f524SJed Brown PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2376945ee14SBarry Smith {
238dfbe8321SBarry Smith   PetscErrorCode ierr;
2396945ee14SBarry Smith 
2403a40ed3dSBarry Smith   PetscFunctionBegin;
2413a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
242bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
243606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
244ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
245bcd2baecSBarry Smith   }
2463a40ed3dSBarry Smith   PetscFunctionReturn(0);
24717ab2063SBarry Smith }
24817ab2063SBarry Smith 
2494a2ae208SSatish Balay #undef __FUNCT__
2504a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
2511a83f524SJed Brown PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2523b2fbd54SBarry Smith {
2533b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
254dfbe8321SBarry Smith   PetscErrorCode ierr;
255d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
25697f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
2573b2fbd54SBarry Smith 
2583a40ed3dSBarry Smith   PetscFunctionBegin;
259899cda47SBarry Smith   *nn = n;
2603a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2613b2fbd54SBarry Smith   if (symmetric) {
2621a83f524SJed Brown     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
2633b2fbd54SBarry Smith   } else {
2641795a4d1SJed Brown     ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
265785e854fSJed Brown     ierr = PetscMalloc1((n+1),&cia);CHKERRQ(ierr);
266785e854fSJed Brown     ierr = PetscMalloc1((nz+1),&cja);CHKERRQ(ierr);
2673b2fbd54SBarry Smith     jj   = a->j;
2683b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
269bfeeae90SHong Zhang       collengths[jj[i]]++;
2703b2fbd54SBarry Smith     }
2713b2fbd54SBarry Smith     cia[0] = oshift;
2723b2fbd54SBarry Smith     for (i=0; i<n; i++) {
2733b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
2743b2fbd54SBarry Smith     }
27597f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
2763b2fbd54SBarry Smith     jj   = a->j;
277a93ec695SBarry Smith     for (row=0; row<m; row++) {
278a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
279a93ec695SBarry Smith       for (i=0; i<mr; i++) {
280bfeeae90SHong Zhang         col = *jj++;
2812205254eSKarl Rupp 
2823b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2833b2fbd54SBarry Smith       }
2843b2fbd54SBarry Smith     }
285606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2863b2fbd54SBarry Smith     *ia  = cia; *ja = cja;
2873b2fbd54SBarry Smith   }
2883a40ed3dSBarry Smith   PetscFunctionReturn(0);
2893b2fbd54SBarry Smith }
2903b2fbd54SBarry Smith 
2914a2ae208SSatish Balay #undef __FUNCT__
2924a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
2931a83f524SJed Brown PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2943b2fbd54SBarry Smith {
295dfbe8321SBarry Smith   PetscErrorCode ierr;
296606d414cSSatish Balay 
2973a40ed3dSBarry Smith   PetscFunctionBegin;
2983a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2993b2fbd54SBarry Smith 
300606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
301606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
3023a40ed3dSBarry Smith   PetscFunctionReturn(0);
3033b2fbd54SBarry Smith }
3043b2fbd54SBarry Smith 
3057cee066cSHong Zhang /*
3067cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
3077cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
308040ebd07SHong Zhang  spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
3097cee066cSHong Zhang */
3107cee066cSHong Zhang #undef __FUNCT__
3117cee066cSHong Zhang #define __FUNCT__ "MatGetColumnIJ_SeqAIJ_Color"
3127cee066cSHong 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)
3137cee066cSHong Zhang {
3147cee066cSHong Zhang   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3157cee066cSHong Zhang   PetscErrorCode ierr;
3167cee066cSHong Zhang   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
3177cee066cSHong Zhang   PetscInt       nz = a->i[m],row,*jj,mr,col;
3187cee066cSHong Zhang   PetscInt       *cspidx;
3197cee066cSHong Zhang 
3207cee066cSHong Zhang   PetscFunctionBegin;
3217cee066cSHong Zhang   *nn = n;
3227cee066cSHong Zhang   if (!ia) PetscFunctionReturn(0);
323625f6d37SHong Zhang 
3241795a4d1SJed Brown   ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
325785e854fSJed Brown   ierr = PetscMalloc1((n+1),&cia);CHKERRQ(ierr);
326785e854fSJed Brown   ierr = PetscMalloc1((nz+1),&cja);CHKERRQ(ierr);
327785e854fSJed Brown   ierr = PetscMalloc1((nz+1),&cspidx);CHKERRQ(ierr);
3287cee066cSHong Zhang   jj   = a->j;
3297cee066cSHong Zhang   for (i=0; i<nz; i++) {
3307cee066cSHong Zhang     collengths[jj[i]]++;
3317cee066cSHong Zhang   }
3327cee066cSHong Zhang   cia[0] = oshift;
3337cee066cSHong Zhang   for (i=0; i<n; i++) {
3347cee066cSHong Zhang     cia[i+1] = cia[i] + collengths[i];
3357cee066cSHong Zhang   }
3367cee066cSHong Zhang   ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
3377cee066cSHong Zhang   jj   = a->j;
3387cee066cSHong Zhang   for (row=0; row<m; row++) {
3397cee066cSHong Zhang     mr = a->i[row+1] - a->i[row];
3407cee066cSHong Zhang     for (i=0; i<mr; i++) {
3417cee066cSHong Zhang       col = *jj++;
3427cee066cSHong Zhang       cspidx[cia[col] + collengths[col] - oshift] = a->i[row] + i; /* index of a->j */
3437cee066cSHong Zhang       cja[cia[col] + collengths[col]++ - oshift]  = row + oshift;
3447cee066cSHong Zhang     }
3457cee066cSHong Zhang   }
3467cee066cSHong Zhang   ierr   = PetscFree(collengths);CHKERRQ(ierr);
3477cee066cSHong Zhang   *ia    = cia; *ja = cja;
3487cee066cSHong Zhang   *spidx = cspidx;
3497cee066cSHong Zhang   PetscFunctionReturn(0);
3507cee066cSHong Zhang }
3517cee066cSHong Zhang 
3527cee066cSHong Zhang #undef __FUNCT__
3537cee066cSHong Zhang #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ_Color"
3547cee066cSHong 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)
3557cee066cSHong Zhang {
3567cee066cSHong Zhang   PetscErrorCode ierr;
3577cee066cSHong Zhang 
3587cee066cSHong Zhang   PetscFunctionBegin;
3595243ef75SHong Zhang   ierr = MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
3607cee066cSHong Zhang   ierr = PetscFree(*spidx);CHKERRQ(ierr);
3617cee066cSHong Zhang   PetscFunctionReturn(0);
3627cee066cSHong Zhang }
3637cee066cSHong Zhang 
36487d4246cSBarry Smith #undef __FUNCT__
36587d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
36687d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
36787d4246cSBarry Smith {
36887d4246cSBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
36987d4246cSBarry Smith   PetscInt       *ai = a->i;
37087d4246cSBarry Smith   PetscErrorCode ierr;
37187d4246cSBarry Smith 
37287d4246cSBarry Smith   PetscFunctionBegin;
37387d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
37487d4246cSBarry Smith   PetscFunctionReturn(0);
37587d4246cSBarry Smith }
37687d4246cSBarry Smith 
377bd04181cSBarry Smith /*
378bd04181cSBarry Smith     MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions
379bd04181cSBarry Smith 
380bd04181cSBarry Smith       -   a single row of values is set with each call
381bd04181cSBarry Smith       -   no row or column indices are negative or (in error) larger than the number of rows or columns
382bd04181cSBarry Smith       -   the values are always added to the matrix, not set
383bd04181cSBarry Smith       -   no new locations are introduced in the nonzero structure of the matrix
384bd04181cSBarry Smith 
3851f763a69SBarry Smith      This does NOT assume the global column indices are sorted
386bd04181cSBarry Smith 
3871f763a69SBarry Smith */
388bd04181cSBarry Smith 
389189e4007SBarry Smith #include <petsc-private/isimpl.h>
390189e4007SBarry Smith #undef __FUNCT__
391189e4007SBarry Smith #define __FUNCT__ "MatSeqAIJSetValuesLocalFast"
392189e4007SBarry Smith PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
393189e4007SBarry Smith {
394189e4007SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3951f763a69SBarry Smith   PetscInt       low,high,t,row,nrow,i,col,l;
3961f763a69SBarry Smith   const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j;
3971f763a69SBarry Smith   PetscInt       lastcol = -1;
398189e4007SBarry Smith   MatScalar      *ap,value,*aa = a->a;
399189e4007SBarry Smith   const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices;
400189e4007SBarry Smith 
401f38dd0b8SBarry Smith   row = ridx[im[0]];
4021f763a69SBarry Smith   rp   = aj + ai[row];
4031f763a69SBarry Smith   ap = aa + ai[row];
4041f763a69SBarry Smith   nrow = ailen[row];
405189e4007SBarry Smith   low  = 0;
406189e4007SBarry Smith   high = nrow;
407189e4007SBarry Smith   for (l=0; l<n; l++) { /* loop over added columns */
408189e4007SBarry Smith     col = cidx[in[l]];
409f38dd0b8SBarry Smith     value = v[l];
410189e4007SBarry Smith 
411189e4007SBarry Smith     if (col <= lastcol) low = 0;
412189e4007SBarry Smith     else high = nrow;
413189e4007SBarry Smith     lastcol = col;
414189e4007SBarry Smith     while (high-low > 5) {
415189e4007SBarry Smith       t = (low+high)/2;
416189e4007SBarry Smith       if (rp[t] > col) high = t;
417189e4007SBarry Smith       else low = t;
418189e4007SBarry Smith     }
419189e4007SBarry Smith     for (i=low; i<high; i++) {
420189e4007SBarry Smith       if (rp[i] == col) {
4211f763a69SBarry Smith         ap[i] += value;
422189e4007SBarry Smith         low = i + 1;
4231f763a69SBarry Smith         break;
424189e4007SBarry Smith       }
425189e4007SBarry Smith     }
426189e4007SBarry Smith   }
427f38dd0b8SBarry Smith   return 0;
428189e4007SBarry Smith }
429189e4007SBarry Smith 
430bd04181cSBarry Smith #undef __FUNCT__
4314a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
43297f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
43317ab2063SBarry Smith {
434416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
435e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
43697f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
4376849ba73SBarry Smith   PetscErrorCode ierr;
438e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
43954f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
440ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
441ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
44217ab2063SBarry Smith 
4433a40ed3dSBarry Smith   PetscFunctionBegin;
44417ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
445416022c9SBarry Smith     row = im[k];
4465ef9f2a5SBarry Smith     if (row < 0) continue;
4472515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
448e32f2f54SBarry 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);
4493b2fbd54SBarry Smith #endif
450bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
45117ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
452416022c9SBarry Smith     low  = 0;
453c71e6ed7SBarry Smith     high = nrow;
45417ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
4555ef9f2a5SBarry Smith       if (in[l] < 0) continue;
4562515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
457e32f2f54SBarry 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);
4583b2fbd54SBarry Smith #endif
459bfeeae90SHong Zhang       col = in[l];
4604b0e389bSBarry Smith       if (roworiented) {
4615ef9f2a5SBarry Smith         value = v[l + k*n];
462bef8e0ddSBarry Smith       } else {
4634b0e389bSBarry Smith         value = v[k + l*m];
4644b0e389bSBarry Smith       }
46533b2b78bSBarry Smith       if ((value == 0.0 && ignorezeroentries) && (is == ADD_VALUES)) continue;
46636db0b34SBarry Smith 
4672205254eSKarl Rupp       if (col <= lastcol) low = 0;
4682205254eSKarl Rupp       else high = nrow;
469e2ee6c50SBarry Smith       lastcol = col;
470416022c9SBarry Smith       while (high-low > 5) {
471416022c9SBarry Smith         t = (low+high)/2;
472416022c9SBarry Smith         if (rp[t] > col) high = t;
473416022c9SBarry Smith         else low = t;
47417ab2063SBarry Smith       }
475416022c9SBarry Smith       for (i=low; i<high; i++) {
47617ab2063SBarry Smith         if (rp[i] > col) break;
47717ab2063SBarry Smith         if (rp[i] == col) {
478416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
47917ab2063SBarry Smith           else ap[i] = value;
480e44c0bd4SBarry Smith           low = i + 1;
48117ab2063SBarry Smith           goto noinsert;
48217ab2063SBarry Smith         }
48317ab2063SBarry Smith       }
484abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
485c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
486e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
487fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
488c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
489416022c9SBarry Smith       /* shift up all the later entries in this row */
490416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
49117ab2063SBarry Smith         rp[ii+1] = rp[ii];
49217ab2063SBarry Smith         ap[ii+1] = ap[ii];
49317ab2063SBarry Smith       }
49417ab2063SBarry Smith       rp[i] = col;
49517ab2063SBarry Smith       ap[i] = value;
496416022c9SBarry Smith       low   = i + 1;
497e56f5c9eSBarry Smith       A->nonzerostate++;
498e44c0bd4SBarry Smith noinsert:;
49917ab2063SBarry Smith     }
50017ab2063SBarry Smith     ailen[row] = nrow;
50117ab2063SBarry Smith   }
5023a40ed3dSBarry Smith   PetscFunctionReturn(0);
50317ab2063SBarry Smith }
50417ab2063SBarry Smith 
50581824310SBarry Smith 
5064a2ae208SSatish Balay #undef __FUNCT__
5074a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
508a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
5097eb43aa7SLois Curfman McInnes {
5107eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
51197f1f81fSBarry Smith   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
51297f1f81fSBarry Smith   PetscInt   *ai = a->i,*ailen = a->ilen;
51354f21887SBarry Smith   MatScalar  *ap,*aa = a->a;
5147eb43aa7SLois Curfman McInnes 
5153a40ed3dSBarry Smith   PetscFunctionBegin;
5167eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
5177eb43aa7SLois Curfman McInnes     row = im[k];
518e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
519e32f2f54SBarry 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);
520bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
5217eb43aa7SLois Curfman McInnes     nrow = ailen[row];
5227eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
523e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
524e32f2f54SBarry 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);
525bfeeae90SHong Zhang       col  = in[l];
5267eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
5277eb43aa7SLois Curfman McInnes       while (high-low > 5) {
5287eb43aa7SLois Curfman McInnes         t = (low+high)/2;
5297eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
5307eb43aa7SLois Curfman McInnes         else low = t;
5317eb43aa7SLois Curfman McInnes       }
5327eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
5337eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
5347eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
535b49de8d1SLois Curfman McInnes           *v++ = ap[i];
5367eb43aa7SLois Curfman McInnes           goto finished;
5377eb43aa7SLois Curfman McInnes         }
5387eb43aa7SLois Curfman McInnes       }
53997e567efSBarry Smith       *v++ = 0.0;
5407eb43aa7SLois Curfman McInnes finished:;
5417eb43aa7SLois Curfman McInnes     }
5427eb43aa7SLois Curfman McInnes   }
5433a40ed3dSBarry Smith   PetscFunctionReturn(0);
5447eb43aa7SLois Curfman McInnes }
5457eb43aa7SLois Curfman McInnes 
54617ab2063SBarry Smith 
5474a2ae208SSatish Balay #undef __FUNCT__
5484a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
549dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
55017ab2063SBarry Smith {
551416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5526849ba73SBarry Smith   PetscErrorCode ierr;
5536f69ff64SBarry Smith   PetscInt       i,*col_lens;
5546f69ff64SBarry Smith   int            fd;
555b37d52dbSMark F. Adams   FILE           *file;
55617ab2063SBarry Smith 
5573a40ed3dSBarry Smith   PetscFunctionBegin;
558b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
559785e854fSJed Brown   ierr = PetscMalloc1((4+A->rmap->n),&col_lens);CHKERRQ(ierr);
5602205254eSKarl Rupp 
5610700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
562d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
563d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
564416022c9SBarry Smith   col_lens[3] = a->nz;
565416022c9SBarry Smith 
566416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
567d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
568416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
56917ab2063SBarry Smith   }
570d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
571606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
572416022c9SBarry Smith 
573416022c9SBarry Smith   /* store column indices (zero start index) */
5746f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
575416022c9SBarry Smith 
576416022c9SBarry Smith   /* store nonzero values */
5776f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
578b37d52dbSMark F. Adams 
579b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
580b37d52dbSMark F. Adams   if (file) {
58133d57670SJed Brown     fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs));
582b37d52dbSMark F. Adams   }
5833a40ed3dSBarry Smith   PetscFunctionReturn(0);
58417ab2063SBarry Smith }
585416022c9SBarry Smith 
58609573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
587cd155464SBarry Smith 
5884a2ae208SSatish Balay #undef __FUNCT__
5894a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
590dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
591416022c9SBarry Smith {
592416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
593dfbe8321SBarry Smith   PetscErrorCode    ierr;
59460e0710aSBarry Smith   PetscInt          i,j,m = A->rmap->n;
595e060cb09SBarry Smith   const char        *name;
596f3ef73ceSBarry Smith   PetscViewerFormat format;
59717ab2063SBarry Smith 
5983a40ed3dSBarry Smith   PetscFunctionBegin;
599b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
60071c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
60197f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
60260e0710aSBarry Smith     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
603c337ccceSJed Brown       /* Need a dummy value to ensure the dimension of the matrix. */
604d00d2cf4SBarry Smith       nofinalvalue = 1;
605d00d2cf4SBarry Smith     }
606d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
607d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
60877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
609fbfe6fa7SJed Brown #if defined(PETSC_USE_COMPLEX)
610fbfe6fa7SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
611fbfe6fa7SJed Brown #else
61277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
613fbfe6fa7SJed Brown #endif
614b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
61517ab2063SBarry Smith 
61617ab2063SBarry Smith     for (i=0; i<m; i++) {
61760e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
618aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
619a9bf72d8SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",i+1,a->j[j]+1,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
62017ab2063SBarry Smith #else
62160e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr);
62217ab2063SBarry Smith #endif
62317ab2063SBarry Smith       }
62417ab2063SBarry Smith     }
625d00d2cf4SBarry Smith     if (nofinalvalue) {
626c337ccceSJed Brown #if defined(PETSC_USE_COMPLEX)
627c337ccceSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr);
628c337ccceSJed Brown #else
629d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
630c337ccceSJed Brown #endif
631d00d2cf4SBarry Smith     }
632317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
633fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
634d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
63568369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
636cd155464SBarry Smith     PetscFunctionReturn(0);
637fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
638d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
63944cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
64077431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
64160e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
642aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
64336db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
64460e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
64536db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
64660e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
64736db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
64860e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
6496831982aSBarry Smith         }
65044cd7ae7SLois Curfman McInnes #else
65160e0710aSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);}
65244cd7ae7SLois Curfman McInnes #endif
65344cd7ae7SLois Curfman McInnes       }
654b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
65544cd7ae7SLois Curfman McInnes     }
656d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
657fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
65897f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
659d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
660785e854fSJed Brown     ierr = PetscMalloc1((m+1),&sptr);CHKERRQ(ierr);
661496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
662496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
66360e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
664496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
665aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
66636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
667496be53dSLois Curfman McInnes #else
668496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
669496be53dSLois Curfman McInnes #endif
670496be53dSLois Curfman McInnes         }
671496be53dSLois Curfman McInnes       }
672496be53dSLois Curfman McInnes     }
6732e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
67477431f27SBarry Smith     ierr    = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
6752e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
6762205254eSKarl Rupp       if (i+4<m) {
6772205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]);CHKERRQ(ierr);
6782205254eSKarl Rupp       } else if (i+3<m) {
6792205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);CHKERRQ(ierr);
6802205254eSKarl Rupp       } else if (i+2<m) {
6812205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);
6822205254eSKarl Rupp       } else if (i+1<m) {
6832205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);
6842205254eSKarl Rupp       } else if (i<m) {
6852205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);
6862205254eSKarl Rupp       } else {
6872205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);
6882205254eSKarl Rupp       }
689496be53dSLois Curfman McInnes     }
690b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
691606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
692496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
69360e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
69477431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
695496be53dSLois Curfman McInnes       }
696b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
697496be53dSLois Curfman McInnes     }
698b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
699496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
70060e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
701496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
702aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
70336db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
70460e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
7056831982aSBarry Smith           }
706496be53dSLois Curfman McInnes #else
70760e0710aSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);}
708496be53dSLois Curfman McInnes #endif
709496be53dSLois Curfman McInnes         }
710496be53dSLois Curfman McInnes       }
711b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
712496be53dSLois Curfman McInnes     }
713d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
714fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
71597f1f81fSBarry Smith     PetscInt    cnt = 0,jcnt;
71687828ca2SBarry Smith     PetscScalar value;
71768f1ed48SBarry Smith #if defined(PETSC_USE_COMPLEX)
71868f1ed48SBarry Smith     PetscBool   realonly = PETSC_TRUE;
71968f1ed48SBarry Smith 
72068f1ed48SBarry Smith     for (i=0; i<a->i[m]; i++) {
72168f1ed48SBarry Smith       if (PetscImaginaryPart(a->a[i]) != 0.0) {
72268f1ed48SBarry Smith         realonly = PETSC_FALSE;
72368f1ed48SBarry Smith         break;
72468f1ed48SBarry Smith       }
72568f1ed48SBarry Smith     }
72668f1ed48SBarry Smith #endif
72702594712SBarry Smith 
728d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
72902594712SBarry Smith     for (i=0; i<m; i++) {
73002594712SBarry Smith       jcnt = 0;
731d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
732e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
73302594712SBarry Smith           value = a->a[cnt++];
734e24b481bSBarry Smith           jcnt++;
73502594712SBarry Smith         } else {
73602594712SBarry Smith           value = 0.0;
73702594712SBarry Smith         }
738aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
73968f1ed48SBarry Smith         if (realonly) {
74060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr);
74168f1ed48SBarry Smith         } else {
74260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr);
74368f1ed48SBarry Smith         }
74402594712SBarry Smith #else
74560e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr);
74602594712SBarry Smith #endif
74702594712SBarry Smith       }
748b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
74902594712SBarry Smith     }
750d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7513c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
752150b93efSMatthew G. Knepley     PetscInt fshift=1;
753d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
7543c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
7553c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
7563c215bfdSMatthew Knepley #else
7573c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
7583c215bfdSMatthew Knepley #endif
759d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
7603c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
76160e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
7623c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
7633c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
76460e0710aSBarry 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);
7653c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
76660e0710aSBarry 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);
7673c215bfdSMatthew Knepley         } else {
76860e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
7693c215bfdSMatthew Knepley         }
7703c215bfdSMatthew Knepley #else
771150b93efSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr);
7723c215bfdSMatthew Knepley #endif
7733c215bfdSMatthew Knepley       }
7743c215bfdSMatthew Knepley     }
775d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7763a40ed3dSBarry Smith   } else {
777d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
778d5f3da31SBarry Smith     if (A->factortype) {
77916cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
78016cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
78116cd7e1dSShri Abhyankar         /* L part */
78260e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
78316cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
78416cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
78560e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
78616cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
7876712e2f1SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
78816cd7e1dSShri Abhyankar           } else {
78960e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
79016cd7e1dSShri Abhyankar           }
79116cd7e1dSShri Abhyankar #else
79260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
79316cd7e1dSShri Abhyankar #endif
79416cd7e1dSShri Abhyankar         }
79516cd7e1dSShri Abhyankar         /* diagonal */
79616cd7e1dSShri Abhyankar         j = a->diag[i];
79716cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
79816cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
79960e0710aSBarry 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);
80016cd7e1dSShri Abhyankar         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
8016712e2f1SBarry 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);
80216cd7e1dSShri Abhyankar         } else {
80360e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
80416cd7e1dSShri Abhyankar         }
80516cd7e1dSShri Abhyankar #else
80660e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr);
80716cd7e1dSShri Abhyankar #endif
80816cd7e1dSShri Abhyankar 
80916cd7e1dSShri Abhyankar         /* U part */
81060e0710aSBarry Smith         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
81116cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
81216cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
81360e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
81416cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
81522ab088eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
81616cd7e1dSShri Abhyankar           } else {
81760e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
81816cd7e1dSShri Abhyankar           }
81916cd7e1dSShri Abhyankar #else
82060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
82116cd7e1dSShri Abhyankar #endif
82216cd7e1dSShri Abhyankar         }
82316cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
82416cd7e1dSShri Abhyankar       }
82516cd7e1dSShri Abhyankar     } else {
82617ab2063SBarry Smith       for (i=0; i<m; i++) {
82777431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
82860e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
829aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
83036db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
83160e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
83236db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
83360e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
8343a40ed3dSBarry Smith           } else {
83560e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
83617ab2063SBarry Smith           }
83717ab2063SBarry Smith #else
83860e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
83917ab2063SBarry Smith #endif
84017ab2063SBarry Smith         }
841b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
84217ab2063SBarry Smith       }
84316cd7e1dSShri Abhyankar     }
844d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
84517ab2063SBarry Smith   }
846b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8473a40ed3dSBarry Smith   PetscFunctionReturn(0);
848416022c9SBarry Smith }
849416022c9SBarry Smith 
8509804daf3SBarry Smith #include <petscdraw.h>
8514a2ae208SSatish Balay #undef __FUNCT__
8524a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
853dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
854416022c9SBarry Smith {
855480ef9eaSBarry Smith   Mat               A  = (Mat) Aa;
856416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
857dfbe8321SBarry Smith   PetscErrorCode    ierr;
858d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
85936db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
860b0a32e0cSBarry Smith   PetscViewer       viewer;
861f3ef73ceSBarry Smith   PetscViewerFormat format;
862cddf8d76SBarry Smith 
8633a40ed3dSBarry Smith   PetscFunctionBegin;
864480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
865b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
86619bcc07fSBarry Smith 
867b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
868416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
8690513a670SBarry Smith 
870fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
8710513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
872b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
873416022c9SBarry Smith     for (i=0; i<m; i++) {
874cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
875bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
876bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
87736db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
878b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
879cddf8d76SBarry Smith       }
880cddf8d76SBarry Smith     }
881b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
882cddf8d76SBarry Smith     for (i=0; i<m; i++) {
883cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
884bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
885bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
886cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
887b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
888cddf8d76SBarry Smith       }
889cddf8d76SBarry Smith     }
890b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
891cddf8d76SBarry Smith     for (i=0; i<m; i++) {
892cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
893bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
894bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
89536db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
896b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
897416022c9SBarry Smith       }
898416022c9SBarry Smith     }
8990513a670SBarry Smith   } else {
9000513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
9010513a670SBarry Smith     /* first determine max of all nonzero values */
90297f1f81fSBarry Smith     PetscInt  nz = a->nz,count;
903b0a32e0cSBarry Smith     PetscDraw popup;
90436db0b34SBarry Smith     PetscReal scale;
9050513a670SBarry Smith 
9060513a670SBarry Smith     for (i=0; i<nz; i++) {
9070513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
9080513a670SBarry Smith     }
909b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
910b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
9112205254eSKarl Rupp     if (popup) {
9122205254eSKarl Rupp       ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);
9132205254eSKarl Rupp     }
9140513a670SBarry Smith     count = 0;
9150513a670SBarry Smith     for (i=0; i<m; i++) {
9160513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
917bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
918bfeeae90SHong Zhang         x_l   = a->j[j]; x_r = x_l + 1.0;
91997f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
920b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
9210513a670SBarry Smith         count++;
9220513a670SBarry Smith       }
9230513a670SBarry Smith     }
9240513a670SBarry Smith   }
925480ef9eaSBarry Smith   PetscFunctionReturn(0);
926480ef9eaSBarry Smith }
927cddf8d76SBarry Smith 
9289804daf3SBarry Smith #include <petscdraw.h>
9294a2ae208SSatish Balay #undef __FUNCT__
9304a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
931dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
932480ef9eaSBarry Smith {
933dfbe8321SBarry Smith   PetscErrorCode ierr;
934b0a32e0cSBarry Smith   PetscDraw      draw;
93536db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
936ace3abfcSBarry Smith   PetscBool      isnull;
937480ef9eaSBarry Smith 
938480ef9eaSBarry Smith   PetscFunctionBegin;
939b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
940b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
941480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
942480ef9eaSBarry Smith 
943480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
944d0f46423SBarry Smith   xr   = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
945480ef9eaSBarry Smith   xr  += w;    yr += h;  xl = -w;     yl = -h;
946b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
947b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
9480298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
9493a40ed3dSBarry Smith   PetscFunctionReturn(0);
950416022c9SBarry Smith }
951416022c9SBarry Smith 
9524a2ae208SSatish Balay #undef __FUNCT__
9534a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
954dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
955416022c9SBarry Smith {
956dfbe8321SBarry Smith   PetscErrorCode ierr;
957ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
958416022c9SBarry Smith 
9593a40ed3dSBarry Smith   PetscFunctionBegin;
960251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
961251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
962251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
963c45a1595SBarry Smith   if (iascii) {
9643a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
9650f5bd95cSBarry Smith   } else if (isbinary) {
9663a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
9670f5bd95cSBarry Smith   } else if (isdraw) {
9683a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
96911aeaf0aSBarry Smith   }
9704108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
9713a40ed3dSBarry Smith   PetscFunctionReturn(0);
97217ab2063SBarry Smith }
97319bcc07fSBarry Smith 
9744a2ae208SSatish Balay #undef __FUNCT__
9754a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
976dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
97717ab2063SBarry Smith {
978416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9796849ba73SBarry Smith   PetscErrorCode ierr;
98097f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
981d0f46423SBarry Smith   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
98254f21887SBarry Smith   MatScalar      *aa    = a->a,*ap;
9833447b6efSHong Zhang   PetscReal      ratio  = 0.6;
98417ab2063SBarry Smith 
9853a40ed3dSBarry Smith   PetscFunctionBegin;
9863a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
98717ab2063SBarry Smith 
98843ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
98917ab2063SBarry Smith   for (i=1; i<m; i++) {
990416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
99117ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
99294a9d846SBarry Smith     rmax    = PetscMax(rmax,ailen[i]);
99317ab2063SBarry Smith     if (fshift) {
994bfeeae90SHong Zhang       ip = aj + ai[i];
995bfeeae90SHong Zhang       ap = aa + ai[i];
99617ab2063SBarry Smith       N  = ailen[i];
99717ab2063SBarry Smith       for (j=0; j<N; j++) {
99817ab2063SBarry Smith         ip[j-fshift] = ip[j];
99917ab2063SBarry Smith         ap[j-fshift] = ap[j];
100017ab2063SBarry Smith       }
100117ab2063SBarry Smith     }
100217ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
100317ab2063SBarry Smith   }
100417ab2063SBarry Smith   if (m) {
100517ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
100617ab2063SBarry Smith     ai[m]   = ai[m-1] + ailen[m-1];
100717ab2063SBarry Smith   }
10087b083b7cSBarry Smith 
100917ab2063SBarry Smith   /* reset ilen and imax for each row */
10107b083b7cSBarry Smith   a->nonzerorowcnt = 0;
101117ab2063SBarry Smith   for (i=0; i<m; i++) {
101217ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
10137b083b7cSBarry Smith     a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
101417ab2063SBarry Smith   }
1015bfeeae90SHong Zhang   a->nz = ai[m];
101665e19b50SBarry Smith   if (fshift && a->nounused == -1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
101717ab2063SBarry Smith 
101809f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1019d0f46423SBarry Smith   ierr = PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap->n,fshift,a->nz);CHKERRQ(ierr);
1020ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
1021ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
10222205254eSKarl Rupp 
10238e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
1024dd5f02e7SSatish Balay   a->reallocs         = 0;
10256712e2f1SBarry Smith   A->info.nz_unneeded = (PetscReal)fshift;
102636db0b34SBarry Smith   a->rmax             = rmax;
10274e220ebcSLois Curfman McInnes 
102811e456e1SBarry Smith   ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
10294108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
1030acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10313a40ed3dSBarry Smith   PetscFunctionReturn(0);
103217ab2063SBarry Smith }
103317ab2063SBarry Smith 
10344a2ae208SSatish Balay #undef __FUNCT__
103599cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
103699cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
103799cafbc1SBarry Smith {
103899cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
103999cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
104054f21887SBarry Smith   MatScalar      *aa = a->a;
1041acf2f550SJed Brown   PetscErrorCode ierr;
104299cafbc1SBarry Smith 
104399cafbc1SBarry Smith   PetscFunctionBegin;
104499cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1045acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
104699cafbc1SBarry Smith   PetscFunctionReturn(0);
104799cafbc1SBarry Smith }
104899cafbc1SBarry Smith 
104999cafbc1SBarry Smith #undef __FUNCT__
105099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
105199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
105299cafbc1SBarry Smith {
105399cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
105499cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
105554f21887SBarry Smith   MatScalar      *aa = a->a;
1056acf2f550SJed Brown   PetscErrorCode ierr;
105799cafbc1SBarry Smith 
105899cafbc1SBarry Smith   PetscFunctionBegin;
105999cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1060acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
106199cafbc1SBarry Smith   PetscFunctionReturn(0);
106299cafbc1SBarry Smith }
106399cafbc1SBarry Smith 
106478b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
106578b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ_Kernel(PetscInt thread_id,Mat A)
106678b84d54SShri Abhyankar {
106778b84d54SShri Abhyankar   PetscErrorCode ierr;
106878b84d54SShri Abhyankar   PetscInt       *trstarts=A->rmap->trstarts;
106978b84d54SShri Abhyankar   PetscInt       n,start,end;
107078b84d54SShri Abhyankar   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
107178b84d54SShri Abhyankar 
107278b84d54SShri Abhyankar   start = trstarts[thread_id];
107378b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
107419baf141SJed Brown   n     = a->i[end] - a->i[start];
107519baf141SJed Brown   ierr  = PetscMemzero(a->a+a->i[start],n*sizeof(PetscScalar));CHKERRQ(ierr);
107678b84d54SShri Abhyankar   return 0;
107778b84d54SShri Abhyankar }
107878b84d54SShri Abhyankar 
107978b84d54SShri Abhyankar #undef __FUNCT__
108078b84d54SShri Abhyankar #define __FUNCT__ "MatZeroEntries_SeqAIJ"
108178b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
108278b84d54SShri Abhyankar {
108378b84d54SShri Abhyankar   PetscErrorCode ierr;
108478b84d54SShri Abhyankar 
108578b84d54SShri Abhyankar   PetscFunctionBegin;
1086ce94432eSBarry Smith   ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatZeroEntries_SeqAIJ_Kernel,1,A);CHKERRQ(ierr);
1087acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
108878b84d54SShri Abhyankar   PetscFunctionReturn(0);
108978b84d54SShri Abhyankar }
109078b84d54SShri Abhyankar #else
109199cafbc1SBarry Smith #undef __FUNCT__
10924a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
1093dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
109417ab2063SBarry Smith {
1095416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1096dfbe8321SBarry Smith   PetscErrorCode ierr;
10973a40ed3dSBarry Smith 
10983a40ed3dSBarry Smith   PetscFunctionBegin;
1099d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
1100acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
11013a40ed3dSBarry Smith   PetscFunctionReturn(0);
110217ab2063SBarry Smith }
110378b84d54SShri Abhyankar #endif
1104416022c9SBarry Smith 
11054a2ae208SSatish Balay #undef __FUNCT__
11064a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
1107dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
110817ab2063SBarry Smith {
1109416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1110dfbe8321SBarry Smith   PetscErrorCode ierr;
1111d5d45c9bSBarry Smith 
11123a40ed3dSBarry Smith   PetscFunctionBegin;
1113aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1114d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
111517ab2063SBarry Smith #endif
1116e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
11176bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
11186bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
111905b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
1120d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
112105b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
112271f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
112305b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
11246bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
112505b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
11266bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
112705b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
11286bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
1129cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
11300b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
1131a30b2313SHong Zhang 
11324108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
1133bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
1134901853e0SKris Buschelman 
1135dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
1136bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
1137bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1138bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1139bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr);
1140bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr);
1141bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr);
1142bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1143bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1144bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1145bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr);
11463a40ed3dSBarry Smith   PetscFunctionReturn(0);
114717ab2063SBarry Smith }
114817ab2063SBarry Smith 
11494a2ae208SSatish Balay #undef __FUNCT__
11504a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
1151ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
115217ab2063SBarry Smith {
1153416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11544846f1f5SKris Buschelman   PetscErrorCode ierr;
11553a40ed3dSBarry Smith 
11563a40ed3dSBarry Smith   PetscFunctionBegin;
1157a65d3064SKris Buschelman   switch (op) {
1158a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
11594e0d8c25SBarry Smith     a->roworiented = flg;
1160a65d3064SKris Buschelman     break;
1161a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1162a9817697SBarry Smith     a->keepnonzeropattern = flg;
1163a65d3064SKris Buschelman     break;
1164512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1165512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1166a65d3064SKris Buschelman     break;
1167a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
11684e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1169a65d3064SKris Buschelman     break;
1170a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
11714e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1172a65d3064SKris Buschelman     break;
117328b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
117428b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
117528b2fa4aSMatthew Knepley     break;
1176a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11774e0d8c25SBarry Smith     a->ignorezeroentries = flg;
11780df259c2SBarry Smith     break;
11793d472b54SHong Zhang   case MAT_SPD:
1180b1646e73SJed Brown   case MAT_SYMMETRIC:
1181b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1182b1646e73SJed Brown   case MAT_HERMITIAN:
1183b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
11845021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
11855021d80fSJed Brown     break;
11864e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1187a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1188a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1189290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1190a65d3064SKris Buschelman     break;
1191b87ac2d8SJed Brown   case MAT_USE_INODES:
1192b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1193b87ac2d8SJed Brown     break;
1194a65d3064SKris Buschelman   default:
1195e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1196a65d3064SKris Buschelman   }
11974108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
11983a40ed3dSBarry Smith   PetscFunctionReturn(0);
119917ab2063SBarry Smith }
120017ab2063SBarry Smith 
12014a2ae208SSatish Balay #undef __FUNCT__
12024a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1203dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
120417ab2063SBarry Smith {
1205416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12066849ba73SBarry Smith   PetscErrorCode ierr;
1207d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
120835e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
120917ab2063SBarry Smith 
12103a40ed3dSBarry Smith   PetscFunctionBegin;
1211d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1212e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
121335e7444dSHong Zhang 
1214d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1215d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
121635e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
12172c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
121835e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
121935e7444dSHong Zhang     PetscFunctionReturn(0);
122035e7444dSHong Zhang   }
122135e7444dSHong Zhang 
12222dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
12231ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
122435e7444dSHong Zhang   for (i=0; i<n; i++) {
122535e7444dSHong Zhang     nz = ai[i+1] - ai[i];
12262f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
122735e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++) {
122835e7444dSHong Zhang       if (aj[j] == i) {
122935e7444dSHong Zhang         x[i] = aa[j];
123017ab2063SBarry Smith         break;
123117ab2063SBarry Smith       }
123217ab2063SBarry Smith     }
123317ab2063SBarry Smith   }
12341ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
12353a40ed3dSBarry Smith   PetscFunctionReturn(0);
123617ab2063SBarry Smith }
123717ab2063SBarry Smith 
1238c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
12394a2ae208SSatish Balay #undef __FUNCT__
12404a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1241dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
124217ab2063SBarry Smith {
1243416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12445c897100SBarry Smith   PetscScalar    *x,*y;
1245dfbe8321SBarry Smith   PetscErrorCode ierr;
1246d0f46423SBarry Smith   PetscInt       m = A->rmap->n;
12475c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1248a77337e4SBarry Smith   MatScalar         *v;
1249a77337e4SBarry Smith   PetscScalar       alpha;
12500298fd71SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=NULL;
12513447b6efSHong Zhang   Mat_CompressedRow cprow    = a->compressedrow;
1252ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
12535c897100SBarry Smith #endif
125417ab2063SBarry Smith 
12553a40ed3dSBarry Smith   PetscFunctionBegin;
12562e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
12571ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
12581ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
12595c897100SBarry Smith 
12605c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1261bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
12625c897100SBarry Smith #else
12633447b6efSHong Zhang   if (usecprow) {
12643447b6efSHong Zhang     m    = cprow.nrows;
12653447b6efSHong Zhang     ii   = cprow.i;
12667b2bb3b9SHong Zhang     ridx = cprow.rindex;
12673447b6efSHong Zhang   } else {
12683447b6efSHong Zhang     ii = a->i;
12693447b6efSHong Zhang   }
127017ab2063SBarry Smith   for (i=0; i<m; i++) {
12713447b6efSHong Zhang     idx = a->j + ii[i];
12723447b6efSHong Zhang     v   = a->a + ii[i];
12733447b6efSHong Zhang     n   = ii[i+1] - ii[i];
12743447b6efSHong Zhang     if (usecprow) {
12757b2bb3b9SHong Zhang       alpha = x[ridx[i]];
12763447b6efSHong Zhang     } else {
127717ab2063SBarry Smith       alpha = x[i];
12783447b6efSHong Zhang     }
127904fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
128017ab2063SBarry Smith   }
12815c897100SBarry Smith #endif
1282dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
12831ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12841ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12853a40ed3dSBarry Smith   PetscFunctionReturn(0);
128617ab2063SBarry Smith }
128717ab2063SBarry Smith 
12884a2ae208SSatish Balay #undef __FUNCT__
12895c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1290dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
12915c897100SBarry Smith {
1292dfbe8321SBarry Smith   PetscErrorCode ierr;
12935c897100SBarry Smith 
12945c897100SBarry Smith   PetscFunctionBegin;
1295170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
12965c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
12975c897100SBarry Smith   PetscFunctionReturn(0);
12985c897100SBarry Smith }
12995c897100SBarry Smith 
1300c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
130178b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
130278b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ_Kernel(PetscInt thread_id,Mat A,Vec xx,Vec yy)
130378b84d54SShri Abhyankar {
130478b84d54SShri Abhyankar   PetscErrorCode    ierr;
130578b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
130678b84d54SShri Abhyankar   PetscScalar       *y;
130778b84d54SShri Abhyankar   const PetscScalar *x;
130878b84d54SShri Abhyankar   const MatScalar   *aa;
130978b84d54SShri Abhyankar   PetscInt          *trstarts=A->rmap->trstarts;
131078b84d54SShri Abhyankar   PetscInt          n,start,end,i;
131178b84d54SShri Abhyankar   const PetscInt    *aj,*ai;
131278b84d54SShri Abhyankar   PetscScalar       sum;
131378b84d54SShri Abhyankar 
131478b84d54SShri Abhyankar   ierr  = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
131578b84d54SShri Abhyankar   ierr  = VecGetArray(yy,&y);CHKERRQ(ierr);
131678b84d54SShri Abhyankar   start = trstarts[thread_id];
131778b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
131878b84d54SShri Abhyankar   aj    = a->j;
131978b84d54SShri Abhyankar   aa    = a->a;
132078b84d54SShri Abhyankar   ai    = a->i;
132178b84d54SShri Abhyankar   for (i=start; i<end; i++) {
132278b84d54SShri Abhyankar     n   = ai[i+1] - ai[i];
132378b84d54SShri Abhyankar     aj  = a->j + ai[i];
132478b84d54SShri Abhyankar     aa  = a->a + ai[i];
132578b84d54SShri Abhyankar     sum = 0.0;
132678b84d54SShri Abhyankar     PetscSparseDensePlusDot(sum,x,aa,aj,n);
132778b84d54SShri Abhyankar     y[i] = sum;
132878b84d54SShri Abhyankar   }
132978b84d54SShri Abhyankar   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
133078b84d54SShri Abhyankar   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
133178b84d54SShri Abhyankar   return 0;
133278b84d54SShri Abhyankar }
133378b84d54SShri Abhyankar 
133478b84d54SShri Abhyankar #undef __FUNCT__
133578b84d54SShri Abhyankar #define __FUNCT__ "MatMult_SeqAIJ"
133678b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
133778b84d54SShri Abhyankar {
133878b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
133978b84d54SShri Abhyankar   PetscScalar       *y;
134078b84d54SShri Abhyankar   const PetscScalar *x;
134178b84d54SShri Abhyankar   const MatScalar   *aa;
134278b84d54SShri Abhyankar   PetscErrorCode    ierr;
134378b84d54SShri Abhyankar   PetscInt          m=A->rmap->n;
13440298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
13457b083b7cSBarry Smith   PetscInt          n,i;
134678b84d54SShri Abhyankar   PetscScalar       sum;
134778b84d54SShri Abhyankar   PetscBool         usecprow=a->compressedrow.use;
134878b84d54SShri Abhyankar 
134978b84d54SShri Abhyankar #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
135078b84d54SShri Abhyankar #pragma disjoint(*x,*y,*aa)
135178b84d54SShri Abhyankar #endif
135278b84d54SShri Abhyankar 
135378b84d54SShri Abhyankar   PetscFunctionBegin;
135478b84d54SShri Abhyankar   aj = a->j;
135578b84d54SShri Abhyankar   aa = a->a;
135678b84d54SShri Abhyankar   ii = a->i;
135778b84d54SShri Abhyankar   if (usecprow) { /* use compressed row format */
135878b84d54SShri Abhyankar     ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
135978b84d54SShri Abhyankar     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
136078b84d54SShri Abhyankar     m    = a->compressedrow.nrows;
136178b84d54SShri Abhyankar     ii   = a->compressedrow.i;
136278b84d54SShri Abhyankar     ridx = a->compressedrow.rindex;
136378b84d54SShri Abhyankar     for (i=0; i<m; i++) {
136478b84d54SShri Abhyankar       n           = ii[i+1] - ii[i];
136578b84d54SShri Abhyankar       aj          = a->j + ii[i];
136678b84d54SShri Abhyankar       aa          = a->a + ii[i];
136778b84d54SShri Abhyankar       sum         = 0.0;
136878b84d54SShri Abhyankar       PetscSparseDensePlusDot(sum,x,aa,aj,n);
136978b84d54SShri Abhyankar       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
137078b84d54SShri Abhyankar       y[*ridx++] = sum;
137178b84d54SShri Abhyankar     }
137278b84d54SShri Abhyankar     ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
137378b84d54SShri Abhyankar     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
137478b84d54SShri Abhyankar   } else { /* do not use compressed row format */
137578b84d54SShri Abhyankar #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
137678b84d54SShri Abhyankar     fortranmultaij_(&m,x,ii,aj,aa,y);
137778b84d54SShri Abhyankar #else
1378ce94432eSBarry Smith     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
137978b84d54SShri Abhyankar #endif
138078b84d54SShri Abhyankar   }
13817b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
138278b84d54SShri Abhyankar   PetscFunctionReturn(0);
138378b84d54SShri Abhyankar }
138478b84d54SShri Abhyankar #else
13855c897100SBarry Smith #undef __FUNCT__
13864a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1387dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
138817ab2063SBarry Smith {
1389416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1390d9fead3dSBarry Smith   PetscScalar       *y;
139154f21887SBarry Smith   const PetscScalar *x;
139254f21887SBarry Smith   const MatScalar   *aa;
1393dfbe8321SBarry Smith   PetscErrorCode    ierr;
1394003131ecSBarry Smith   PetscInt          m=A->rmap->n;
13950298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
13967b083b7cSBarry Smith   PetscInt          n,i;
1397362ced78SSatish Balay   PetscScalar       sum;
1398ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
139917ab2063SBarry Smith 
1400b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
140197952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1402fee21e36SBarry Smith #endif
1403fee21e36SBarry Smith 
14043a40ed3dSBarry Smith   PetscFunctionBegin;
14053649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
14061ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
140797952fefSHong Zhang   aj   = a->j;
140897952fefSHong Zhang   aa   = a->a;
1409416022c9SBarry Smith   ii   = a->i;
14104eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
141197952fefSHong Zhang     m    = a->compressedrow.nrows;
141297952fefSHong Zhang     ii   = a->compressedrow.i;
141397952fefSHong Zhang     ridx = a->compressedrow.rindex;
141497952fefSHong Zhang     for (i=0; i<m; i++) {
141597952fefSHong Zhang       n           = ii[i+1] - ii[i];
141697952fefSHong Zhang       aj          = a->j + ii[i];
141797952fefSHong Zhang       aa          = a->a + ii[i];
141897952fefSHong Zhang       sum         = 0.0;
1419003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1420003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
142197952fefSHong Zhang       y[*ridx++] = sum;
142297952fefSHong Zhang     }
142397952fefSHong Zhang   } else { /* do not use compressed row format */
1424b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1425b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1426b05257ddSBarry Smith #else
142778b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
1428ce94432eSBarry Smith     ierr = PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
142978b84d54SShri Abhyankar #else
143017ab2063SBarry Smith     for (i=0; i<m; i++) {
1431003131ecSBarry Smith       n           = ii[i+1] - ii[i];
1432003131ecSBarry Smith       aj          = a->j + ii[i];
1433003131ecSBarry Smith       aa          = a->a + ii[i];
143417ab2063SBarry Smith       sum         = 0.0;
1435003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
143617ab2063SBarry Smith       y[i] = sum;
143717ab2063SBarry Smith     }
14388d195f9aSBarry Smith #endif
143978b84d54SShri Abhyankar #endif
1440b05257ddSBarry Smith   }
14417b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
14423649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
14431ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
14443a40ed3dSBarry Smith   PetscFunctionReturn(0);
144517ab2063SBarry Smith }
144678b84d54SShri Abhyankar #endif
144717ab2063SBarry Smith 
1448b434eb95SMatthew G. Knepley #undef __FUNCT__
1449b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultMax_SeqAIJ"
1450b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1451b434eb95SMatthew G. Knepley {
1452b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1453b434eb95SMatthew G. Knepley   PetscScalar       *y;
1454b434eb95SMatthew G. Knepley   const PetscScalar *x;
1455b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1456b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1457b434eb95SMatthew G. Knepley   PetscInt          m=A->rmap->n;
1458b434eb95SMatthew G. Knepley   const PetscInt    *aj,*ii,*ridx=NULL;
1459b434eb95SMatthew G. Knepley   PetscInt          n,i,nonzerorow=0;
1460b434eb95SMatthew G. Knepley   PetscScalar       sum;
1461b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1462b434eb95SMatthew G. Knepley 
1463b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1464b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa)
1465b434eb95SMatthew G. Knepley #endif
1466b434eb95SMatthew G. Knepley 
1467b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1468b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1469b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1470b434eb95SMatthew G. Knepley   aj   = a->j;
1471b434eb95SMatthew G. Knepley   aa   = a->a;
1472b434eb95SMatthew G. Knepley   ii   = a->i;
1473b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1474b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1475b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1476b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1477b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1478b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1479b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1480b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1481b434eb95SMatthew G. Knepley       sum         = 0.0;
1482b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1483b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1484b434eb95SMatthew G. Knepley       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1485b434eb95SMatthew G. Knepley       y[*ridx++] = sum;
1486b434eb95SMatthew G. Knepley     }
1487b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
1488b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1489b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1490b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1491b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1492b434eb95SMatthew G. Knepley       sum         = 0.0;
1493b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1494b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1495b434eb95SMatthew G. Knepley       y[i] = sum;
1496b434eb95SMatthew G. Knepley     }
1497b434eb95SMatthew G. Knepley   }
1498b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1499b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1500b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1501b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1502b434eb95SMatthew G. Knepley }
1503b434eb95SMatthew G. Knepley 
1504b434eb95SMatthew G. Knepley #undef __FUNCT__
1505b434eb95SMatthew G. Knepley #define __FUNCT__ "MatMultAddMax_SeqAIJ"
1506b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1507b434eb95SMatthew G. Knepley {
1508b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1509b434eb95SMatthew G. Knepley   PetscScalar       *y,*z;
1510b434eb95SMatthew G. Knepley   const PetscScalar *x;
1511b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1512b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1513b434eb95SMatthew G. Knepley   PetscInt          m = A->rmap->n,*aj,*ii;
1514b434eb95SMatthew G. Knepley   PetscInt          n,i,*ridx=NULL;
1515b434eb95SMatthew G. Knepley   PetscScalar       sum;
1516b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1517b434eb95SMatthew G. Knepley 
1518b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1519b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1520b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1521b434eb95SMatthew G. Knepley   if (zz != yy) {
1522b434eb95SMatthew G. Knepley     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
1523b434eb95SMatthew G. Knepley   } else {
1524b434eb95SMatthew G. Knepley     z = y;
1525b434eb95SMatthew G. Knepley   }
1526b434eb95SMatthew G. Knepley 
1527b434eb95SMatthew G. Knepley   aj = a->j;
1528b434eb95SMatthew G. Knepley   aa = a->a;
1529b434eb95SMatthew G. Knepley   ii = a->i;
1530b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1531b434eb95SMatthew G. Knepley     if (zz != yy) {
1532b434eb95SMatthew G. Knepley       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1533b434eb95SMatthew G. Knepley     }
1534b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1535b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1536b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1537b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1538b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1539b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1540b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1541b434eb95SMatthew G. Knepley       sum = y[*ridx];
1542b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1543b434eb95SMatthew G. Knepley       z[*ridx++] = sum;
1544b434eb95SMatthew G. Knepley     }
1545b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
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[i];
1551b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1552b434eb95SMatthew G. Knepley       z[i] = sum;
1553b434eb95SMatthew G. Knepley     }
1554b434eb95SMatthew G. Knepley   }
1555b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1556b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1557b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1558b434eb95SMatthew G. Knepley   if (zz != yy) {
1559b434eb95SMatthew G. Knepley     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
1560b434eb95SMatthew G. Knepley   }
1561b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1562b434eb95SMatthew G. Knepley }
1563b434eb95SMatthew G. Knepley 
1564c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
15654a2ae208SSatish Balay #undef __FUNCT__
15664a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1567dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
156817ab2063SBarry Smith {
1569416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1570f15663dcSBarry Smith   PetscScalar       *y,*z;
1571f15663dcSBarry Smith   const PetscScalar *x;
157254f21887SBarry Smith   const MatScalar   *aa;
1573dfbe8321SBarry Smith   PetscErrorCode    ierr;
1574d0f46423SBarry Smith   PetscInt          m = A->rmap->n,*aj,*ii;
15750298fd71SBarry Smith   PetscInt          n,i,*ridx=NULL;
1576362ced78SSatish Balay   PetscScalar       sum;
1577ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
15789ea0dfa2SSatish Balay 
15793a40ed3dSBarry Smith   PetscFunctionBegin;
1580f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
15811ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
15822e8a6d31SBarry Smith   if (zz != yy) {
15831ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
15842e8a6d31SBarry Smith   } else {
15852e8a6d31SBarry Smith     z = y;
15862e8a6d31SBarry Smith   }
1587bfeeae90SHong Zhang 
158897952fefSHong Zhang   aj = a->j;
158997952fefSHong Zhang   aa = a->a;
1590cddf8d76SBarry Smith   ii = a->i;
15914eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
15924eb6d288SHong Zhang     if (zz != yy) {
15934eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
15944eb6d288SHong Zhang     }
159597952fefSHong Zhang     m    = a->compressedrow.nrows;
159697952fefSHong Zhang     ii   = a->compressedrow.i;
159797952fefSHong Zhang     ridx = a->compressedrow.rindex;
159897952fefSHong Zhang     for (i=0; i<m; i++) {
159997952fefSHong Zhang       n   = ii[i+1] - ii[i];
160097952fefSHong Zhang       aj  = a->j + ii[i];
160197952fefSHong Zhang       aa  = a->a + ii[i];
160297952fefSHong Zhang       sum = y[*ridx];
1603f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
160497952fefSHong Zhang       z[*ridx++] = sum;
160597952fefSHong Zhang     }
160697952fefSHong Zhang   } else { /* do not use compressed row format */
1607f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1608f15663dcSBarry Smith     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1609f15663dcSBarry Smith #else
161017ab2063SBarry Smith     for (i=0; i<m; i++) {
1611f15663dcSBarry Smith       n   = ii[i+1] - ii[i];
1612f15663dcSBarry Smith       aj  = a->j + ii[i];
1613f15663dcSBarry Smith       aa  = a->a + ii[i];
161417ab2063SBarry Smith       sum = y[i];
1615f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
161617ab2063SBarry Smith       z[i] = sum;
161717ab2063SBarry Smith     }
161802ab625aSSatish Balay #endif
1619f15663dcSBarry Smith   }
1620dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1621f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
16221ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
16232e8a6d31SBarry Smith   if (zz != yy) {
16241ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
16252e8a6d31SBarry Smith   }
16268154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
16276b375ea7SVictor Minden   /*
1628918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1629918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1630918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
16316b375ea7SVictor Minden   */
1632918e98c3SVictor Minden #endif
16333a40ed3dSBarry Smith   PetscFunctionReturn(0);
163417ab2063SBarry Smith }
163517ab2063SBarry Smith 
163617ab2063SBarry Smith /*
163717ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
163817ab2063SBarry Smith */
16394a2ae208SSatish Balay #undef __FUNCT__
16404a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1641dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
164217ab2063SBarry Smith {
1643416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
16446849ba73SBarry Smith   PetscErrorCode ierr;
1645d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
164617ab2063SBarry Smith 
16473a40ed3dSBarry Smith   PetscFunctionBegin;
164809f38230SBarry Smith   if (!a->diag) {
1649785e854fSJed Brown     ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr);
16503bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr);
165109f38230SBarry Smith   }
1652d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
165309f38230SBarry Smith     a->diag[i] = a->i[i+1];
1654bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1655bfeeae90SHong Zhang       if (a->j[j] == i) {
165609f38230SBarry Smith         a->diag[i] = j;
165717ab2063SBarry Smith         break;
165817ab2063SBarry Smith       }
165917ab2063SBarry Smith     }
166017ab2063SBarry Smith   }
16613a40ed3dSBarry Smith   PetscFunctionReturn(0);
166217ab2063SBarry Smith }
166317ab2063SBarry Smith 
1664be5855fcSBarry Smith /*
1665be5855fcSBarry Smith      Checks for missing diagonals
1666be5855fcSBarry Smith */
16674a2ae208SSatish Balay #undef __FUNCT__
16684a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1669ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1670be5855fcSBarry Smith {
1671be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1672*7734d3b5SMatthew G. Knepley   PetscInt   *diag,*ii = a->i,i;
1673be5855fcSBarry Smith 
1674be5855fcSBarry Smith   PetscFunctionBegin;
167509f38230SBarry Smith   *missing = PETSC_FALSE;
1676*7734d3b5SMatthew G. Knepley   if (A->rmap->n > 0 && !ii) {
167709f38230SBarry Smith     *missing = PETSC_TRUE;
167809f38230SBarry Smith     if (d) *d = 0;
1679358d2f5dSShri Abhyankar     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
168009f38230SBarry Smith   } else {
1681f1e2ffcdSBarry Smith     diag = a->diag;
1682d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1683*7734d3b5SMatthew G. Knepley       if (diag[i] >= ii[i+1]) {
168409f38230SBarry Smith         *missing = PETSC_TRUE;
168509f38230SBarry Smith         if (d) *d = i;
168609f38230SBarry Smith         PetscInfo1(A,"Matrix is missing diagonal number %D",i);
1687358d2f5dSShri Abhyankar         break;
168809f38230SBarry Smith       }
1689be5855fcSBarry Smith     }
1690be5855fcSBarry Smith   }
1691be5855fcSBarry Smith   PetscFunctionReturn(0);
1692be5855fcSBarry Smith }
1693be5855fcSBarry Smith 
169471f1c65dSBarry Smith #undef __FUNCT__
169571f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
16967087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
169771f1c65dSBarry Smith {
169871f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
169971f1c65dSBarry Smith   PetscErrorCode ierr;
1700d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
170154f21887SBarry Smith   MatScalar      *v = a->a;
170254f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
170371f1c65dSBarry Smith 
170471f1c65dSBarry Smith   PetscFunctionBegin;
170571f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
170671f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
170771f1c65dSBarry Smith   diag = a->diag;
170871f1c65dSBarry Smith   if (!a->idiag) {
1709dcca6d9dSJed Brown     ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr);
17103bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
171171f1c65dSBarry Smith     v    = a->a;
171271f1c65dSBarry Smith   }
171371f1c65dSBarry Smith   mdiag = a->mdiag;
171471f1c65dSBarry Smith   idiag = a->idiag;
171571f1c65dSBarry Smith 
1716028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
171771f1c65dSBarry Smith     for (i=0; i<m; i++) {
171871f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1719e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
172071f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
172171f1c65dSBarry Smith     }
172271f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
172371f1c65dSBarry Smith   } else {
172471f1c65dSBarry Smith     for (i=0; i<m; i++) {
172571f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
172671f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
172771f1c65dSBarry Smith     }
1728dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
172971f1c65dSBarry Smith   }
173071f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
173171f1c65dSBarry Smith   PetscFunctionReturn(0);
173271f1c65dSBarry Smith }
173371f1c65dSBarry Smith 
1734c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
17354a2ae208SSatish Balay #undef __FUNCT__
173641f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
173741f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
173817ab2063SBarry Smith {
1739416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1740e6d1f457SBarry Smith   PetscScalar       *x,d,sum,*t,scale;
1741e6d1f457SBarry Smith   const MatScalar   *v = a->a,*idiag=0,*mdiag;
174254f21887SBarry Smith   const PetscScalar *b, *bs,*xb, *ts;
1743dfbe8321SBarry Smith   PetscErrorCode    ierr;
1744d0f46423SBarry Smith   PetscInt          n = A->cmap->n,m = A->rmap->n,i;
174597f1f81fSBarry Smith   const PetscInt    *idx,*diag;
174617ab2063SBarry Smith 
17473a40ed3dSBarry Smith   PetscFunctionBegin;
1748b965ef7fSBarry Smith   its = its*lits;
174991723122SBarry Smith 
175071f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
175171f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
175271f1c65dSBarry Smith   a->fshift = fshift;
175371f1c65dSBarry Smith   a->omega  = omega;
1754ed480e8bSBarry Smith 
175571f1c65dSBarry Smith   diag  = a->diag;
175671f1c65dSBarry Smith   t     = a->ssor_work;
1757ed480e8bSBarry Smith   idiag = a->idiag;
175871f1c65dSBarry Smith   mdiag = a->mdiag;
1759ed480e8bSBarry Smith 
17601ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
17613649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1762ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
176317ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
176417ab2063SBarry Smith     /* apply (U + D/omega) to the vector */
1765ed480e8bSBarry Smith     bs = b;
176617ab2063SBarry Smith     for (i=0; i<m; i++) {
176771f1c65dSBarry Smith       d   = fshift + mdiag[i];
1768416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1769ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1770ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
177117ab2063SBarry Smith       sum = b[i]*d/omega;
1772003131ecSBarry Smith       PetscSparseDensePlusDot(sum,bs,v,idx,n);
177317ab2063SBarry Smith       x[i] = sum;
177417ab2063SBarry Smith     }
17751ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
17763649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1777efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17783a40ed3dSBarry Smith     PetscFunctionReturn(0);
177917ab2063SBarry Smith   }
1780c783ea89SBarry Smith 
17812205254eSKarl Rupp   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
17822205254eSKarl Rupp   else if (flag & SOR_EISENSTAT) {
178317ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1784887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
178517ab2063SBarry Smith 
178617ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
178717ab2063SBarry Smith 
1788887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
178917ab2063SBarry Smith     */
179017ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
179117ab2063SBarry Smith 
179217ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
179317ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1794416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1795ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1796ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
179717ab2063SBarry Smith       sum = b[i];
1798e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1799ed480e8bSBarry Smith       x[i] = sum*idiag[i];
180017ab2063SBarry Smith     }
180117ab2063SBarry Smith 
180217ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1803416022c9SBarry Smith     v = a->a;
18042205254eSKarl Rupp     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
180517ab2063SBarry Smith 
180617ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1807ed480e8bSBarry Smith     ts   = t;
1808416022c9SBarry Smith     diag = a->diag;
180917ab2063SBarry Smith     for (i=0; i<m; i++) {
1810416022c9SBarry Smith       n   = diag[i] - a->i[i];
1811ed480e8bSBarry Smith       idx = a->j + a->i[i];
1812ed480e8bSBarry Smith       v   = a->a + a->i[i];
181317ab2063SBarry Smith       sum = t[i];
1814003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1815ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1816733d66baSBarry Smith       /*  x = x + t */
1817733d66baSBarry Smith       x[i] += t[i];
181817ab2063SBarry Smith     }
181917ab2063SBarry Smith 
1820dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
18211ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
18223649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
18233a40ed3dSBarry Smith     PetscFunctionReturn(0);
182417ab2063SBarry Smith   }
182517ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
182617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
182717ab2063SBarry Smith       for (i=0; i<m; i++) {
1828416022c9SBarry Smith         n   = diag[i] - a->i[i];
1829ed480e8bSBarry Smith         idx = a->j + a->i[i];
1830ed480e8bSBarry Smith         v   = a->a + a->i[i];
183117ab2063SBarry Smith         sum = b[i];
1832e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
18335c99c7daSBarry Smith         t[i] = sum;
1834ed480e8bSBarry Smith         x[i] = sum*idiag[i];
183517ab2063SBarry Smith       }
18365c99c7daSBarry Smith       xb   = t;
1837efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18383a40ed3dSBarry Smith     } else xb = b;
183917ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
184017ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1841416022c9SBarry Smith         n   = a->i[i+1] - diag[i] - 1;
1842ed480e8bSBarry Smith         idx = a->j + diag[i] + 1;
1843ed480e8bSBarry Smith         v   = a->a + diag[i] + 1;
184417ab2063SBarry Smith         sum = xb[i];
1845e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
18465c99c7daSBarry Smith         if (xb == b) {
1847ed480e8bSBarry Smith           x[i] = sum*idiag[i];
18485c99c7daSBarry Smith         } else {
1849b19a5dc2SMark Adams           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
185017ab2063SBarry Smith         }
18515c99c7daSBarry Smith       }
1852b19a5dc2SMark Adams       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
185317ab2063SBarry Smith     }
185417ab2063SBarry Smith     its--;
185517ab2063SBarry Smith   }
185617ab2063SBarry Smith   while (its--) {
185717ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
185817ab2063SBarry Smith       for (i=0; i<m; i++) {
1859b19a5dc2SMark Adams         /* lower */
1860b19a5dc2SMark Adams         n   = diag[i] - a->i[i];
1861ed480e8bSBarry Smith         idx = a->j + a->i[i];
1862ed480e8bSBarry Smith         v   = a->a + a->i[i];
186317ab2063SBarry Smith         sum = b[i];
1864e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1865b19a5dc2SMark Adams         t[i] = sum;             /* save application of the lower-triangular part */
1866b19a5dc2SMark Adams         /* upper */
1867b19a5dc2SMark Adams         n   = a->i[i+1] - diag[i] - 1;
1868b19a5dc2SMark Adams         idx = a->j + diag[i] + 1;
1869b19a5dc2SMark Adams         v   = a->a + diag[i] + 1;
1870b19a5dc2SMark Adams         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1871b19a5dc2SMark Adams         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
187217ab2063SBarry Smith       }
1873b19a5dc2SMark Adams       xb   = t;
18749f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1875b19a5dc2SMark Adams     } else xb = b;
187617ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
187717ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1878b19a5dc2SMark Adams         sum = xb[i];
1879b19a5dc2SMark Adams         if (xb == b) {
1880b19a5dc2SMark Adams           /* whole matrix (no checkpointing available) */
1881416022c9SBarry Smith           n   = a->i[i+1] - a->i[i];
1882ed480e8bSBarry Smith           idx = a->j + a->i[i];
1883ed480e8bSBarry Smith           v   = a->a + a->i[i];
1884e6d1f457SBarry Smith           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1885ed480e8bSBarry Smith           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1886b19a5dc2SMark Adams         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1887b19a5dc2SMark Adams           n   = a->i[i+1] - diag[i] - 1;
1888b19a5dc2SMark Adams           idx = a->j + diag[i] + 1;
1889b19a5dc2SMark Adams           v   = a->a + diag[i] + 1;
1890b19a5dc2SMark Adams           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1891b19a5dc2SMark Adams           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
189217ab2063SBarry Smith         }
1893b19a5dc2SMark Adams       }
1894b19a5dc2SMark Adams       if (xb == b) {
18959f863219SBarry Smith         ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1896b19a5dc2SMark Adams       } else {
1897b19a5dc2SMark Adams         ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1898b19a5dc2SMark Adams       }
189917ab2063SBarry Smith     }
190017ab2063SBarry Smith   }
19011ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
19023649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1903365a8a9eSBarry Smith   PetscFunctionReturn(0);
190417ab2063SBarry Smith }
190517ab2063SBarry Smith 
19062af78befSBarry Smith 
19074a2ae208SSatish Balay #undef __FUNCT__
19084a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1909dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
191017ab2063SBarry Smith {
1911416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
19124e220ebcSLois Curfman McInnes 
19133a40ed3dSBarry Smith   PetscFunctionBegin;
19144e220ebcSLois Curfman McInnes   info->block_size   = 1.0;
19154e220ebcSLois Curfman McInnes   info->nz_allocated = (double)a->maxnz;
19164e220ebcSLois Curfman McInnes   info->nz_used      = (double)a->nz;
19174e220ebcSLois Curfman McInnes   info->nz_unneeded  = (double)(a->maxnz - a->nz);
19184e220ebcSLois Curfman McInnes   info->assemblies   = (double)A->num_ass;
19198e58a170SBarry Smith   info->mallocs      = (double)A->info.mallocs;
19207adad957SLisandro Dalcin   info->memory       = ((PetscObject)A)->mem;
1921d5f3da31SBarry Smith   if (A->factortype) {
19224e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
19234e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
19244e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
19254e220ebcSLois Curfman McInnes   } else {
19264e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
19274e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
19284e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
19294e220ebcSLois Curfman McInnes   }
19303a40ed3dSBarry Smith   PetscFunctionReturn(0);
193117ab2063SBarry Smith }
193217ab2063SBarry Smith 
19334a2ae208SSatish Balay #undef __FUNCT__
19344a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
19352b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
193617ab2063SBarry Smith {
1937416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
19383b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
19396849ba73SBarry Smith   PetscErrorCode    ierr;
194097b48c8fSBarry Smith   const PetscScalar *xx;
194197b48c8fSBarry Smith   PetscScalar       *bb;
1942ace3abfcSBarry Smith   PetscBool         missing;
194317ab2063SBarry Smith 
19443a40ed3dSBarry Smith   PetscFunctionBegin;
194597b48c8fSBarry Smith   if (x && b) {
194697b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
194797b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
194897b48c8fSBarry Smith     for (i=0; i<N; i++) {
194997b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
195097b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
195197b48c8fSBarry Smith     }
195297b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
195397b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
195497b48c8fSBarry Smith   }
195597b48c8fSBarry Smith 
1956a9817697SBarry Smith   if (a->keepnonzeropattern) {
1957f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1958e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1959bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1960f1e2ffcdSBarry Smith     }
1961f4df32b1SMatthew Knepley     if (diag != 0.0) {
196209f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1963e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1964f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1965f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1966f1e2ffcdSBarry Smith       }
1967f1e2ffcdSBarry Smith     }
1968f1e2ffcdSBarry Smith   } else {
1969f4df32b1SMatthew Knepley     if (diag != 0.0) {
197017ab2063SBarry Smith       for (i=0; i<N; i++) {
1971e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
19727ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1973416022c9SBarry Smith           a->ilen[rows[i]]    = 1;
1974f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1975bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
19767ae801bdSBarry Smith         } else { /* in case row was completely empty */
1977f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
197817ab2063SBarry Smith         }
197917ab2063SBarry Smith       }
19803a40ed3dSBarry Smith     } else {
198117ab2063SBarry Smith       for (i=0; i<N; i++) {
1982e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1983416022c9SBarry Smith         a->ilen[rows[i]] = 0;
198417ab2063SBarry Smith       }
198517ab2063SBarry Smith     }
1986e56f5c9eSBarry Smith     A->nonzerostate++;
1987f1e2ffcdSBarry Smith   }
198843a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19893a40ed3dSBarry Smith   PetscFunctionReturn(0);
199017ab2063SBarry Smith }
199117ab2063SBarry Smith 
19924a2ae208SSatish Balay #undef __FUNCT__
19936e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
19946e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
19956e169961SBarry Smith {
19966e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
19976e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
19986e169961SBarry Smith   PetscErrorCode    ierr;
19992b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
20006e169961SBarry Smith   const PetscScalar *xx;
20016e169961SBarry Smith   PetscScalar       *bb;
20026e169961SBarry Smith 
20036e169961SBarry Smith   PetscFunctionBegin;
20046e169961SBarry Smith   if (x && b) {
20056e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
20066e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
20072b40b63fSBarry Smith     vecs = PETSC_TRUE;
20086e169961SBarry Smith   }
20091795a4d1SJed Brown   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
20106e169961SBarry Smith   for (i=0; i<N; i++) {
20116e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
20126e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
20132205254eSKarl Rupp 
20146e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
20156e169961SBarry Smith   }
20166e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
20176e169961SBarry Smith     if (!zeroed[i]) {
20186e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
20196e169961SBarry Smith         if (zeroed[a->j[j]]) {
20202b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
20216e169961SBarry Smith           a->a[j] = 0.0;
20226e169961SBarry Smith         }
20236e169961SBarry Smith       }
20242b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
20256e169961SBarry Smith   }
20266e169961SBarry Smith   if (x && b) {
20276e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
20286e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
20296e169961SBarry Smith   }
20306e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
20316e169961SBarry Smith   if (diag != 0.0) {
20326e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
20336e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
20346e169961SBarry Smith     for (i=0; i<N; i++) {
20356e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
20366e169961SBarry Smith     }
20376e169961SBarry Smith   }
20386e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20396e169961SBarry Smith   PetscFunctionReturn(0);
20406e169961SBarry Smith }
20416e169961SBarry Smith 
20426e169961SBarry Smith #undef __FUNCT__
20434a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
2044a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
204517ab2063SBarry Smith {
2046416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
204797f1f81fSBarry Smith   PetscInt   *itmp;
204817ab2063SBarry Smith 
20493a40ed3dSBarry Smith   PetscFunctionBegin;
2050e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
205117ab2063SBarry Smith 
2052416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
2053bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
205417ab2063SBarry Smith   if (idx) {
2055bfeeae90SHong Zhang     itmp = a->j + a->i[row];
205626fbe8dcSKarl Rupp     if (*nz) *idx = itmp;
205717ab2063SBarry Smith     else *idx = 0;
205817ab2063SBarry Smith   }
20593a40ed3dSBarry Smith   PetscFunctionReturn(0);
206017ab2063SBarry Smith }
206117ab2063SBarry Smith 
2062bfeeae90SHong Zhang /* remove this function? */
20634a2ae208SSatish Balay #undef __FUNCT__
20644a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
2065a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
206617ab2063SBarry Smith {
20673a40ed3dSBarry Smith   PetscFunctionBegin;
20683a40ed3dSBarry Smith   PetscFunctionReturn(0);
206917ab2063SBarry Smith }
207017ab2063SBarry Smith 
20714a2ae208SSatish Balay #undef __FUNCT__
20724a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
2073dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
207417ab2063SBarry Smith {
2075416022c9SBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
207654f21887SBarry Smith   MatScalar      *v  = a->a;
207736db0b34SBarry Smith   PetscReal      sum = 0.0;
20786849ba73SBarry Smith   PetscErrorCode ierr;
207997f1f81fSBarry Smith   PetscInt       i,j;
208017ab2063SBarry Smith 
20813a40ed3dSBarry Smith   PetscFunctionBegin;
208217ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
2083416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
208436db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
208517ab2063SBarry Smith     }
20868f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
20873a40ed3dSBarry Smith   } else if (type == NORM_1) {
208836db0b34SBarry Smith     PetscReal *tmp;
208997f1f81fSBarry Smith     PetscInt  *jj = a->j;
20901795a4d1SJed Brown     ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr);
2091064f8208SBarry Smith     *nrm = 0.0;
2092416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
2093bfeeae90SHong Zhang       tmp[*jj++] += PetscAbsScalar(*v);  v++;
209417ab2063SBarry Smith     }
2095d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
2096064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
209717ab2063SBarry Smith     }
2098606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
20993a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
2100064f8208SBarry Smith     *nrm = 0.0;
2101d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
2102bfeeae90SHong Zhang       v   = a->a + a->i[j];
210317ab2063SBarry Smith       sum = 0.0;
2104416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2105cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
210617ab2063SBarry Smith       }
2107064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
210817ab2063SBarry Smith     }
2109f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
21103a40ed3dSBarry Smith   PetscFunctionReturn(0);
211117ab2063SBarry Smith }
211217ab2063SBarry Smith 
21134e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
21144e938277SHong Zhang #undef __FUNCT__
21154e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
21164e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
21174e938277SHong Zhang {
21184e938277SHong Zhang   PetscErrorCode ierr;
21194e938277SHong Zhang   PetscInt       i,j,anzj;
21204e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
21214e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
21224e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
21234e938277SHong Zhang 
21244e938277SHong Zhang   PetscFunctionBegin;
21254e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
21261795a4d1SJed Brown   ierr = PetscCalloc1((an+1),&ati);CHKERRQ(ierr);
2127785e854fSJed Brown   ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr);
2128785e854fSJed Brown   ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr);
21294e938277SHong Zhang 
21304e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
21314e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
213226fbe8dcSKarl Rupp   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
21334e938277SHong Zhang   /* Form ati for csr format of A^T. */
213426fbe8dcSKarl Rupp   for (i=0;i<an;i++) ati[i+1] += ati[i];
21354e938277SHong Zhang 
21364e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
21374e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
21384e938277SHong Zhang 
21394e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
21404e938277SHong Zhang   for (i=0;i<am;i++) {
21414e938277SHong Zhang     anzj = ai[i+1] - ai[i];
21424e938277SHong Zhang     for (j=0;j<anzj;j++) {
21434e938277SHong Zhang       atj[atfill[*aj]] = i;
21444e938277SHong Zhang       atfill[*aj++]   += 1;
21454e938277SHong Zhang     }
21464e938277SHong Zhang   }
21474e938277SHong Zhang 
21484e938277SHong Zhang   /* Clean up temporary space and complete requests. */
21494e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
2150ce94432eSBarry Smith   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
215133d57670SJed Brown   ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
2152a2f3521dSMark F. Adams 
21534e938277SHong Zhang   b          = (Mat_SeqAIJ*)((*B)->data);
21544e938277SHong Zhang   b->free_a  = PETSC_FALSE;
21554e938277SHong Zhang   b->free_ij = PETSC_TRUE;
21564e938277SHong Zhang   b->nonew   = 0;
21574e938277SHong Zhang   PetscFunctionReturn(0);
21584e938277SHong Zhang }
21594e938277SHong Zhang 
21604a2ae208SSatish Balay #undef __FUNCT__
21614a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
2162fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
216317ab2063SBarry Smith {
2164416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2165416022c9SBarry Smith   Mat            C;
21666849ba73SBarry Smith   PetscErrorCode ierr;
2167d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
216854f21887SBarry Smith   MatScalar      *array = a->a;
216917ab2063SBarry Smith 
21703a40ed3dSBarry Smith   PetscFunctionBegin;
2171e32f2f54SBarry 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");
2172fc4dec0aSBarry Smith 
2173fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
21741795a4d1SJed Brown     ierr = PetscCalloc1((1+A->cmap->n),&col);CHKERRQ(ierr);
2175bfeeae90SHong Zhang 
2176bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
2177ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2178d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
217933d57670SJed Brown     ierr = MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
21807adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2181ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
2182606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
2183a541d17aSBarry Smith   } else {
2184a541d17aSBarry Smith     C = *B;
2185a541d17aSBarry Smith   }
2186a541d17aSBarry Smith 
218717ab2063SBarry Smith   for (i=0; i<m; i++) {
218817ab2063SBarry Smith     len    = ai[i+1]-ai[i];
218987d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
2190b9b97703SBarry Smith     array += len;
2191b9b97703SBarry Smith     aj    += len;
219217ab2063SBarry Smith   }
21936d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21946d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
219517ab2063SBarry Smith 
2196815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
2197416022c9SBarry Smith     *B = C;
219817ab2063SBarry Smith   } else {
2199eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
220017ab2063SBarry Smith   }
22013a40ed3dSBarry Smith   PetscFunctionReturn(0);
220217ab2063SBarry Smith }
220317ab2063SBarry Smith 
2204cd0d46ebSvictorle #undef __FUNCT__
22055fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
22067087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2207cd0d46ebSvictorle {
2208cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
220954f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
221054f21887SBarry Smith   MatScalar      *va,*vb;
22116849ba73SBarry Smith   PetscErrorCode ierr;
221297f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
2213cd0d46ebSvictorle 
2214cd0d46ebSvictorle   PetscFunctionBegin;
2215cd0d46ebSvictorle   bij = (Mat_SeqAIJ*) B->data;
2216cd0d46ebSvictorle 
2217cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2218cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
22195485867bSBarry Smith   if (ma!=nb || na!=mb) {
22205485867bSBarry Smith     *f = PETSC_FALSE;
22215485867bSBarry Smith     PetscFunctionReturn(0);
22225485867bSBarry Smith   }
2223cd0d46ebSvictorle   aii  = aij->i; bii = bij->i;
2224cd0d46ebSvictorle   adx  = aij->j; bdx = bij->j;
2225cd0d46ebSvictorle   va   = aij->a; vb = bij->a;
2226785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2227785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
2228cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
2229cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
2230cd0d46ebSvictorle 
2231cd0d46ebSvictorle   *f = PETSC_TRUE;
2232cd0d46ebSvictorle   for (i=0; i<ma; i++) {
2233cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
223497f1f81fSBarry Smith       PetscInt    idc,idr;
22355485867bSBarry Smith       PetscScalar vc,vr;
2236cd0d46ebSvictorle       /* column/row index/value */
22375485867bSBarry Smith       idc = adx[aptr[i]];
22385485867bSBarry Smith       idr = bdx[bptr[idc]];
22395485867bSBarry Smith       vc  = va[aptr[i]];
22405485867bSBarry Smith       vr  = vb[bptr[idc]];
22415485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
22425485867bSBarry Smith         *f = PETSC_FALSE;
22435485867bSBarry Smith         goto done;
2244cd0d46ebSvictorle       } else {
22455485867bSBarry Smith         aptr[i]++;
22465485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
2247cd0d46ebSvictorle       }
2248cd0d46ebSvictorle     }
2249cd0d46ebSvictorle   }
2250cd0d46ebSvictorle done:
2251cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
22523aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
2253cd0d46ebSvictorle   PetscFunctionReturn(0);
2254cd0d46ebSvictorle }
2255cd0d46ebSvictorle 
22561cbb95d3SBarry Smith #undef __FUNCT__
22571cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
22587087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
22591cbb95d3SBarry Smith {
22601cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) A->data;
226154f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
226254f21887SBarry Smith   MatScalar      *va,*vb;
22631cbb95d3SBarry Smith   PetscErrorCode ierr;
22641cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
22651cbb95d3SBarry Smith 
22661cbb95d3SBarry Smith   PetscFunctionBegin;
22671cbb95d3SBarry Smith   bij = (Mat_SeqAIJ*) B->data;
22681cbb95d3SBarry Smith 
22691cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
22701cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
22711cbb95d3SBarry Smith   if (ma!=nb || na!=mb) {
22721cbb95d3SBarry Smith     *f = PETSC_FALSE;
22731cbb95d3SBarry Smith     PetscFunctionReturn(0);
22741cbb95d3SBarry Smith   }
22751cbb95d3SBarry Smith   aii  = aij->i; bii = bij->i;
22761cbb95d3SBarry Smith   adx  = aij->j; bdx = bij->j;
22771cbb95d3SBarry Smith   va   = aij->a; vb = bij->a;
2278785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2279785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
22801cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
22811cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
22821cbb95d3SBarry Smith 
22831cbb95d3SBarry Smith   *f = PETSC_TRUE;
22841cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
22851cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
22861cbb95d3SBarry Smith       PetscInt    idc,idr;
22871cbb95d3SBarry Smith       PetscScalar vc,vr;
22881cbb95d3SBarry Smith       /* column/row index/value */
22891cbb95d3SBarry Smith       idc = adx[aptr[i]];
22901cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
22911cbb95d3SBarry Smith       vc  = va[aptr[i]];
22921cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
22931cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
22941cbb95d3SBarry Smith         *f = PETSC_FALSE;
22951cbb95d3SBarry Smith         goto done;
22961cbb95d3SBarry Smith       } else {
22971cbb95d3SBarry Smith         aptr[i]++;
22981cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
22991cbb95d3SBarry Smith       }
23001cbb95d3SBarry Smith     }
23011cbb95d3SBarry Smith   }
23021cbb95d3SBarry Smith done:
23031cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
23041cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
23051cbb95d3SBarry Smith   PetscFunctionReturn(0);
23061cbb95d3SBarry Smith }
23071cbb95d3SBarry Smith 
23089e29f15eSvictorle #undef __FUNCT__
23099e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2310ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
23119e29f15eSvictorle {
2312dfbe8321SBarry Smith   PetscErrorCode ierr;
23136e111a19SKarl Rupp 
23149e29f15eSvictorle   PetscFunctionBegin;
23155485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
23169e29f15eSvictorle   PetscFunctionReturn(0);
23179e29f15eSvictorle }
23189e29f15eSvictorle 
23194a2ae208SSatish Balay #undef __FUNCT__
23201cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2321ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
23221cbb95d3SBarry Smith {
23231cbb95d3SBarry Smith   PetscErrorCode ierr;
23246e111a19SKarl Rupp 
23251cbb95d3SBarry Smith   PetscFunctionBegin;
23261cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
23271cbb95d3SBarry Smith   PetscFunctionReturn(0);
23281cbb95d3SBarry Smith }
23291cbb95d3SBarry Smith 
23301cbb95d3SBarry Smith #undef __FUNCT__
23314a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2332dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
233317ab2063SBarry Smith {
2334416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
233554f21887SBarry Smith   PetscScalar    *l,*r,x;
233654f21887SBarry Smith   MatScalar      *v;
2337dfbe8321SBarry Smith   PetscErrorCode ierr;
2338d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
233917ab2063SBarry Smith 
23403a40ed3dSBarry Smith   PetscFunctionBegin;
234117ab2063SBarry Smith   if (ll) {
23423ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
23433ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2344e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2345e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
23461ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2347416022c9SBarry Smith     v    = a->a;
234817ab2063SBarry Smith     for (i=0; i<m; i++) {
234917ab2063SBarry Smith       x = l[i];
2350416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
23512205254eSKarl Rupp       for (j=0; j<M; j++) (*v++) *= x;
235217ab2063SBarry Smith     }
23531ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2354efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
235517ab2063SBarry Smith   }
235617ab2063SBarry Smith   if (rr) {
2357e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2358e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
23591ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2360416022c9SBarry Smith     v    = a->a; jj = a->j;
23612205254eSKarl Rupp     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
23621ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2363efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
236417ab2063SBarry Smith   }
2365acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
23663a40ed3dSBarry Smith   PetscFunctionReturn(0);
236717ab2063SBarry Smith }
236817ab2063SBarry Smith 
23694a2ae208SSatish Balay #undef __FUNCT__
23704a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
237197f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
237217ab2063SBarry Smith {
2373db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
23746849ba73SBarry Smith   PetscErrorCode ierr;
2375d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
237697f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
23775d0c19d7SBarry Smith   const PetscInt *irow,*icol;
23785d0c19d7SBarry Smith   PetscInt       nrows,ncols;
237997f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
238054f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2381416022c9SBarry Smith   Mat            C;
2382ace3abfcSBarry Smith   PetscBool      stride,sorted;
238317ab2063SBarry Smith 
23843a40ed3dSBarry Smith   PetscFunctionBegin;
238514ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
2386e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
238714ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
2388e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
238999141d43SSatish Balay 
239017ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2391b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2392b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
239317ab2063SBarry Smith 
2394fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2395251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2396fee21e36SBarry Smith   if (stride && step == 1) {
239702834360SBarry Smith     /* special case of contiguous rows */
2398dcca6d9dSJed Brown     ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr);
239902834360SBarry Smith     /* loop over new rows determining lens and starting points */
240002834360SBarry Smith     for (i=0; i<nrows; i++) {
2401bfeeae90SHong Zhang       kstart = ai[irow[i]];
2402a2744918SBarry Smith       kend   = kstart + ailen[irow[i]];
240302834360SBarry Smith       for (k=kstart; k<kend; k++) {
2404bfeeae90SHong Zhang         if (aj[k] >= first) {
240502834360SBarry Smith           starts[i] = k;
240602834360SBarry Smith           break;
240702834360SBarry Smith         }
240802834360SBarry Smith       }
2409a2744918SBarry Smith       sum = 0;
241002834360SBarry Smith       while (k < kend) {
2411bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2412a2744918SBarry Smith         sum++;
241302834360SBarry Smith       }
2414a2744918SBarry Smith       lens[i] = sum;
241502834360SBarry Smith     }
241602834360SBarry Smith     /* create submatrix */
2417cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
241897f1f81fSBarry Smith       PetscInt n_cols,n_rows;
241908480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2420e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2421d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
242208480c60SBarry Smith       C    = *B;
24233a40ed3dSBarry Smith     } else {
24243bef6203SJed Brown       PetscInt rbs,cbs;
2425ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2426f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
24273bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
24283bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
24293bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
24307adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2431ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
243208480c60SBarry Smith     }
2433db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2434db02288aSLois Curfman McInnes 
243502834360SBarry Smith     /* loop over rows inserting into submatrix */
2436db02288aSLois Curfman McInnes     a_new = c->a;
2437db02288aSLois Curfman McInnes     j_new = c->j;
2438db02288aSLois Curfman McInnes     i_new = c->i;
2439bfeeae90SHong Zhang 
244002834360SBarry Smith     for (i=0; i<nrows; i++) {
2441a2744918SBarry Smith       ii    = starts[i];
2442a2744918SBarry Smith       lensi = lens[i];
2443a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2444a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
244502834360SBarry Smith       }
244687828ca2SBarry Smith       ierr       = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2447a2744918SBarry Smith       a_new     += lensi;
2448a2744918SBarry Smith       i_new[i+1] = i_new[i] + lensi;
2449a2744918SBarry Smith       c->ilen[i] = lensi;
245002834360SBarry Smith     }
24510e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
24523a40ed3dSBarry Smith   } else {
245302834360SBarry Smith     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
24541795a4d1SJed Brown     ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr);
2455785e854fSJed Brown     ierr = PetscMalloc1((1+nrows),&lens);CHKERRQ(ierr);
24564dcab191SBarry Smith     for (i=0; i<ncols; i++) {
24574dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
24584dcab191SBarry 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);
24594dcab191SBarry Smith #endif
24604dcab191SBarry Smith       smap[icol[i]] = i+1;
24614dcab191SBarry Smith     }
24624dcab191SBarry Smith 
246302834360SBarry Smith     /* determine lens of each row */
246402834360SBarry Smith     for (i=0; i<nrows; i++) {
2465bfeeae90SHong Zhang       kstart  = ai[irow[i]];
246602834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
246702834360SBarry Smith       lens[i] = 0;
246802834360SBarry Smith       for (k=kstart; k<kend; k++) {
2469bfeeae90SHong Zhang         if (smap[aj[k]]) {
247002834360SBarry Smith           lens[i]++;
247102834360SBarry Smith         }
247202834360SBarry Smith       }
247302834360SBarry Smith     }
247417ab2063SBarry Smith     /* Create and fill new matrix */
2475a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2476ace3abfcSBarry Smith       PetscBool equal;
24770f5bd95cSBarry Smith 
247899141d43SSatish Balay       c = (Mat_SeqAIJ*)((*B)->data);
2479e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2480d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
2481f23aa3ddSBarry Smith       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2482d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
248308480c60SBarry Smith       C    = *B;
24843a40ed3dSBarry Smith     } else {
24853bef6203SJed Brown       PetscInt rbs,cbs;
2486ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2487f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
24883bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
24893bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
24903bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
24917adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2492ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
249308480c60SBarry Smith     }
249499141d43SSatish Balay     c = (Mat_SeqAIJ*)(C->data);
249517ab2063SBarry Smith     for (i=0; i<nrows; i++) {
249699141d43SSatish Balay       row      = irow[i];
2497bfeeae90SHong Zhang       kstart   = ai[row];
249899141d43SSatish Balay       kend     = kstart + a->ilen[row];
2499bfeeae90SHong Zhang       mat_i    = c->i[i];
250099141d43SSatish Balay       mat_j    = c->j + mat_i;
250199141d43SSatish Balay       mat_a    = c->a + mat_i;
250299141d43SSatish Balay       mat_ilen = c->ilen + i;
250317ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2504bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2505ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
250699141d43SSatish Balay           *mat_a++ = a->a[k];
250799141d43SSatish Balay           (*mat_ilen)++;
250899141d43SSatish Balay 
250917ab2063SBarry Smith         }
251017ab2063SBarry Smith       }
251117ab2063SBarry Smith     }
251202834360SBarry Smith     /* Free work space */
251302834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2514606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2515606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
251602834360SBarry Smith   }
25176d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
25186d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
251917ab2063SBarry Smith 
252017ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2521416022c9SBarry Smith   *B   = C;
25223a40ed3dSBarry Smith   PetscFunctionReturn(0);
252317ab2063SBarry Smith }
252417ab2063SBarry Smith 
25251df811f5SHong Zhang #undef __FUNCT__
252682d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2527fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
252882d44351SHong Zhang {
252982d44351SHong Zhang   PetscErrorCode ierr;
253082d44351SHong Zhang   Mat            B;
253182d44351SHong Zhang 
253282d44351SHong Zhang   PetscFunctionBegin;
2533c2d650bdSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
253482d44351SHong Zhang     ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
253582d44351SHong Zhang     ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
253633d57670SJed Brown     ierr    = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr);
253782d44351SHong Zhang     ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
253882d44351SHong Zhang     ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
253982d44351SHong Zhang     *subMat = B;
2540c2d650bdSHong Zhang   } else {
2541c2d650bdSHong Zhang     ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
2542c2d650bdSHong Zhang   }
254382d44351SHong Zhang   PetscFunctionReturn(0);
254482d44351SHong Zhang }
254582d44351SHong Zhang 
254682d44351SHong Zhang #undef __FUNCT__
25474a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
25480481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2549a871dcd8SBarry Smith {
255063b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2551dfbe8321SBarry Smith   PetscErrorCode ierr;
255263b91edcSBarry Smith   Mat            outA;
2553ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
255463b91edcSBarry Smith 
25553a40ed3dSBarry Smith   PetscFunctionBegin;
2556e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
25571df811f5SHong Zhang 
2558b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2559b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2560a871dcd8SBarry Smith 
256163b91edcSBarry Smith   outA             = inA;
2562d5f3da31SBarry Smith   outA->factortype = MAT_FACTOR_LU;
25632205254eSKarl Rupp 
2564c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
25656bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
25662205254eSKarl Rupp 
2567c3122656SLisandro Dalcin   a->row = row;
25682205254eSKarl Rupp 
2569c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
25706bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
25712205254eSKarl Rupp 
2572c3122656SLisandro Dalcin   a->col = col;
257363b91edcSBarry Smith 
257436db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
25756bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
25764c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
25773bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
2578f0ec6fceSSatish Balay 
257994a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2580785e854fSJed Brown     ierr = PetscMalloc1((inA->rmap->n+1),&a->solve_work);CHKERRQ(ierr);
25813bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
258294a9d846SBarry Smith   }
258363b91edcSBarry Smith 
2584f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2585137fb511SHong Zhang   if (row_identity && col_identity) {
2586ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2587137fb511SHong Zhang   } else {
2588719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2589137fb511SHong Zhang   }
25903a40ed3dSBarry Smith   PetscFunctionReturn(0);
2591a871dcd8SBarry Smith }
2592a871dcd8SBarry Smith 
25934a2ae208SSatish Balay #undef __FUNCT__
25944a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2595f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2596f0b747eeSBarry Smith {
2597f0b747eeSBarry Smith   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2598f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2599efee365bSSatish Balay   PetscErrorCode ierr;
2600c5df96a5SBarry Smith   PetscBLASInt   one = 1,bnz;
26013a40ed3dSBarry Smith 
26023a40ed3dSBarry Smith   PetscFunctionBegin;
2603c5df96a5SBarry Smith   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
26048b83055fSJed Brown   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2605efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2606acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
26073a40ed3dSBarry Smith   PetscFunctionReturn(0);
2608f0b747eeSBarry Smith }
2609f0b747eeSBarry Smith 
26104a2ae208SSatish Balay #undef __FUNCT__
26114a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
261297f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2613cddf8d76SBarry Smith {
2614dfbe8321SBarry Smith   PetscErrorCode ierr;
261597f1f81fSBarry Smith   PetscInt       i;
2616cddf8d76SBarry Smith 
26173a40ed3dSBarry Smith   PetscFunctionBegin;
2618cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2619785e854fSJed Brown     ierr = PetscMalloc1((n+1),B);CHKERRQ(ierr);
2620cddf8d76SBarry Smith   }
2621cddf8d76SBarry Smith 
2622cddf8d76SBarry Smith   for (i=0; i<n; i++) {
26236a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2624cddf8d76SBarry Smith   }
26253a40ed3dSBarry Smith   PetscFunctionReturn(0);
2626cddf8d76SBarry Smith }
2627cddf8d76SBarry Smith 
26284a2ae208SSatish Balay #undef __FUNCT__
26294a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
263097f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
26314dcbc457SBarry Smith {
2632e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
26336849ba73SBarry Smith   PetscErrorCode ierr;
26345d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
26355d0c19d7SBarry Smith   const PetscInt *idx;
263697f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2637f1af5d2fSBarry Smith   PetscBT        table;
2638bbd702dbSSatish Balay 
26393a40ed3dSBarry Smith   PetscFunctionBegin;
2640d0f46423SBarry Smith   m  = A->rmap->n;
2641e4d965acSSatish Balay   ai = a->i;
2642bfeeae90SHong Zhang   aj = a->j;
26438a047759SSatish Balay 
2644e32f2f54SBarry Smith   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
264506763907SSatish Balay 
2646785e854fSJed Brown   ierr = PetscMalloc1((m+1),&nidx);CHKERRQ(ierr);
264753b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
264806763907SSatish Balay 
2649e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2650b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2651e4d965acSSatish Balay     isz  = 0;
26526831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2653e4d965acSSatish Balay 
2654e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
26554dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2656b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2657e4d965acSSatish Balay 
2658dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2659e4d965acSSatish Balay     for (j=0; j<n; ++j) {
26602205254eSKarl Rupp       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
26614dcbc457SBarry Smith     }
266206763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
26636bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2664e4d965acSSatish Balay 
266504a348a9SBarry Smith     k = 0;
266604a348a9SBarry Smith     for (j=0; j<ov; j++) { /* for each overlap */
266704a348a9SBarry Smith       n = isz;
266806763907SSatish Balay       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2669e4d965acSSatish Balay         row   = nidx[k];
2670e4d965acSSatish Balay         start = ai[row];
2671e4d965acSSatish Balay         end   = ai[row+1];
267204a348a9SBarry Smith         for (l = start; l<end; l++) {
2673efb16452SHong Zhang           val = aj[l];
26742205254eSKarl Rupp           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2675e4d965acSSatish Balay         }
2676e4d965acSSatish Balay       }
2677e4d965acSSatish Balay     }
267870b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2679e4d965acSSatish Balay   }
268094bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2681606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
26823a40ed3dSBarry Smith   PetscFunctionReturn(0);
26834dcbc457SBarry Smith }
268417ab2063SBarry Smith 
26850513a670SBarry Smith /* -------------------------------------------------------------- */
26864a2ae208SSatish Balay #undef __FUNCT__
26874a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2688dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
26890513a670SBarry Smith {
26900513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
26916849ba73SBarry Smith   PetscErrorCode ierr;
26923b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
26935d0c19d7SBarry Smith   const PetscInt *row,*col;
26945d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
269556cd22aeSBarry Smith   IS             icolp,irowp;
26960298fd71SBarry Smith   PetscInt       *cwork = NULL;
26970298fd71SBarry Smith   PetscScalar    *vwork = NULL;
26980513a670SBarry Smith 
26993a40ed3dSBarry Smith   PetscFunctionBegin;
27004c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
270156cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
27024c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
270356cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
27040513a670SBarry Smith 
27050513a670SBarry Smith   /* determine lengths of permuted rows */
2706785e854fSJed Brown   ierr = PetscMalloc1((m+1),&lens);CHKERRQ(ierr);
27072205254eSKarl Rupp   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2708ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2709f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
271033d57670SJed Brown   ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
27117adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2712ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2713606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
27140513a670SBarry Smith 
2715785e854fSJed Brown   ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr);
27160513a670SBarry Smith   for (i=0; i<m; i++) {
271732ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
27182205254eSKarl Rupp     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2719cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
272032ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
27210513a670SBarry Smith   }
2722606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
27232205254eSKarl Rupp 
27243c7d62e4SBarry Smith   (*B)->assembled = PETSC_FALSE;
27252205254eSKarl Rupp 
27260513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27270513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
272856cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
272956cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
27306bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
27316bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
27323a40ed3dSBarry Smith   PetscFunctionReturn(0);
27330513a670SBarry Smith }
27340513a670SBarry Smith 
27354a2ae208SSatish Balay #undef __FUNCT__
27364a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2737dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2738cb5b572fSBarry Smith {
2739dfbe8321SBarry Smith   PetscErrorCode ierr;
2740cb5b572fSBarry Smith 
2741cb5b572fSBarry Smith   PetscFunctionBegin;
274233f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
274333f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2744be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2745be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2746be6bf707SBarry Smith 
2747700c5bfcSBarry 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");
2748d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2749cb5b572fSBarry Smith   } else {
2750cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2751cb5b572fSBarry Smith   }
2752cb5b572fSBarry Smith   PetscFunctionReturn(0);
2753cb5b572fSBarry Smith }
2754cb5b572fSBarry Smith 
27554a2ae208SSatish Balay #undef __FUNCT__
27564994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
27574994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2758273d9f13SBarry Smith {
2759dfbe8321SBarry Smith   PetscErrorCode ierr;
2760273d9f13SBarry Smith 
2761273d9f13SBarry Smith   PetscFunctionBegin;
2762ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2763273d9f13SBarry Smith   PetscFunctionReturn(0);
2764273d9f13SBarry Smith }
2765273d9f13SBarry Smith 
27664a2ae208SSatish Balay #undef __FUNCT__
27678c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray_SeqAIJ"
27688c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
27696c0721eeSBarry Smith {
27706c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
27716e111a19SKarl Rupp 
27726c0721eeSBarry Smith   PetscFunctionBegin;
27736c0721eeSBarry Smith   *array = a->a;
27746c0721eeSBarry Smith   PetscFunctionReturn(0);
27756c0721eeSBarry Smith }
27766c0721eeSBarry Smith 
27774a2ae208SSatish Balay #undef __FUNCT__
27788c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray_SeqAIJ"
27798c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
27806c0721eeSBarry Smith {
27816c0721eeSBarry Smith   PetscFunctionBegin;
27826c0721eeSBarry Smith   PetscFunctionReturn(0);
27836c0721eeSBarry Smith }
2784273d9f13SBarry Smith 
27858229c054SShri Abhyankar /*
27868229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
27878229c054SShri Abhyankar    have different nonzero structure.
27888229c054SShri Abhyankar */
2789ac90fabeSBarry Smith #undef __FUNCT__
27908229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
27918229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2792ec7775f6SShri Abhyankar {
27938229c054SShri Abhyankar   PetscInt       i,m=Y->rmap->N;
2794ec7775f6SShri Abhyankar   Mat_SeqAIJ     *x  = (Mat_SeqAIJ*)X->data;
2795ec7775f6SShri Abhyankar   Mat_SeqAIJ     *y  = (Mat_SeqAIJ*)Y->data;
2796ec7775f6SShri Abhyankar   const PetscInt *xi = x->i,*yi = y->i;
2797ec7775f6SShri Abhyankar 
2798ec7775f6SShri Abhyankar   PetscFunctionBegin;
2799ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2800ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
28018af7cee1SJed Brown     PetscInt       j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
28028af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
28038af7cee1SJed Brown     nnz[i] = 0;
28048af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
28058af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
28068af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
28078af7cee1SJed Brown       nnz[i]++;
28088af7cee1SJed Brown     }
28098af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2810ec7775f6SShri Abhyankar   }
2811ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2812ec7775f6SShri Abhyankar }
2813ec7775f6SShri Abhyankar 
2814ec7775f6SShri Abhyankar #undef __FUNCT__
2815ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2816f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2817ac90fabeSBarry Smith {
2818dfbe8321SBarry Smith   PetscErrorCode ierr;
281997f1f81fSBarry Smith   PetscInt       i;
2820ac90fabeSBarry Smith   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2821c5df96a5SBarry Smith   PetscBLASInt   one=1,bnz;
2822ac90fabeSBarry Smith 
2823ac90fabeSBarry Smith   PetscFunctionBegin;
2824c5df96a5SBarry Smith   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2825ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2826f4df32b1SMatthew Knepley     PetscScalar alpha = a;
28278b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2828acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2829a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2830c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2831a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2832a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
28336bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2834a30b2313SHong Zhang     }
2835a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
28360298fd71SBarry Smith       ierr    = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,NULL, y->i,y->j,NULL, &y->xtoy);CHKERRQ(ierr);
2837a30b2313SHong Zhang       y->XtoY = X;
2838407f6b05SHong Zhang       ierr    = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2839c537a176SHong Zhang     }
2840f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2841a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
28426712e2f1SBarry 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);
2843ac90fabeSBarry Smith   } else {
28448229c054SShri Abhyankar     Mat      B;
28458229c054SShri Abhyankar     PetscInt *nnz;
2846785e854fSJed Brown     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
2847ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2848bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
28494aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
285033d57670SJed Brown     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
2851176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
28528229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2853ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2854ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2855ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
28568229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2857ac90fabeSBarry Smith   }
2858ac90fabeSBarry Smith   PetscFunctionReturn(0);
2859ac90fabeSBarry Smith }
2860ac90fabeSBarry Smith 
2861521d7252SBarry Smith #undef __FUNCT__
2862354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
28637087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2864354c94deSBarry Smith {
2865354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2866354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2867354c94deSBarry Smith   PetscInt    i,nz;
2868354c94deSBarry Smith   PetscScalar *a;
2869354c94deSBarry Smith 
2870354c94deSBarry Smith   PetscFunctionBegin;
2871354c94deSBarry Smith   nz = aij->nz;
2872354c94deSBarry Smith   a  = aij->a;
28732205254eSKarl Rupp   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2874354c94deSBarry Smith #else
2875354c94deSBarry Smith   PetscFunctionBegin;
2876354c94deSBarry Smith #endif
2877354c94deSBarry Smith   PetscFunctionReturn(0);
2878354c94deSBarry Smith }
2879354c94deSBarry Smith 
2880e34fafa9SBarry Smith #undef __FUNCT__
2881985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2882985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2883e34fafa9SBarry Smith {
2884e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2885e34fafa9SBarry Smith   PetscErrorCode ierr;
2886d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2887e34fafa9SBarry Smith   PetscReal      atmp;
2888985db425SBarry Smith   PetscScalar    *x;
2889e34fafa9SBarry Smith   MatScalar      *aa;
2890e34fafa9SBarry Smith 
2891e34fafa9SBarry Smith   PetscFunctionBegin;
2892e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2893e34fafa9SBarry Smith   aa = a->a;
2894e34fafa9SBarry Smith   ai = a->i;
2895e34fafa9SBarry Smith   aj = a->j;
2896e34fafa9SBarry Smith 
2897985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2898e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2899e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2900e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2901e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2902e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
29039189402eSHong Zhang     x[i]  = 0.0;
2904e34fafa9SBarry Smith     for (j=0; j<ncols; j++) {
2905985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2906985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2907985db425SBarry Smith       aa++; aj++;
2908985db425SBarry Smith     }
2909985db425SBarry Smith   }
2910985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2911985db425SBarry Smith   PetscFunctionReturn(0);
2912985db425SBarry Smith }
2913985db425SBarry Smith 
2914985db425SBarry Smith #undef __FUNCT__
2915985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2916985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2917985db425SBarry Smith {
2918985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2919985db425SBarry Smith   PetscErrorCode ierr;
2920d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2921985db425SBarry Smith   PetscScalar    *x;
2922985db425SBarry Smith   MatScalar      *aa;
2923985db425SBarry Smith 
2924985db425SBarry Smith   PetscFunctionBegin;
2925e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2926985db425SBarry Smith   aa = a->a;
2927985db425SBarry Smith   ai = a->i;
2928985db425SBarry Smith   aj = a->j;
2929985db425SBarry Smith 
2930985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2931985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2932985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2933e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2934985db425SBarry Smith   for (i=0; i<m; i++) {
2935985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2936d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2937985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2938985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2939985db425SBarry Smith       x[i] = 0.0;
2940985db425SBarry Smith       if (idx) {
2941985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2942985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2943985db425SBarry Smith           if (aj[j] > j) {
2944985db425SBarry Smith             idx[i] = j;
2945985db425SBarry Smith             break;
2946985db425SBarry Smith           }
2947985db425SBarry Smith         }
2948985db425SBarry Smith       }
2949985db425SBarry Smith     }
2950985db425SBarry Smith     for (j=0; j<ncols; j++) {
2951985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2952985db425SBarry Smith       aa++; aj++;
2953985db425SBarry Smith     }
2954985db425SBarry Smith   }
2955985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2956985db425SBarry Smith   PetscFunctionReturn(0);
2957985db425SBarry Smith }
2958985db425SBarry Smith 
2959985db425SBarry Smith #undef __FUNCT__
2960c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2961c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2962c87e5d42SMatthew Knepley {
2963c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2964c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2965c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2966c87e5d42SMatthew Knepley   PetscReal      atmp;
2967c87e5d42SMatthew Knepley   PetscScalar    *x;
2968c87e5d42SMatthew Knepley   MatScalar      *aa;
2969c87e5d42SMatthew Knepley 
2970c87e5d42SMatthew Knepley   PetscFunctionBegin;
2971e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2972c87e5d42SMatthew Knepley   aa = a->a;
2973c87e5d42SMatthew Knepley   ai = a->i;
2974c87e5d42SMatthew Knepley   aj = a->j;
2975c87e5d42SMatthew Knepley 
2976c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2977c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2978c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
297960e0710aSBarry 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);
2980c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2981c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2982289a08f5SMatthew Knepley     if (ncols) {
2983289a08f5SMatthew Knepley       /* Get first nonzero */
2984289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
2985289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
29862205254eSKarl Rupp         if (atmp > 1.0e-12) {
29872205254eSKarl Rupp           x[i] = atmp;
29882205254eSKarl Rupp           if (idx) idx[i] = aj[j];
29892205254eSKarl Rupp           break;
29902205254eSKarl Rupp         }
2991289a08f5SMatthew Knepley       }
299212431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2993289a08f5SMatthew Knepley     } else {
2994289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2995289a08f5SMatthew Knepley     }
2996c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
2997c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2998289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2999c87e5d42SMatthew Knepley       aa++; aj++;
3000c87e5d42SMatthew Knepley     }
3001c87e5d42SMatthew Knepley   }
3002c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3003c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
3004c87e5d42SMatthew Knepley }
3005c87e5d42SMatthew Knepley 
3006c87e5d42SMatthew Knepley #undef __FUNCT__
3007985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
3008985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3009985db425SBarry Smith {
3010985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3011985db425SBarry Smith   PetscErrorCode ierr;
3012d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3013985db425SBarry Smith   PetscScalar    *x;
3014985db425SBarry Smith   MatScalar      *aa;
3015985db425SBarry Smith 
3016985db425SBarry Smith   PetscFunctionBegin;
3017e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3018985db425SBarry Smith   aa = a->a;
3019985db425SBarry Smith   ai = a->i;
3020985db425SBarry Smith   aj = a->j;
3021985db425SBarry Smith 
3022985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3023985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3024985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
3025e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3026985db425SBarry Smith   for (i=0; i<m; i++) {
3027985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
3028d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
3029985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
3030985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3031985db425SBarry Smith       x[i] = 0.0;
3032985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
3033985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
3034985db425SBarry Smith         for (j=0; j<ncols; j++) {
3035985db425SBarry Smith           if (aj[j] > j) {
3036985db425SBarry Smith             idx[i] = j;
3037985db425SBarry Smith             break;
3038985db425SBarry Smith           }
3039985db425SBarry Smith         }
3040985db425SBarry Smith       }
3041985db425SBarry Smith     }
3042985db425SBarry Smith     for (j=0; j<ncols; j++) {
3043985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3044985db425SBarry Smith       aa++; aj++;
3045e34fafa9SBarry Smith     }
3046e34fafa9SBarry Smith   }
3047e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3048e34fafa9SBarry Smith   PetscFunctionReturn(0);
3049e34fafa9SBarry Smith }
3050bbead8a2SBarry Smith 
3051bbead8a2SBarry Smith #include <petscblaslapack.h>
305206873bf2SBarry Smith #include <petsc-private/kernels/blockinvert.h>
3053bbead8a2SBarry Smith 
3054bbead8a2SBarry Smith #undef __FUNCT__
3055bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
3056713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3057bbead8a2SBarry Smith {
3058bbead8a2SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
3059bbead8a2SBarry Smith   PetscErrorCode ierr;
306033d57670SJed Brown   PetscInt       i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3061bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
3062bbead8a2SBarry Smith   PetscReal      shift = 0.0;
3063bbead8a2SBarry Smith 
3064bbead8a2SBarry Smith   PetscFunctionBegin;
30654a0d0026SBarry Smith   if (a->ibdiagvalid) {
30664a0d0026SBarry Smith     if (values) *values = a->ibdiag;
30674a0d0026SBarry Smith     PetscFunctionReturn(0);
30684a0d0026SBarry Smith   }
3069bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
3070bbead8a2SBarry Smith   if (!a->ibdiag) {
3071785e854fSJed Brown     ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr);
30723bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
3073bbead8a2SBarry Smith   }
3074bbead8a2SBarry Smith   diag = a->ibdiag;
3075bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
3076bbead8a2SBarry Smith   /* factor and invert each block */
3077bbead8a2SBarry Smith   switch (bs) {
3078bbead8a2SBarry Smith   case 1:
3079bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3080bbead8a2SBarry Smith       ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
3081bbead8a2SBarry Smith       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3082bbead8a2SBarry Smith     }
3083bbead8a2SBarry Smith     break;
3084bbead8a2SBarry Smith   case 2:
3085bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3086bbead8a2SBarry Smith       ij[0] = 2*i; ij[1] = 2*i + 1;
3087bbead8a2SBarry Smith       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
308896b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
308996b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
3090bbead8a2SBarry Smith       diag += 4;
3091bbead8a2SBarry Smith     }
3092bbead8a2SBarry Smith     break;
3093bbead8a2SBarry Smith   case 3:
3094bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3095bbead8a2SBarry Smith       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3096bbead8a2SBarry Smith       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
309796b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
309896b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
3099bbead8a2SBarry Smith       diag += 9;
3100bbead8a2SBarry Smith     }
3101bbead8a2SBarry Smith     break;
3102bbead8a2SBarry Smith   case 4:
3103bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3104bbead8a2SBarry Smith       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3105bbead8a2SBarry Smith       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
310696b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
310796b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
3108bbead8a2SBarry Smith       diag += 16;
3109bbead8a2SBarry Smith     }
3110bbead8a2SBarry Smith     break;
3111bbead8a2SBarry Smith   case 5:
3112bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3113bbead8a2SBarry 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;
3114bbead8a2SBarry Smith       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
311596b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
311696b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
3117bbead8a2SBarry Smith       diag += 25;
3118bbead8a2SBarry Smith     }
3119bbead8a2SBarry Smith     break;
3120bbead8a2SBarry Smith   case 6:
3121bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3122bbead8a2SBarry 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;
3123bbead8a2SBarry Smith       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
312496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
312596b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3126bbead8a2SBarry Smith       diag += 36;
3127bbead8a2SBarry Smith     }
3128bbead8a2SBarry Smith     break;
3129bbead8a2SBarry Smith   case 7:
3130bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3131bbead8a2SBarry 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;
3132bbead8a2SBarry Smith       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
313396b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
313496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3135bbead8a2SBarry Smith       diag += 49;
3136bbead8a2SBarry Smith     }
3137bbead8a2SBarry Smith     break;
3138bbead8a2SBarry Smith   default:
3139dcca6d9dSJed Brown     ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr);
3140bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3141bbead8a2SBarry Smith       for (j=0; j<bs; j++) {
3142bbead8a2SBarry Smith         IJ[j] = bs*i + j;
3143bbead8a2SBarry Smith       }
3144bbead8a2SBarry Smith       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
314596b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
314696b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3147bbead8a2SBarry Smith       diag += bs2;
3148bbead8a2SBarry Smith     }
3149bbead8a2SBarry Smith     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3150bbead8a2SBarry Smith   }
3151bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3152bbead8a2SBarry Smith   PetscFunctionReturn(0);
3153bbead8a2SBarry Smith }
3154bbead8a2SBarry Smith 
315573a71a0fSBarry Smith #undef __FUNCT__
315673a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_SeqAIJ"
315773a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
315873a71a0fSBarry Smith {
315973a71a0fSBarry Smith   PetscErrorCode ierr;
316073a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
316173a71a0fSBarry Smith   PetscScalar    a;
316273a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
316373a71a0fSBarry Smith 
316473a71a0fSBarry Smith   PetscFunctionBegin;
316573a71a0fSBarry Smith   if (!x->assembled) {
316673a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
316773a71a0fSBarry Smith     for (i=0; i<m; i++) {
316873a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
316973a71a0fSBarry Smith         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
317073a71a0fSBarry Smith         col  = (PetscInt)(n*PetscRealPart(a));
317173a71a0fSBarry Smith         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
317273a71a0fSBarry Smith       }
317373a71a0fSBarry Smith     }
317473a71a0fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
317573a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
317673a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
317773a71a0fSBarry Smith   PetscFunctionReturn(0);
317873a71a0fSBarry Smith }
317973a71a0fSBarry Smith 
3180682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
31810a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3182cb5b572fSBarry Smith                                         MatGetRow_SeqAIJ,
3183cb5b572fSBarry Smith                                         MatRestoreRow_SeqAIJ,
3184cb5b572fSBarry Smith                                         MatMult_SeqAIJ,
318597304618SKris Buschelman                                 /*  4*/ MatMultAdd_SeqAIJ,
31867c922b88SBarry Smith                                         MatMultTranspose_SeqAIJ,
31877c922b88SBarry Smith                                         MatMultTransposeAdd_SeqAIJ,
3188db4efbfdSBarry Smith                                         0,
3189db4efbfdSBarry Smith                                         0,
3190db4efbfdSBarry Smith                                         0,
3191db4efbfdSBarry Smith                                 /* 10*/ 0,
3192cb5b572fSBarry Smith                                         MatLUFactor_SeqAIJ,
3193cb5b572fSBarry Smith                                         0,
319441f059aeSBarry Smith                                         MatSOR_SeqAIJ,
319517ab2063SBarry Smith                                         MatTranspose_SeqAIJ,
319697304618SKris Buschelman                                 /*1 5*/ MatGetInfo_SeqAIJ,
3197cb5b572fSBarry Smith                                         MatEqual_SeqAIJ,
3198cb5b572fSBarry Smith                                         MatGetDiagonal_SeqAIJ,
3199cb5b572fSBarry Smith                                         MatDiagonalScale_SeqAIJ,
3200cb5b572fSBarry Smith                                         MatNorm_SeqAIJ,
320197304618SKris Buschelman                                 /* 20*/ 0,
3202cb5b572fSBarry Smith                                         MatAssemblyEnd_SeqAIJ,
3203cb5b572fSBarry Smith                                         MatSetOption_SeqAIJ,
3204cb5b572fSBarry Smith                                         MatZeroEntries_SeqAIJ,
3205d519adbfSMatthew Knepley                                 /* 24*/ MatZeroRows_SeqAIJ,
3206db4efbfdSBarry Smith                                         0,
3207db4efbfdSBarry Smith                                         0,
3208db4efbfdSBarry Smith                                         0,
3209db4efbfdSBarry Smith                                         0,
32104994cf47SJed Brown                                 /* 29*/ MatSetUp_SeqAIJ,
3211db4efbfdSBarry Smith                                         0,
3212db4efbfdSBarry Smith                                         0,
32138c778c55SBarry Smith                                         0,
32148c778c55SBarry Smith                                         0,
3215d519adbfSMatthew Knepley                                 /* 34*/ MatDuplicate_SeqAIJ,
3216cb5b572fSBarry Smith                                         0,
3217cb5b572fSBarry Smith                                         0,
3218cb5b572fSBarry Smith                                         MatILUFactor_SeqAIJ,
3219cb5b572fSBarry Smith                                         0,
3220d519adbfSMatthew Knepley                                 /* 39*/ MatAXPY_SeqAIJ,
3221cb5b572fSBarry Smith                                         MatGetSubMatrices_SeqAIJ,
3222cb5b572fSBarry Smith                                         MatIncreaseOverlap_SeqAIJ,
3223cb5b572fSBarry Smith                                         MatGetValues_SeqAIJ,
3224cb5b572fSBarry Smith                                         MatCopy_SeqAIJ,
3225d519adbfSMatthew Knepley                                 /* 44*/ MatGetRowMax_SeqAIJ,
3226cb5b572fSBarry Smith                                         MatScale_SeqAIJ,
3227cb5b572fSBarry Smith                                         0,
322879299369SBarry Smith                                         MatDiagonalSet_SeqAIJ,
32296e169961SBarry Smith                                         MatZeroRowsColumns_SeqAIJ,
323073a71a0fSBarry Smith                                 /* 49*/ MatSetRandom_SeqAIJ,
32313b2fbd54SBarry Smith                                         MatGetRowIJ_SeqAIJ,
32323b2fbd54SBarry Smith                                         MatRestoreRowIJ_SeqAIJ,
32333b2fbd54SBarry Smith                                         MatGetColumnIJ_SeqAIJ,
3234a93ec695SBarry Smith                                         MatRestoreColumnIJ_SeqAIJ,
323593dfae19SHong Zhang                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3236b9617806SBarry Smith                                         0,
32370513a670SBarry Smith                                         0,
3238cda55fadSBarry Smith                                         MatPermute_SeqAIJ,
3239cda55fadSBarry Smith                                         0,
3240d519adbfSMatthew Knepley                                 /* 59*/ 0,
3241b9b97703SBarry Smith                                         MatDestroy_SeqAIJ,
3242b9b97703SBarry Smith                                         MatView_SeqAIJ,
3243357abbc8SBarry Smith                                         0,
3244321b30b9SSatish Balay                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3245321b30b9SSatish Balay                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3246321b30b9SSatish Balay                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3247ee4f033dSBarry Smith                                         0,
3248ee4f033dSBarry Smith                                         0,
3249ee4f033dSBarry Smith                                         0,
3250d519adbfSMatthew Knepley                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3251c87e5d42SMatthew Knepley                                         MatGetRowMinAbs_SeqAIJ,
3252ee4f033dSBarry Smith                                         0,
3253ee4f033dSBarry Smith                                         MatSetColoring_SeqAIJ,
3254dcf5cc72SBarry Smith                                         0,
3255d519adbfSMatthew Knepley                                 /* 74*/ MatSetValuesAdifor_SeqAIJ,
32563acb8795SBarry Smith                                         MatFDColoringApply_AIJ,
325797304618SKris Buschelman                                         0,
325897304618SKris Buschelman                                         0,
325997304618SKris Buschelman                                         0,
32606ce1633cSBarry Smith                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
326197304618SKris Buschelman                                         0,
326297304618SKris Buschelman                                         0,
326397304618SKris Buschelman                                         0,
3264bc011b1eSHong Zhang                                         MatLoad_SeqAIJ,
3265d519adbfSMatthew Knepley                                 /* 84*/ MatIsSymmetric_SeqAIJ,
32661cbb95d3SBarry Smith                                         MatIsHermitian_SeqAIJ,
32676284ec50SHong Zhang                                         0,
32686284ec50SHong Zhang                                         0,
3269bc011b1eSHong Zhang                                         0,
3270d519adbfSMatthew Knepley                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
327126be0446SHong Zhang                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
327226be0446SHong Zhang                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
327365e8a0caSHong Zhang                                         MatPtAP_SeqAIJ_SeqAIJ,
32744a1b09b7SHong Zhang                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_DenseAxpy,
327565e8a0caSHong Zhang                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
32766fc122caSHong Zhang                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
32776fc122caSHong Zhang                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
32786fc122caSHong Zhang                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
32792121bac1SHong Zhang                                         0,
32802121bac1SHong Zhang                                 /* 99*/ 0,
3281609c6c4dSKris Buschelman                                         0,
3282609c6c4dSKris Buschelman                                         0,
328387d4246cSBarry Smith                                         MatConjugate_SeqAIJ,
328487d4246cSBarry Smith                                         0,
3285d519adbfSMatthew Knepley                                 /*104*/ MatSetValuesRow_SeqAIJ,
328699cafbc1SBarry Smith                                         MatRealPart_SeqAIJ,
3287f5edf698SHong Zhang                                         MatImaginaryPart_SeqAIJ,
3288f5edf698SHong Zhang                                         0,
32892bebee5dSHong Zhang                                         0,
3290cbd44569SHong Zhang                                 /*109*/ MatMatSolve_SeqAIJ,
3291985db425SBarry Smith                                         0,
32922af78befSBarry Smith                                         MatGetRowMin_SeqAIJ,
32932af78befSBarry Smith                                         0,
3294599ef60dSHong Zhang                                         MatMissingDiagonal_SeqAIJ,
3295d519adbfSMatthew Knepley                                 /*114*/ 0,
3296599ef60dSHong Zhang                                         0,
32973c2a7987SHong Zhang                                         0,
3298fe97e370SBarry Smith                                         0,
3299fbdbba38SShri Abhyankar                                         0,
3300fbdbba38SShri Abhyankar                                 /*119*/ 0,
3301fbdbba38SShri Abhyankar                                         0,
3302fbdbba38SShri Abhyankar                                         0,
330382d44351SHong Zhang                                         0,
3304b3a44c85SBarry Smith                                         MatGetMultiProcBlock_SeqAIJ,
33050716a85fSBarry Smith                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3306bbead8a2SBarry Smith                                         MatGetColumnNorms_SeqAIJ,
330737868618SMatthew G Knepley                                         MatInvertBlockDiagonal_SeqAIJ,
330837868618SMatthew G Knepley                                         0,
330937868618SMatthew G Knepley                                         0,
33105df89d91SHong Zhang                                 /*129*/ 0,
331175648e8dSHong Zhang                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
331275648e8dSHong Zhang                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
331375648e8dSHong Zhang                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3314b9af6bddSHong Zhang                                         MatTransposeColoringCreate_SeqAIJ,
3315b9af6bddSHong Zhang                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
33162b8ad9a3SHong Zhang                                         MatTransColoringApplyDenToSp_SeqAIJ,
33172b8ad9a3SHong Zhang                                         MatRARt_SeqAIJ_SeqAIJ,
33182b8ad9a3SHong Zhang                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
33193964eb88SJed Brown                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
33203964eb88SJed Brown                                  /*139*/0,
3321f9426fe0SMark Adams                                         0,
33221919a2e2SJed Brown                                         0,
33233a062f41SBarry Smith                                         MatFDColoringSetUp_SeqXAIJ,
33243a062f41SBarry Smith                                         MatFindOffBlockDiagonalEntries_SeqAIJ
33259e29f15eSvictorle };
332617ab2063SBarry Smith 
33274a2ae208SSatish Balay #undef __FUNCT__
33284a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
33297087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3330bef8e0ddSBarry Smith {
3331bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
333297f1f81fSBarry Smith   PetscInt   i,nz,n;
3333bef8e0ddSBarry Smith 
3334bef8e0ddSBarry Smith   PetscFunctionBegin;
3335bef8e0ddSBarry Smith   nz = aij->maxnz;
3336d0f46423SBarry Smith   n  = mat->rmap->n;
3337bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3338bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3339bef8e0ddSBarry Smith   }
3340bef8e0ddSBarry Smith   aij->nz = nz;
3341bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3342bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3343bef8e0ddSBarry Smith   }
3344bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3345bef8e0ddSBarry Smith }
3346bef8e0ddSBarry Smith 
33474a2ae208SSatish Balay #undef __FUNCT__
33484a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3349bef8e0ddSBarry Smith /*@
3350bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3351bef8e0ddSBarry Smith        in the matrix.
3352bef8e0ddSBarry Smith 
3353bef8e0ddSBarry Smith   Input Parameters:
3354bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3355bef8e0ddSBarry Smith -  indices - the column indices
3356bef8e0ddSBarry Smith 
335715091d37SBarry Smith   Level: advanced
335815091d37SBarry Smith 
3359bef8e0ddSBarry Smith   Notes:
3360bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3361bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3362bef8e0ddSBarry Smith   of the MatSetValues() operation.
3363bef8e0ddSBarry Smith 
3364bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3365d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3366bef8e0ddSBarry Smith 
3367bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3368bef8e0ddSBarry Smith 
3369b9617806SBarry Smith     The indices should start with zero, not one.
3370b9617806SBarry Smith 
3371bef8e0ddSBarry Smith @*/
33727087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3373bef8e0ddSBarry Smith {
33744ac538c5SBarry Smith   PetscErrorCode ierr;
3375bef8e0ddSBarry Smith 
3376bef8e0ddSBarry Smith   PetscFunctionBegin;
33770700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
33784482741eSBarry Smith   PetscValidPointer(indices,2);
33794ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3380bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3381bef8e0ddSBarry Smith }
3382bef8e0ddSBarry Smith 
3383be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3384be6bf707SBarry Smith 
33854a2ae208SSatish Balay #undef __FUNCT__
33864a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
33877087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3388be6bf707SBarry Smith {
3389be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
33906849ba73SBarry Smith   PetscErrorCode ierr;
3391d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3392be6bf707SBarry Smith 
3393be6bf707SBarry Smith   PetscFunctionBegin;
3394169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3395be6bf707SBarry Smith 
3396be6bf707SBarry Smith   /* allocate space for values if not already there */
3397be6bf707SBarry Smith   if (!aij->saved_values) {
3398785e854fSJed Brown     ierr = PetscMalloc1((nz+1),&aij->saved_values);CHKERRQ(ierr);
33993bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3400be6bf707SBarry Smith   }
3401be6bf707SBarry Smith 
3402be6bf707SBarry Smith   /* copy values over */
340387828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3404be6bf707SBarry Smith   PetscFunctionReturn(0);
3405be6bf707SBarry Smith }
3406be6bf707SBarry Smith 
34074a2ae208SSatish Balay #undef __FUNCT__
3408b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3409be6bf707SBarry Smith /*@
3410be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3411be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3412be6bf707SBarry Smith        nonlinear portion.
3413be6bf707SBarry Smith 
3414be6bf707SBarry Smith    Collect on Mat
3415be6bf707SBarry Smith 
3416be6bf707SBarry Smith   Input Parameters:
34170e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3418be6bf707SBarry Smith 
341915091d37SBarry Smith   Level: advanced
342015091d37SBarry Smith 
3421be6bf707SBarry Smith   Common Usage, with SNESSolve():
3422be6bf707SBarry Smith $    Create Jacobian matrix
3423be6bf707SBarry Smith $    Set linear terms into matrix
3424be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3425be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3426be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3427512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3428be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3429be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3430be6bf707SBarry Smith $    In your Jacobian routine
3431be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3432be6bf707SBarry Smith $      Set nonlinear terms in matrix
3433be6bf707SBarry Smith 
3434be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3435be6bf707SBarry Smith $    // build linear portion of Jacobian
3436512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3437be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3438be6bf707SBarry Smith $    loop over nonlinear iterations
3439be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3440be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3441be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3442be6bf707SBarry Smith $       Solve linear system with Jacobian
3443be6bf707SBarry Smith $    endloop
3444be6bf707SBarry Smith 
3445be6bf707SBarry Smith   Notes:
3446be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3447512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3448be6bf707SBarry Smith     calling this routine.
3449be6bf707SBarry Smith 
34500c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
34510c468ba9SBarry Smith     and does not allocated additional space.
34520c468ba9SBarry Smith 
3453be6bf707SBarry Smith .seealso: MatRetrieveValues()
3454be6bf707SBarry Smith 
3455be6bf707SBarry Smith @*/
34567087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3457be6bf707SBarry Smith {
34584ac538c5SBarry Smith   PetscErrorCode ierr;
3459be6bf707SBarry Smith 
3460be6bf707SBarry Smith   PetscFunctionBegin;
34610700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3462e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3463e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
34644ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3465be6bf707SBarry Smith   PetscFunctionReturn(0);
3466be6bf707SBarry Smith }
3467be6bf707SBarry Smith 
34684a2ae208SSatish Balay #undef __FUNCT__
34694a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
34707087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3471be6bf707SBarry Smith {
3472be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
34736849ba73SBarry Smith   PetscErrorCode ierr;
3474d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3475be6bf707SBarry Smith 
3476be6bf707SBarry Smith   PetscFunctionBegin;
3477169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3478f23aa3ddSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3479be6bf707SBarry Smith   /* copy values over */
348087828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3481be6bf707SBarry Smith   PetscFunctionReturn(0);
3482be6bf707SBarry Smith }
3483be6bf707SBarry Smith 
34844a2ae208SSatish Balay #undef __FUNCT__
34854a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3486be6bf707SBarry Smith /*@
3487be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3488be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3489be6bf707SBarry Smith        nonlinear portion.
3490be6bf707SBarry Smith 
3491be6bf707SBarry Smith    Collect on Mat
3492be6bf707SBarry Smith 
3493be6bf707SBarry Smith   Input Parameters:
3494be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3495be6bf707SBarry Smith 
349615091d37SBarry Smith   Level: advanced
349715091d37SBarry Smith 
3498be6bf707SBarry Smith .seealso: MatStoreValues()
3499be6bf707SBarry Smith 
3500be6bf707SBarry Smith @*/
35017087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3502be6bf707SBarry Smith {
35034ac538c5SBarry Smith   PetscErrorCode ierr;
3504be6bf707SBarry Smith 
3505be6bf707SBarry Smith   PetscFunctionBegin;
35060700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3507e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3508e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
35094ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3510be6bf707SBarry Smith   PetscFunctionReturn(0);
3511be6bf707SBarry Smith }
3512be6bf707SBarry Smith 
3513f83d6046SBarry Smith 
3514be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
35154a2ae208SSatish Balay #undef __FUNCT__
35164a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
351717ab2063SBarry Smith /*@C
3518682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
35190d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
35206e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
352151c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
35222bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
352317ab2063SBarry Smith 
3524db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3525db81eaa0SLois Curfman McInnes 
352617ab2063SBarry Smith    Input Parameters:
3527db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
352817ab2063SBarry Smith .  m - number of rows
352917ab2063SBarry Smith .  n - number of columns
353017ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
353151c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
35320298fd71SBarry Smith          (possibly different for each row) or NULL
353317ab2063SBarry Smith 
353417ab2063SBarry Smith    Output Parameter:
3535416022c9SBarry Smith .  A - the matrix
353617ab2063SBarry Smith 
3537175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3538ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3539175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3540175b88e8SBarry Smith 
3541b259b22eSLois Curfman McInnes    Notes:
354249a6f317SBarry Smith    If nnz is given then nz is ignored
354349a6f317SBarry Smith 
354417ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
354517ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
35460002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
354744cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
354817ab2063SBarry Smith 
354917ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
35500298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
35513d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
35526da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
355317ab2063SBarry Smith 
3554682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
35554fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3556682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
35576c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
35586c7ebb05SLois Curfman McInnes 
35596c7ebb05SLois Curfman McInnes    Options Database Keys:
3560698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
35619db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
356217ab2063SBarry Smith 
3563027ccd11SLois Curfman McInnes    Level: intermediate
3564027ccd11SLois Curfman McInnes 
356569b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
356636db0b34SBarry Smith 
356717ab2063SBarry Smith @*/
35687087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
356917ab2063SBarry Smith {
3570dfbe8321SBarry Smith   PetscErrorCode ierr;
35716945ee14SBarry Smith 
35723a40ed3dSBarry Smith   PetscFunctionBegin;
3573f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3574117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3575c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3576d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3577273d9f13SBarry Smith   PetscFunctionReturn(0);
3578273d9f13SBarry Smith }
3579273d9f13SBarry Smith 
35804a2ae208SSatish Balay #undef __FUNCT__
35814a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3582273d9f13SBarry Smith /*@C
3583273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3584273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3585273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3586273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3587273d9f13SBarry Smith 
3588273d9f13SBarry Smith    Collective on MPI_Comm
3589273d9f13SBarry Smith 
3590273d9f13SBarry Smith    Input Parameters:
3591117016b1SBarry Smith +  B - The matrix-free
3592273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3593273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
35940298fd71SBarry Smith          (possibly different for each row) or NULL
3595273d9f13SBarry Smith 
3596273d9f13SBarry Smith    Notes:
359749a6f317SBarry Smith      If nnz is given then nz is ignored
359849a6f317SBarry Smith 
3599273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3600273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3601273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3602273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3603273d9f13SBarry Smith 
3604273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
36050298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3606273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3607273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3608273d9f13SBarry Smith 
3609aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3610aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3611aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3612aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3613aa95bbe8SBarry Smith 
3614a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3615a96a251dSBarry Smith    entries or columns indices
3616a96a251dSBarry Smith 
3617273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3618273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3619273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3620273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3621273d9f13SBarry Smith 
3622273d9f13SBarry Smith    Options Database Keys:
3623698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3624698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3625273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3626273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3627273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3628273d9f13SBarry Smith 
3629273d9f13SBarry Smith    Level: intermediate
3630273d9f13SBarry Smith 
363169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3632273d9f13SBarry Smith 
3633273d9f13SBarry Smith @*/
36347087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3635273d9f13SBarry Smith {
36364ac538c5SBarry Smith   PetscErrorCode ierr;
3637a23d5eceSKris Buschelman 
3638a23d5eceSKris Buschelman   PetscFunctionBegin;
36396ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
36406ba663aaSJed Brown   PetscValidType(B,1);
36414ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3642a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3643a23d5eceSKris Buschelman }
3644a23d5eceSKris Buschelman 
3645a23d5eceSKris Buschelman #undef __FUNCT__
3646a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
36477087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3648a23d5eceSKris Buschelman {
3649273d9f13SBarry Smith   Mat_SeqAIJ     *b;
36502576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
36516849ba73SBarry Smith   PetscErrorCode ierr;
365297f1f81fSBarry Smith   PetscInt       i;
3653273d9f13SBarry Smith 
3654273d9f13SBarry Smith   PetscFunctionBegin;
36552576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3656a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3657c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3658c461c341SBarry Smith     nz             = 0;
3659c461c341SBarry Smith   }
3660c461c341SBarry Smith 
366126283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
366226283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3663899cda47SBarry Smith 
3664435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
366560e0710aSBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3666b73539f3SBarry Smith   if (nnz) {
3667d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
366860e0710aSBarry 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]);
366960e0710aSBarry 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);
3670b73539f3SBarry Smith     }
3671b73539f3SBarry Smith   }
3672b73539f3SBarry Smith 
3673273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
36742205254eSKarl Rupp 
3675273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3676273d9f13SBarry Smith 
3677ab93d7beSBarry Smith   if (!skipallocation) {
36782ee49352SLisandro Dalcin     if (!b->imax) {
3679dcca6d9dSJed Brown       ierr = PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);CHKERRQ(ierr);
36803bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
36812ee49352SLisandro Dalcin     }
3682273d9f13SBarry Smith     if (!nnz) {
3683435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3684c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3685d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3686d0f46423SBarry Smith       nz = nz*B->rmap->n;
3687273d9f13SBarry Smith     } else {
3688273d9f13SBarry Smith       nz = 0;
3689d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3690273d9f13SBarry Smith     }
3691ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
36922205254eSKarl Rupp     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;
3693ab93d7beSBarry Smith 
3694273d9f13SBarry Smith     /* allocate the matrix space */
36952ee49352SLisandro Dalcin     ierr    = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3696dcca6d9dSJed Brown     ierr    = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr);
36973bb1ff40SBarry Smith     ierr    = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3698bfeeae90SHong Zhang     b->i[0] = 0;
3699d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
37005da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
37015da197adSKris Buschelman     }
3702273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3703e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3704e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3705b31eba2aSShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
3706b31eba2aSShri Abhyankar     ierr = MatZeroEntries_SeqAIJ(B);CHKERRQ(ierr);
3707b31eba2aSShri Abhyankar #endif
3708c461c341SBarry Smith   } else {
3709e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
3710e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
3711c461c341SBarry Smith   }
3712273d9f13SBarry Smith 
3713273d9f13SBarry Smith   b->nz               = 0;
3714273d9f13SBarry Smith   b->maxnz            = nz;
3715273d9f13SBarry Smith   B->info.nz_unneeded = (double)b->maxnz;
37162205254eSKarl Rupp   if (realalloc) {
37172205254eSKarl Rupp     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
37182205254eSKarl Rupp   }
3719273d9f13SBarry Smith   PetscFunctionReturn(0);
3720273d9f13SBarry Smith }
3721273d9f13SBarry Smith 
3722a1661176SMatthew Knepley #undef  __FUNCT__
3723a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
372458d36128SBarry Smith /*@
3725a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3726a1661176SMatthew Knepley 
3727a1661176SMatthew Knepley    Input Parameters:
3728a1661176SMatthew Knepley +  B - the matrix
3729a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3730a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3731a1661176SMatthew Knepley -  v - optional values in the matrix
3732a1661176SMatthew Knepley 
3733a1661176SMatthew Knepley    Level: developer
3734a1661176SMatthew Knepley 
373558d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
373658d36128SBarry Smith 
3737a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3738a1661176SMatthew Knepley 
3739a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3740a1661176SMatthew Knepley @*/
3741a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3742a1661176SMatthew Knepley {
3743a1661176SMatthew Knepley   PetscErrorCode ierr;
3744a1661176SMatthew Knepley 
3745a1661176SMatthew Knepley   PetscFunctionBegin;
37460700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
37476ba663aaSJed Brown   PetscValidType(B,1);
37484ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3749a1661176SMatthew Knepley   PetscFunctionReturn(0);
3750a1661176SMatthew Knepley }
3751a1661176SMatthew Knepley 
3752a1661176SMatthew Knepley #undef  __FUNCT__
3753a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
37547087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3755a1661176SMatthew Knepley {
3756a1661176SMatthew Knepley   PetscInt       i;
3757a1661176SMatthew Knepley   PetscInt       m,n;
3758a1661176SMatthew Knepley   PetscInt       nz;
3759a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3760a1661176SMatthew Knepley   PetscScalar    *values;
3761a1661176SMatthew Knepley   PetscErrorCode ierr;
3762a1661176SMatthew Knepley 
3763a1661176SMatthew Knepley   PetscFunctionBegin;
376465e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3765779a8d59SSatish Balay 
3766779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3767779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3768779a8d59SSatish Balay 
3769779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3770785e854fSJed Brown   ierr = PetscMalloc1((m+1), &nnz);CHKERRQ(ierr);
3771a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3772b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3773a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
377465e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3775a1661176SMatthew Knepley     nnz[i] = nz;
3776a1661176SMatthew Knepley   }
3777a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3778a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3779a1661176SMatthew Knepley 
3780a1661176SMatthew Knepley   if (v) {
3781a1661176SMatthew Knepley     values = (PetscScalar*) v;
3782a1661176SMatthew Knepley   } else {
37831795a4d1SJed Brown     ierr = PetscCalloc1(nz_max, &values);CHKERRQ(ierr);
3784a1661176SMatthew Knepley   }
3785a1661176SMatthew Knepley 
3786a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3787b7940d39SSatish Balay     nz   = Ii[i+1] - Ii[i];
3788b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3789a1661176SMatthew Knepley   }
3790a1661176SMatthew Knepley 
3791a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3792a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3793a1661176SMatthew Knepley 
3794a1661176SMatthew Knepley   if (!v) {
3795a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3796a1661176SMatthew Knepley   }
37977827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3798a1661176SMatthew Knepley   PetscFunctionReturn(0);
3799a1661176SMatthew Knepley }
3800a1661176SMatthew Knepley 
3801c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
380206873bf2SBarry Smith #include <petsc-private/kernels/petscaxpy.h>
3803170fe5c8SBarry Smith 
3804170fe5c8SBarry Smith #undef __FUNCT__
3805170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3806170fe5c8SBarry Smith /*
3807170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3808170fe5c8SBarry Smith 
3809170fe5c8SBarry Smith                n                       p                          p
3810170fe5c8SBarry Smith         (              )       (              )         (                  )
3811170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3812170fe5c8SBarry Smith         (              )       (              )         (                  )
3813170fe5c8SBarry Smith 
3814170fe5c8SBarry Smith */
3815170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3816170fe5c8SBarry Smith {
3817170fe5c8SBarry Smith   PetscErrorCode    ierr;
3818170fe5c8SBarry Smith   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3819170fe5c8SBarry Smith   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3820170fe5c8SBarry Smith   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
38211de00fd4SBarry Smith   PetscInt          i,n,m,q,p;
3822170fe5c8SBarry Smith   const PetscInt    *ii,*idx;
3823170fe5c8SBarry Smith   const PetscScalar *b,*a,*a_q;
3824170fe5c8SBarry Smith   PetscScalar       *c,*c_q;
3825170fe5c8SBarry Smith 
3826170fe5c8SBarry Smith   PetscFunctionBegin;
3827d0f46423SBarry Smith   m    = A->rmap->n;
3828d0f46423SBarry Smith   n    = A->cmap->n;
3829d0f46423SBarry Smith   p    = B->cmap->n;
3830170fe5c8SBarry Smith   a    = sub_a->v;
3831170fe5c8SBarry Smith   b    = sub_b->a;
3832170fe5c8SBarry Smith   c    = sub_c->v;
3833170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3834170fe5c8SBarry Smith 
3835170fe5c8SBarry Smith   ii  = sub_b->i;
3836170fe5c8SBarry Smith   idx = sub_b->j;
3837170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3838170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3839170fe5c8SBarry Smith     while (q-->0) {
3840170fe5c8SBarry Smith       c_q = c + m*(*idx);
3841170fe5c8SBarry Smith       a_q = a + m*i;
3842854c7f52SBarry Smith       PetscKernelAXPY(c_q,*b,a_q,m);
3843170fe5c8SBarry Smith       idx++;
3844170fe5c8SBarry Smith       b++;
3845170fe5c8SBarry Smith     }
3846170fe5c8SBarry Smith   }
3847170fe5c8SBarry Smith   PetscFunctionReturn(0);
3848170fe5c8SBarry Smith }
3849170fe5c8SBarry Smith 
3850170fe5c8SBarry Smith #undef __FUNCT__
3851170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3852170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3853170fe5c8SBarry Smith {
3854170fe5c8SBarry Smith   PetscErrorCode ierr;
3855d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3856170fe5c8SBarry Smith   Mat            Cmat;
3857170fe5c8SBarry Smith 
3858170fe5c8SBarry Smith   PetscFunctionBegin;
385960e0710aSBarry 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);
3860ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
3861170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
386233d57670SJed Brown   ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr);
3863170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
38640298fd71SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
3865d73949e8SHong Zhang 
3866d73949e8SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
38672205254eSKarl Rupp 
3868170fe5c8SBarry Smith   *C = Cmat;
3869170fe5c8SBarry Smith   PetscFunctionReturn(0);
3870170fe5c8SBarry Smith }
3871170fe5c8SBarry Smith 
3872170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3873170fe5c8SBarry Smith #undef __FUNCT__
3874170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3875170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3876170fe5c8SBarry Smith {
3877170fe5c8SBarry Smith   PetscErrorCode ierr;
3878170fe5c8SBarry Smith 
3879170fe5c8SBarry Smith   PetscFunctionBegin;
3880170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
38813ff4c91cSHong Zhang     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3882170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
38833ff4c91cSHong Zhang     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
3884170fe5c8SBarry Smith   }
38853ff4c91cSHong Zhang   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3886170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
38873ff4c91cSHong Zhang   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
3888170fe5c8SBarry Smith   PetscFunctionReturn(0);
3889170fe5c8SBarry Smith }
3890170fe5c8SBarry Smith 
3891170fe5c8SBarry Smith 
38920bad9183SKris Buschelman /*MC
3893fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
38940bad9183SKris Buschelman    based on compressed sparse row format.
38950bad9183SKris Buschelman 
38960bad9183SKris Buschelman    Options Database Keys:
38970bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
38980bad9183SKris Buschelman 
38990bad9183SKris Buschelman   Level: beginner
39000bad9183SKris Buschelman 
3901f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
39020bad9183SKris Buschelman M*/
39030bad9183SKris Buschelman 
3904ccd284c7SBarry Smith /*MC
3905ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
3906ccd284c7SBarry Smith 
3907ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
3908ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
3909ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
3910ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3911ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3912ccd284c7SBarry Smith 
3913ccd284c7SBarry Smith    Options Database Keys:
3914ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
3915ccd284c7SBarry Smith 
3916ccd284c7SBarry Smith   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
3917ccd284c7SBarry Smith    enough exist.
3918ccd284c7SBarry Smith 
3919ccd284c7SBarry Smith   Level: beginner
3920ccd284c7SBarry Smith 
3921ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3922ccd284c7SBarry Smith M*/
3923ccd284c7SBarry Smith 
3924ccd284c7SBarry Smith /*MC
3925ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
3926ccd284c7SBarry Smith 
3927ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3928ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3929ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3930ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3931ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3932ccd284c7SBarry Smith 
3933ccd284c7SBarry Smith    Options Database Keys:
3934ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3935ccd284c7SBarry Smith 
3936ccd284c7SBarry Smith   Level: beginner
3937ccd284c7SBarry Smith 
3938ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3939ccd284c7SBarry Smith M*/
3940ccd284c7SBarry Smith 
3941b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
39428cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3943b5e56a35SBarry Smith #endif
3944ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
39458cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat*);
3946af1023dbSSatish Balay #endif
39478cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
39488cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
39498cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
39507087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool*);
3951611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
39528cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3953611f576cSBarry Smith #endif
3954611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
39558cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3956611f576cSBarry Smith #endif
3957f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
39588cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3959f3c0ef26SHong Zhang #endif
3960913c410bSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
39618cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3962eb3b5408SSatish Balay #endif
3963913c410bSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
39648cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3965586621ddSJed Brown #endif
39667087e0deSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
396769e15a41SShri Abhyankar PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_klu(Mat,MatFactorType,Mat*);
396869e15a41SShri Abhyankar #endif
3969719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
39708cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3971719d5645SBarry Smith #endif
3972b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
39738cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
39747087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
39757087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3976b3866ffcSBarry Smith #endif
397717f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
39788cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
397917f1a0eaSHong Zhang #endif
398017667f90SBarry Smith 
3981c0c8ee5eSDmitry Karpeev 
39828c778c55SBarry Smith #undef __FUNCT__
39838c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJGetArray"
39848c778c55SBarry Smith /*@C
39858c778c55SBarry Smith    MatSeqAIJGetArray - gives access to the array where the data for a SeqSeqAIJ matrix is stored
39868c778c55SBarry Smith 
39878c778c55SBarry Smith    Not Collective
39888c778c55SBarry Smith 
39898c778c55SBarry Smith    Input Parameter:
39908c778c55SBarry Smith .  mat - a MATSEQDENSE matrix
39918c778c55SBarry Smith 
39928c778c55SBarry Smith    Output Parameter:
39938c778c55SBarry Smith .   array - pointer to the data
39948c778c55SBarry Smith 
39958c778c55SBarry Smith    Level: intermediate
39968c778c55SBarry Smith 
3997774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
39988c778c55SBarry Smith @*/
39998c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
40008c778c55SBarry Smith {
40018c778c55SBarry Smith   PetscErrorCode ierr;
40028c778c55SBarry Smith 
40038c778c55SBarry Smith   PetscFunctionBegin;
40048c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
40058c778c55SBarry Smith   PetscFunctionReturn(0);
40068c778c55SBarry Smith }
40078c778c55SBarry Smith 
40088c778c55SBarry Smith #undef __FUNCT__
40098c778c55SBarry Smith #define __FUNCT__ "MatSeqAIJRestoreArray"
40108c778c55SBarry Smith /*@C
40118c778c55SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a SeqSeqAIJ matrix is stored obtained by MatSeqAIJGetArray()
40128c778c55SBarry Smith 
40138c778c55SBarry Smith    Not Collective
40148c778c55SBarry Smith 
40158c778c55SBarry Smith    Input Parameters:
40168c778c55SBarry Smith .  mat - a MATSEQDENSE matrix
40178c778c55SBarry Smith .  array - pointer to the data
40188c778c55SBarry Smith 
40198c778c55SBarry Smith    Level: intermediate
40208c778c55SBarry Smith 
4021774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
40228c778c55SBarry Smith @*/
40238c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
40248c778c55SBarry Smith {
40258c778c55SBarry Smith   PetscErrorCode ierr;
40268c778c55SBarry Smith 
40278c778c55SBarry Smith   PetscFunctionBegin;
40288c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
40298c778c55SBarry Smith   PetscFunctionReturn(0);
40308c778c55SBarry Smith }
40318c778c55SBarry Smith 
40324a2ae208SSatish Balay #undef __FUNCT__
40334a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
40348cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4035273d9f13SBarry Smith {
4036273d9f13SBarry Smith   Mat_SeqAIJ     *b;
4037dfbe8321SBarry Smith   PetscErrorCode ierr;
403838baddfdSBarry Smith   PetscMPIInt    size;
4039273d9f13SBarry Smith 
4040273d9f13SBarry Smith   PetscFunctionBegin;
4041ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
4042e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
4043273d9f13SBarry Smith 
4044b00a9115SJed Brown   ierr = PetscNewLog(B,&b);CHKERRQ(ierr);
40452205254eSKarl Rupp 
4046b0a32e0cSBarry Smith   B->data = (void*)b;
40472205254eSKarl Rupp 
4048549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
40492205254eSKarl Rupp 
4050416022c9SBarry Smith   b->row                = 0;
4051416022c9SBarry Smith   b->col                = 0;
405282bf6240SBarry Smith   b->icol               = 0;
4053b810aeb4SBarry Smith   b->reallocs           = 0;
405436db0b34SBarry Smith   b->ignorezeroentries  = PETSC_FALSE;
4055f1e2ffcdSBarry Smith   b->roworiented        = PETSC_TRUE;
4056416022c9SBarry Smith   b->nonew              = 0;
4057416022c9SBarry Smith   b->diag               = 0;
4058416022c9SBarry Smith   b->solve_work         = 0;
40592a1b7f2aSHong Zhang   B->spptr              = 0;
4060be6bf707SBarry Smith   b->saved_values       = 0;
4061d7f994e1SBarry Smith   b->idiag              = 0;
406271f1c65dSBarry Smith   b->mdiag              = 0;
406371f1c65dSBarry Smith   b->ssor_work          = 0;
406471f1c65dSBarry Smith   b->omega              = 1.0;
406571f1c65dSBarry Smith   b->fshift             = 0.0;
406671f1c65dSBarry Smith   b->idiagvalid         = PETSC_FALSE;
4067bbead8a2SBarry Smith   b->ibdiagvalid        = PETSC_FALSE;
4068a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
4069a30b2313SHong Zhang   b->xtoy               = 0;
4070a30b2313SHong Zhang   b->XtoY               = 0;
407117ab2063SBarry Smith 
407235d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
4073bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
4074bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
40758c778c55SBarry Smith 
4076b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4077bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_matlab_C",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
4078bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
4079bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
4080b3866ffcSBarry Smith #endif
4081b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
4082bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
4083b5e56a35SBarry Smith #endif
4084ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
4085bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_essl_C",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
4086719d5645SBarry Smith #endif
4087611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
4088bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_C",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
4089611f576cSBarry Smith #endif
4090f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
4091bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_dist_C",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
4092f3c0ef26SHong Zhang #endif
4093611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
4094bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_aij_mumps);CHKERRQ(ierr);
4095611f576cSBarry Smith #endif
4096913c410bSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
4097bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_umfpack_C",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
4098eb3b5408SSatish Balay #endif
4099913c410bSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
4100bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_cholmod_C",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
4101586621ddSJed Brown #endif
41027087e0deSShri Abhyankar #if defined(PETSC_HAVE_SUITESPARSE)
410369e15a41SShri Abhyankar   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_klu_C",MatGetFactor_seqaij_klu);CHKERRQ(ierr);
410469e15a41SShri Abhyankar #endif
4105719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
4106bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_lusol_C",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
4107719d5645SBarry Smith #endif
410817f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
4109bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_clique_C",MatGetFactor_aij_clique);CHKERRQ(ierr);
411017f1a0eaSHong Zhang #endif
411117f1a0eaSHong Zhang 
4112bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_petsc_C",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
4113bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactorAvailable_petsc_C",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
4114bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_bas_C",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
4115bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
4116bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
4117bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
4118bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
4119bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
4120bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
4121bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
4122bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4123bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4124bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
4125bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
4126bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
4127bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
4128bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
4129bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
41304108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
413117667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
41323a40ed3dSBarry Smith   PetscFunctionReturn(0);
413317ab2063SBarry Smith }
413417ab2063SBarry Smith 
41354a2ae208SSatish Balay #undef __FUNCT__
4136b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
4137b24902e0SBarry Smith /*
4138b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4139b24902e0SBarry Smith */
4140ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
414117ab2063SBarry Smith {
4142416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
41436849ba73SBarry Smith   PetscErrorCode ierr;
4144d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
414517ab2063SBarry Smith 
41463a40ed3dSBarry Smith   PetscFunctionBegin;
4147273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4148273d9f13SBarry Smith 
4149d5f3da31SBarry Smith   C->factortype = A->factortype;
4150416022c9SBarry Smith   c->row        = 0;
4151416022c9SBarry Smith   c->col        = 0;
415282bf6240SBarry Smith   c->icol       = 0;
41536ad4291fSHong Zhang   c->reallocs   = 0;
415417ab2063SBarry Smith 
41556ad4291fSHong Zhang   C->assembled = PETSC_TRUE;
415617ab2063SBarry Smith 
4157aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4158aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4159eec197d1SBarry Smith 
4160dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&c->imax,m,&c->ilen);CHKERRQ(ierr);
41613bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
416217ab2063SBarry Smith   for (i=0; i<m; i++) {
4163416022c9SBarry Smith     c->imax[i] = a->imax[i];
4164416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
416517ab2063SBarry Smith   }
416617ab2063SBarry Smith 
416717ab2063SBarry Smith   /* allocate the matrix space */
4168f77e22a1SHong Zhang   if (mallocmatspace) {
4169dcca6d9dSJed Brown     ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr);
41703bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
41712205254eSKarl Rupp 
4172f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
41732205254eSKarl Rupp 
417497f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
417517ab2063SBarry Smith     if (m > 0) {
417697f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4177be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4178bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4179be6bf707SBarry Smith       } else {
4180bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
418117ab2063SBarry Smith       }
418208480c60SBarry Smith     }
4183f77e22a1SHong Zhang   }
418417ab2063SBarry Smith 
41856ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4186416022c9SBarry Smith   c->roworiented       = a->roworiented;
4187416022c9SBarry Smith   c->nonew             = a->nonew;
4188416022c9SBarry Smith   if (a->diag) {
4189785e854fSJed Brown     ierr = PetscMalloc1((m+1),&c->diag);CHKERRQ(ierr);
41903bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
419117ab2063SBarry Smith     for (i=0; i<m; i++) {
4192416022c9SBarry Smith       c->diag[i] = a->diag[i];
419317ab2063SBarry Smith     }
41943a40ed3dSBarry Smith   } else c->diag = 0;
41952205254eSKarl Rupp 
41966ad4291fSHong Zhang   c->solve_work         = 0;
41976ad4291fSHong Zhang   c->saved_values       = 0;
41986ad4291fSHong Zhang   c->idiag              = 0;
419971f1c65dSBarry Smith   c->ssor_work          = 0;
4200a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
4201e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
4202e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
42036ad4291fSHong Zhang   c->xtoy               = 0;
42046ad4291fSHong Zhang   c->XtoY               = 0;
42056ad4291fSHong Zhang 
4206893ad86cSHong Zhang   c->rmax         = a->rmax;
4207416022c9SBarry Smith   c->nz           = a->nz;
42088ed568f8SMatthew G Knepley   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4209273d9f13SBarry Smith   C->preallocated = PETSC_TRUE;
4210754ec7b1SSatish Balay 
42116ad4291fSHong Zhang   c->compressedrow.use   = a->compressedrow.use;
42126ad4291fSHong Zhang   c->compressedrow.nrows = a->compressedrow.nrows;
4213cd6b891eSBarry Smith   if (a->compressedrow.use) {
42146ad4291fSHong Zhang     i    = a->compressedrow.nrows;
4215dcca6d9dSJed Brown     ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr);
42166ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
42176ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
421827ea64f8SHong Zhang   } else {
421927ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
42200298fd71SBarry Smith     c->compressedrow.i      = NULL;
42210298fd71SBarry Smith     c->compressedrow.rindex = NULL;
42226ad4291fSHong Zhang   }
4223e56f5c9eSBarry Smith   C->nonzerostate = A->nonzerostate;
42244846f1f5SKris Buschelman 
42252205254eSKarl Rupp   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4226140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
42273a40ed3dSBarry Smith   PetscFunctionReturn(0);
422817ab2063SBarry Smith }
422917ab2063SBarry Smith 
42304a2ae208SSatish Balay #undef __FUNCT__
4231b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
4232b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4233b24902e0SBarry Smith {
4234b24902e0SBarry Smith   PetscErrorCode ierr;
4235b24902e0SBarry Smith 
4236b24902e0SBarry Smith   PetscFunctionBegin;
4237ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
42384b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4239cfd3f464SBarry Smith   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
424033d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
4241cfd3f464SBarry Smith   }
4242a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4243f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4244b24902e0SBarry Smith   PetscFunctionReturn(0);
4245b24902e0SBarry Smith }
4246b24902e0SBarry Smith 
4247b24902e0SBarry Smith #undef __FUNCT__
42484a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
4249112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4250fbdbba38SShri Abhyankar {
4251fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4252fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4253fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4254fbdbba38SShri Abhyankar   int            fd;
4255fbdbba38SShri Abhyankar   PetscMPIInt    size;
4256fbdbba38SShri Abhyankar   MPI_Comm       comm;
4257bbead8a2SBarry Smith   PetscInt       bs = 1;
4258fbdbba38SShri Abhyankar 
4259fbdbba38SShri Abhyankar   PetscFunctionBegin;
4260fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4261fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4262fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4263bbead8a2SBarry Smith 
42640298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
42650298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
4266bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
42671814747fSJed Brown   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
4268bbead8a2SBarry Smith 
4269fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4270fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4271fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4272fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4273fbdbba38SShri Abhyankar 
4274bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4275fbdbba38SShri Abhyankar 
4276fbdbba38SShri Abhyankar   /* read in row lengths */
4277785e854fSJed Brown   ierr = PetscMalloc1(M,&rowlengths);CHKERRQ(ierr);
4278fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4279fbdbba38SShri Abhyankar 
4280fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4281fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
428260e0710aSBarry 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);
4283fbdbba38SShri Abhyankar 
4284fbdbba38SShri Abhyankar   /* set global size if not set already*/
4285f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4286fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4287aabbc4fbSShri Abhyankar   } else {
4288fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
4289fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
42904c5b953cSHong Zhang     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
42914c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
42924c5b953cSHong Zhang     }
429360e0710aSBarry 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);
4294aabbc4fbSShri Abhyankar   }
4295fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4296fbdbba38SShri Abhyankar   a    = (Mat_SeqAIJ*)newMat->data;
4297fbdbba38SShri Abhyankar 
4298fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4299fbdbba38SShri Abhyankar 
4300fbdbba38SShri Abhyankar   /* read in nonzero values */
4301fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4302fbdbba38SShri Abhyankar 
4303fbdbba38SShri Abhyankar   /* set matrix "i" values */
4304fbdbba38SShri Abhyankar   a->i[0] = 0;
4305fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4306fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4307fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4308fbdbba38SShri Abhyankar   }
4309fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4310fbdbba38SShri Abhyankar 
4311fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4312fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4313fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4314fbdbba38SShri Abhyankar }
4315fbdbba38SShri Abhyankar 
4316fbdbba38SShri Abhyankar #undef __FUNCT__
4317b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4318ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
43197264ac53SSatish Balay {
43207264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4321dfbe8321SBarry Smith   PetscErrorCode ierr;
4322eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4323eeffb40dSHong Zhang   PetscInt k;
4324eeffb40dSHong Zhang #endif
43257264ac53SSatish Balay 
43263a40ed3dSBarry Smith   PetscFunctionBegin;
4327bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4328d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4329ca44d042SBarry Smith     *flg = PETSC_FALSE;
4330ca44d042SBarry Smith     PetscFunctionReturn(0);
4331bcd2baecSBarry Smith   }
43327264ac53SSatish Balay 
43337264ac53SSatish Balay   /* if the a->i are the same */
4334d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4335abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
43367264ac53SSatish Balay 
43377264ac53SSatish Balay   /* if a->j are the same */
433897f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4339abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4340bcd2baecSBarry Smith 
4341bcd2baecSBarry Smith   /* if a->a are the same */
4342eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4343eeffb40dSHong Zhang   for (k=0; k<a->nz; k++) {
4344eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4345eeffb40dSHong Zhang       *flg = PETSC_FALSE;
43463a40ed3dSBarry Smith       PetscFunctionReturn(0);
4347eeffb40dSHong Zhang     }
4348eeffb40dSHong Zhang   }
4349eeffb40dSHong Zhang #else
4350eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4351eeffb40dSHong Zhang #endif
4352eeffb40dSHong Zhang   PetscFunctionReturn(0);
43537264ac53SSatish Balay }
435436db0b34SBarry Smith 
43554a2ae208SSatish Balay #undef __FUNCT__
43564a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
435705869f15SSatish Balay /*@
435836db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
435936db0b34SBarry Smith               provided by the user.
436036db0b34SBarry Smith 
4361c75a6043SHong Zhang       Collective on MPI_Comm
436236db0b34SBarry Smith 
436336db0b34SBarry Smith    Input Parameters:
436436db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
436536db0b34SBarry Smith .   m - number of rows
436636db0b34SBarry Smith .   n - number of columns
436736db0b34SBarry Smith .   i - row indices
436836db0b34SBarry Smith .   j - column indices
436936db0b34SBarry Smith -   a - matrix values
437036db0b34SBarry Smith 
437136db0b34SBarry Smith    Output Parameter:
437236db0b34SBarry Smith .   mat - the matrix
437336db0b34SBarry Smith 
437436db0b34SBarry Smith    Level: intermediate
437536db0b34SBarry Smith 
437636db0b34SBarry Smith    Notes:
43770551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4378292fb18eSBarry Smith     once the matrix is destroyed and not before
437936db0b34SBarry Smith 
438036db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
438136db0b34SBarry Smith 
4382bfeeae90SHong Zhang        The i and j indices are 0 based
438336db0b34SBarry Smith 
4384a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4385a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
4386a4552177SSatish Balay     as shown:
4387a4552177SSatish Balay 
4388a4552177SSatish Balay         1 0 0
4389a4552177SSatish Balay         2 0 3
4390a4552177SSatish Balay         4 5 6
4391a4552177SSatish Balay 
4392a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
43939985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4394a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
4395a4552177SSatish Balay 
43969985e31cSBarry Smith 
439769b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
439836db0b34SBarry Smith 
439936db0b34SBarry Smith @*/
44007087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
440136db0b34SBarry Smith {
4402dfbe8321SBarry Smith   PetscErrorCode ierr;
4403cbcfb4deSHong Zhang   PetscInt       ii;
440436db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4405cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4406cbcfb4deSHong Zhang   PetscInt jj;
4407cbcfb4deSHong Zhang #endif
440836db0b34SBarry Smith 
440936db0b34SBarry Smith   PetscFunctionBegin;
4410f23aa3ddSBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4411f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4412f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4413a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4414ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4415ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4416ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4417dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&aij->imax,m,&aij->ilen);CHKERRQ(ierr);
4418ab93d7beSBarry Smith 
441936db0b34SBarry Smith   aij->i            = i;
442036db0b34SBarry Smith   aij->j            = j;
442136db0b34SBarry Smith   aij->a            = a;
442236db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
442336db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4424e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4425e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
442636db0b34SBarry Smith 
442736db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
442836db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
44292515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
443060e0710aSBarry 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]);
44319985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4432e32f2f54SBarry 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);
4433e32f2f54SBarry 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);
44349985e31cSBarry Smith     }
443536db0b34SBarry Smith #endif
443636db0b34SBarry Smith   }
44372515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
443836db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
443960e0710aSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
444060e0710aSBarry 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]);
444136db0b34SBarry Smith   }
444236db0b34SBarry Smith #endif
444336db0b34SBarry Smith 
4444b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4445b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
444636db0b34SBarry Smith   PetscFunctionReturn(0);
444736db0b34SBarry Smith }
44488a0b0e6bSVictor Minden #undef __FUNCT__
44498a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
445080ef6e79SMatthew G Knepley /*@C
4451d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
44528a0b0e6bSVictor Minden               provided by the user.
44538a0b0e6bSVictor Minden 
44548a0b0e6bSVictor Minden       Collective on MPI_Comm
44558a0b0e6bSVictor Minden 
44568a0b0e6bSVictor Minden    Input Parameters:
44578a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
44588a0b0e6bSVictor Minden .   m   - number of rows
44598a0b0e6bSVictor Minden .   n   - number of columns
44608a0b0e6bSVictor Minden .   i   - row indices
44618a0b0e6bSVictor Minden .   j   - column indices
44621230e6d1SVictor Minden .   a   - matrix values
44631230e6d1SVictor Minden .   nz  - number of nonzeros
44641230e6d1SVictor Minden -   idx - 0 or 1 based
44658a0b0e6bSVictor Minden 
44668a0b0e6bSVictor Minden    Output Parameter:
44678a0b0e6bSVictor Minden .   mat - the matrix
44688a0b0e6bSVictor Minden 
44698a0b0e6bSVictor Minden    Level: intermediate
44708a0b0e6bSVictor Minden 
44718a0b0e6bSVictor Minden    Notes:
44728a0b0e6bSVictor Minden        The i and j indices are 0 based
44738a0b0e6bSVictor Minden 
44748a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
44758a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
44768a0b0e6bSVictor Minden     as shown:
44778a0b0e6bSVictor Minden 
44788a0b0e6bSVictor Minden         1 0 0
44798a0b0e6bSVictor Minden         2 0 3
44808a0b0e6bSVictor Minden         4 5 6
44818a0b0e6bSVictor Minden 
44828a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
44838a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
44848a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
44858a0b0e6bSVictor Minden 
44868a0b0e6bSVictor Minden 
448769b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
44888a0b0e6bSVictor Minden 
44898a0b0e6bSVictor Minden @*/
44901230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
44918a0b0e6bSVictor Minden {
44928a0b0e6bSVictor Minden   PetscErrorCode ierr;
4493d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
44948a0b0e6bSVictor Minden 
44958a0b0e6bSVictor Minden 
44968a0b0e6bSVictor Minden   PetscFunctionBegin;
44971795a4d1SJed Brown   ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr);
44981230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
4499c8d679ebSHong Zhang     nnz[i[ii] - !!idx] += 1;
45001230e6d1SVictor Minden   }
45018a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
45028a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
45038a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
45041230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
45051230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
45061230e6d1SVictor Minden     if (idx) {
45071230e6d1SVictor Minden       row = i[ii] - 1;
45081230e6d1SVictor Minden       col = j[ii] - 1;
45091230e6d1SVictor Minden     } else {
45101230e6d1SVictor Minden       row = i[ii];
45111230e6d1SVictor Minden       col = j[ii];
45128a0b0e6bSVictor Minden     }
45131230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
45148a0b0e6bSVictor Minden   }
45158a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
45168a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4517d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
45188a0b0e6bSVictor Minden   PetscFunctionReturn(0);
45198a0b0e6bSVictor Minden }
452036db0b34SBarry Smith 
4521cc8ba8e1SBarry Smith #undef __FUNCT__
4522ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4523dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4524cc8ba8e1SBarry Smith {
4525dfbe8321SBarry Smith   PetscErrorCode ierr;
4526cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
452736db0b34SBarry Smith 
4528cc8ba8e1SBarry Smith   PetscFunctionBegin;
45298ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4530cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4531cc8ba8e1SBarry Smith     a->coloring = coloring;
453212c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
453397f1f81fSBarry Smith     PetscInt        i,*larray;
453412c595b3SBarry Smith     ISColoring      ocoloring;
453508b6dcc0SBarry Smith     ISColoringValue *colors;
453612c595b3SBarry Smith 
453712c595b3SBarry Smith     /* set coloring for diagonal portion */
4538785e854fSJed Brown     ierr = PetscMalloc1(A->cmap->n,&larray);CHKERRQ(ierr);
45392205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) larray[i] = i;
45400298fd71SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);CHKERRQ(ierr);
4541785e854fSJed Brown     ierr = PetscMalloc1(A->cmap->n,&colors);CHKERRQ(ierr);
45422205254eSKarl Rupp     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
454312c595b3SBarry Smith     ierr        = PetscFree(larray);CHKERRQ(ierr);
4544d0f46423SBarry Smith     ierr        = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
454512c595b3SBarry Smith     a->coloring = ocoloring;
454612c595b3SBarry Smith   }
4547cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4548cc8ba8e1SBarry Smith }
4549cc8ba8e1SBarry Smith 
4550ee4f033dSBarry Smith #undef __FUNCT__
4551ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
455297f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4553ee4f033dSBarry Smith {
4554ee4f033dSBarry Smith   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
4555d0f46423SBarry Smith   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
455654f21887SBarry Smith   MatScalar       *v      = a->a;
455754f21887SBarry Smith   PetscScalar     *values = (PetscScalar*)advalues;
455808b6dcc0SBarry Smith   ISColoringValue *color;
4559ee4f033dSBarry Smith 
4560ee4f033dSBarry Smith   PetscFunctionBegin;
4561e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4562ee4f033dSBarry Smith   color = a->coloring->colors;
4563ee4f033dSBarry Smith   /* loop over rows */
4564ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4565ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4566ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
45672205254eSKarl Rupp     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
4568ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4569cc8ba8e1SBarry Smith   }
4570cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4571cc8ba8e1SBarry Smith }
457236db0b34SBarry Smith 
4573acf2f550SJed Brown #undef __FUNCT__
4574acf2f550SJed Brown #define __FUNCT__ "MatSeqAIJInvalidateDiagonal"
4575acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4576acf2f550SJed Brown {
4577acf2f550SJed Brown   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4578acf2f550SJed Brown   PetscErrorCode ierr;
4579acf2f550SJed Brown 
4580acf2f550SJed Brown   PetscFunctionBegin;
4581acf2f550SJed Brown   a->idiagvalid  = PETSC_FALSE;
4582acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
45832205254eSKarl Rupp 
4584acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4585acf2f550SJed Brown   PetscFunctionReturn(0);
4586acf2f550SJed Brown }
4587acf2f550SJed Brown 
458881824310SBarry Smith /*
458981824310SBarry Smith     Special version for direct calls from Fortran
459081824310SBarry Smith */
4591b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
459281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
459381824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
459481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
459581824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
459681824310SBarry Smith #endif
459781824310SBarry Smith 
459881824310SBarry Smith /* Change these macros so can be used in void function */
459981824310SBarry Smith #undef CHKERRQ
4600ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
460181824310SBarry Smith #undef SETERRQ2
4602e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
46034994cf47SJed Brown #undef SETERRQ3
46044994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
460581824310SBarry Smith 
460681824310SBarry Smith #undef __FUNCT__
460781824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
46088cc058d9SJed 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)
460981824310SBarry Smith {
461081824310SBarry Smith   Mat            A  = *AA;
461181824310SBarry Smith   PetscInt       m  = *mm, n = *nn;
461281824310SBarry Smith   InsertMode     is = *isis;
461381824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
461481824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
461581824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
461681824310SBarry Smith   PetscErrorCode ierr;
461781824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
461854f21887SBarry Smith   MatScalar      *ap,value,*aa;
4619ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4620ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
462181824310SBarry Smith 
462281824310SBarry Smith   PetscFunctionBegin;
46234994cf47SJed Brown   MatCheckPreallocated(A,1);
462481824310SBarry Smith   imax  = a->imax;
462581824310SBarry Smith   ai    = a->i;
462681824310SBarry Smith   ailen = a->ilen;
462781824310SBarry Smith   aj    = a->j;
462881824310SBarry Smith   aa    = a->a;
462981824310SBarry Smith 
463081824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
463181824310SBarry Smith     row = im[k];
463281824310SBarry Smith     if (row < 0) continue;
463381824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4634ce94432eSBarry Smith     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
463581824310SBarry Smith #endif
463681824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
463781824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
463881824310SBarry Smith     low  = 0;
463981824310SBarry Smith     high = nrow;
464081824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
464181824310SBarry Smith       if (in[l] < 0) continue;
464281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4643ce94432eSBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
464481824310SBarry Smith #endif
464581824310SBarry Smith       col = in[l];
46462205254eSKarl Rupp       if (roworiented) value = v[l + k*n];
46472205254eSKarl Rupp       else value = v[k + l*m];
46482205254eSKarl Rupp 
464981824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
465081824310SBarry Smith 
46512205254eSKarl Rupp       if (col <= lastcol) low = 0;
46522205254eSKarl Rupp       else high = nrow;
465381824310SBarry Smith       lastcol = col;
465481824310SBarry Smith       while (high-low > 5) {
465581824310SBarry Smith         t = (low+high)/2;
465681824310SBarry Smith         if (rp[t] > col) high = t;
465781824310SBarry Smith         else             low  = t;
465881824310SBarry Smith       }
465981824310SBarry Smith       for (i=low; i<high; i++) {
466081824310SBarry Smith         if (rp[i] > col) break;
466181824310SBarry Smith         if (rp[i] == col) {
466281824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
466381824310SBarry Smith           else                  ap[i] = value;
466481824310SBarry Smith           goto noinsert;
466581824310SBarry Smith         }
466681824310SBarry Smith       }
466781824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
466881824310SBarry Smith       if (nonew == 1) goto noinsert;
4669ce94432eSBarry Smith       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4670fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
467181824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
467281824310SBarry Smith       /* shift up all the later entries in this row */
467381824310SBarry Smith       for (ii=N; ii>=i; ii--) {
467481824310SBarry Smith         rp[ii+1] = rp[ii];
467581824310SBarry Smith         ap[ii+1] = ap[ii];
467681824310SBarry Smith       }
467781824310SBarry Smith       rp[i] = col;
467881824310SBarry Smith       ap[i] = value;
4679e56f5c9eSBarry Smith       A->nonzerostate++;
468081824310SBarry Smith noinsert:;
468181824310SBarry Smith       low = i + 1;
468281824310SBarry Smith     }
468381824310SBarry Smith     ailen[row] = nrow;
468481824310SBarry Smith   }
468581824310SBarry Smith   PetscFunctionReturnVoid();
468681824310SBarry Smith }
46879f7953f8SBarry Smith 
468862298a1eSBarry Smith 
4689