xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 91e9d3e276872736fc6788e770582d7a274f8072)
1b377110cSBarry Smith 
2d5d45c9bSBarry Smith /*
33369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
4d5d45c9bSBarry Smith   matrix storage format.
5d5d45c9bSBarry Smith */
63369ce9aSBarry Smith 
77c4f633dSBarry Smith 
8c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
9c6db04a5SJed Brown #include <petscblaslapack.h>
10c6db04a5SJed Brown #include <petscbt.h>
11af0996ceSBarry Smith #include <petsc/private/kernels/blocktranspose.h>
120716a85fSBarry Smith 
134099cc6bSBarry Smith PetscErrorCode MatSeqAIJSetTypeFromOptions(Mat A)
144099cc6bSBarry Smith {
154099cc6bSBarry Smith   PetscErrorCode       ierr;
164099cc6bSBarry Smith   PetscBool            flg;
174099cc6bSBarry Smith   char                 type[256];
184099cc6bSBarry Smith 
194099cc6bSBarry Smith   PetscFunctionBegin;
204099cc6bSBarry Smith   ierr = PetscObjectOptionsBegin((PetscObject)A);
214099cc6bSBarry Smith   ierr = PetscOptionsFList("-mat_seqaij_type","Matrix SeqAIJ type","MatSeqAIJSetType",MatSeqAIJList,"seqaij",type,256,&flg);CHKERRQ(ierr);
224099cc6bSBarry Smith   if (flg) {
234099cc6bSBarry Smith     ierr = MatSeqAIJSetType(A,type);CHKERRQ(ierr);
244099cc6bSBarry Smith   }
254099cc6bSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
264099cc6bSBarry Smith   PetscFunctionReturn(0);
274099cc6bSBarry Smith }
284099cc6bSBarry Smith 
290716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
300716a85fSBarry Smith {
310716a85fSBarry Smith   PetscErrorCode ierr;
320716a85fSBarry Smith   PetscInt       i,m,n;
330716a85fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
340716a85fSBarry Smith 
350716a85fSBarry Smith   PetscFunctionBegin;
360716a85fSBarry Smith   ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr);
370716a85fSBarry Smith   ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr);
380716a85fSBarry Smith   if (type == NORM_2) {
390716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
400716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
410716a85fSBarry Smith     }
420716a85fSBarry Smith   } else if (type == NORM_1) {
430716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
440716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
450716a85fSBarry Smith     }
460716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
470716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
480716a85fSBarry Smith       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
490716a85fSBarry Smith     }
500716a85fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");
510716a85fSBarry Smith 
520716a85fSBarry Smith   if (type == NORM_2) {
538f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
540716a85fSBarry Smith   }
550716a85fSBarry Smith   PetscFunctionReturn(0);
560716a85fSBarry Smith }
570716a85fSBarry Smith 
583a062f41SBarry Smith PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A,IS *is)
593a062f41SBarry Smith {
603a062f41SBarry Smith   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
613a062f41SBarry Smith   PetscInt        i,m=A->rmap->n,cnt = 0, bs = A->rmap->bs;
623a062f41SBarry Smith   const PetscInt  *jj = a->j,*ii = a->i;
633a062f41SBarry Smith   PetscInt        *rows;
643a062f41SBarry Smith   PetscErrorCode  ierr;
653a062f41SBarry Smith 
663a062f41SBarry Smith   PetscFunctionBegin;
673a062f41SBarry Smith   for (i=0; i<m; i++) {
683a062f41SBarry Smith     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
693a062f41SBarry Smith       cnt++;
703a062f41SBarry Smith     }
713a062f41SBarry Smith   }
723a062f41SBarry Smith   ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr);
733a062f41SBarry Smith   cnt  = 0;
743a062f41SBarry Smith   for (i=0; i<m; i++) {
753a062f41SBarry Smith     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
763a062f41SBarry Smith       rows[cnt] = i;
773a062f41SBarry Smith       cnt++;
783a062f41SBarry Smith     }
793a062f41SBarry Smith   }
803a062f41SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,is);CHKERRQ(ierr);
813a062f41SBarry Smith   PetscFunctionReturn(0);
823a062f41SBarry Smith }
833a062f41SBarry Smith 
84f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows)
856ce1633cSBarry Smith {
866ce1633cSBarry Smith   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
876ce1633cSBarry Smith   const MatScalar *aa = a->a;
886ce1633cSBarry Smith   PetscInt        i,m=A->rmap->n,cnt = 0;
89b2db7409Sstefano_zampini   const PetscInt  *ii = a->i,*jj = a->j,*diag;
906ce1633cSBarry Smith   PetscInt        *rows;
916ce1633cSBarry Smith   PetscErrorCode  ierr;
926ce1633cSBarry Smith 
936ce1633cSBarry Smith   PetscFunctionBegin;
946ce1633cSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
956ce1633cSBarry Smith   diag = a->diag;
966ce1633cSBarry Smith   for (i=0; i<m; i++) {
97b2db7409Sstefano_zampini     if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
986ce1633cSBarry Smith       cnt++;
996ce1633cSBarry Smith     }
1006ce1633cSBarry Smith   }
101785e854fSJed Brown   ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr);
1026ce1633cSBarry Smith   cnt  = 0;
1036ce1633cSBarry Smith   for (i=0; i<m; i++) {
104b2db7409Sstefano_zampini     if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
1056ce1633cSBarry Smith       rows[cnt++] = i;
1066ce1633cSBarry Smith     }
1076ce1633cSBarry Smith   }
108f1f41ecbSJed Brown   *nrows = cnt;
109f1f41ecbSJed Brown   *zrows = rows;
110f1f41ecbSJed Brown   PetscFunctionReturn(0);
111f1f41ecbSJed Brown }
112f1f41ecbSJed Brown 
113f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
114f1f41ecbSJed Brown {
115f1f41ecbSJed Brown   PetscInt       nrows,*rows;
116f1f41ecbSJed Brown   PetscErrorCode ierr;
117f1f41ecbSJed Brown 
118f1f41ecbSJed Brown   PetscFunctionBegin;
1190298fd71SBarry Smith   *zrows = NULL;
120f1f41ecbSJed Brown   ierr   = MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);CHKERRQ(ierr);
121ce94432eSBarry Smith   ierr   = ISCreateGeneral(PetscObjectComm((PetscObject)A),nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
1226ce1633cSBarry Smith   PetscFunctionReturn(0);
1236ce1633cSBarry Smith }
1246ce1633cSBarry Smith 
125b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
126b3a44c85SBarry Smith {
127b3a44c85SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
128b3a44c85SBarry Smith   const MatScalar *aa;
129b3a44c85SBarry Smith   PetscInt        m=A->rmap->n,cnt = 0;
130b3a44c85SBarry Smith   const PetscInt  *ii;
131b3a44c85SBarry Smith   PetscInt        n,i,j,*rows;
132b3a44c85SBarry Smith   PetscErrorCode  ierr;
133b3a44c85SBarry Smith 
134b3a44c85SBarry Smith   PetscFunctionBegin;
135b3a44c85SBarry Smith   *keptrows = 0;
136b3a44c85SBarry Smith   ii        = a->i;
137b3a44c85SBarry Smith   for (i=0; i<m; i++) {
138b3a44c85SBarry Smith     n = ii[i+1] - ii[i];
139b3a44c85SBarry Smith     if (!n) {
140b3a44c85SBarry Smith       cnt++;
141b3a44c85SBarry Smith       goto ok1;
142b3a44c85SBarry Smith     }
143b3a44c85SBarry Smith     aa = a->a + ii[i];
144b3a44c85SBarry Smith     for (j=0; j<n; j++) {
145b3a44c85SBarry Smith       if (aa[j] != 0.0) goto ok1;
146b3a44c85SBarry Smith     }
147b3a44c85SBarry Smith     cnt++;
148b3a44c85SBarry Smith ok1:;
149b3a44c85SBarry Smith   }
150b3a44c85SBarry Smith   if (!cnt) PetscFunctionReturn(0);
151854ce69bSBarry Smith   ierr = PetscMalloc1(A->rmap->n-cnt,&rows);CHKERRQ(ierr);
152b3a44c85SBarry Smith   cnt  = 0;
153b3a44c85SBarry Smith   for (i=0; i<m; i++) {
154b3a44c85SBarry Smith     n = ii[i+1] - ii[i];
155b3a44c85SBarry Smith     if (!n) continue;
156b3a44c85SBarry Smith     aa = a->a + ii[i];
157b3a44c85SBarry Smith     for (j=0; j<n; j++) {
158b3a44c85SBarry Smith       if (aa[j] != 0.0) {
159b3a44c85SBarry Smith         rows[cnt++] = i;
160b3a44c85SBarry Smith         break;
161b3a44c85SBarry Smith       }
162b3a44c85SBarry Smith     }
163b3a44c85SBarry Smith   }
164b3a44c85SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
165b3a44c85SBarry Smith   PetscFunctionReturn(0);
166b3a44c85SBarry Smith }
167b3a44c85SBarry Smith 
1687087cfbeSBarry Smith PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
16979299369SBarry Smith {
17079299369SBarry Smith   PetscErrorCode    ierr;
17179299369SBarry Smith   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*) Y->data;
17299e65526SBarry Smith   PetscInt          i,m = Y->rmap->n;
17399e65526SBarry Smith   const PetscInt    *diag;
17454f21887SBarry Smith   MatScalar         *aa = aij->a;
17599e65526SBarry Smith   const PetscScalar *v;
176ace3abfcSBarry Smith   PetscBool         missing;
17779299369SBarry Smith 
17879299369SBarry Smith   PetscFunctionBegin;
17909f38230SBarry Smith   if (Y->assembled) {
1800298fd71SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);CHKERRQ(ierr);
18109f38230SBarry Smith     if (!missing) {
18279299369SBarry Smith       diag = aij->diag;
18399e65526SBarry Smith       ierr = VecGetArrayRead(D,&v);CHKERRQ(ierr);
18479299369SBarry Smith       if (is == INSERT_VALUES) {
18579299369SBarry Smith         for (i=0; i<m; i++) {
18679299369SBarry Smith           aa[diag[i]] = v[i];
18779299369SBarry Smith         }
18879299369SBarry Smith       } else {
18979299369SBarry Smith         for (i=0; i<m; i++) {
19079299369SBarry Smith           aa[diag[i]] += v[i];
19179299369SBarry Smith         }
19279299369SBarry Smith       }
19399e65526SBarry Smith       ierr = VecRestoreArrayRead(D,&v);CHKERRQ(ierr);
19479299369SBarry Smith       PetscFunctionReturn(0);
19579299369SBarry Smith     }
196acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
19709f38230SBarry Smith   }
19809f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
19909f38230SBarry Smith   PetscFunctionReturn(0);
20009f38230SBarry Smith }
20179299369SBarry Smith 
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) {
2132462f5fdSStefano Zampini     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,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 */
218854ce69bSBarry Smith     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;
223854ce69bSBarry Smith       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 
2341a83f524SJed Brown PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2356945ee14SBarry Smith {
236dfbe8321SBarry Smith   PetscErrorCode ierr;
2376945ee14SBarry Smith 
2383a40ed3dSBarry Smith   PetscFunctionBegin;
2393a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
240bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
241606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
242ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
243bcd2baecSBarry Smith   }
2443a40ed3dSBarry Smith   PetscFunctionReturn(0);
24517ab2063SBarry Smith }
24617ab2063SBarry Smith 
2471a83f524SJed Brown PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2483b2fbd54SBarry Smith {
2493b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
250dfbe8321SBarry Smith   PetscErrorCode ierr;
251d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
25297f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
2533b2fbd54SBarry Smith 
2543a40ed3dSBarry Smith   PetscFunctionBegin;
255899cda47SBarry Smith   *nn = n;
2563a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2573b2fbd54SBarry Smith   if (symmetric) {
2582462f5fdSStefano Zampini     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,0,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
2593b2fbd54SBarry Smith   } else {
2601795a4d1SJed Brown     ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
261854ce69bSBarry Smith     ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr);
262854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&cja);CHKERRQ(ierr);
2633b2fbd54SBarry Smith     jj   = a->j;
2643b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
265bfeeae90SHong Zhang       collengths[jj[i]]++;
2663b2fbd54SBarry Smith     }
2673b2fbd54SBarry Smith     cia[0] = oshift;
2683b2fbd54SBarry Smith     for (i=0; i<n; i++) {
2693b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
2703b2fbd54SBarry Smith     }
27197f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
2723b2fbd54SBarry Smith     jj   = a->j;
273a93ec695SBarry Smith     for (row=0; row<m; row++) {
274a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
275a93ec695SBarry Smith       for (i=0; i<mr; i++) {
276bfeeae90SHong Zhang         col = *jj++;
2772205254eSKarl Rupp 
2783b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2793b2fbd54SBarry Smith       }
2803b2fbd54SBarry Smith     }
281606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2823b2fbd54SBarry Smith     *ia  = cia; *ja = cja;
2833b2fbd54SBarry Smith   }
2843a40ed3dSBarry Smith   PetscFunctionReturn(0);
2853b2fbd54SBarry Smith }
2863b2fbd54SBarry Smith 
2871a83f524SJed Brown PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2883b2fbd54SBarry Smith {
289dfbe8321SBarry Smith   PetscErrorCode ierr;
290606d414cSSatish Balay 
2913a40ed3dSBarry Smith   PetscFunctionBegin;
2923a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2933b2fbd54SBarry Smith 
294606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
295606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
2963a40ed3dSBarry Smith   PetscFunctionReturn(0);
2973b2fbd54SBarry Smith }
2983b2fbd54SBarry Smith 
2997cee066cSHong Zhang /*
3007cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
3017cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
302040ebd07SHong Zhang  spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
3037cee066cSHong Zhang */
3047cee066cSHong 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)
3057cee066cSHong Zhang {
3067cee066cSHong Zhang   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3077cee066cSHong Zhang   PetscErrorCode ierr;
3087cee066cSHong Zhang   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
3097cee066cSHong Zhang   PetscInt       nz = a->i[m],row,*jj,mr,col;
3107cee066cSHong Zhang   PetscInt       *cspidx;
3117cee066cSHong Zhang 
3127cee066cSHong Zhang   PetscFunctionBegin;
3137cee066cSHong Zhang   *nn = n;
3147cee066cSHong Zhang   if (!ia) PetscFunctionReturn(0);
315625f6d37SHong Zhang 
3161795a4d1SJed Brown   ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr);
317854ce69bSBarry Smith   ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr);
318854ce69bSBarry Smith   ierr = PetscMalloc1(nz+1,&cja);CHKERRQ(ierr);
319854ce69bSBarry Smith   ierr = PetscMalloc1(nz+1,&cspidx);CHKERRQ(ierr);
3207cee066cSHong Zhang   jj   = a->j;
3217cee066cSHong Zhang   for (i=0; i<nz; i++) {
3227cee066cSHong Zhang     collengths[jj[i]]++;
3237cee066cSHong Zhang   }
3247cee066cSHong Zhang   cia[0] = oshift;
3257cee066cSHong Zhang   for (i=0; i<n; i++) {
3267cee066cSHong Zhang     cia[i+1] = cia[i] + collengths[i];
3277cee066cSHong Zhang   }
3287cee066cSHong Zhang   ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
3297cee066cSHong Zhang   jj   = a->j;
3307cee066cSHong Zhang   for (row=0; row<m; row++) {
3317cee066cSHong Zhang     mr = a->i[row+1] - a->i[row];
3327cee066cSHong Zhang     for (i=0; i<mr; i++) {
3337cee066cSHong Zhang       col = *jj++;
3347cee066cSHong Zhang       cspidx[cia[col] + collengths[col] - oshift] = a->i[row] + i; /* index of a->j */
3357cee066cSHong Zhang       cja[cia[col] + collengths[col]++ - oshift]  = row + oshift;
3367cee066cSHong Zhang     }
3377cee066cSHong Zhang   }
3387cee066cSHong Zhang   ierr   = PetscFree(collengths);CHKERRQ(ierr);
3397cee066cSHong Zhang   *ia    = cia; *ja = cja;
3407cee066cSHong Zhang   *spidx = cspidx;
3417cee066cSHong Zhang   PetscFunctionReturn(0);
3427cee066cSHong Zhang }
3437cee066cSHong Zhang 
3447cee066cSHong 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)
3457cee066cSHong Zhang {
3467cee066cSHong Zhang   PetscErrorCode ierr;
3477cee066cSHong Zhang 
3487cee066cSHong Zhang   PetscFunctionBegin;
3495243ef75SHong Zhang   ierr = MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
3507cee066cSHong Zhang   ierr = PetscFree(*spidx);CHKERRQ(ierr);
3517cee066cSHong Zhang   PetscFunctionReturn(0);
3527cee066cSHong Zhang }
3537cee066cSHong Zhang 
35487d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
35587d4246cSBarry Smith {
35687d4246cSBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
35787d4246cSBarry Smith   PetscInt       *ai = a->i;
35887d4246cSBarry Smith   PetscErrorCode ierr;
35987d4246cSBarry Smith 
36087d4246cSBarry Smith   PetscFunctionBegin;
36187d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
36287d4246cSBarry Smith   PetscFunctionReturn(0);
36387d4246cSBarry Smith }
36487d4246cSBarry Smith 
365bd04181cSBarry Smith /*
366bd04181cSBarry Smith     MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions
367bd04181cSBarry Smith 
368bd04181cSBarry Smith       -   a single row of values is set with each call
369bd04181cSBarry Smith       -   no row or column indices are negative or (in error) larger than the number of rows or columns
370bd04181cSBarry Smith       -   the values are always added to the matrix, not set
371bd04181cSBarry Smith       -   no new locations are introduced in the nonzero structure of the matrix
372bd04181cSBarry Smith 
3731f763a69SBarry Smith      This does NOT assume the global column indices are sorted
374bd04181cSBarry Smith 
3751f763a69SBarry Smith */
376bd04181cSBarry Smith 
377af0996ceSBarry Smith #include <petsc/private/isimpl.h>
378189e4007SBarry Smith PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
379189e4007SBarry Smith {
380189e4007SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3811f763a69SBarry Smith   PetscInt       low,high,t,row,nrow,i,col,l;
3821f763a69SBarry Smith   const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j;
3831f763a69SBarry Smith   PetscInt       lastcol = -1;
384189e4007SBarry Smith   MatScalar      *ap,value,*aa = a->a;
385189e4007SBarry Smith   const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices;
386189e4007SBarry Smith 
387f38dd0b8SBarry Smith   row = ridx[im[0]];
3881f763a69SBarry Smith   rp   = aj + ai[row];
3891f763a69SBarry Smith   ap = aa + ai[row];
3901f763a69SBarry Smith   nrow = ailen[row];
391189e4007SBarry Smith   low  = 0;
392189e4007SBarry Smith   high = nrow;
393189e4007SBarry Smith   for (l=0; l<n; l++) { /* loop over added columns */
394189e4007SBarry Smith     col = cidx[in[l]];
395f38dd0b8SBarry Smith     value = v[l];
396189e4007SBarry Smith 
397189e4007SBarry Smith     if (col <= lastcol) low = 0;
398189e4007SBarry Smith     else high = nrow;
399189e4007SBarry Smith     lastcol = col;
400189e4007SBarry Smith     while (high-low > 5) {
401189e4007SBarry Smith       t = (low+high)/2;
402189e4007SBarry Smith       if (rp[t] > col) high = t;
403189e4007SBarry Smith       else low = t;
404189e4007SBarry Smith     }
405189e4007SBarry Smith     for (i=low; i<high; i++) {
406189e4007SBarry Smith       if (rp[i] == col) {
4071f763a69SBarry Smith         ap[i] += value;
408189e4007SBarry Smith         low = i + 1;
4091f763a69SBarry Smith         break;
410189e4007SBarry Smith       }
411189e4007SBarry Smith     }
412189e4007SBarry Smith   }
413f38dd0b8SBarry Smith   return 0;
414189e4007SBarry Smith }
415189e4007SBarry Smith 
41697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
41717ab2063SBarry Smith {
418416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
419e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
42097f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
4216849ba73SBarry Smith   PetscErrorCode ierr;
422e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
423d8cdefa3SHong Zhang   MatScalar      *ap=NULL,value=0.0,*aa = a->a;
424ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
425ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
42617ab2063SBarry Smith 
4273a40ed3dSBarry Smith   PetscFunctionBegin;
42817ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
429416022c9SBarry Smith     row = im[k];
4305ef9f2a5SBarry Smith     if (row < 0) continue;
4312515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
432e32f2f54SBarry 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);
4333b2fbd54SBarry Smith #endif
434720833daSHong Zhang     rp   = aj + ai[row];
435876c6284SHong Zhang     if (!A->structure_only) ap = aa + ai[row];
43617ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
437416022c9SBarry Smith     low  = 0;
438c71e6ed7SBarry Smith     high = nrow;
43917ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
4405ef9f2a5SBarry Smith       if (in[l] < 0) continue;
4412515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
442e32f2f54SBarry 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);
4433b2fbd54SBarry Smith #endif
444bfeeae90SHong Zhang       col = in[l];
445720833daSHong Zhang       if (!A->structure_only) {
4464b0e389bSBarry Smith         if (roworiented) {
4475ef9f2a5SBarry Smith           value = v[l + k*n];
448bef8e0ddSBarry Smith         } else {
4494b0e389bSBarry Smith           value = v[k + l*m];
4504b0e389bSBarry Smith         }
451720833daSHong Zhang       } else { /* A->structure_only */
452720833daSHong Zhang         value = 1; /* avoid 'continue' below?  */
453720833daSHong Zhang       }
454dcd36c23SBarry Smith       if ((value == 0.0 && ignorezeroentries) && (is == ADD_VALUES) && row != col) continue;
45536db0b34SBarry Smith 
4562205254eSKarl Rupp       if (col <= lastcol) low = 0;
4572205254eSKarl Rupp       else high = nrow;
458e2ee6c50SBarry Smith       lastcol = col;
459416022c9SBarry Smith       while (high-low > 5) {
460416022c9SBarry Smith         t = (low+high)/2;
461416022c9SBarry Smith         if (rp[t] > col) high = t;
462416022c9SBarry Smith         else low = t;
46317ab2063SBarry Smith       }
464416022c9SBarry Smith       for (i=low; i<high; i++) {
46517ab2063SBarry Smith         if (rp[i] > col) break;
46617ab2063SBarry Smith         if (rp[i] == col) {
467876c6284SHong Zhang           if (!A->structure_only) {
468416022c9SBarry Smith             if (is == ADD_VALUES) ap[i] += value;
46917ab2063SBarry Smith             else ap[i] = value;
470720833daSHong Zhang           }
471e44c0bd4SBarry Smith           low = i + 1;
47217ab2063SBarry Smith           goto noinsert;
47317ab2063SBarry Smith         }
47417ab2063SBarry Smith       }
475dcd36c23SBarry Smith       if (value == 0.0 && ignorezeroentries && row != col) goto noinsert;
476c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
477e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
478720833daSHong Zhang       if (A->structure_only) {
479876c6284SHong Zhang         MatSeqXAIJReallocateAIJ_structure_only(A,A->rmap->n,1,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar);
480720833daSHong Zhang       } else {
481fef13f97SBarry Smith         MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
482720833daSHong Zhang       }
483c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
484416022c9SBarry Smith       /* shift up all the later entries in this row */
485416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
48617ab2063SBarry Smith         rp[ii+1] = rp[ii];
487876c6284SHong Zhang         if (!A->structure_only) ap[ii+1] = ap[ii];
488720833daSHong Zhang       }
48917ab2063SBarry Smith       rp[i] = col;
490876c6284SHong Zhang       if (!A->structure_only) ap[i] = value;
491416022c9SBarry Smith       low   = i + 1;
492e56f5c9eSBarry Smith       A->nonzerostate++;
493e44c0bd4SBarry Smith noinsert:;
49417ab2063SBarry Smith     }
49517ab2063SBarry Smith     ailen[row] = nrow;
49617ab2063SBarry Smith   }
4973a40ed3dSBarry Smith   PetscFunctionReturn(0);
49817ab2063SBarry Smith }
49917ab2063SBarry Smith 
50081824310SBarry Smith 
501a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
5027eb43aa7SLois Curfman McInnes {
5037eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
50497f1f81fSBarry Smith   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
50597f1f81fSBarry Smith   PetscInt   *ai = a->i,*ailen = a->ilen;
50654f21887SBarry Smith   MatScalar  *ap,*aa = a->a;
5077eb43aa7SLois Curfman McInnes 
5083a40ed3dSBarry Smith   PetscFunctionBegin;
5097eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
5107eb43aa7SLois Curfman McInnes     row = im[k];
511e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
512e32f2f54SBarry 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);
513bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
5147eb43aa7SLois Curfman McInnes     nrow = ailen[row];
5157eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
516e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
517e32f2f54SBarry 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);
518bfeeae90SHong Zhang       col  = in[l];
5197eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
5207eb43aa7SLois Curfman McInnes       while (high-low > 5) {
5217eb43aa7SLois Curfman McInnes         t = (low+high)/2;
5227eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
5237eb43aa7SLois Curfman McInnes         else low = t;
5247eb43aa7SLois Curfman McInnes       }
5257eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
5267eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
5277eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
528b49de8d1SLois Curfman McInnes           *v++ = ap[i];
5297eb43aa7SLois Curfman McInnes           goto finished;
5307eb43aa7SLois Curfman McInnes         }
5317eb43aa7SLois Curfman McInnes       }
53297e567efSBarry Smith       *v++ = 0.0;
5337eb43aa7SLois Curfman McInnes finished:;
5347eb43aa7SLois Curfman McInnes     }
5357eb43aa7SLois Curfman McInnes   }
5363a40ed3dSBarry Smith   PetscFunctionReturn(0);
5377eb43aa7SLois Curfman McInnes }
5387eb43aa7SLois Curfman McInnes 
53917ab2063SBarry Smith 
540dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
54117ab2063SBarry Smith {
542416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5436849ba73SBarry Smith   PetscErrorCode ierr;
5446f69ff64SBarry Smith   PetscInt       i,*col_lens;
5456f69ff64SBarry Smith   int            fd;
546b37d52dbSMark F. Adams   FILE           *file;
54717ab2063SBarry Smith 
5483a40ed3dSBarry Smith   PetscFunctionBegin;
549b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
550854ce69bSBarry Smith   ierr = PetscMalloc1(4+A->rmap->n,&col_lens);CHKERRQ(ierr);
5512205254eSKarl Rupp 
5520700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
553d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
554d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
555416022c9SBarry Smith   col_lens[3] = a->nz;
556416022c9SBarry Smith 
557416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
558d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
559416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
56017ab2063SBarry Smith   }
561d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
562606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
563416022c9SBarry Smith 
564416022c9SBarry Smith   /* store column indices (zero start index) */
5656f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
566416022c9SBarry Smith 
567416022c9SBarry Smith   /* store nonzero values */
5686f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
569b37d52dbSMark F. Adams 
570b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
571b37d52dbSMark F. Adams   if (file) {
57233d57670SJed Brown     fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs));
573b37d52dbSMark F. Adams   }
5743a40ed3dSBarry Smith   PetscFunctionReturn(0);
57517ab2063SBarry Smith }
576416022c9SBarry Smith 
5777dc0baabSHong Zhang static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer)
5787dc0baabSHong Zhang {
5797dc0baabSHong Zhang   PetscErrorCode ierr;
5807dc0baabSHong Zhang   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5817dc0baabSHong Zhang   PetscInt       i,k,m=A->rmap->N;
5827dc0baabSHong Zhang 
5837dc0baabSHong Zhang   PetscFunctionBegin;
5847dc0baabSHong Zhang   ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5857dc0baabSHong Zhang   for (i=0; i<m; i++) {
5867dc0baabSHong Zhang     ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
5877dc0baabSHong Zhang     for (k=a->i[i]; k<a->i[i+1]; k++) {
5887dc0baabSHong Zhang       ierr = PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);CHKERRQ(ierr);
5897dc0baabSHong Zhang     }
5907dc0baabSHong Zhang     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
5917dc0baabSHong Zhang   }
5927dc0baabSHong Zhang   ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5937dc0baabSHong Zhang   PetscFunctionReturn(0);
5947dc0baabSHong Zhang }
5957dc0baabSHong Zhang 
59609573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
597cd155464SBarry Smith 
598dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
599416022c9SBarry Smith {
600416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
601dfbe8321SBarry Smith   PetscErrorCode    ierr;
60260e0710aSBarry Smith   PetscInt          i,j,m = A->rmap->n;
603e060cb09SBarry Smith   const char        *name;
604f3ef73ceSBarry Smith   PetscViewerFormat format;
60517ab2063SBarry Smith 
6063a40ed3dSBarry Smith   PetscFunctionBegin;
6077dc0baabSHong Zhang   if (A->structure_only) {
6087dc0baabSHong Zhang     ierr = MatView_SeqAIJ_ASCII_structonly(A,viewer);CHKERRQ(ierr);
6097dc0baabSHong Zhang     PetscFunctionReturn(0);
6107dc0baabSHong Zhang   }
61143e49210SHong Zhang 
612b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
61371c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
61497f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
61560e0710aSBarry Smith     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
616c337ccceSJed Brown       /* Need a dummy value to ensure the dimension of the matrix. */
617d00d2cf4SBarry Smith       nofinalvalue = 1;
618d00d2cf4SBarry Smith     }
619d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
620d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
62177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
622fbfe6fa7SJed Brown #if defined(PETSC_USE_COMPLEX)
623fbfe6fa7SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
624fbfe6fa7SJed Brown #else
62577431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
626fbfe6fa7SJed Brown #endif
627b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
62817ab2063SBarry Smith 
62917ab2063SBarry Smith     for (i=0; i<m; i++) {
63060e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
631aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
632a9bf72d8SJed 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);
63317ab2063SBarry Smith #else
63460e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr);
63517ab2063SBarry Smith #endif
63617ab2063SBarry Smith       }
63717ab2063SBarry Smith     }
638d00d2cf4SBarry Smith     if (nofinalvalue) {
639c337ccceSJed Brown #if defined(PETSC_USE_COMPLEX)
640c337ccceSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr);
641c337ccceSJed Brown #else
642d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
643c337ccceSJed Brown #endif
644d00d2cf4SBarry Smith     }
645317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
646fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
647d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
64868369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
649cd155464SBarry Smith     PetscFunctionReturn(0);
650fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
651d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
65244cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
65377431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
65460e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
655aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
65636db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
65760e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
65836db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
65960e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
66036db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
66160e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
6626831982aSBarry Smith         }
66344cd7ae7SLois Curfman McInnes #else
66460e0710aSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);}
66544cd7ae7SLois Curfman McInnes #endif
66644cd7ae7SLois Curfman McInnes       }
667b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
66844cd7ae7SLois Curfman McInnes     }
669d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
670fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
67197f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
672d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
673854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&sptr);CHKERRQ(ierr);
674496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
675496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
67660e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
677496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
678aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
67936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
680496be53dSLois Curfman McInnes #else
681496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
682496be53dSLois Curfman McInnes #endif
683496be53dSLois Curfman McInnes         }
684496be53dSLois Curfman McInnes       }
685496be53dSLois Curfman McInnes     }
6862e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
68777431f27SBarry Smith     ierr    = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
6882e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
6892205254eSKarl Rupp       if (i+4<m) {
6902205254eSKarl 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);
6912205254eSKarl Rupp       } else if (i+3<m) {
6922205254eSKarl 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);
6932205254eSKarl Rupp       } else if (i+2<m) {
6942205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);
6952205254eSKarl Rupp       } else if (i+1<m) {
6962205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);
6972205254eSKarl Rupp       } else if (i<m) {
6982205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);
6992205254eSKarl Rupp       } else {
7002205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);
7012205254eSKarl Rupp       }
702496be53dSLois Curfman McInnes     }
703b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
704606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
705496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
70660e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
70777431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
708496be53dSLois Curfman McInnes       }
709b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
710496be53dSLois Curfman McInnes     }
711b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
712496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
71360e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
714496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
715aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
71636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
71760e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
7186831982aSBarry Smith           }
719496be53dSLois Curfman McInnes #else
72060e0710aSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);}
721496be53dSLois Curfman McInnes #endif
722496be53dSLois Curfman McInnes         }
723496be53dSLois Curfman McInnes       }
724b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
725496be53dSLois Curfman McInnes     }
726d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
727fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
72897f1f81fSBarry Smith     PetscInt    cnt = 0,jcnt;
72987828ca2SBarry Smith     PetscScalar value;
73068f1ed48SBarry Smith #if defined(PETSC_USE_COMPLEX)
73168f1ed48SBarry Smith     PetscBool   realonly = PETSC_TRUE;
73268f1ed48SBarry Smith 
73368f1ed48SBarry Smith     for (i=0; i<a->i[m]; i++) {
73468f1ed48SBarry Smith       if (PetscImaginaryPart(a->a[i]) != 0.0) {
73568f1ed48SBarry Smith         realonly = PETSC_FALSE;
73668f1ed48SBarry Smith         break;
73768f1ed48SBarry Smith       }
73868f1ed48SBarry Smith     }
73968f1ed48SBarry Smith #endif
74002594712SBarry Smith 
741d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
74202594712SBarry Smith     for (i=0; i<m; i++) {
74302594712SBarry Smith       jcnt = 0;
744d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
745e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
74602594712SBarry Smith           value = a->a[cnt++];
747e24b481bSBarry Smith           jcnt++;
74802594712SBarry Smith         } else {
74902594712SBarry Smith           value = 0.0;
75002594712SBarry Smith         }
751aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
75268f1ed48SBarry Smith         if (realonly) {
75360e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr);
75468f1ed48SBarry Smith         } else {
75560e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr);
75668f1ed48SBarry Smith         }
75702594712SBarry Smith #else
75860e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr);
75902594712SBarry Smith #endif
76002594712SBarry Smith       }
761b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
76202594712SBarry Smith     }
763d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7643c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
765150b93efSMatthew G. Knepley     PetscInt fshift=1;
766d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
7673c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
76819303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");CHKERRQ(ierr);
7693c215bfdSMatthew Knepley #else
77019303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");CHKERRQ(ierr);
7713c215bfdSMatthew Knepley #endif
772d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
7733c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
77460e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
7753c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
776a9a0e077SKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
7773c215bfdSMatthew Knepley #else
778150b93efSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr);
7793c215bfdSMatthew Knepley #endif
7803c215bfdSMatthew Knepley       }
7813c215bfdSMatthew Knepley     }
782d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7833a40ed3dSBarry Smith   } else {
784d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
785d5f3da31SBarry Smith     if (A->factortype) {
78616cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
78716cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
78816cd7e1dSShri Abhyankar         /* L part */
78960e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
79016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
79116cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
79260e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
79316cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
7946712e2f1SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
79516cd7e1dSShri Abhyankar           } else {
79660e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
79716cd7e1dSShri Abhyankar           }
79816cd7e1dSShri Abhyankar #else
79960e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
80016cd7e1dSShri Abhyankar #endif
80116cd7e1dSShri Abhyankar         }
80216cd7e1dSShri Abhyankar         /* diagonal */
80316cd7e1dSShri Abhyankar         j = a->diag[i];
80416cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
80516cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
80660e0710aSBarry 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);
80716cd7e1dSShri Abhyankar         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
8086712e2f1SBarry 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);
80916cd7e1dSShri Abhyankar         } else {
81060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
81116cd7e1dSShri Abhyankar         }
81216cd7e1dSShri Abhyankar #else
81360e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr);
81416cd7e1dSShri Abhyankar #endif
81516cd7e1dSShri Abhyankar 
81616cd7e1dSShri Abhyankar         /* U part */
81760e0710aSBarry Smith         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
81816cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
81916cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
82060e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
82116cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
82222ab088eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
82316cd7e1dSShri Abhyankar           } else {
82460e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
82516cd7e1dSShri Abhyankar           }
82616cd7e1dSShri Abhyankar #else
82760e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
82816cd7e1dSShri Abhyankar #endif
82916cd7e1dSShri Abhyankar         }
83016cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
83116cd7e1dSShri Abhyankar       }
83216cd7e1dSShri Abhyankar     } else {
83317ab2063SBarry Smith       for (i=0; i<m; i++) {
83477431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
83560e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
836aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
83736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
83860e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
83936db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
84060e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
8413a40ed3dSBarry Smith           } else {
84260e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
84317ab2063SBarry Smith           }
84417ab2063SBarry Smith #else
84560e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
84617ab2063SBarry Smith #endif
84717ab2063SBarry Smith         }
848b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
84917ab2063SBarry Smith       }
85016cd7e1dSShri Abhyankar     }
851d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
85217ab2063SBarry Smith   }
853b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8543a40ed3dSBarry Smith   PetscFunctionReturn(0);
855416022c9SBarry Smith }
856416022c9SBarry Smith 
8579804daf3SBarry Smith #include <petscdraw.h>
858dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
859416022c9SBarry Smith {
860480ef9eaSBarry Smith   Mat               A  = (Mat) Aa;
861416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
862dfbe8321SBarry Smith   PetscErrorCode    ierr;
863383922c3SLisandro Dalcin   PetscInt          i,j,m = A->rmap->n;
864383922c3SLisandro Dalcin   int               color;
865b05fc000SLisandro Dalcin   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
866b0a32e0cSBarry Smith   PetscViewer       viewer;
867f3ef73ceSBarry Smith   PetscViewerFormat format;
868cddf8d76SBarry Smith 
8693a40ed3dSBarry Smith   PetscFunctionBegin;
870480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
871b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
872b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
873383922c3SLisandro Dalcin 
874416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
8750513a670SBarry Smith 
876fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
877383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
8780513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
879b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
880416022c9SBarry Smith     for (i=0; i<m; i++) {
881cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
882bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
883bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
88436db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
885b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
886cddf8d76SBarry Smith       }
887cddf8d76SBarry Smith     }
888b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
889cddf8d76SBarry Smith     for (i=0; i<m; i++) {
890cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
891bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
892bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
893cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
894b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
895cddf8d76SBarry Smith       }
896cddf8d76SBarry Smith     }
897b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
898cddf8d76SBarry Smith     for (i=0; i<m; i++) {
899cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
900bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
901bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
90236db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
903b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
904416022c9SBarry Smith       }
905416022c9SBarry Smith     }
906383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
9070513a670SBarry Smith   } else {
9080513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
9090513a670SBarry Smith     /* first determine max of all nonzero values */
910b05fc000SLisandro Dalcin     PetscReal minv = 0.0, maxv = 0.0;
911383922c3SLisandro Dalcin     PetscInt  nz = a->nz, count = 0;
912b0a32e0cSBarry Smith     PetscDraw popup;
9130513a670SBarry Smith 
9140513a670SBarry Smith     for (i=0; i<nz; i++) {
9150513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
9160513a670SBarry Smith     }
917383922c3SLisandro Dalcin     if (minv >= maxv) maxv = minv + PETSC_SMALL;
918b0a32e0cSBarry Smith     ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
91945f3bb6eSLisandro Dalcin     ierr = PetscDrawScalePopup(popup,minv,maxv);CHKERRQ(ierr);
920383922c3SLisandro Dalcin 
921383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
9220513a670SBarry Smith     for (i=0; i<m; i++) {
923383922c3SLisandro Dalcin       y_l = m - i - 1.0;
924383922c3SLisandro Dalcin       y_r = y_l + 1.0;
925bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
926383922c3SLisandro Dalcin         x_l = a->j[j];
927383922c3SLisandro Dalcin         x_r = x_l + 1.0;
928b05fc000SLisandro Dalcin         color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
929b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
9300513a670SBarry Smith         count++;
9310513a670SBarry Smith       }
9320513a670SBarry Smith     }
933383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
9340513a670SBarry Smith   }
935480ef9eaSBarry Smith   PetscFunctionReturn(0);
936480ef9eaSBarry Smith }
937cddf8d76SBarry Smith 
9389804daf3SBarry Smith #include <petscdraw.h>
939dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
940480ef9eaSBarry Smith {
941dfbe8321SBarry Smith   PetscErrorCode ierr;
942b0a32e0cSBarry Smith   PetscDraw      draw;
94336db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
944ace3abfcSBarry Smith   PetscBool      isnull;
945480ef9eaSBarry Smith 
946480ef9eaSBarry Smith   PetscFunctionBegin;
947b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
948b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
949480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
950480ef9eaSBarry Smith 
951d0f46423SBarry Smith   xr   = A->cmap->n; yr  = A->rmap->n; h = yr/10.0; w = xr/10.0;
952480ef9eaSBarry Smith   xr  += w;          yr += h;         xl = -w;     yl = -h;
953b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
954832b7cebSLisandro Dalcin   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
955b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
9560298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
957832b7cebSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
9583a40ed3dSBarry Smith   PetscFunctionReturn(0);
959416022c9SBarry Smith }
960416022c9SBarry Smith 
961dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
962416022c9SBarry Smith {
963dfbe8321SBarry Smith   PetscErrorCode ierr;
964ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
965416022c9SBarry Smith 
9663a40ed3dSBarry Smith   PetscFunctionBegin;
967251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
968251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
969251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
970c45a1595SBarry Smith   if (iascii) {
9713a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
9720f5bd95cSBarry Smith   } else if (isbinary) {
9733a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
9740f5bd95cSBarry Smith   } else if (isdraw) {
9753a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
97611aeaf0aSBarry Smith   }
9774108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
9783a40ed3dSBarry Smith   PetscFunctionReturn(0);
97917ab2063SBarry Smith }
98019bcc07fSBarry Smith 
981dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
98217ab2063SBarry Smith {
983416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9846849ba73SBarry Smith   PetscErrorCode ierr;
98597f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
986d0f46423SBarry Smith   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
98754f21887SBarry Smith   MatScalar      *aa    = a->a,*ap;
9883447b6efSHong Zhang   PetscReal      ratio  = 0.6;
98917ab2063SBarry Smith 
9903a40ed3dSBarry Smith   PetscFunctionBegin;
9913a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
99217ab2063SBarry Smith 
99343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
99417ab2063SBarry Smith   for (i=1; i<m; i++) {
995416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
99617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
99794a9d846SBarry Smith     rmax    = PetscMax(rmax,ailen[i]);
99817ab2063SBarry Smith     if (fshift) {
999bfeeae90SHong Zhang       ip = aj + ai[i];
1000bfeeae90SHong Zhang       ap = aa + ai[i];
100117ab2063SBarry Smith       N  = ailen[i];
100217ab2063SBarry Smith       for (j=0; j<N; j++) {
100317ab2063SBarry Smith         ip[j-fshift] = ip[j];
1004876c6284SHong Zhang         if (!A->structure_only) ap[j-fshift] = ap[j];
100517ab2063SBarry Smith       }
100617ab2063SBarry Smith     }
100717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
100817ab2063SBarry Smith   }
100917ab2063SBarry Smith   if (m) {
101017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
101117ab2063SBarry Smith     ai[m]   = ai[m-1] + ailen[m-1];
101217ab2063SBarry Smith   }
10137b083b7cSBarry Smith 
101417ab2063SBarry Smith   /* reset ilen and imax for each row */
10157b083b7cSBarry Smith   a->nonzerorowcnt = 0;
1016396832f4SHong Zhang   if (A->structure_only) {
1017396832f4SHong Zhang     ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
1018396832f4SHong Zhang   } else { /* !A->structure_only */
101917ab2063SBarry Smith     for (i=0; i<m; i++) {
102017ab2063SBarry Smith       ailen[i] = imax[i] = ai[i+1] - ai[i];
10217b083b7cSBarry Smith       a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
102217ab2063SBarry Smith     }
1023396832f4SHong Zhang   }
1024bfeeae90SHong Zhang   a->nz = ai[m];
102565e19b50SBarry 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);
102617ab2063SBarry Smith 
102709f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1028d0f46423SBarry 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);
1029ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
1030ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
10312205254eSKarl Rupp 
10328e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
1033dd5f02e7SSatish Balay   a->reallocs         = 0;
10346712e2f1SBarry Smith   A->info.nz_unneeded = (PetscReal)fshift;
103536db0b34SBarry Smith   a->rmax             = rmax;
10364e220ebcSLois Curfman McInnes 
1037396832f4SHong Zhang   if (!A->structure_only) {
103811e456e1SBarry Smith     ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
1039396832f4SHong Zhang   }
10404108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
1041acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10423a40ed3dSBarry Smith   PetscFunctionReturn(0);
104317ab2063SBarry Smith }
104417ab2063SBarry Smith 
104599cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
104699cafbc1SBarry Smith {
104799cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
104899cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
104954f21887SBarry Smith   MatScalar      *aa = a->a;
1050acf2f550SJed Brown   PetscErrorCode ierr;
105199cafbc1SBarry Smith 
105299cafbc1SBarry Smith   PetscFunctionBegin;
105399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1054acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
105599cafbc1SBarry Smith   PetscFunctionReturn(0);
105699cafbc1SBarry Smith }
105799cafbc1SBarry Smith 
105899cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
105999cafbc1SBarry Smith {
106099cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
106199cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
106254f21887SBarry Smith   MatScalar      *aa = a->a;
1063acf2f550SJed Brown   PetscErrorCode ierr;
106499cafbc1SBarry Smith 
106599cafbc1SBarry Smith   PetscFunctionBegin;
106699cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1067acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
106899cafbc1SBarry Smith   PetscFunctionReturn(0);
106999cafbc1SBarry Smith }
107099cafbc1SBarry Smith 
1071dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
107217ab2063SBarry Smith {
1073416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1074dfbe8321SBarry Smith   PetscErrorCode ierr;
10753a40ed3dSBarry Smith 
10763a40ed3dSBarry Smith   PetscFunctionBegin;
1077d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
1078acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10793a40ed3dSBarry Smith   PetscFunctionReturn(0);
108017ab2063SBarry Smith }
1081416022c9SBarry Smith 
1082dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
108317ab2063SBarry Smith {
1084416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1085dfbe8321SBarry Smith   PetscErrorCode ierr;
1086d5d45c9bSBarry Smith 
10873a40ed3dSBarry Smith   PetscFunctionBegin;
1088aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1089d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
109017ab2063SBarry Smith #endif
1091e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
10926bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
10936bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
109405b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
1095d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
109605b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
1097846b4da1SFande Kong   ierr = PetscFree(a->ipre);CHKERRQ(ierr);
109871f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
109905b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
11006bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
110105b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
11026bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
1103cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
11040b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
1105a30b2313SHong Zhang 
11064108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
1107bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
1108901853e0SKris Buschelman 
1109dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
1110bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
1111bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1112bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1113bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr);
1114bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr);
1115bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr);
1116af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
1117af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);CHKERRQ(ierr);
1118af8000cdSHong Zhang #endif
111963c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE)
112063c07aadSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);CHKERRQ(ierr);
11213dad0653Sstefano_zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMatMatMult_transpose_seqaij_seqaij_C",NULL);CHKERRQ(ierr);
112263c07aadSStefano Zampini #endif
1123b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);CHKERRQ(ierr);
1124c9225affSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsell_C",NULL);CHKERRQ(ierr);
1125c9225affSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_is_C",NULL);CHKERRQ(ierr);
1126bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1127bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1128846b4da1SFande Kong   ierr = PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);CHKERRQ(ierr);
1129bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1130bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr);
113175d48cdbSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatPtAP_is_seqaij_C",NULL);CHKERRQ(ierr);
11323a40ed3dSBarry Smith   PetscFunctionReturn(0);
113317ab2063SBarry Smith }
113417ab2063SBarry Smith 
1135ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
113617ab2063SBarry Smith {
1137416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11384846f1f5SKris Buschelman   PetscErrorCode ierr;
11393a40ed3dSBarry Smith 
11403a40ed3dSBarry Smith   PetscFunctionBegin;
1141a65d3064SKris Buschelman   switch (op) {
1142a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
11434e0d8c25SBarry Smith     a->roworiented = flg;
1144a65d3064SKris Buschelman     break;
1145a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1146a9817697SBarry Smith     a->keepnonzeropattern = flg;
1147a65d3064SKris Buschelman     break;
1148512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1149512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1150a65d3064SKris Buschelman     break;
1151a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
11524e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1153a65d3064SKris Buschelman     break;
1154a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
11554e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1156a65d3064SKris Buschelman     break;
115728b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
115828b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
115928b2fa4aSMatthew Knepley     break;
1160a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11614e0d8c25SBarry Smith     a->ignorezeroentries = flg;
11620df259c2SBarry Smith     break;
11633d472b54SHong Zhang   case MAT_SPD:
1164b1646e73SJed Brown   case MAT_SYMMETRIC:
1165b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1166b1646e73SJed Brown   case MAT_HERMITIAN:
1167b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
1168957cac9fSHong Zhang   case MAT_STRUCTURE_ONLY:
11695021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
11705021d80fSJed Brown     break;
11714e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1172a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1173a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1174290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1175a65d3064SKris Buschelman     break;
1176b87ac2d8SJed Brown   case MAT_USE_INODES:
1177b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1178b87ac2d8SJed Brown     break;
1179c10200c1SHong Zhang   case MAT_SUBMAT_SINGLEIS:
1180c10200c1SHong Zhang     A->submat_singleis = flg;
1181c10200c1SHong Zhang     break;
1182a65d3064SKris Buschelman   default:
1183e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1184a65d3064SKris Buschelman   }
11854108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
11863a40ed3dSBarry Smith   PetscFunctionReturn(0);
118717ab2063SBarry Smith }
118817ab2063SBarry Smith 
1189dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
119017ab2063SBarry Smith {
1191416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11926849ba73SBarry Smith   PetscErrorCode ierr;
1193d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
119435e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
119517ab2063SBarry Smith 
11963a40ed3dSBarry Smith   PetscFunctionBegin;
1197d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1198e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
119935e7444dSHong Zhang 
1200d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1201d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
120235e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
12032c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
120435e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
120535e7444dSHong Zhang     PetscFunctionReturn(0);
120635e7444dSHong Zhang   }
120735e7444dSHong Zhang 
12082dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
12091ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
121035e7444dSHong Zhang   for (i=0; i<n; i++) {
121135e7444dSHong Zhang     nz = ai[i+1] - ai[i];
12122f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
121335e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++) {
121435e7444dSHong Zhang       if (aj[j] == i) {
121535e7444dSHong Zhang         x[i] = aa[j];
121617ab2063SBarry Smith         break;
121717ab2063SBarry Smith       }
121817ab2063SBarry Smith     }
121917ab2063SBarry Smith   }
12201ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
12213a40ed3dSBarry Smith   PetscFunctionReturn(0);
122217ab2063SBarry Smith }
122317ab2063SBarry Smith 
1224c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1225dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
122617ab2063SBarry Smith {
1227416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1228d9ca1df4SBarry Smith   PetscScalar       *y;
1229d9ca1df4SBarry Smith   const PetscScalar *x;
1230dfbe8321SBarry Smith   PetscErrorCode    ierr;
1231d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
12325c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1233d9ca1df4SBarry Smith   const MatScalar   *v;
1234a77337e4SBarry Smith   PetscScalar       alpha;
1235d9ca1df4SBarry Smith   PetscInt          n,i,j;
1236d9ca1df4SBarry Smith   const PetscInt    *idx,*ii,*ridx=NULL;
12373447b6efSHong Zhang   Mat_CompressedRow cprow    = a->compressedrow;
1238ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
12395c897100SBarry Smith #endif
124017ab2063SBarry Smith 
12413a40ed3dSBarry Smith   PetscFunctionBegin;
12422e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
1243d9ca1df4SBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12441ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
12455c897100SBarry Smith 
12465c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1247bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
12485c897100SBarry Smith #else
12493447b6efSHong Zhang   if (usecprow) {
12503447b6efSHong Zhang     m    = cprow.nrows;
12513447b6efSHong Zhang     ii   = cprow.i;
12527b2bb3b9SHong Zhang     ridx = cprow.rindex;
12533447b6efSHong Zhang   } else {
12543447b6efSHong Zhang     ii = a->i;
12553447b6efSHong Zhang   }
125617ab2063SBarry Smith   for (i=0; i<m; i++) {
12573447b6efSHong Zhang     idx = a->j + ii[i];
12583447b6efSHong Zhang     v   = a->a + ii[i];
12593447b6efSHong Zhang     n   = ii[i+1] - ii[i];
12603447b6efSHong Zhang     if (usecprow) {
12617b2bb3b9SHong Zhang       alpha = x[ridx[i]];
12623447b6efSHong Zhang     } else {
126317ab2063SBarry Smith       alpha = x[i];
12643447b6efSHong Zhang     }
126504fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
126617ab2063SBarry Smith   }
12675c897100SBarry Smith #endif
1268dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1269d9ca1df4SBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12701ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12713a40ed3dSBarry Smith   PetscFunctionReturn(0);
127217ab2063SBarry Smith }
127317ab2063SBarry Smith 
1274dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
12755c897100SBarry Smith {
1276dfbe8321SBarry Smith   PetscErrorCode ierr;
12775c897100SBarry Smith 
12785c897100SBarry Smith   PetscFunctionBegin;
1279170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
12805c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
12815c897100SBarry Smith   PetscFunctionReturn(0);
12825c897100SBarry Smith }
12835c897100SBarry Smith 
1284c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
128578b84d54SShri Abhyankar 
1286dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
128717ab2063SBarry Smith {
1288416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1289d9fead3dSBarry Smith   PetscScalar       *y;
129054f21887SBarry Smith   const PetscScalar *x;
129154f21887SBarry Smith   const MatScalar   *aa;
1292dfbe8321SBarry Smith   PetscErrorCode    ierr;
1293003131ecSBarry Smith   PetscInt          m=A->rmap->n;
12940298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
12957b083b7cSBarry Smith   PetscInt          n,i;
1296362ced78SSatish Balay   PetscScalar       sum;
1297ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
129817ab2063SBarry Smith 
1299b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
130097952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1301fee21e36SBarry Smith #endif
1302fee21e36SBarry Smith 
13033a40ed3dSBarry Smith   PetscFunctionBegin;
13043649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
13051ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1306416022c9SBarry Smith   ii   = a->i;
13074eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
13084f390cb1SBarry Smith     ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr);
130997952fefSHong Zhang     m    = a->compressedrow.nrows;
131097952fefSHong Zhang     ii   = a->compressedrow.i;
131197952fefSHong Zhang     ridx = a->compressedrow.rindex;
131297952fefSHong Zhang     for (i=0; i<m; i++) {
131397952fefSHong Zhang       n           = ii[i+1] - ii[i];
131497952fefSHong Zhang       aj          = a->j + ii[i];
131597952fefSHong Zhang       aa          = a->a + ii[i];
131697952fefSHong Zhang       sum         = 0.0;
1317003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1318003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
131997952fefSHong Zhang       y[*ridx++] = sum;
132097952fefSHong Zhang     }
132197952fefSHong Zhang   } else { /* do not use compressed row format */
1322b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
13233d3eaba7SBarry Smith     aj   = a->j;
13243d3eaba7SBarry Smith     aa   = a->a;
1325b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1326b05257ddSBarry Smith #else
132717ab2063SBarry Smith     for (i=0; i<m; i++) {
1328003131ecSBarry Smith       n           = ii[i+1] - ii[i];
1329003131ecSBarry Smith       aj          = a->j + ii[i];
1330003131ecSBarry Smith       aa          = a->a + ii[i];
133117ab2063SBarry Smith       sum         = 0.0;
1332003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
133317ab2063SBarry Smith       y[i] = sum;
133417ab2063SBarry Smith     }
13358d195f9aSBarry Smith #endif
1336b05257ddSBarry Smith   }
13377b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
13383649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13391ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13403a40ed3dSBarry Smith   PetscFunctionReturn(0);
134117ab2063SBarry Smith }
134217ab2063SBarry Smith 
1343b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1344b434eb95SMatthew G. Knepley {
1345b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1346b434eb95SMatthew G. Knepley   PetscScalar       *y;
1347b434eb95SMatthew G. Knepley   const PetscScalar *x;
1348b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1349b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1350b434eb95SMatthew G. Knepley   PetscInt          m=A->rmap->n;
1351b434eb95SMatthew G. Knepley   const PetscInt    *aj,*ii,*ridx=NULL;
1352b434eb95SMatthew G. Knepley   PetscInt          n,i,nonzerorow=0;
1353b434eb95SMatthew G. Knepley   PetscScalar       sum;
1354b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1355b434eb95SMatthew G. Knepley 
1356b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1357b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa)
1358b434eb95SMatthew G. Knepley #endif
1359b434eb95SMatthew G. Knepley 
1360b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1361b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1362b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1363b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1364b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1365b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1366b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1367b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1368b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1369b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1370b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1371b434eb95SMatthew G. Knepley       sum         = 0.0;
1372b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1373b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1374b434eb95SMatthew G. Knepley       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1375b434eb95SMatthew G. Knepley       y[*ridx++] = sum;
1376b434eb95SMatthew G. Knepley     }
1377b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
13783d3eaba7SBarry Smith     ii = a->i;
1379b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1380b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1381b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1382b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1383b434eb95SMatthew G. Knepley       sum         = 0.0;
1384b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1385b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1386b434eb95SMatthew G. Knepley       y[i] = sum;
1387b434eb95SMatthew G. Knepley     }
1388b434eb95SMatthew G. Knepley   }
1389b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1390b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1391b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1392b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1393b434eb95SMatthew G. Knepley }
1394b434eb95SMatthew G. Knepley 
1395b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1396b434eb95SMatthew G. Knepley {
1397b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1398b434eb95SMatthew G. Knepley   PetscScalar       *y,*z;
1399b434eb95SMatthew G. Knepley   const PetscScalar *x;
1400b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1401b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1402b434eb95SMatthew G. Knepley   PetscInt          m = A->rmap->n,*aj,*ii;
1403b434eb95SMatthew G. Knepley   PetscInt          n,i,*ridx=NULL;
1404b434eb95SMatthew G. Knepley   PetscScalar       sum;
1405b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1406b434eb95SMatthew G. Knepley 
1407b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1408b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1409d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1410b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1411b434eb95SMatthew G. Knepley     if (zz != yy) {
1412b434eb95SMatthew G. Knepley       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1413b434eb95SMatthew G. Knepley     }
1414b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1415b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1416b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1417b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1418b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1419b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1420b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1421b434eb95SMatthew G. Knepley       sum = y[*ridx];
1422b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1423b434eb95SMatthew G. Knepley       z[*ridx++] = sum;
1424b434eb95SMatthew G. Knepley     }
1425b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
14263d3eaba7SBarry Smith     ii = a->i;
1427b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1428b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1429b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1430b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1431b434eb95SMatthew G. Knepley       sum = y[i];
1432b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1433b434eb95SMatthew G. Knepley       z[i] = sum;
1434b434eb95SMatthew G. Knepley     }
1435b434eb95SMatthew G. Knepley   }
1436b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1437b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1438d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1439b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1440b434eb95SMatthew G. Knepley }
1441b434eb95SMatthew G. Knepley 
1442c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1443dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
144417ab2063SBarry Smith {
1445416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1446f15663dcSBarry Smith   PetscScalar       *y,*z;
1447f15663dcSBarry Smith   const PetscScalar *x;
144854f21887SBarry Smith   const MatScalar   *aa;
1449dfbe8321SBarry Smith   PetscErrorCode    ierr;
1450d9ca1df4SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
1451d9ca1df4SBarry Smith   PetscInt          m = A->rmap->n,n,i;
1452362ced78SSatish Balay   PetscScalar       sum;
1453ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
14549ea0dfa2SSatish Balay 
14553a40ed3dSBarry Smith   PetscFunctionBegin;
1456f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1457d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
14584eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
14594eb6d288SHong Zhang     if (zz != yy) {
14604eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
14614eb6d288SHong Zhang     }
146297952fefSHong Zhang     m    = a->compressedrow.nrows;
146397952fefSHong Zhang     ii   = a->compressedrow.i;
146497952fefSHong Zhang     ridx = a->compressedrow.rindex;
146597952fefSHong Zhang     for (i=0; i<m; i++) {
146697952fefSHong Zhang       n   = ii[i+1] - ii[i];
146797952fefSHong Zhang       aj  = a->j + ii[i];
146897952fefSHong Zhang       aa  = a->a + ii[i];
146997952fefSHong Zhang       sum = y[*ridx];
1470f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
147197952fefSHong Zhang       z[*ridx++] = sum;
147297952fefSHong Zhang     }
147397952fefSHong Zhang   } else { /* do not use compressed row format */
14743d3eaba7SBarry Smith     ii = a->i;
1475f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
14763d3eaba7SBarry Smith     aj = a->j;
14773d3eaba7SBarry Smith     aa = a->a;
1478f15663dcSBarry Smith     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1479f15663dcSBarry Smith #else
148017ab2063SBarry Smith     for (i=0; i<m; i++) {
1481f15663dcSBarry Smith       n   = ii[i+1] - ii[i];
1482f15663dcSBarry Smith       aj  = a->j + ii[i];
1483f15663dcSBarry Smith       aa  = a->a + ii[i];
148417ab2063SBarry Smith       sum = y[i];
1485f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
148617ab2063SBarry Smith       z[i] = sum;
148717ab2063SBarry Smith     }
148802ab625aSSatish Balay #endif
1489f15663dcSBarry Smith   }
1490dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1491f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1492d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
14933a40ed3dSBarry Smith   PetscFunctionReturn(0);
149417ab2063SBarry Smith }
149517ab2063SBarry Smith 
149617ab2063SBarry Smith /*
149717ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
149817ab2063SBarry Smith */
1499dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
150017ab2063SBarry Smith {
1501416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15026849ba73SBarry Smith   PetscErrorCode ierr;
1503d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
150417ab2063SBarry Smith 
15053a40ed3dSBarry Smith   PetscFunctionBegin;
150609f38230SBarry Smith   if (!a->diag) {
1507785e854fSJed Brown     ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr);
15083bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr);
150909f38230SBarry Smith   }
1510d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
151109f38230SBarry Smith     a->diag[i] = a->i[i+1];
1512bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1513bfeeae90SHong Zhang       if (a->j[j] == i) {
151409f38230SBarry Smith         a->diag[i] = j;
151517ab2063SBarry Smith         break;
151617ab2063SBarry Smith       }
151717ab2063SBarry Smith     }
151817ab2063SBarry Smith   }
15193a40ed3dSBarry Smith   PetscFunctionReturn(0);
152017ab2063SBarry Smith }
152117ab2063SBarry Smith 
152261ecd0c6SBarry Smith PetscErrorCode MatShift_SeqAIJ(Mat A,PetscScalar v)
152361ecd0c6SBarry Smith {
152461ecd0c6SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
152561ecd0c6SBarry Smith   const PetscInt    *diag = (const PetscInt*)a->diag;
152661ecd0c6SBarry Smith   const PetscInt    *ii = (const PetscInt*) a->i;
152761ecd0c6SBarry Smith   PetscInt          i,*mdiag = NULL;
152861ecd0c6SBarry Smith   PetscErrorCode    ierr;
152961ecd0c6SBarry Smith   PetscInt          cnt = 0; /* how many diagonals are missing */
153061ecd0c6SBarry Smith 
153161ecd0c6SBarry Smith   PetscFunctionBegin;
153261ecd0c6SBarry Smith   if (!A->preallocated || !a->nz) {
153361ecd0c6SBarry Smith     ierr = MatSeqAIJSetPreallocation(A,1,NULL);CHKERRQ(ierr);
153461ecd0c6SBarry Smith     ierr = MatShift_Basic(A,v);CHKERRQ(ierr);
153561ecd0c6SBarry Smith     PetscFunctionReturn(0);
153661ecd0c6SBarry Smith   }
153761ecd0c6SBarry Smith 
153861ecd0c6SBarry Smith   if (a->diagonaldense) {
153961ecd0c6SBarry Smith     cnt = 0;
154061ecd0c6SBarry Smith   } else {
154161ecd0c6SBarry Smith     ierr = PetscCalloc1(A->rmap->n,&mdiag);CHKERRQ(ierr);
154261ecd0c6SBarry Smith     for (i=0; i<A->rmap->n; i++) {
154361ecd0c6SBarry Smith       if (diag[i] >= ii[i+1]) {
154461ecd0c6SBarry Smith         cnt++;
154561ecd0c6SBarry Smith         mdiag[i] = 1;
154661ecd0c6SBarry Smith       }
154761ecd0c6SBarry Smith     }
154861ecd0c6SBarry Smith   }
154961ecd0c6SBarry Smith   if (!cnt) {
155061ecd0c6SBarry Smith     ierr = MatShift_Basic(A,v);CHKERRQ(ierr);
155161ecd0c6SBarry Smith   } else {
1552b6f2aa54SBarry Smith     PetscScalar *olda = a->a;  /* preserve pointers to current matrix nonzeros structure and values */
1553b6f2aa54SBarry Smith     PetscInt    *oldj = a->j, *oldi = a->i;
155461ecd0c6SBarry Smith     PetscBool   singlemalloc = a->singlemalloc,free_a = a->free_a,free_ij = a->free_ij;
155561ecd0c6SBarry Smith 
155661ecd0c6SBarry Smith     a->a = NULL;
155761ecd0c6SBarry Smith     a->j = NULL;
155861ecd0c6SBarry Smith     a->i = NULL;
155961ecd0c6SBarry Smith     /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
156061ecd0c6SBarry Smith     for (i=0; i<A->rmap->n; i++) {
156161ecd0c6SBarry Smith       a->imax[i] += mdiag[i];
1562447d62f5SStefano Zampini       a->imax[i] = PetscMin(a->imax[i],A->cmap->n);
156361ecd0c6SBarry Smith     }
156461ecd0c6SBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,0,a->imax);CHKERRQ(ierr);
156561ecd0c6SBarry Smith 
156661ecd0c6SBarry Smith     /* copy old values into new matrix data structure */
156761ecd0c6SBarry Smith     for (i=0; i<A->rmap->n; i++) {
156861ecd0c6SBarry Smith       ierr = MatSetValues(A,1,&i,a->imax[i] - mdiag[i],&oldj[oldi[i]],&olda[oldi[i]],ADD_VALUES);CHKERRQ(ierr);
1569447d62f5SStefano Zampini       if (i < A->cmap->n) {
157061ecd0c6SBarry Smith         ierr = MatSetValue(A,i,i,v,ADD_VALUES);CHKERRQ(ierr);
157161ecd0c6SBarry Smith       }
1572447d62f5SStefano Zampini     }
157361ecd0c6SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
157461ecd0c6SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
157561ecd0c6SBarry Smith     if (singlemalloc) {
157661ecd0c6SBarry Smith       ierr = PetscFree3(olda,oldj,oldi);CHKERRQ(ierr);
157761ecd0c6SBarry Smith     } else {
157861ecd0c6SBarry Smith       if (free_a)  {ierr = PetscFree(olda);CHKERRQ(ierr);}
157961ecd0c6SBarry Smith       if (free_ij) {ierr = PetscFree(oldj);CHKERRQ(ierr);}
158061ecd0c6SBarry Smith       if (free_ij) {ierr = PetscFree(oldi);CHKERRQ(ierr);}
158161ecd0c6SBarry Smith     }
158261ecd0c6SBarry Smith   }
158361ecd0c6SBarry Smith   ierr = PetscFree(mdiag);CHKERRQ(ierr);
158461ecd0c6SBarry Smith   a->diagonaldense = PETSC_TRUE;
158561ecd0c6SBarry Smith   PetscFunctionReturn(0);
158661ecd0c6SBarry Smith }
158761ecd0c6SBarry Smith 
1588be5855fcSBarry Smith /*
1589be5855fcSBarry Smith      Checks for missing diagonals
1590be5855fcSBarry Smith */
1591ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1592be5855fcSBarry Smith {
1593be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15947734d3b5SMatthew G. Knepley   PetscInt       *diag,*ii = a->i,i;
1595994fe344SLisandro Dalcin   PetscErrorCode ierr;
1596be5855fcSBarry Smith 
1597be5855fcSBarry Smith   PetscFunctionBegin;
159809f38230SBarry Smith   *missing = PETSC_FALSE;
15997734d3b5SMatthew G. Knepley   if (A->rmap->n > 0 && !ii) {
160009f38230SBarry Smith     *missing = PETSC_TRUE;
160109f38230SBarry Smith     if (d) *d = 0;
1602994fe344SLisandro Dalcin     ierr = PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");CHKERRQ(ierr);
160309f38230SBarry Smith   } else {
1604f1e2ffcdSBarry Smith     diag = a->diag;
1605d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
16067734d3b5SMatthew G. Knepley       if (diag[i] >= ii[i+1]) {
160709f38230SBarry Smith         *missing = PETSC_TRUE;
160809f38230SBarry Smith         if (d) *d = i;
1609994fe344SLisandro Dalcin         ierr = PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);CHKERRQ(ierr);
1610358d2f5dSShri Abhyankar         break;
161109f38230SBarry Smith       }
1612be5855fcSBarry Smith     }
1613be5855fcSBarry Smith   }
1614be5855fcSBarry Smith   PetscFunctionReturn(0);
1615be5855fcSBarry Smith }
1616be5855fcSBarry Smith 
16170da83c2eSBarry Smith #include <petscblaslapack.h>
16180da83c2eSBarry Smith #include <petsc/private/kernels/blockinvert.h>
16190da83c2eSBarry Smith 
16200da83c2eSBarry Smith /*
16210da83c2eSBarry Smith     Note that values is allocated externally by the PC and then passed into this routine
16220da83c2eSBarry Smith */
16230da83c2eSBarry Smith PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag)
16240da83c2eSBarry Smith {
16250da83c2eSBarry Smith   PetscErrorCode  ierr;
16260da83c2eSBarry Smith   PetscInt        n = A->rmap->n, i, ncnt = 0, *indx,j,bsizemax = 0,*v_pivots;
16270da83c2eSBarry Smith   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;
16280da83c2eSBarry Smith   const PetscReal shift = 0.0;
16290da83c2eSBarry Smith   PetscInt        ipvt[5];
16300da83c2eSBarry Smith   PetscScalar     work[25],*v_work;
16310da83c2eSBarry Smith 
16320da83c2eSBarry Smith   PetscFunctionBegin;
16330da83c2eSBarry Smith   allowzeropivot = PetscNot(A->erroriffailure);
16340da83c2eSBarry Smith   for (i=0; i<nblocks; i++) ncnt += bsizes[i];
16350da83c2eSBarry Smith   if (ncnt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Total blocksizes %D doesn't match number matrix rows %D",ncnt,n);
16360da83c2eSBarry Smith   for (i=0; i<nblocks; i++) {
16370da83c2eSBarry Smith     bsizemax = PetscMax(bsizemax,bsizes[i]);
16380da83c2eSBarry Smith   }
16390da83c2eSBarry Smith   ierr = PetscMalloc1(bsizemax,&indx);CHKERRQ(ierr);
16400da83c2eSBarry Smith   if (bsizemax > 7) {
16410da83c2eSBarry Smith     ierr = PetscMalloc2(bsizemax,&v_work,bsizemax,&v_pivots);CHKERRQ(ierr);
16420da83c2eSBarry Smith   }
16430da83c2eSBarry Smith   ncnt = 0;
16440da83c2eSBarry Smith   for (i=0; i<nblocks; i++) {
16450da83c2eSBarry Smith     for (j=0; j<bsizes[i]; j++) indx[j] = ncnt+j;
16460da83c2eSBarry Smith     ierr    = MatGetValues(A,bsizes[i],indx,bsizes[i],indx,diag);CHKERRQ(ierr);
16470da83c2eSBarry Smith     switch (bsizes[i]) {
16480da83c2eSBarry Smith     case 1:
16490da83c2eSBarry Smith       *diag = 1.0/(*diag);
16500da83c2eSBarry Smith       break;
16510da83c2eSBarry Smith     case 2:
16520da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16530da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16540da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
16550da83c2eSBarry Smith       break;
16560da83c2eSBarry Smith     case 3:
16570da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16580da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16590da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
16600da83c2eSBarry Smith       break;
16610da83c2eSBarry Smith     case 4:
16620da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16630da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16640da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
16650da83c2eSBarry Smith       break;
16660da83c2eSBarry Smith     case 5:
16670da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16680da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16690da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
16700da83c2eSBarry Smith       break;
16710da83c2eSBarry Smith     case 6:
16720da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16730da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16740da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
16750da83c2eSBarry Smith       break;
16760da83c2eSBarry Smith     case 7:
16770da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16780da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16790da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
16800da83c2eSBarry Smith       break;
16810da83c2eSBarry Smith     default:
16820da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A(bsizes[i],diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16830da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16840da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bsizes[i]);CHKERRQ(ierr);
16850da83c2eSBarry Smith     }
16860da83c2eSBarry Smith     ncnt   += bsizes[i];
16870da83c2eSBarry Smith     diag += bsizes[i]*bsizes[i];
16880da83c2eSBarry Smith   }
16890da83c2eSBarry Smith   if (bsizemax > 7) {
16900da83c2eSBarry Smith     ierr = PetscFree2(v_work,v_pivots);CHKERRQ(ierr);
16910da83c2eSBarry Smith   }
16920da83c2eSBarry Smith   ierr = PetscFree(indx);CHKERRQ(ierr);
16930da83c2eSBarry Smith   PetscFunctionReturn(0);
16940da83c2eSBarry Smith }
16950da83c2eSBarry Smith 
1696422a814eSBarry Smith /*
1697422a814eSBarry Smith    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1698422a814eSBarry Smith */
16997087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
170071f1c65dSBarry Smith {
170171f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
170271f1c65dSBarry Smith   PetscErrorCode ierr;
1703d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
170454f21887SBarry Smith   MatScalar      *v = a->a;
170554f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
170671f1c65dSBarry Smith 
170771f1c65dSBarry Smith   PetscFunctionBegin;
170871f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
170971f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
171071f1c65dSBarry Smith   diag = a->diag;
171171f1c65dSBarry Smith   if (!a->idiag) {
1712dcca6d9dSJed Brown     ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr);
17133bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
171471f1c65dSBarry Smith     v    = a->a;
171571f1c65dSBarry Smith   }
171671f1c65dSBarry Smith   mdiag = a->mdiag;
171771f1c65dSBarry Smith   idiag = a->idiag;
171871f1c65dSBarry Smith 
1719422a814eSBarry Smith   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
172071f1c65dSBarry Smith     for (i=0; i<m; i++) {
172171f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1722899639b0SHong Zhang       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1723899639b0SHong Zhang         if (PetscRealPart(fshift)) {
1724899639b0SHong Zhang           ierr = PetscInfo1(A,"Zero diagonal on row %D\n",i);CHKERRQ(ierr);
17257b6c816cSBarry Smith           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
17267b6c816cSBarry Smith           A->factorerror_zeropivot_value = 0.0;
17277b6c816cSBarry Smith           A->factorerror_zeropivot_row   = i;
1728a6fa060aSHong Zhang         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1729899639b0SHong Zhang       }
173071f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
173171f1c65dSBarry Smith     }
173271f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
173371f1c65dSBarry Smith   } else {
173471f1c65dSBarry Smith     for (i=0; i<m; i++) {
173571f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
173671f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
173771f1c65dSBarry Smith     }
1738dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
173971f1c65dSBarry Smith   }
174071f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
174171f1c65dSBarry Smith   PetscFunctionReturn(0);
174271f1c65dSBarry Smith }
174371f1c65dSBarry Smith 
1744c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
174541f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
174617ab2063SBarry Smith {
1747416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1748e6d1f457SBarry Smith   PetscScalar       *x,d,sum,*t,scale;
17493d3eaba7SBarry Smith   const MatScalar   *v,*idiag=0,*mdiag;
175054f21887SBarry Smith   const PetscScalar *b, *bs,*xb, *ts;
1751dfbe8321SBarry Smith   PetscErrorCode    ierr;
17523d3eaba7SBarry Smith   PetscInt          n,m = A->rmap->n,i;
175397f1f81fSBarry Smith   const PetscInt    *idx,*diag;
175417ab2063SBarry Smith 
17553a40ed3dSBarry Smith   PetscFunctionBegin;
1756b965ef7fSBarry Smith   its = its*lits;
175791723122SBarry Smith 
175871f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
175971f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
176071f1c65dSBarry Smith   a->fshift = fshift;
176171f1c65dSBarry Smith   a->omega  = omega;
1762ed480e8bSBarry Smith 
176371f1c65dSBarry Smith   diag  = a->diag;
176471f1c65dSBarry Smith   t     = a->ssor_work;
1765ed480e8bSBarry Smith   idiag = a->idiag;
176671f1c65dSBarry Smith   mdiag = a->mdiag;
1767ed480e8bSBarry Smith 
17681ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
17693649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1770ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
177117ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
177217ab2063SBarry Smith     /* apply (U + D/omega) to the vector */
1773ed480e8bSBarry Smith     bs = b;
177417ab2063SBarry Smith     for (i=0; i<m; i++) {
177571f1c65dSBarry Smith       d   = fshift + mdiag[i];
1776416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1777ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1778ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
177917ab2063SBarry Smith       sum = b[i]*d/omega;
1780003131ecSBarry Smith       PetscSparseDensePlusDot(sum,bs,v,idx,n);
178117ab2063SBarry Smith       x[i] = sum;
178217ab2063SBarry Smith     }
17831ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
17843649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1785efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17863a40ed3dSBarry Smith     PetscFunctionReturn(0);
178717ab2063SBarry Smith   }
1788c783ea89SBarry Smith 
17892205254eSKarl Rupp   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
17902205254eSKarl Rupp   else if (flag & SOR_EISENSTAT) {
179117ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1792887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
179317ab2063SBarry Smith 
179417ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
179517ab2063SBarry Smith 
1796887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
179717ab2063SBarry Smith     */
179817ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
179917ab2063SBarry Smith 
180017ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
180117ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1802416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1803ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1804ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
180517ab2063SBarry Smith       sum = b[i];
1806e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1807ed480e8bSBarry Smith       x[i] = sum*idiag[i];
180817ab2063SBarry Smith     }
180917ab2063SBarry Smith 
181017ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1811416022c9SBarry Smith     v = a->a;
18122205254eSKarl Rupp     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
181317ab2063SBarry Smith 
181417ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1815ed480e8bSBarry Smith     ts   = t;
1816416022c9SBarry Smith     diag = a->diag;
181717ab2063SBarry Smith     for (i=0; i<m; i++) {
1818416022c9SBarry Smith       n   = diag[i] - a->i[i];
1819ed480e8bSBarry Smith       idx = a->j + a->i[i];
1820ed480e8bSBarry Smith       v   = a->a + a->i[i];
182117ab2063SBarry Smith       sum = t[i];
1822003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1823ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1824733d66baSBarry Smith       /*  x = x + t */
1825733d66baSBarry Smith       x[i] += t[i];
182617ab2063SBarry Smith     }
182717ab2063SBarry Smith 
1828dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
18291ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
18303649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
18313a40ed3dSBarry Smith     PetscFunctionReturn(0);
183217ab2063SBarry Smith   }
183317ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
183417ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
183517ab2063SBarry Smith       for (i=0; i<m; i++) {
1836416022c9SBarry Smith         n   = diag[i] - a->i[i];
1837ed480e8bSBarry Smith         idx = a->j + a->i[i];
1838ed480e8bSBarry Smith         v   = a->a + a->i[i];
183917ab2063SBarry Smith         sum = b[i];
1840e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
18415c99c7daSBarry Smith         t[i] = sum;
1842ed480e8bSBarry Smith         x[i] = sum*idiag[i];
184317ab2063SBarry Smith       }
18445c99c7daSBarry Smith       xb   = t;
1845efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18463a40ed3dSBarry Smith     } else xb = b;
184717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
184817ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1849416022c9SBarry Smith         n   = a->i[i+1] - diag[i] - 1;
1850ed480e8bSBarry Smith         idx = a->j + diag[i] + 1;
1851ed480e8bSBarry Smith         v   = a->a + diag[i] + 1;
185217ab2063SBarry Smith         sum = xb[i];
1853e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
18545c99c7daSBarry Smith         if (xb == b) {
1855ed480e8bSBarry Smith           x[i] = sum*idiag[i];
18565c99c7daSBarry Smith         } else {
1857b19a5dc2SMark Adams           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
185817ab2063SBarry Smith         }
18595c99c7daSBarry Smith       }
1860b19a5dc2SMark Adams       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
186117ab2063SBarry Smith     }
186217ab2063SBarry Smith     its--;
186317ab2063SBarry Smith   }
186417ab2063SBarry Smith   while (its--) {
186517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
186617ab2063SBarry Smith       for (i=0; i<m; i++) {
1867b19a5dc2SMark Adams         /* lower */
1868b19a5dc2SMark Adams         n   = diag[i] - a->i[i];
1869ed480e8bSBarry Smith         idx = a->j + a->i[i];
1870ed480e8bSBarry Smith         v   = a->a + a->i[i];
187117ab2063SBarry Smith         sum = b[i];
1872e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1873b19a5dc2SMark Adams         t[i] = sum;             /* save application of the lower-triangular part */
1874b19a5dc2SMark Adams         /* upper */
1875b19a5dc2SMark Adams         n   = a->i[i+1] - diag[i] - 1;
1876b19a5dc2SMark Adams         idx = a->j + diag[i] + 1;
1877b19a5dc2SMark Adams         v   = a->a + diag[i] + 1;
1878b19a5dc2SMark Adams         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1879b19a5dc2SMark Adams         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
188017ab2063SBarry Smith       }
1881b19a5dc2SMark Adams       xb   = t;
18829f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1883b19a5dc2SMark Adams     } else xb = b;
188417ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
188517ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1886b19a5dc2SMark Adams         sum = xb[i];
1887b19a5dc2SMark Adams         if (xb == b) {
1888b19a5dc2SMark Adams           /* whole matrix (no checkpointing available) */
1889416022c9SBarry Smith           n   = a->i[i+1] - a->i[i];
1890ed480e8bSBarry Smith           idx = a->j + a->i[i];
1891ed480e8bSBarry Smith           v   = a->a + a->i[i];
1892e6d1f457SBarry Smith           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1893ed480e8bSBarry Smith           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1894b19a5dc2SMark Adams         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1895b19a5dc2SMark Adams           n   = a->i[i+1] - diag[i] - 1;
1896b19a5dc2SMark Adams           idx = a->j + diag[i] + 1;
1897b19a5dc2SMark Adams           v   = a->a + diag[i] + 1;
1898b19a5dc2SMark Adams           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1899b19a5dc2SMark Adams           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
190017ab2063SBarry Smith         }
1901b19a5dc2SMark Adams       }
1902b19a5dc2SMark Adams       if (xb == b) {
19039f863219SBarry Smith         ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1904b19a5dc2SMark Adams       } else {
1905b19a5dc2SMark Adams         ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1906b19a5dc2SMark Adams       }
190717ab2063SBarry Smith     }
190817ab2063SBarry Smith   }
19091ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
19103649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1911365a8a9eSBarry Smith   PetscFunctionReturn(0);
191217ab2063SBarry Smith }
191317ab2063SBarry Smith 
19142af78befSBarry Smith 
1915dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
191617ab2063SBarry Smith {
1917416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
19184e220ebcSLois Curfman McInnes 
19193a40ed3dSBarry Smith   PetscFunctionBegin;
19204e220ebcSLois Curfman McInnes   info->block_size   = 1.0;
19214e220ebcSLois Curfman McInnes   info->nz_allocated = (double)a->maxnz;
19224e220ebcSLois Curfman McInnes   info->nz_used      = (double)a->nz;
19234e220ebcSLois Curfman McInnes   info->nz_unneeded  = (double)(a->maxnz - a->nz);
19244e220ebcSLois Curfman McInnes   info->assemblies   = (double)A->num_ass;
19258e58a170SBarry Smith   info->mallocs      = (double)A->info.mallocs;
19267adad957SLisandro Dalcin   info->memory       = ((PetscObject)A)->mem;
1927d5f3da31SBarry Smith   if (A->factortype) {
19284e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
19294e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
19304e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
19314e220ebcSLois Curfman McInnes   } else {
19324e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
19334e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
19344e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
19354e220ebcSLois Curfman McInnes   }
19363a40ed3dSBarry Smith   PetscFunctionReturn(0);
193717ab2063SBarry Smith }
193817ab2063SBarry Smith 
19392b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
194017ab2063SBarry Smith {
1941416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1942c7da8527SEric Chamberland   PetscInt          i,m = A->rmap->n - 1;
19436849ba73SBarry Smith   PetscErrorCode    ierr;
194497b48c8fSBarry Smith   const PetscScalar *xx;
194597b48c8fSBarry Smith   PetscScalar       *bb;
1946c7da8527SEric Chamberland   PetscInt          d = 0;
194717ab2063SBarry Smith 
19483a40ed3dSBarry Smith   PetscFunctionBegin;
194997b48c8fSBarry Smith   if (x && b) {
195097b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
195197b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
195297b48c8fSBarry Smith     for (i=0; i<N; i++) {
195397b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1954447d62f5SStefano Zampini       if (rows[i] >= A->cmap->n) continue;
195597b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
195697b48c8fSBarry Smith     }
195797b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
195897b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
195997b48c8fSBarry Smith   }
196097b48c8fSBarry Smith 
1961a9817697SBarry Smith   if (a->keepnonzeropattern) {
1962f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1963e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1964bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1965f1e2ffcdSBarry Smith     }
1966f4df32b1SMatthew Knepley     if (diag != 0.0) {
1967c7da8527SEric Chamberland       for (i=0; i<N; i++) {
1968c7da8527SEric Chamberland         d = rows[i];
1969447d62f5SStefano Zampini         if (rows[i] >= A->cmap->n) continue;
1970c7da8527SEric Chamberland         if (a->diag[d] >= a->i[d+1]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in the zeroed row %D",d);
1971c7da8527SEric Chamberland       }
1972f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1973447d62f5SStefano Zampini         if (rows[i] >= A->cmap->n) continue;
1974f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1975f1e2ffcdSBarry Smith       }
1976f1e2ffcdSBarry Smith     }
1977f1e2ffcdSBarry Smith   } else {
1978f4df32b1SMatthew Knepley     if (diag != 0.0) {
197917ab2063SBarry Smith       for (i=0; i<N; i++) {
1980e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
19817ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1982447d62f5SStefano Zampini 	  if (rows[i] >= A->cmap->n) {
1983447d62f5SStefano Zampini             a->ilen[rows[i]] = 0;
1984447d62f5SStefano Zampini           } else {
1985416022c9SBarry Smith             a->ilen[rows[i]]    = 1;
1986f4df32b1SMatthew Knepley             a->a[a->i[rows[i]]] = diag;
1987bfeeae90SHong Zhang             a->j[a->i[rows[i]]] = rows[i];
1988447d62f5SStefano Zampini           }
1989447d62f5SStefano Zampini         } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
1990f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
199117ab2063SBarry Smith         }
199217ab2063SBarry Smith       }
19933a40ed3dSBarry Smith     } else {
199417ab2063SBarry Smith       for (i=0; i<N; i++) {
1995e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1996416022c9SBarry Smith         a->ilen[rows[i]] = 0;
199717ab2063SBarry Smith       }
199817ab2063SBarry Smith     }
1999e56f5c9eSBarry Smith     A->nonzerostate++;
2000f1e2ffcdSBarry Smith   }
20014099cc6bSBarry Smith   ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20023a40ed3dSBarry Smith   PetscFunctionReturn(0);
200317ab2063SBarry Smith }
200417ab2063SBarry Smith 
20056e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
20066e169961SBarry Smith {
20076e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
20086e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
20096e169961SBarry Smith   PetscErrorCode    ierr;
20102b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
20116e169961SBarry Smith   const PetscScalar *xx;
20126e169961SBarry Smith   PetscScalar       *bb;
20136e169961SBarry Smith 
20146e169961SBarry Smith   PetscFunctionBegin;
20156e169961SBarry Smith   if (x && b) {
20166e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
20176e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
20182b40b63fSBarry Smith     vecs = PETSC_TRUE;
20196e169961SBarry Smith   }
20201795a4d1SJed Brown   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
20216e169961SBarry Smith   for (i=0; i<N; i++) {
20226e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
20236e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
20242205254eSKarl Rupp 
20256e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
20266e169961SBarry Smith   }
20276e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
20286e169961SBarry Smith     if (!zeroed[i]) {
20296e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
20304cf107fdSStefano Zampini         if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
20312b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
20326e169961SBarry Smith           a->a[j] = 0.0;
20336e169961SBarry Smith         }
20346e169961SBarry Smith       }
20354cf107fdSStefano Zampini     } else if (vecs && i < A->cmap->N) bb[i] = diag*xx[i];
20366e169961SBarry Smith   }
20376e169961SBarry Smith   if (x && b) {
20386e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
20396e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
20406e169961SBarry Smith   }
20416e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
20426e169961SBarry Smith   if (diag != 0.0) {
20436e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
20441d5a398dSstefano_zampini     if (missing) {
20451d5a398dSstefano_zampini       for (i=0; i<N; i++) {
20464cf107fdSStefano Zampini         if (rows[i] >= A->cmap->N) continue;
20474cf107fdSStefano Zampini         if (a->nonew && rows[i] >= d) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D (%D)",d,rows[i]);
20481d5a398dSstefano_zampini         ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
20491d5a398dSstefano_zampini       }
20501d5a398dSstefano_zampini     } else {
20516e169961SBarry Smith       for (i=0; i<N; i++) {
20526e169961SBarry Smith         a->a[a->diag[rows[i]]] = diag;
20536e169961SBarry Smith       }
20546e169961SBarry Smith     }
20551d5a398dSstefano_zampini   }
20564099cc6bSBarry Smith   ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20576e169961SBarry Smith   PetscFunctionReturn(0);
20586e169961SBarry Smith }
20596e169961SBarry Smith 
2060a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
206117ab2063SBarry Smith {
2062416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
206397f1f81fSBarry Smith   PetscInt   *itmp;
206417ab2063SBarry Smith 
20653a40ed3dSBarry Smith   PetscFunctionBegin;
2066e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
206717ab2063SBarry Smith 
2068416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
2069bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
207017ab2063SBarry Smith   if (idx) {
2071bfeeae90SHong Zhang     itmp = a->j + a->i[row];
207226fbe8dcSKarl Rupp     if (*nz) *idx = itmp;
207317ab2063SBarry Smith     else *idx = 0;
207417ab2063SBarry Smith   }
20753a40ed3dSBarry Smith   PetscFunctionReturn(0);
207617ab2063SBarry Smith }
207717ab2063SBarry Smith 
2078bfeeae90SHong Zhang /* remove this function? */
2079a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
208017ab2063SBarry Smith {
20813a40ed3dSBarry Smith   PetscFunctionBegin;
20823a40ed3dSBarry Smith   PetscFunctionReturn(0);
208317ab2063SBarry Smith }
208417ab2063SBarry Smith 
2085dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
208617ab2063SBarry Smith {
2087416022c9SBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
208854f21887SBarry Smith   MatScalar      *v  = a->a;
208936db0b34SBarry Smith   PetscReal      sum = 0.0;
20906849ba73SBarry Smith   PetscErrorCode ierr;
209197f1f81fSBarry Smith   PetscInt       i,j;
209217ab2063SBarry Smith 
20933a40ed3dSBarry Smith   PetscFunctionBegin;
209417ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
2095570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FP16)
2096570b7f6dSBarry Smith     PetscBLASInt one = 1,nz = a->nz;
2097570b7f6dSBarry Smith     *nrm = BLASnrm2_(&nz,v,&one);
2098570b7f6dSBarry Smith #else
2099416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
210036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
210117ab2063SBarry Smith     }
21028f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
2103570b7f6dSBarry Smith #endif
210451f70360SJed Brown     ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
21053a40ed3dSBarry Smith   } else if (type == NORM_1) {
210636db0b34SBarry Smith     PetscReal *tmp;
210797f1f81fSBarry Smith     PetscInt  *jj = a->j;
21081795a4d1SJed Brown     ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr);
2109064f8208SBarry Smith     *nrm = 0.0;
2110416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
2111bfeeae90SHong Zhang       tmp[*jj++] += PetscAbsScalar(*v);  v++;
211217ab2063SBarry Smith     }
2113d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
2114064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
211517ab2063SBarry Smith     }
2116606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
211751f70360SJed Brown     ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr);
21183a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
2119064f8208SBarry Smith     *nrm = 0.0;
2120d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
2121bfeeae90SHong Zhang       v   = a->a + a->i[j];
212217ab2063SBarry Smith       sum = 0.0;
2123416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2124cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
212517ab2063SBarry Smith       }
2126064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
212717ab2063SBarry Smith     }
212851f70360SJed Brown     ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr);
2129f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
21303a40ed3dSBarry Smith   PetscFunctionReturn(0);
213117ab2063SBarry Smith }
213217ab2063SBarry Smith 
21334e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
21344e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
21354e938277SHong Zhang {
21364e938277SHong Zhang   PetscErrorCode ierr;
21374e938277SHong Zhang   PetscInt       i,j,anzj;
21384e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
21394e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
21404e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
21414e938277SHong Zhang 
21424e938277SHong Zhang   PetscFunctionBegin;
21434e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
2144854ce69bSBarry Smith   ierr = PetscCalloc1(an+1,&ati);CHKERRQ(ierr);
2145785e854fSJed Brown   ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr);
2146785e854fSJed Brown   ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr);
21474e938277SHong Zhang 
21484e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
21494e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
215026fbe8dcSKarl Rupp   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
21514e938277SHong Zhang   /* Form ati for csr format of A^T. */
215226fbe8dcSKarl Rupp   for (i=0;i<an;i++) ati[i+1] += ati[i];
21534e938277SHong Zhang 
21544e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
21554e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
21564e938277SHong Zhang 
21574e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
21584e938277SHong Zhang   for (i=0;i<am;i++) {
21594e938277SHong Zhang     anzj = ai[i+1] - ai[i];
21604e938277SHong Zhang     for (j=0;j<anzj;j++) {
21614e938277SHong Zhang       atj[atfill[*aj]] = i;
21624e938277SHong Zhang       atfill[*aj++]   += 1;
21634e938277SHong Zhang     }
21644e938277SHong Zhang   }
21654e938277SHong Zhang 
21664e938277SHong Zhang   /* Clean up temporary space and complete requests. */
21674e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
2168ce94432eSBarry Smith   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
216933d57670SJed Brown   ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
2170a2f3521dSMark F. Adams 
21714e938277SHong Zhang   b          = (Mat_SeqAIJ*)((*B)->data);
21724e938277SHong Zhang   b->free_a  = PETSC_FALSE;
21734e938277SHong Zhang   b->free_ij = PETSC_TRUE;
21744e938277SHong Zhang   b->nonew   = 0;
21754e938277SHong Zhang   PetscFunctionReturn(0);
21764e938277SHong Zhang }
21774e938277SHong Zhang 
21787087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2179cd0d46ebSvictorle {
21803d3eaba7SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
218154f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
218254f21887SBarry Smith   MatScalar      *va,*vb;
21836849ba73SBarry Smith   PetscErrorCode ierr;
218497f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
2185cd0d46ebSvictorle 
2186cd0d46ebSvictorle   PetscFunctionBegin;
2187cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2188cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
21895485867bSBarry Smith   if (ma!=nb || na!=mb) {
21905485867bSBarry Smith     *f = PETSC_FALSE;
21915485867bSBarry Smith     PetscFunctionReturn(0);
21925485867bSBarry Smith   }
2193cd0d46ebSvictorle   aii  = aij->i; bii = bij->i;
2194cd0d46ebSvictorle   adx  = aij->j; bdx = bij->j;
2195cd0d46ebSvictorle   va   = aij->a; vb = bij->a;
2196785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2197785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
2198cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
2199cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
2200cd0d46ebSvictorle 
2201cd0d46ebSvictorle   *f = PETSC_TRUE;
2202cd0d46ebSvictorle   for (i=0; i<ma; i++) {
2203cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
220497f1f81fSBarry Smith       PetscInt    idc,idr;
22055485867bSBarry Smith       PetscScalar vc,vr;
2206cd0d46ebSvictorle       /* column/row index/value */
22075485867bSBarry Smith       idc = adx[aptr[i]];
22085485867bSBarry Smith       idr = bdx[bptr[idc]];
22095485867bSBarry Smith       vc  = va[aptr[i]];
22105485867bSBarry Smith       vr  = vb[bptr[idc]];
22115485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
22125485867bSBarry Smith         *f = PETSC_FALSE;
22135485867bSBarry Smith         goto done;
2214cd0d46ebSvictorle       } else {
22155485867bSBarry Smith         aptr[i]++;
22165485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
2217cd0d46ebSvictorle       }
2218cd0d46ebSvictorle     }
2219cd0d46ebSvictorle   }
2220cd0d46ebSvictorle done:
2221cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
22223aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
2223cd0d46ebSvictorle   PetscFunctionReturn(0);
2224cd0d46ebSvictorle }
2225cd0d46ebSvictorle 
22267087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
22271cbb95d3SBarry Smith {
22283d3eaba7SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
222954f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
223054f21887SBarry Smith   MatScalar      *va,*vb;
22311cbb95d3SBarry Smith   PetscErrorCode ierr;
22321cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
22331cbb95d3SBarry Smith 
22341cbb95d3SBarry Smith   PetscFunctionBegin;
22351cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
22361cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
22371cbb95d3SBarry Smith   if (ma!=nb || na!=mb) {
22381cbb95d3SBarry Smith     *f = PETSC_FALSE;
22391cbb95d3SBarry Smith     PetscFunctionReturn(0);
22401cbb95d3SBarry Smith   }
22411cbb95d3SBarry Smith   aii  = aij->i; bii = bij->i;
22421cbb95d3SBarry Smith   adx  = aij->j; bdx = bij->j;
22431cbb95d3SBarry Smith   va   = aij->a; vb = bij->a;
2244785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2245785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
22461cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
22471cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
22481cbb95d3SBarry Smith 
22491cbb95d3SBarry Smith   *f = PETSC_TRUE;
22501cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
22511cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
22521cbb95d3SBarry Smith       PetscInt    idc,idr;
22531cbb95d3SBarry Smith       PetscScalar vc,vr;
22541cbb95d3SBarry Smith       /* column/row index/value */
22551cbb95d3SBarry Smith       idc = adx[aptr[i]];
22561cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
22571cbb95d3SBarry Smith       vc  = va[aptr[i]];
22581cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
22591cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
22601cbb95d3SBarry Smith         *f = PETSC_FALSE;
22611cbb95d3SBarry Smith         goto done;
22621cbb95d3SBarry Smith       } else {
22631cbb95d3SBarry Smith         aptr[i]++;
22641cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
22651cbb95d3SBarry Smith       }
22661cbb95d3SBarry Smith     }
22671cbb95d3SBarry Smith   }
22681cbb95d3SBarry Smith done:
22691cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
22701cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
22711cbb95d3SBarry Smith   PetscFunctionReturn(0);
22721cbb95d3SBarry Smith }
22731cbb95d3SBarry Smith 
2274ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
22759e29f15eSvictorle {
2276dfbe8321SBarry Smith   PetscErrorCode ierr;
22776e111a19SKarl Rupp 
22789e29f15eSvictorle   PetscFunctionBegin;
22795485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
22809e29f15eSvictorle   PetscFunctionReturn(0);
22819e29f15eSvictorle }
22829e29f15eSvictorle 
2283ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
22841cbb95d3SBarry Smith {
22851cbb95d3SBarry Smith   PetscErrorCode ierr;
22866e111a19SKarl Rupp 
22871cbb95d3SBarry Smith   PetscFunctionBegin;
22881cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
22891cbb95d3SBarry Smith   PetscFunctionReturn(0);
22901cbb95d3SBarry Smith }
22911cbb95d3SBarry Smith 
2292dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
229317ab2063SBarry Smith {
2294416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2295fff8e43fSBarry Smith   const PetscScalar *l,*r;
2296fff8e43fSBarry Smith   PetscScalar       x;
229754f21887SBarry Smith   MatScalar         *v;
2298dfbe8321SBarry Smith   PetscErrorCode    ierr;
2299fff8e43fSBarry Smith   PetscInt          i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz;
2300fff8e43fSBarry Smith   const PetscInt    *jj;
230117ab2063SBarry Smith 
23023a40ed3dSBarry Smith   PetscFunctionBegin;
230317ab2063SBarry Smith   if (ll) {
23043ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
23053ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2306e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2307e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
2308fff8e43fSBarry Smith     ierr = VecGetArrayRead(ll,&l);CHKERRQ(ierr);
2309416022c9SBarry Smith     v    = a->a;
231017ab2063SBarry Smith     for (i=0; i<m; i++) {
231117ab2063SBarry Smith       x = l[i];
2312416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
23132205254eSKarl Rupp       for (j=0; j<M; j++) (*v++) *= x;
231417ab2063SBarry Smith     }
2315fff8e43fSBarry Smith     ierr = VecRestoreArrayRead(ll,&l);CHKERRQ(ierr);
2316efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
231717ab2063SBarry Smith   }
231817ab2063SBarry Smith   if (rr) {
2319e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2320e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
2321fff8e43fSBarry Smith     ierr = VecGetArrayRead(rr,&r);CHKERRQ(ierr);
2322416022c9SBarry Smith     v    = a->a; jj = a->j;
23232205254eSKarl Rupp     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
2324fff8e43fSBarry Smith     ierr = VecRestoreArrayRead(rr,&r);CHKERRQ(ierr);
2325efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
232617ab2063SBarry Smith   }
2327acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
23283a40ed3dSBarry Smith   PetscFunctionReturn(0);
232917ab2063SBarry Smith }
233017ab2063SBarry Smith 
23317dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
233217ab2063SBarry Smith {
2333db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
23346849ba73SBarry Smith   PetscErrorCode ierr;
2335d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
233697f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
23375d0c19d7SBarry Smith   const PetscInt *irow,*icol;
23385d0c19d7SBarry Smith   PetscInt       nrows,ncols;
233997f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
234054f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2341416022c9SBarry Smith   Mat            C;
2342cdc6f3adSToby Isaac   PetscBool      stride;
234317ab2063SBarry Smith 
23443a40ed3dSBarry Smith   PetscFunctionBegin;
234599141d43SSatish Balay 
234617ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2347b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2348b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
234917ab2063SBarry Smith 
2350251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2351ff718158SBarry Smith   if (stride) {
2352ff718158SBarry Smith     ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2353ff718158SBarry Smith   } else {
2354ff718158SBarry Smith     first = 0;
2355ff718158SBarry Smith     step  = 0;
2356ff718158SBarry Smith   }
2357fee21e36SBarry Smith   if (stride && step == 1) {
235802834360SBarry Smith     /* special case of contiguous rows */
2359dcca6d9dSJed Brown     ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr);
236002834360SBarry Smith     /* loop over new rows determining lens and starting points */
236102834360SBarry Smith     for (i=0; i<nrows; i++) {
2362bfeeae90SHong Zhang       kstart = ai[irow[i]];
2363a2744918SBarry Smith       kend   = kstart + ailen[irow[i]];
2364a91a9bebSLisandro Dalcin       starts[i] = kstart;
236502834360SBarry Smith       for (k=kstart; k<kend; k++) {
2366bfeeae90SHong Zhang         if (aj[k] >= first) {
236702834360SBarry Smith           starts[i] = k;
236802834360SBarry Smith           break;
236902834360SBarry Smith         }
237002834360SBarry Smith       }
2371a2744918SBarry Smith       sum = 0;
237202834360SBarry Smith       while (k < kend) {
2373bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2374a2744918SBarry Smith         sum++;
237502834360SBarry Smith       }
2376a2744918SBarry Smith       lens[i] = sum;
237702834360SBarry Smith     }
237802834360SBarry Smith     /* create submatrix */
2379cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
238097f1f81fSBarry Smith       PetscInt n_cols,n_rows;
238108480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2382e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2383d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
238408480c60SBarry Smith       C    = *B;
23853a40ed3dSBarry Smith     } else {
23863bef6203SJed Brown       PetscInt rbs,cbs;
2387ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2388f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
23893bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
23903bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
23913bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
23927adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2393ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
239408480c60SBarry Smith     }
2395db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2396db02288aSLois Curfman McInnes 
239702834360SBarry Smith     /* loop over rows inserting into submatrix */
2398db02288aSLois Curfman McInnes     a_new = c->a;
2399db02288aSLois Curfman McInnes     j_new = c->j;
2400db02288aSLois Curfman McInnes     i_new = c->i;
2401bfeeae90SHong Zhang 
240202834360SBarry Smith     for (i=0; i<nrows; i++) {
2403a2744918SBarry Smith       ii    = starts[i];
2404a2744918SBarry Smith       lensi = lens[i];
2405a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2406a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
240702834360SBarry Smith       }
240887828ca2SBarry Smith       ierr       = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2409a2744918SBarry Smith       a_new     += lensi;
2410a2744918SBarry Smith       i_new[i+1] = i_new[i] + lensi;
2411a2744918SBarry Smith       c->ilen[i] = lensi;
241202834360SBarry Smith     }
24130e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
24143a40ed3dSBarry Smith   } else {
241502834360SBarry Smith     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
24161795a4d1SJed Brown     ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr);
2417854ce69bSBarry Smith     ierr = PetscMalloc1(1+nrows,&lens);CHKERRQ(ierr);
24184dcab191SBarry Smith     for (i=0; i<ncols; i++) {
24194dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
24204dcab191SBarry 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);
24214dcab191SBarry Smith #endif
24224dcab191SBarry Smith       smap[icol[i]] = i+1;
24234dcab191SBarry Smith     }
24244dcab191SBarry Smith 
242502834360SBarry Smith     /* determine lens of each row */
242602834360SBarry Smith     for (i=0; i<nrows; i++) {
2427bfeeae90SHong Zhang       kstart  = ai[irow[i]];
242802834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
242902834360SBarry Smith       lens[i] = 0;
243002834360SBarry Smith       for (k=kstart; k<kend; k++) {
2431bfeeae90SHong Zhang         if (smap[aj[k]]) {
243202834360SBarry Smith           lens[i]++;
243302834360SBarry Smith         }
243402834360SBarry Smith       }
243502834360SBarry Smith     }
243617ab2063SBarry Smith     /* Create and fill new matrix */
2437a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2438ace3abfcSBarry Smith       PetscBool equal;
24390f5bd95cSBarry Smith 
244099141d43SSatish Balay       c = (Mat_SeqAIJ*)((*B)->data);
2441e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2442d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
2443f23aa3ddSBarry Smith       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2444d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
244508480c60SBarry Smith       C    = *B;
24463a40ed3dSBarry Smith     } else {
24473bef6203SJed Brown       PetscInt rbs,cbs;
2448ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2449f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
24503bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
24513bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
24523bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
24537adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2454ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
245508480c60SBarry Smith     }
245699141d43SSatish Balay     c = (Mat_SeqAIJ*)(C->data);
245717ab2063SBarry Smith     for (i=0; i<nrows; i++) {
245899141d43SSatish Balay       row      = irow[i];
2459bfeeae90SHong Zhang       kstart   = ai[row];
246099141d43SSatish Balay       kend     = kstart + a->ilen[row];
2461bfeeae90SHong Zhang       mat_i    = c->i[i];
246299141d43SSatish Balay       mat_j    = c->j + mat_i;
246399141d43SSatish Balay       mat_a    = c->a + mat_i;
246499141d43SSatish Balay       mat_ilen = c->ilen + i;
246517ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2466bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2467ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
246899141d43SSatish Balay           *mat_a++ = a->a[k];
246999141d43SSatish Balay           (*mat_ilen)++;
247099141d43SSatish Balay 
247117ab2063SBarry Smith         }
247217ab2063SBarry Smith       }
247317ab2063SBarry Smith     }
247402834360SBarry Smith     /* Free work space */
247502834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2476606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2477606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
2478cdc6f3adSToby Isaac     /* sort */
2479cdc6f3adSToby Isaac     for (i = 0; i < nrows; i++) {
2480cdc6f3adSToby Isaac       PetscInt ilen;
2481cdc6f3adSToby Isaac 
2482cdc6f3adSToby Isaac       mat_i = c->i[i];
2483cdc6f3adSToby Isaac       mat_j = c->j + mat_i;
2484cdc6f3adSToby Isaac       mat_a = c->a + mat_i;
2485cdc6f3adSToby Isaac       ilen  = c->ilen[i];
2486390e1bf2SBarry Smith       ierr  = PetscSortIntWithScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr);
2487cdc6f3adSToby Isaac     }
248802834360SBarry Smith   }
24896d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24906d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
249117ab2063SBarry Smith 
249217ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2493416022c9SBarry Smith   *B   = C;
24943a40ed3dSBarry Smith   PetscFunctionReturn(0);
249517ab2063SBarry Smith }
249617ab2063SBarry Smith 
2497fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
249882d44351SHong Zhang {
249982d44351SHong Zhang   PetscErrorCode ierr;
250082d44351SHong Zhang   Mat            B;
250182d44351SHong Zhang 
250282d44351SHong Zhang   PetscFunctionBegin;
2503c2d650bdSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
250482d44351SHong Zhang     ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
250582d44351SHong Zhang     ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
250633d57670SJed Brown     ierr    = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr);
250782d44351SHong Zhang     ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
250882d44351SHong Zhang     ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
250982d44351SHong Zhang     *subMat = B;
2510c2d650bdSHong Zhang   } else {
2511c2d650bdSHong Zhang     ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
2512c2d650bdSHong Zhang   }
251382d44351SHong Zhang   PetscFunctionReturn(0);
251482d44351SHong Zhang }
251582d44351SHong Zhang 
25169a625307SHong Zhang PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2517a871dcd8SBarry Smith {
251863b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2519dfbe8321SBarry Smith   PetscErrorCode ierr;
252063b91edcSBarry Smith   Mat            outA;
2521ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
252263b91edcSBarry Smith 
25233a40ed3dSBarry Smith   PetscFunctionBegin;
2524e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
25251df811f5SHong Zhang 
2526b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2527b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2528a871dcd8SBarry Smith 
252963b91edcSBarry Smith   outA             = inA;
2530d5f3da31SBarry Smith   outA->factortype = MAT_FACTOR_LU;
2531f6224b95SHong Zhang   ierr = PetscFree(inA->solvertype);CHKERRQ(ierr);
2532f6224b95SHong Zhang   ierr = PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);CHKERRQ(ierr);
25332205254eSKarl Rupp 
2534c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
25356bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
25362205254eSKarl Rupp 
2537c3122656SLisandro Dalcin   a->row = row;
25382205254eSKarl Rupp 
2539c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
25406bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
25412205254eSKarl Rupp 
2542c3122656SLisandro Dalcin   a->col = col;
254363b91edcSBarry Smith 
254436db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
25456bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
25464c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
25473bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
2548f0ec6fceSSatish Balay 
254994a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2550854ce69bSBarry Smith     ierr = PetscMalloc1(inA->rmap->n+1,&a->solve_work);CHKERRQ(ierr);
25513bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
255294a9d846SBarry Smith   }
255363b91edcSBarry Smith 
2554f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2555137fb511SHong Zhang   if (row_identity && col_identity) {
2556ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2557137fb511SHong Zhang   } else {
2558719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2559137fb511SHong Zhang   }
25603a40ed3dSBarry Smith   PetscFunctionReturn(0);
2561a871dcd8SBarry Smith }
2562a871dcd8SBarry Smith 
2563f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2564f0b747eeSBarry Smith {
2565f0b747eeSBarry Smith   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2566f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2567efee365bSSatish Balay   PetscErrorCode ierr;
2568c5df96a5SBarry Smith   PetscBLASInt   one = 1,bnz;
25693a40ed3dSBarry Smith 
25703a40ed3dSBarry Smith   PetscFunctionBegin;
2571c5df96a5SBarry Smith   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
25728b83055fSJed Brown   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2573efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2574acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
25753a40ed3dSBarry Smith   PetscFunctionReturn(0);
2576f0b747eeSBarry Smith }
2577f0b747eeSBarry Smith 
2578f68bb481SHong Zhang PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
257916b64355SHong Zhang {
258016b64355SHong Zhang   PetscErrorCode ierr;
258116b64355SHong Zhang   PetscInt       i;
258216b64355SHong Zhang 
258316b64355SHong Zhang   PetscFunctionBegin;
258416b64355SHong Zhang   if (!submatj->id) { /* delete data that are linked only to submats[id=0] */
258516b64355SHong Zhang     ierr = PetscFree4(submatj->sbuf1,submatj->ptr,submatj->tmp,submatj->ctr);CHKERRQ(ierr);
258616b64355SHong Zhang 
258716b64355SHong Zhang     for (i=0; i<submatj->nrqr; ++i) {
258816b64355SHong Zhang       ierr = PetscFree(submatj->sbuf2[i]);CHKERRQ(ierr);
258916b64355SHong Zhang     }
259016b64355SHong Zhang     ierr = PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);CHKERRQ(ierr);
259116b64355SHong Zhang 
259216b64355SHong Zhang     if (submatj->rbuf1) {
259316b64355SHong Zhang       ierr = PetscFree(submatj->rbuf1[0]);CHKERRQ(ierr);
259416b64355SHong Zhang       ierr = PetscFree(submatj->rbuf1);CHKERRQ(ierr);
259516b64355SHong Zhang     }
259616b64355SHong Zhang 
259716b64355SHong Zhang     for (i=0; i<submatj->nrqs; ++i) {
259816b64355SHong Zhang       ierr = PetscFree(submatj->rbuf3[i]);CHKERRQ(ierr);
259916b64355SHong Zhang     }
260016b64355SHong Zhang     ierr = PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);CHKERRQ(ierr);
260116b64355SHong Zhang     ierr = PetscFree(submatj->pa);CHKERRQ(ierr);
260216b64355SHong Zhang   }
260316b64355SHong Zhang 
260416b64355SHong Zhang #if defined(PETSC_USE_CTABLE)
260516b64355SHong Zhang   ierr = PetscTableDestroy((PetscTable*)&submatj->rmap);CHKERRQ(ierr);
260616b64355SHong Zhang   if (submatj->cmap_loc) {ierr = PetscFree(submatj->cmap_loc);CHKERRQ(ierr);}
260716b64355SHong Zhang   ierr = PetscFree(submatj->rmap_loc);CHKERRQ(ierr);
260816b64355SHong Zhang #else
260916b64355SHong Zhang   ierr = PetscFree(submatj->rmap);CHKERRQ(ierr);
261016b64355SHong Zhang #endif
261116b64355SHong Zhang 
261216b64355SHong Zhang   if (!submatj->allcolumns) {
261316b64355SHong Zhang #if defined(PETSC_USE_CTABLE)
261416b64355SHong Zhang     ierr = PetscTableDestroy((PetscTable*)&submatj->cmap);CHKERRQ(ierr);
261516b64355SHong Zhang #else
261616b64355SHong Zhang     ierr = PetscFree(submatj->cmap);CHKERRQ(ierr);
261716b64355SHong Zhang #endif
261816b64355SHong Zhang   }
261916b64355SHong Zhang   ierr = PetscFree(submatj->row2proc);CHKERRQ(ierr);
262016b64355SHong Zhang 
262116b64355SHong Zhang   ierr = PetscFree(submatj);CHKERRQ(ierr);
262216b64355SHong Zhang   PetscFunctionReturn(0);
262316b64355SHong Zhang }
262416b64355SHong Zhang 
26250fb991dcSHong Zhang PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
262616b64355SHong Zhang {
262716b64355SHong Zhang   PetscErrorCode ierr;
262816b64355SHong Zhang   Mat_SeqAIJ     *c = (Mat_SeqAIJ*)C->data;
26295c39f6d9SHong Zhang   Mat_SubSppt    *submatj = c->submatis1;
263016b64355SHong Zhang 
263116b64355SHong Zhang   PetscFunctionBegin;
263234136279SStefano Zampini   ierr = (*submatj->destroy)(C);CHKERRQ(ierr);
2633f68bb481SHong Zhang   ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr);
263416b64355SHong Zhang   PetscFunctionReturn(0);
263516b64355SHong Zhang }
263616b64355SHong Zhang 
26372d033e1fSHong Zhang PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[])
26382d033e1fSHong Zhang {
26392d033e1fSHong Zhang   PetscErrorCode ierr;
26402d033e1fSHong Zhang   PetscInt       i;
26410fb991dcSHong Zhang   Mat            C;
26420fb991dcSHong Zhang   Mat_SeqAIJ     *c;
26430fb991dcSHong Zhang   Mat_SubSppt    *submatj;
26442d033e1fSHong Zhang 
26452d033e1fSHong Zhang   PetscFunctionBegin;
26462d033e1fSHong Zhang   for (i=0; i<n; i++) {
26470fb991dcSHong Zhang     C       = (*mat)[i];
26480fb991dcSHong Zhang     c       = (Mat_SeqAIJ*)C->data;
26490fb991dcSHong Zhang     submatj = c->submatis1;
26502d033e1fSHong Zhang     if (submatj) {
2651682e4c99SStefano Zampini       if (--((PetscObject)C)->refct <= 0) {
265234136279SStefano Zampini         ierr = (*submatj->destroy)(C);CHKERRQ(ierr);
2653f68bb481SHong Zhang         ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr);
265434136279SStefano Zampini         ierr = PetscFree(C->defaultvectype);CHKERRQ(ierr);
26552d033e1fSHong Zhang         ierr = PetscLayoutDestroy(&C->rmap);CHKERRQ(ierr);
26562d033e1fSHong Zhang         ierr = PetscLayoutDestroy(&C->cmap);CHKERRQ(ierr);
26572d033e1fSHong Zhang         ierr = PetscHeaderDestroy(&C);CHKERRQ(ierr);
2658682e4c99SStefano Zampini       }
26592d033e1fSHong Zhang     } else {
26602d033e1fSHong Zhang       ierr = MatDestroy(&C);CHKERRQ(ierr);
26612d033e1fSHong Zhang     }
26622d033e1fSHong Zhang   }
266386e85357SHong Zhang 
266463a75b2aSHong Zhang   /* Destroy Dummy submatrices created for reuse */
266563a75b2aSHong Zhang   ierr = MatDestroySubMatrices_Dummy(n,mat);CHKERRQ(ierr);
266663a75b2aSHong Zhang 
26672d033e1fSHong Zhang   ierr = PetscFree(*mat);CHKERRQ(ierr);
26682d033e1fSHong Zhang   PetscFunctionReturn(0);
26692d033e1fSHong Zhang }
26702d033e1fSHong Zhang 
26717dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2672cddf8d76SBarry Smith {
2673dfbe8321SBarry Smith   PetscErrorCode ierr;
267497f1f81fSBarry Smith   PetscInt       i;
2675cddf8d76SBarry Smith 
26763a40ed3dSBarry Smith   PetscFunctionBegin;
2677cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2678df750dc8SHong Zhang     ierr = PetscCalloc1(n+1,B);CHKERRQ(ierr);
2679cddf8d76SBarry Smith   }
2680cddf8d76SBarry Smith 
2681cddf8d76SBarry Smith   for (i=0; i<n; i++) {
26827dae84e0SHong Zhang     ierr = MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2683cddf8d76SBarry Smith   }
26843a40ed3dSBarry Smith   PetscFunctionReturn(0);
2685cddf8d76SBarry Smith }
2686cddf8d76SBarry Smith 
268797f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
26884dcbc457SBarry Smith {
2689e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
26906849ba73SBarry Smith   PetscErrorCode ierr;
26915d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
26925d0c19d7SBarry Smith   const PetscInt *idx;
269397f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2694f1af5d2fSBarry Smith   PetscBT        table;
2695bbd702dbSSatish Balay 
26963a40ed3dSBarry Smith   PetscFunctionBegin;
2697d0f46423SBarry Smith   m  = A->rmap->n;
2698e4d965acSSatish Balay   ai = a->i;
2699bfeeae90SHong Zhang   aj = a->j;
27008a047759SSatish Balay 
2701e32f2f54SBarry Smith   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
270206763907SSatish Balay 
2703854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&nidx);CHKERRQ(ierr);
270453b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
270506763907SSatish Balay 
2706e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2707b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2708e4d965acSSatish Balay     isz  = 0;
27096831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2710e4d965acSSatish Balay 
2711e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
27124dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2713b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2714e4d965acSSatish Balay 
2715dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2716e4d965acSSatish Balay     for (j=0; j<n; ++j) {
27172205254eSKarl Rupp       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
27184dcbc457SBarry Smith     }
271906763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
27206bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2721e4d965acSSatish Balay 
272204a348a9SBarry Smith     k = 0;
272304a348a9SBarry Smith     for (j=0; j<ov; j++) { /* for each overlap */
272404a348a9SBarry Smith       n = isz;
272506763907SSatish Balay       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2726e4d965acSSatish Balay         row   = nidx[k];
2727e4d965acSSatish Balay         start = ai[row];
2728e4d965acSSatish Balay         end   = ai[row+1];
272904a348a9SBarry Smith         for (l = start; l<end; l++) {
2730efb16452SHong Zhang           val = aj[l];
27312205254eSKarl Rupp           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2732e4d965acSSatish Balay         }
2733e4d965acSSatish Balay       }
2734e4d965acSSatish Balay     }
273570b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2736e4d965acSSatish Balay   }
273794bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2738606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
27393a40ed3dSBarry Smith   PetscFunctionReturn(0);
27404dcbc457SBarry Smith }
274117ab2063SBarry Smith 
27420513a670SBarry Smith /* -------------------------------------------------------------- */
2743dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
27440513a670SBarry Smith {
27450513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
27466849ba73SBarry Smith   PetscErrorCode ierr;
27473b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
27485d0c19d7SBarry Smith   const PetscInt *row,*col;
27495d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
275056cd22aeSBarry Smith   IS             icolp,irowp;
27510298fd71SBarry Smith   PetscInt       *cwork = NULL;
27520298fd71SBarry Smith   PetscScalar    *vwork = NULL;
27530513a670SBarry Smith 
27543a40ed3dSBarry Smith   PetscFunctionBegin;
27554c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
275656cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
27574c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
275856cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
27590513a670SBarry Smith 
27600513a670SBarry Smith   /* determine lengths of permuted rows */
2761854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&lens);CHKERRQ(ierr);
27622205254eSKarl Rupp   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2763ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2764f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
276533d57670SJed Brown   ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
27667adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2767ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2768606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
27690513a670SBarry Smith 
2770785e854fSJed Brown   ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr);
27710513a670SBarry Smith   for (i=0; i<m; i++) {
277232ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
27732205254eSKarl Rupp     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2774cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
277532ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
27760513a670SBarry Smith   }
2777606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
27782205254eSKarl Rupp 
27793c7d62e4SBarry Smith   (*B)->assembled = PETSC_FALSE;
27802205254eSKarl Rupp 
27810513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27820513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
278356cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
278456cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
27856bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
27866bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
27873a40ed3dSBarry Smith   PetscFunctionReturn(0);
27880513a670SBarry Smith }
27890513a670SBarry Smith 
2790dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2791cb5b572fSBarry Smith {
2792dfbe8321SBarry Smith   PetscErrorCode ierr;
2793cb5b572fSBarry Smith 
2794cb5b572fSBarry Smith   PetscFunctionBegin;
279533f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
279633f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2797be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2798be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2799be6bf707SBarry Smith 
2800700c5bfcSBarry 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");
2801d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2802cdc753b6SBarry Smith     ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr);
2803cb5b572fSBarry Smith   } else {
2804cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2805cb5b572fSBarry Smith   }
2806cb5b572fSBarry Smith   PetscFunctionReturn(0);
2807cb5b572fSBarry Smith }
2808cb5b572fSBarry Smith 
28094994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2810273d9f13SBarry Smith {
2811dfbe8321SBarry Smith   PetscErrorCode ierr;
2812273d9f13SBarry Smith 
2813273d9f13SBarry Smith   PetscFunctionBegin;
2814ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2815273d9f13SBarry Smith   PetscFunctionReturn(0);
2816273d9f13SBarry Smith }
2817273d9f13SBarry Smith 
28188c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
28196c0721eeSBarry Smith {
28206c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
28216e111a19SKarl Rupp 
28226c0721eeSBarry Smith   PetscFunctionBegin;
28236c0721eeSBarry Smith   *array = a->a;
28246c0721eeSBarry Smith   PetscFunctionReturn(0);
28256c0721eeSBarry Smith }
28266c0721eeSBarry Smith 
28278c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
28286c0721eeSBarry Smith {
28296c0721eeSBarry Smith   PetscFunctionBegin;
28306c0721eeSBarry Smith   PetscFunctionReturn(0);
28316c0721eeSBarry Smith }
2832273d9f13SBarry Smith 
28338229c054SShri Abhyankar /*
28348229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
28358229c054SShri Abhyankar    have different nonzero structure.
28368229c054SShri Abhyankar */
2837b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2838ec7775f6SShri Abhyankar {
2839b264fe52SHong Zhang   PetscInt       i,j,k,nzx,nzy;
2840ec7775f6SShri Abhyankar 
2841ec7775f6SShri Abhyankar   PetscFunctionBegin;
2842ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2843ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
2844b264fe52SHong Zhang     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2845b264fe52SHong Zhang     nzx = xi[i+1] - xi[i];
2846b264fe52SHong Zhang     nzy = yi[i+1] - yi[i];
28478af7cee1SJed Brown     nnz[i] = 0;
28488af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2849b264fe52SHong Zhang       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2850b264fe52SHong Zhang       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
28518af7cee1SJed Brown       nnz[i]++;
28528af7cee1SJed Brown     }
28538af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2854ec7775f6SShri Abhyankar   }
2855ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2856ec7775f6SShri Abhyankar }
2857ec7775f6SShri Abhyankar 
2858b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2859b264fe52SHong Zhang {
2860b264fe52SHong Zhang   PetscInt       m = Y->rmap->N;
2861b264fe52SHong Zhang   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2862b264fe52SHong Zhang   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;
2863b264fe52SHong Zhang   PetscErrorCode ierr;
2864b264fe52SHong Zhang 
2865b264fe52SHong Zhang   PetscFunctionBegin;
2866b264fe52SHong Zhang   /* Set the number of nonzeros in the new matrix */
2867b264fe52SHong Zhang   ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr);
2868b264fe52SHong Zhang   PetscFunctionReturn(0);
2869b264fe52SHong Zhang }
2870b264fe52SHong Zhang 
2871f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2872ac90fabeSBarry Smith {
2873dfbe8321SBarry Smith   PetscErrorCode ierr;
2874ac90fabeSBarry Smith   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2875c5df96a5SBarry Smith   PetscBLASInt   one=1,bnz;
2876ac90fabeSBarry Smith 
2877ac90fabeSBarry Smith   PetscFunctionBegin;
2878c5df96a5SBarry Smith   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2879ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2880f4df32b1SMatthew Knepley     PetscScalar alpha = a;
28818b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2882acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2883a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2884ab784542SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2885ab784542SHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2886ac90fabeSBarry Smith   } else {
28878229c054SShri Abhyankar     Mat      B;
28888229c054SShri Abhyankar     PetscInt *nnz;
2889785e854fSJed Brown     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
2890ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2891bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
28924aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
289333d57670SJed Brown     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
2894176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
28958229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2896ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2897ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
289828be2f97SBarry Smith     ierr = MatHeaderReplace(Y,&B);CHKERRQ(ierr);
28998229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2900ac90fabeSBarry Smith   }
2901ac90fabeSBarry Smith   PetscFunctionReturn(0);
2902ac90fabeSBarry Smith }
2903ac90fabeSBarry Smith 
29047087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2905354c94deSBarry Smith {
2906354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2907354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2908354c94deSBarry Smith   PetscInt    i,nz;
2909354c94deSBarry Smith   PetscScalar *a;
2910354c94deSBarry Smith 
2911354c94deSBarry Smith   PetscFunctionBegin;
2912354c94deSBarry Smith   nz = aij->nz;
2913354c94deSBarry Smith   a  = aij->a;
29142205254eSKarl Rupp   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2915354c94deSBarry Smith #else
2916354c94deSBarry Smith   PetscFunctionBegin;
2917354c94deSBarry Smith #endif
2918354c94deSBarry Smith   PetscFunctionReturn(0);
2919354c94deSBarry Smith }
2920354c94deSBarry Smith 
2921985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2922e34fafa9SBarry Smith {
2923e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2924e34fafa9SBarry Smith   PetscErrorCode ierr;
2925d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2926e34fafa9SBarry Smith   PetscReal      atmp;
2927985db425SBarry Smith   PetscScalar    *x;
2928e34fafa9SBarry Smith   MatScalar      *aa;
2929e34fafa9SBarry Smith 
2930e34fafa9SBarry Smith   PetscFunctionBegin;
2931e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2932e34fafa9SBarry Smith   aa = a->a;
2933e34fafa9SBarry Smith   ai = a->i;
2934e34fafa9SBarry Smith   aj = a->j;
2935e34fafa9SBarry Smith 
2936985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2937e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2938e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2939e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2940e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2941e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
29429189402eSHong Zhang     x[i]  = 0.0;
2943e34fafa9SBarry Smith     for (j=0; j<ncols; j++) {
2944985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2945985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2946985db425SBarry Smith       aa++; aj++;
2947985db425SBarry Smith     }
2948985db425SBarry Smith   }
2949985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2950985db425SBarry Smith   PetscFunctionReturn(0);
2951985db425SBarry Smith }
2952985db425SBarry Smith 
2953985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2954985db425SBarry Smith {
2955985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2956985db425SBarry Smith   PetscErrorCode ierr;
2957d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2958985db425SBarry Smith   PetscScalar    *x;
2959985db425SBarry Smith   MatScalar      *aa;
2960985db425SBarry Smith 
2961985db425SBarry Smith   PetscFunctionBegin;
2962e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2963985db425SBarry Smith   aa = a->a;
2964985db425SBarry Smith   ai = a->i;
2965985db425SBarry Smith   aj = a->j;
2966985db425SBarry Smith 
2967985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2968985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2969985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2970e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2971985db425SBarry Smith   for (i=0; i<m; i++) {
2972985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2973d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2974985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2975985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2976985db425SBarry Smith       x[i] = 0.0;
2977985db425SBarry Smith       if (idx) {
2978985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2979985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2980985db425SBarry Smith           if (aj[j] > j) {
2981985db425SBarry Smith             idx[i] = j;
2982985db425SBarry Smith             break;
2983985db425SBarry Smith           }
2984985db425SBarry Smith         }
2985985db425SBarry Smith       }
2986985db425SBarry Smith     }
2987985db425SBarry Smith     for (j=0; j<ncols; j++) {
2988985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2989985db425SBarry Smith       aa++; aj++;
2990985db425SBarry Smith     }
2991985db425SBarry Smith   }
2992985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2993985db425SBarry Smith   PetscFunctionReturn(0);
2994985db425SBarry Smith }
2995985db425SBarry Smith 
2996c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2997c87e5d42SMatthew Knepley {
2998c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2999c87e5d42SMatthew Knepley   PetscErrorCode ierr;
3000c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3001c87e5d42SMatthew Knepley   PetscReal      atmp;
3002c87e5d42SMatthew Knepley   PetscScalar    *x;
3003c87e5d42SMatthew Knepley   MatScalar      *aa;
3004c87e5d42SMatthew Knepley 
3005c87e5d42SMatthew Knepley   PetscFunctionBegin;
3006e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3007c87e5d42SMatthew Knepley   aa = a->a;
3008c87e5d42SMatthew Knepley   ai = a->i;
3009c87e5d42SMatthew Knepley   aj = a->j;
3010c87e5d42SMatthew Knepley 
3011c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3012c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3013c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
301460e0710aSBarry 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);
3015c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
3016c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
3017289a08f5SMatthew Knepley     if (ncols) {
3018289a08f5SMatthew Knepley       /* Get first nonzero */
3019289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
3020289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
30212205254eSKarl Rupp         if (atmp > 1.0e-12) {
30222205254eSKarl Rupp           x[i] = atmp;
30232205254eSKarl Rupp           if (idx) idx[i] = aj[j];
30242205254eSKarl Rupp           break;
30252205254eSKarl Rupp         }
3026289a08f5SMatthew Knepley       }
302712431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
3028289a08f5SMatthew Knepley     } else {
3029289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
3030289a08f5SMatthew Knepley     }
3031c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
3032c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
3033289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3034c87e5d42SMatthew Knepley       aa++; aj++;
3035c87e5d42SMatthew Knepley     }
3036c87e5d42SMatthew Knepley   }
3037c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3038c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
3039c87e5d42SMatthew Knepley }
3040c87e5d42SMatthew Knepley 
3041985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3042985db425SBarry Smith {
3043985db425SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3044985db425SBarry Smith   PetscErrorCode  ierr;
3045d9ca1df4SBarry Smith   PetscInt        i,j,m = A->rmap->n,ncols,n;
3046d9ca1df4SBarry Smith   const PetscInt  *ai,*aj;
3047985db425SBarry Smith   PetscScalar     *x;
3048d9ca1df4SBarry Smith   const MatScalar *aa;
3049985db425SBarry Smith 
3050985db425SBarry Smith   PetscFunctionBegin;
3051e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3052985db425SBarry Smith   aa = a->a;
3053985db425SBarry Smith   ai = a->i;
3054985db425SBarry Smith   aj = a->j;
3055985db425SBarry Smith 
3056985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3057985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3058985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
3059e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3060985db425SBarry Smith   for (i=0; i<m; i++) {
3061985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
3062d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
3063985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
3064985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3065985db425SBarry Smith       x[i] = 0.0;
3066985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
3067985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
3068985db425SBarry Smith         for (j=0; j<ncols; j++) {
3069985db425SBarry Smith           if (aj[j] > j) {
3070985db425SBarry Smith             idx[i] = j;
3071985db425SBarry Smith             break;
3072985db425SBarry Smith           }
3073985db425SBarry Smith         }
3074985db425SBarry Smith       }
3075985db425SBarry Smith     }
3076985db425SBarry Smith     for (j=0; j<ncols; j++) {
3077985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3078985db425SBarry Smith       aa++; aj++;
3079e34fafa9SBarry Smith     }
3080e34fafa9SBarry Smith   }
3081e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3082e34fafa9SBarry Smith   PetscFunctionReturn(0);
3083e34fafa9SBarry Smith }
3084bbead8a2SBarry Smith 
3085713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3086bbead8a2SBarry Smith {
3087bbead8a2SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*) A->data;
3088bbead8a2SBarry Smith   PetscErrorCode  ierr;
308933d57670SJed Brown   PetscInt        i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3090bbead8a2SBarry Smith   MatScalar       *diag,work[25],*v_work;
30910da83c2eSBarry Smith   const PetscReal shift = 0.0;
30921a9391e3SHong Zhang   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;
3093bbead8a2SBarry Smith 
3094bbead8a2SBarry Smith   PetscFunctionBegin;
3095a455e926SHong Zhang   allowzeropivot = PetscNot(A->erroriffailure);
30964a0d0026SBarry Smith   if (a->ibdiagvalid) {
30974a0d0026SBarry Smith     if (values) *values = a->ibdiag;
30984a0d0026SBarry Smith     PetscFunctionReturn(0);
30994a0d0026SBarry Smith   }
3100bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
3101bbead8a2SBarry Smith   if (!a->ibdiag) {
3102785e854fSJed Brown     ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr);
31033bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
3104bbead8a2SBarry Smith   }
3105bbead8a2SBarry Smith   diag = a->ibdiag;
3106bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
3107bbead8a2SBarry Smith   /* factor and invert each block */
3108bbead8a2SBarry Smith   switch (bs) {
3109bbead8a2SBarry Smith   case 1:
3110bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3111bbead8a2SBarry Smith       ierr = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
3112ec1892c8SHong Zhang       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3113ec1892c8SHong Zhang         if (allowzeropivot) {
31147b6c816cSBarry Smith           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
31157b6c816cSBarry Smith           A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
31167b6c816cSBarry Smith           A->factorerror_zeropivot_row   = i;
31177b6c816cSBarry Smith           ierr = PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);CHKERRQ(ierr);
31187b6c816cSBarry Smith         } else SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D pivot %g tolerance %g",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);
3119ec1892c8SHong Zhang       }
3120bbead8a2SBarry Smith       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3121bbead8a2SBarry Smith     }
3122bbead8a2SBarry Smith     break;
3123bbead8a2SBarry Smith   case 2:
3124bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3125bbead8a2SBarry Smith       ij[0] = 2*i; ij[1] = 2*i + 1;
3126bbead8a2SBarry Smith       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
3127a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31287b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
312996b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
3130bbead8a2SBarry Smith       diag += 4;
3131bbead8a2SBarry Smith     }
3132bbead8a2SBarry Smith     break;
3133bbead8a2SBarry Smith   case 3:
3134bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3135bbead8a2SBarry Smith       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3136bbead8a2SBarry Smith       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
3137a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31387b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
313996b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
3140bbead8a2SBarry Smith       diag += 9;
3141bbead8a2SBarry Smith     }
3142bbead8a2SBarry Smith     break;
3143bbead8a2SBarry Smith   case 4:
3144bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3145bbead8a2SBarry Smith       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3146bbead8a2SBarry Smith       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
3147a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31487b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
314996b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
3150bbead8a2SBarry Smith       diag += 16;
3151bbead8a2SBarry Smith     }
3152bbead8a2SBarry Smith     break;
3153bbead8a2SBarry Smith   case 5:
3154bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3155bbead8a2SBarry 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;
3156bbead8a2SBarry Smith       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
3157a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31587b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
315996b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
3160bbead8a2SBarry Smith       diag += 25;
3161bbead8a2SBarry Smith     }
3162bbead8a2SBarry Smith     break;
3163bbead8a2SBarry Smith   case 6:
3164bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3165bbead8a2SBarry 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;
3166bbead8a2SBarry Smith       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
3167a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31687b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
316996b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3170bbead8a2SBarry Smith       diag += 36;
3171bbead8a2SBarry Smith     }
3172bbead8a2SBarry Smith     break;
3173bbead8a2SBarry Smith   case 7:
3174bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3175bbead8a2SBarry 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;
3176bbead8a2SBarry Smith       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
3177a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31787b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
317996b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3180bbead8a2SBarry Smith       diag += 49;
3181bbead8a2SBarry Smith     }
3182bbead8a2SBarry Smith     break;
3183bbead8a2SBarry Smith   default:
3184dcca6d9dSJed Brown     ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr);
3185bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3186bbead8a2SBarry Smith       for (j=0; j<bs; j++) {
3187bbead8a2SBarry Smith         IJ[j] = bs*i + j;
3188bbead8a2SBarry Smith       }
3189bbead8a2SBarry Smith       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
31905f8bbccaSHong Zhang       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31917b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
319296b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3193bbead8a2SBarry Smith       diag += bs2;
3194bbead8a2SBarry Smith     }
3195bbead8a2SBarry Smith     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3196bbead8a2SBarry Smith   }
3197bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3198bbead8a2SBarry Smith   PetscFunctionReturn(0);
3199bbead8a2SBarry Smith }
3200bbead8a2SBarry Smith 
320173a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
320273a71a0fSBarry Smith {
320373a71a0fSBarry Smith   PetscErrorCode ierr;
320473a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
320573a71a0fSBarry Smith   PetscScalar    a;
320673a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
320773a71a0fSBarry Smith 
320873a71a0fSBarry Smith   PetscFunctionBegin;
320973a71a0fSBarry Smith   if (!x->assembled) {
321073a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
321173a71a0fSBarry Smith     for (i=0; i<m; i++) {
321273a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
321373a71a0fSBarry Smith         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
321473a71a0fSBarry Smith         col  = (PetscInt)(n*PetscRealPart(a));
321573a71a0fSBarry Smith         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
321673a71a0fSBarry Smith       }
321773a71a0fSBarry Smith     }
321873a71a0fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
321973a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
322073a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
322173a71a0fSBarry Smith   PetscFunctionReturn(0);
322273a71a0fSBarry Smith }
322373a71a0fSBarry Smith 
3224682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
32250a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3226cb5b572fSBarry Smith                                         MatGetRow_SeqAIJ,
3227cb5b572fSBarry Smith                                         MatRestoreRow_SeqAIJ,
3228cb5b572fSBarry Smith                                         MatMult_SeqAIJ,
322997304618SKris Buschelman                                 /*  4*/ MatMultAdd_SeqAIJ,
32307c922b88SBarry Smith                                         MatMultTranspose_SeqAIJ,
32317c922b88SBarry Smith                                         MatMultTransposeAdd_SeqAIJ,
3232db4efbfdSBarry Smith                                         0,
3233db4efbfdSBarry Smith                                         0,
3234db4efbfdSBarry Smith                                         0,
3235db4efbfdSBarry Smith                                 /* 10*/ 0,
3236cb5b572fSBarry Smith                                         MatLUFactor_SeqAIJ,
3237cb5b572fSBarry Smith                                         0,
323841f059aeSBarry Smith                                         MatSOR_SeqAIJ,
3239*91e9d3e2SHong Zhang                                         MatTranspose_SeqAIJ,
324097304618SKris Buschelman                                 /*1 5*/ MatGetInfo_SeqAIJ,
3241cb5b572fSBarry Smith                                         MatEqual_SeqAIJ,
3242cb5b572fSBarry Smith                                         MatGetDiagonal_SeqAIJ,
3243cb5b572fSBarry Smith                                         MatDiagonalScale_SeqAIJ,
3244cb5b572fSBarry Smith                                         MatNorm_SeqAIJ,
324597304618SKris Buschelman                                 /* 20*/ 0,
3246cb5b572fSBarry Smith                                         MatAssemblyEnd_SeqAIJ,
3247cb5b572fSBarry Smith                                         MatSetOption_SeqAIJ,
3248cb5b572fSBarry Smith                                         MatZeroEntries_SeqAIJ,
3249d519adbfSMatthew Knepley                                 /* 24*/ MatZeroRows_SeqAIJ,
3250db4efbfdSBarry Smith                                         0,
3251db4efbfdSBarry Smith                                         0,
3252db4efbfdSBarry Smith                                         0,
3253db4efbfdSBarry Smith                                         0,
32544994cf47SJed Brown                                 /* 29*/ MatSetUp_SeqAIJ,
3255db4efbfdSBarry Smith                                         0,
3256db4efbfdSBarry Smith                                         0,
32578c778c55SBarry Smith                                         0,
32588c778c55SBarry Smith                                         0,
3259d519adbfSMatthew Knepley                                 /* 34*/ MatDuplicate_SeqAIJ,
3260cb5b572fSBarry Smith                                         0,
3261cb5b572fSBarry Smith                                         0,
3262cb5b572fSBarry Smith                                         MatILUFactor_SeqAIJ,
3263cb5b572fSBarry Smith                                         0,
3264d519adbfSMatthew Knepley                                 /* 39*/ MatAXPY_SeqAIJ,
32657dae84e0SHong Zhang                                         MatCreateSubMatrices_SeqAIJ,
3266cb5b572fSBarry Smith                                         MatIncreaseOverlap_SeqAIJ,
3267cb5b572fSBarry Smith                                         MatGetValues_SeqAIJ,
3268cb5b572fSBarry Smith                                         MatCopy_SeqAIJ,
3269d519adbfSMatthew Knepley                                 /* 44*/ MatGetRowMax_SeqAIJ,
3270cb5b572fSBarry Smith                                         MatScale_SeqAIJ,
32717d68702bSBarry Smith                                         MatShift_SeqAIJ,
327279299369SBarry Smith                                         MatDiagonalSet_SeqAIJ,
32736e169961SBarry Smith                                         MatZeroRowsColumns_SeqAIJ,
327473a71a0fSBarry Smith                                 /* 49*/ MatSetRandom_SeqAIJ,
32753b2fbd54SBarry Smith                                         MatGetRowIJ_SeqAIJ,
32763b2fbd54SBarry Smith                                         MatRestoreRowIJ_SeqAIJ,
32773b2fbd54SBarry Smith                                         MatGetColumnIJ_SeqAIJ,
3278a93ec695SBarry Smith                                         MatRestoreColumnIJ_SeqAIJ,
327993dfae19SHong Zhang                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3280b9617806SBarry Smith                                         0,
32810513a670SBarry Smith                                         0,
3282cda55fadSBarry Smith                                         MatPermute_SeqAIJ,
3283cda55fadSBarry Smith                                         0,
3284d519adbfSMatthew Knepley                                 /* 59*/ 0,
3285b9b97703SBarry Smith                                         MatDestroy_SeqAIJ,
3286b9b97703SBarry Smith                                         MatView_SeqAIJ,
3287357abbc8SBarry Smith                                         0,
3288321b30b9SSatish Balay                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3289321b30b9SSatish Balay                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3290321b30b9SSatish Balay                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3291ee4f033dSBarry Smith                                         0,
3292ee4f033dSBarry Smith                                         0,
3293ee4f033dSBarry Smith                                         0,
3294d519adbfSMatthew Knepley                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3295c87e5d42SMatthew Knepley                                         MatGetRowMinAbs_SeqAIJ,
3296ee4f033dSBarry Smith                                         0,
3297dcf5cc72SBarry Smith                                         0,
32982c93a97aSBarry Smith                                         0,
32992c93a97aSBarry Smith                                 /* 74*/ 0,
33003acb8795SBarry Smith                                         MatFDColoringApply_AIJ,
330197304618SKris Buschelman                                         0,
330297304618SKris Buschelman                                         0,
330397304618SKris Buschelman                                         0,
33046ce1633cSBarry Smith                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
330597304618SKris Buschelman                                         0,
330697304618SKris Buschelman                                         0,
330797304618SKris Buschelman                                         0,
3308bc011b1eSHong Zhang                                         MatLoad_SeqAIJ,
3309d519adbfSMatthew Knepley                                 /* 84*/ MatIsSymmetric_SeqAIJ,
33101cbb95d3SBarry Smith                                         MatIsHermitian_SeqAIJ,
33116284ec50SHong Zhang                                         0,
33126284ec50SHong Zhang                                         0,
3313bc011b1eSHong Zhang                                         0,
3314d519adbfSMatthew Knepley                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
331526be0446SHong Zhang                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
331626be0446SHong Zhang                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
331765e8a0caSHong Zhang                                         MatPtAP_SeqAIJ_SeqAIJ,
33188fa4b5a6SHong Zhang                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy,
33198fa4b5a6SHong Zhang                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
33206fc122caSHong Zhang                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
33216fc122caSHong Zhang                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
33226fc122caSHong Zhang                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
33232121bac1SHong Zhang                                         0,
33242121bac1SHong Zhang                                 /* 99*/ 0,
3325609c6c4dSKris Buschelman                                         0,
3326609c6c4dSKris Buschelman                                         0,
332787d4246cSBarry Smith                                         MatConjugate_SeqAIJ,
332887d4246cSBarry Smith                                         0,
3329d519adbfSMatthew Knepley                                 /*104*/ MatSetValuesRow_SeqAIJ,
333099cafbc1SBarry Smith                                         MatRealPart_SeqAIJ,
3331f5edf698SHong Zhang                                         MatImaginaryPart_SeqAIJ,
3332f5edf698SHong Zhang                                         0,
33332bebee5dSHong Zhang                                         0,
3334cbd44569SHong Zhang                                 /*109*/ MatMatSolve_SeqAIJ,
3335985db425SBarry Smith                                         0,
33362af78befSBarry Smith                                         MatGetRowMin_SeqAIJ,
33372af78befSBarry Smith                                         0,
3338599ef60dSHong Zhang                                         MatMissingDiagonal_SeqAIJ,
3339d519adbfSMatthew Knepley                                 /*114*/ 0,
3340599ef60dSHong Zhang                                         0,
33413c2a7987SHong Zhang                                         0,
3342fe97e370SBarry Smith                                         0,
3343fbdbba38SShri Abhyankar                                         0,
3344fbdbba38SShri Abhyankar                                 /*119*/ 0,
3345fbdbba38SShri Abhyankar                                         0,
3346fbdbba38SShri Abhyankar                                         0,
334782d44351SHong Zhang                                         0,
3348b3a44c85SBarry Smith                                         MatGetMultiProcBlock_SeqAIJ,
33490716a85fSBarry Smith                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3350bbead8a2SBarry Smith                                         MatGetColumnNorms_SeqAIJ,
335137868618SMatthew G Knepley                                         MatInvertBlockDiagonal_SeqAIJ,
33520da83c2eSBarry Smith                                         MatInvertVariableBlockDiagonal_SeqAIJ,
335337868618SMatthew G Knepley                                         0,
33545df89d91SHong Zhang                                 /*129*/ 0,
335575648e8dSHong Zhang                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
335675648e8dSHong Zhang                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
335775648e8dSHong Zhang                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3358b9af6bddSHong Zhang                                         MatTransposeColoringCreate_SeqAIJ,
3359b9af6bddSHong Zhang                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
33602b8ad9a3SHong Zhang                                         MatTransColoringApplyDenToSp_SeqAIJ,
33612b8ad9a3SHong Zhang                                         MatRARt_SeqAIJ_SeqAIJ,
33622b8ad9a3SHong Zhang                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
33633964eb88SJed Brown                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
33643964eb88SJed Brown                                  /*139*/0,
3365f9426fe0SMark Adams                                         0,
33661919a2e2SJed Brown                                         0,
33673a062f41SBarry Smith                                         MatFDColoringSetUp_SeqXAIJ,
33689c8f2541SHong Zhang                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
33692d033e1fSHong Zhang                                  /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
33702d033e1fSHong Zhang                                         MatDestroySubMatrices_SeqAIJ
33719e29f15eSvictorle };
337217ab2063SBarry Smith 
33737087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3374bef8e0ddSBarry Smith {
3375bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
337697f1f81fSBarry Smith   PetscInt   i,nz,n;
3377bef8e0ddSBarry Smith 
3378bef8e0ddSBarry Smith   PetscFunctionBegin;
3379bef8e0ddSBarry Smith   nz = aij->maxnz;
3380d0f46423SBarry Smith   n  = mat->rmap->n;
3381bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3382bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3383bef8e0ddSBarry Smith   }
3384bef8e0ddSBarry Smith   aij->nz = nz;
3385bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3386bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3387bef8e0ddSBarry Smith   }
3388bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3389bef8e0ddSBarry Smith }
3390bef8e0ddSBarry Smith 
3391a3bb6f32SFande Kong /*
3392e8b528d9SFande Kong  * When a sparse matrix has many zero columns, we should compact them out to save the space
3393a3bb6f32SFande Kong  * This happens in MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3394a3bb6f32SFande Kong  * */
3395a3bb6f32SFande Kong PetscErrorCode  MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3396a3bb6f32SFande Kong {
3397a3bb6f32SFande Kong   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3398a3bb6f32SFande Kong   PetscTable         gid1_lid1;
3399a3bb6f32SFande Kong   PetscTablePosition tpos;
3400a3bb6f32SFande Kong   PetscInt           gid,lid,i,j,ncols,ec;
3401a3bb6f32SFande Kong   PetscInt           *garray;
3402a3bb6f32SFande Kong   PetscErrorCode  ierr;
3403a3bb6f32SFande Kong 
3404a3bb6f32SFande Kong   PetscFunctionBegin;
3405a3bb6f32SFande Kong   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3406a3bb6f32SFande Kong   PetscValidPointer(mapping,2);
3407a3bb6f32SFande Kong   /* use a table */
3408a3bb6f32SFande Kong   ierr = PetscTableCreate(mat->rmap->n,mat->cmap->N+1,&gid1_lid1);CHKERRQ(ierr);
3409a3bb6f32SFande Kong   ec = 0;
3410a3bb6f32SFande Kong   for (i=0; i<mat->rmap->n; i++) {
3411a3bb6f32SFande Kong     ncols = aij->i[i+1] - aij->i[i];
3412a3bb6f32SFande Kong     for (j=0; j<ncols; j++) {
3413a3bb6f32SFande Kong       PetscInt data,gid1 = aij->j[aij->i[i] + j] + 1;
3414a3bb6f32SFande Kong       ierr = PetscTableFind(gid1_lid1,gid1,&data);CHKERRQ(ierr);
3415a3bb6f32SFande Kong       if (!data) {
3416a3bb6f32SFande Kong         /* one based table */
3417a3bb6f32SFande Kong         ierr = PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);CHKERRQ(ierr);
3418a3bb6f32SFande Kong       }
3419a3bb6f32SFande Kong     }
3420a3bb6f32SFande Kong   }
3421a3bb6f32SFande Kong   /* form array of columns we need */
3422a3bb6f32SFande Kong   ierr = PetscMalloc1(ec+1,&garray);CHKERRQ(ierr);
3423a3bb6f32SFande Kong   ierr = PetscTableGetHeadPosition(gid1_lid1,&tpos);CHKERRQ(ierr);
3424a3bb6f32SFande Kong   while (tpos) {
3425a3bb6f32SFande Kong     ierr = PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);CHKERRQ(ierr);
3426a3bb6f32SFande Kong     gid--;
3427a3bb6f32SFande Kong     lid--;
3428a3bb6f32SFande Kong     garray[lid] = gid;
3429a3bb6f32SFande Kong   }
3430a3bb6f32SFande Kong   ierr = PetscSortInt(ec,garray);CHKERRQ(ierr); /* sort, and rebuild */
3431a3bb6f32SFande Kong   ierr = PetscTableRemoveAll(gid1_lid1);CHKERRQ(ierr);
3432a3bb6f32SFande Kong   for (i=0; i<ec; i++) {
3433a3bb6f32SFande Kong     ierr = PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr);
3434a3bb6f32SFande Kong   }
3435a3bb6f32SFande Kong   /* compact out the extra columns in B */
3436a3bb6f32SFande Kong   for (i=0; i<mat->rmap->n; i++) {
3437a3bb6f32SFande Kong 	ncols = aij->i[i+1] - aij->i[i];
3438a3bb6f32SFande Kong     for (j=0; j<ncols; j++) {
3439a3bb6f32SFande Kong       PetscInt gid1 = aij->j[aij->i[i] + j] + 1;
3440a3bb6f32SFande Kong       ierr = PetscTableFind(gid1_lid1,gid1,&lid);CHKERRQ(ierr);
3441a3bb6f32SFande Kong       lid--;
3442a3bb6f32SFande Kong       aij->j[aij->i[i] + j] = lid;
3443a3bb6f32SFande Kong     }
3444a3bb6f32SFande Kong   }
3445a3bb6f32SFande Kong   mat->cmap->n = mat->cmap->N = ec;
3446a3bb6f32SFande Kong   mat->cmap->bs = 1;
3447a3bb6f32SFande Kong 
3448a3bb6f32SFande Kong   ierr = PetscTableDestroy(&gid1_lid1);CHKERRQ(ierr);
3449a3bb6f32SFande Kong   ierr = PetscLayoutSetUp((mat->cmap));CHKERRQ(ierr);
3450a3bb6f32SFande Kong   ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,mat->cmap->bs,mat->cmap->n,garray,PETSC_OWN_POINTER,mapping);CHKERRQ(ierr);
3451a3bb6f32SFande Kong   ierr = ISLocalToGlobalMappingSetType(*mapping,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
3452a3bb6f32SFande Kong   PetscFunctionReturn(0);
3453a3bb6f32SFande Kong }
3454a3bb6f32SFande Kong 
3455bef8e0ddSBarry Smith /*@
3456bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3457bef8e0ddSBarry Smith        in the matrix.
3458bef8e0ddSBarry Smith 
3459bef8e0ddSBarry Smith   Input Parameters:
3460bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3461bef8e0ddSBarry Smith -  indices - the column indices
3462bef8e0ddSBarry Smith 
346315091d37SBarry Smith   Level: advanced
346415091d37SBarry Smith 
3465bef8e0ddSBarry Smith   Notes:
3466bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3467bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3468bef8e0ddSBarry Smith   of the MatSetValues() operation.
3469bef8e0ddSBarry Smith 
3470bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3471d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3472bef8e0ddSBarry Smith 
3473bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3474bef8e0ddSBarry Smith 
3475b9617806SBarry Smith     The indices should start with zero, not one.
3476b9617806SBarry Smith 
3477bef8e0ddSBarry Smith @*/
34787087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3479bef8e0ddSBarry Smith {
34804ac538c5SBarry Smith   PetscErrorCode ierr;
3481bef8e0ddSBarry Smith 
3482bef8e0ddSBarry Smith   PetscFunctionBegin;
34830700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
34844482741eSBarry Smith   PetscValidPointer(indices,2);
34854ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3486bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3487bef8e0ddSBarry Smith }
3488bef8e0ddSBarry Smith 
3489be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3490be6bf707SBarry Smith 
34917087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3492be6bf707SBarry Smith {
3493be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
34946849ba73SBarry Smith   PetscErrorCode ierr;
3495d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3496be6bf707SBarry Smith 
3497be6bf707SBarry Smith   PetscFunctionBegin;
3498169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3499be6bf707SBarry Smith 
3500be6bf707SBarry Smith   /* allocate space for values if not already there */
3501be6bf707SBarry Smith   if (!aij->saved_values) {
3502854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr);
35033bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3504be6bf707SBarry Smith   }
3505be6bf707SBarry Smith 
3506be6bf707SBarry Smith   /* copy values over */
350787828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3508be6bf707SBarry Smith   PetscFunctionReturn(0);
3509be6bf707SBarry Smith }
3510be6bf707SBarry Smith 
3511be6bf707SBarry Smith /*@
3512be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3513be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3514be6bf707SBarry Smith        nonlinear portion.
3515be6bf707SBarry Smith 
3516be6bf707SBarry Smith    Collect on Mat
3517be6bf707SBarry Smith 
3518be6bf707SBarry Smith   Input Parameters:
35190e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3520be6bf707SBarry Smith 
352115091d37SBarry Smith   Level: advanced
352215091d37SBarry Smith 
3523be6bf707SBarry Smith   Common Usage, with SNESSolve():
3524be6bf707SBarry Smith $    Create Jacobian matrix
3525be6bf707SBarry Smith $    Set linear terms into matrix
3526be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3527be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3528be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3529512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3530be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3531be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3532be6bf707SBarry Smith $    In your Jacobian routine
3533be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3534be6bf707SBarry Smith $      Set nonlinear terms in matrix
3535be6bf707SBarry Smith 
3536be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3537be6bf707SBarry Smith $    // build linear portion of Jacobian
3538512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3539be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3540be6bf707SBarry Smith $    loop over nonlinear iterations
3541be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3542be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3543be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3544be6bf707SBarry Smith $       Solve linear system with Jacobian
3545be6bf707SBarry Smith $    endloop
3546be6bf707SBarry Smith 
3547be6bf707SBarry Smith   Notes:
3548be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3549512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3550be6bf707SBarry Smith     calling this routine.
3551be6bf707SBarry Smith 
35520c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
35530c468ba9SBarry Smith     and does not allocated additional space.
35540c468ba9SBarry Smith 
3555be6bf707SBarry Smith .seealso: MatRetrieveValues()
3556be6bf707SBarry Smith 
3557be6bf707SBarry Smith @*/
35587087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3559be6bf707SBarry Smith {
35604ac538c5SBarry Smith   PetscErrorCode ierr;
3561be6bf707SBarry Smith 
3562be6bf707SBarry Smith   PetscFunctionBegin;
35630700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3564e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3565e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
35664ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3567be6bf707SBarry Smith   PetscFunctionReturn(0);
3568be6bf707SBarry Smith }
3569be6bf707SBarry Smith 
35707087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3571be6bf707SBarry Smith {
3572be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
35736849ba73SBarry Smith   PetscErrorCode ierr;
3574d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3575be6bf707SBarry Smith 
3576be6bf707SBarry Smith   PetscFunctionBegin;
3577169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3578f23aa3ddSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3579be6bf707SBarry Smith   /* copy values over */
358087828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3581be6bf707SBarry Smith   PetscFunctionReturn(0);
3582be6bf707SBarry Smith }
3583be6bf707SBarry Smith 
3584be6bf707SBarry Smith /*@
3585be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3586be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3587be6bf707SBarry Smith        nonlinear portion.
3588be6bf707SBarry Smith 
3589be6bf707SBarry Smith    Collect on Mat
3590be6bf707SBarry Smith 
3591be6bf707SBarry Smith   Input Parameters:
3592386f7cf9SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3593be6bf707SBarry Smith 
359415091d37SBarry Smith   Level: advanced
359515091d37SBarry Smith 
3596be6bf707SBarry Smith .seealso: MatStoreValues()
3597be6bf707SBarry Smith 
3598be6bf707SBarry Smith @*/
35997087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3600be6bf707SBarry Smith {
36014ac538c5SBarry Smith   PetscErrorCode ierr;
3602be6bf707SBarry Smith 
3603be6bf707SBarry Smith   PetscFunctionBegin;
36040700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3605e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3606e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
36074ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3608be6bf707SBarry Smith   PetscFunctionReturn(0);
3609be6bf707SBarry Smith }
3610be6bf707SBarry Smith 
3611f83d6046SBarry Smith 
3612be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
361317ab2063SBarry Smith /*@C
3614682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
36150d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
36166e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
361751c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
36182bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
361917ab2063SBarry Smith 
3620db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3621db81eaa0SLois Curfman McInnes 
362217ab2063SBarry Smith    Input Parameters:
3623db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
362417ab2063SBarry Smith .  m - number of rows
362517ab2063SBarry Smith .  n - number of columns
362617ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
362751c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
36280298fd71SBarry Smith          (possibly different for each row) or NULL
362917ab2063SBarry Smith 
363017ab2063SBarry Smith    Output Parameter:
3631416022c9SBarry Smith .  A - the matrix
363217ab2063SBarry Smith 
3633175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3634ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3635175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3636175b88e8SBarry Smith 
3637b259b22eSLois Curfman McInnes    Notes:
363849a6f317SBarry Smith    If nnz is given then nz is ignored
363949a6f317SBarry Smith 
364017ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
364117ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
36420002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
364344cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
364417ab2063SBarry Smith 
364517ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
36460298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
36473d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
36486da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
364917ab2063SBarry Smith 
3650682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
36514fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3652682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
36536c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
36546c7ebb05SLois Curfman McInnes 
36556c7ebb05SLois Curfman McInnes    Options Database Keys:
3656698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
36579db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
365817ab2063SBarry Smith 
3659027ccd11SLois Curfman McInnes    Level: intermediate
3660027ccd11SLois Curfman McInnes 
366169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
366236db0b34SBarry Smith 
366317ab2063SBarry Smith @*/
36647087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
366517ab2063SBarry Smith {
3666dfbe8321SBarry Smith   PetscErrorCode ierr;
36676945ee14SBarry Smith 
36683a40ed3dSBarry Smith   PetscFunctionBegin;
3669f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3670117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3671c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3672d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3673273d9f13SBarry Smith   PetscFunctionReturn(0);
3674273d9f13SBarry Smith }
3675273d9f13SBarry Smith 
3676273d9f13SBarry Smith /*@C
3677273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3678273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3679273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3680273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3681273d9f13SBarry Smith 
3682273d9f13SBarry Smith    Collective on MPI_Comm
3683273d9f13SBarry Smith 
3684273d9f13SBarry Smith    Input Parameters:
36851c4f3114SJed Brown +  B - The matrix
3686273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3687273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
36880298fd71SBarry Smith          (possibly different for each row) or NULL
3689273d9f13SBarry Smith 
3690273d9f13SBarry Smith    Notes:
369149a6f317SBarry Smith      If nnz is given then nz is ignored
369249a6f317SBarry Smith 
3693273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3694273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3695273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3696273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3697273d9f13SBarry Smith 
3698273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
36990298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3700273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3701273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3702273d9f13SBarry Smith 
3703aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3704aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3705aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3706aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3707aa95bbe8SBarry Smith 
3708a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3709a96a251dSBarry Smith    entries or columns indices
3710a96a251dSBarry Smith 
3711273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3712273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3713273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3714273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3715273d9f13SBarry Smith 
3716273d9f13SBarry Smith    Options Database Keys:
3717698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
371847b2e64bSBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3719273d9f13SBarry Smith 
3720273d9f13SBarry Smith    Level: intermediate
3721273d9f13SBarry Smith 
372269b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3723273d9f13SBarry Smith 
3724273d9f13SBarry Smith @*/
37257087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3726273d9f13SBarry Smith {
37274ac538c5SBarry Smith   PetscErrorCode ierr;
3728a23d5eceSKris Buschelman 
3729a23d5eceSKris Buschelman   PetscFunctionBegin;
37306ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
37316ba663aaSJed Brown   PetscValidType(B,1);
37324ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3733a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3734a23d5eceSKris Buschelman }
3735a23d5eceSKris Buschelman 
37367087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3737a23d5eceSKris Buschelman {
3738273d9f13SBarry Smith   Mat_SeqAIJ     *b;
37392576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
37406849ba73SBarry Smith   PetscErrorCode ierr;
374197f1f81fSBarry Smith   PetscInt       i;
3742273d9f13SBarry Smith 
3743273d9f13SBarry Smith   PetscFunctionBegin;
37442576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3745a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3746c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3747c461c341SBarry Smith     nz             = 0;
3748c461c341SBarry Smith   }
374926283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
375026283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3751899cda47SBarry Smith 
3752435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
375360e0710aSBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3754b73539f3SBarry Smith   if (nnz) {
3755d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
375660e0710aSBarry 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]);
375760e0710aSBarry 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);
3758b73539f3SBarry Smith     }
3759b73539f3SBarry Smith   }
3760b73539f3SBarry Smith 
3761273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
37622205254eSKarl Rupp 
3763273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3764273d9f13SBarry Smith 
3765ab93d7beSBarry Smith   if (!skipallocation) {
37662ee49352SLisandro Dalcin     if (!b->imax) {
3767dcca6d9dSJed Brown       ierr = PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);CHKERRQ(ierr);
37683bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
37692ee49352SLisandro Dalcin     }
3770846b4da1SFande Kong     if (!b->ipre) {
3771846b4da1SFande Kong       ierr = PetscMalloc1(B->rmap->n,&b->ipre);CHKERRQ(ierr);
3772846b4da1SFande Kong       ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3773846b4da1SFande Kong     }
3774273d9f13SBarry Smith     if (!nnz) {
3775435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3776c62bd62aSJed Brown       else if (nz < 0) nz = 1;
37775d2a9ed1SStefano Zampini       nz = PetscMin(nz,B->cmap->n);
3778d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3779d0f46423SBarry Smith       nz = nz*B->rmap->n;
3780273d9f13SBarry Smith     } else {
3781273d9f13SBarry Smith       nz = 0;
3782d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3783273d9f13SBarry Smith     }
3784ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
37852205254eSKarl Rupp     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;
3786ab93d7beSBarry Smith 
3787273d9f13SBarry Smith     /* allocate the matrix space */
378853dd7562SDmitry Karpeev     /* FIXME: should B's old memory be unlogged? */
37892ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3790396832f4SHong Zhang     if (B->structure_only) {
37915848002fSHong Zhang       ierr = PetscMalloc1(nz,&b->j);CHKERRQ(ierr);
37925848002fSHong Zhang       ierr = PetscMalloc1(B->rmap->n+1,&b->i);CHKERRQ(ierr);
3793396832f4SHong Zhang       ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));CHKERRQ(ierr);
3794396832f4SHong Zhang     } else {
3795dcca6d9dSJed Brown       ierr = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr);
37963bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3797396832f4SHong Zhang     }
3798bfeeae90SHong Zhang     b->i[0] = 0;
3799d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
38005da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
38015da197adSKris Buschelman     }
3802396832f4SHong Zhang     if (B->structure_only) {
3803396832f4SHong Zhang       b->singlemalloc = PETSC_FALSE;
3804396832f4SHong Zhang       b->free_a       = PETSC_FALSE;
3805396832f4SHong Zhang     } else {
3806273d9f13SBarry Smith       b->singlemalloc = PETSC_TRUE;
3807e6b907acSBarry Smith       b->free_a       = PETSC_TRUE;
3808396832f4SHong Zhang     }
3809e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3810c461c341SBarry Smith   } else {
3811e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
3812e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
3813c461c341SBarry Smith   }
3814273d9f13SBarry Smith 
3815846b4da1SFande Kong   if (b->ipre && nnz != b->ipre  && b->imax) {
3816846b4da1SFande Kong     /* reserve user-requested sparsity */
3817846b4da1SFande Kong     ierr = PetscMemcpy(b->ipre,b->imax,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3818846b4da1SFande Kong   }
3819846b4da1SFande Kong 
3820846b4da1SFande Kong 
3821273d9f13SBarry Smith   b->nz               = 0;
3822273d9f13SBarry Smith   b->maxnz            = nz;
3823273d9f13SBarry Smith   B->info.nz_unneeded = (double)b->maxnz;
38242205254eSKarl Rupp   if (realalloc) {
38252205254eSKarl Rupp     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
38262205254eSKarl Rupp   }
3827cb7b82ddSBarry Smith   B->was_assembled = PETSC_FALSE;
3828cb7b82ddSBarry Smith   B->assembled     = PETSC_FALSE;
3829273d9f13SBarry Smith   PetscFunctionReturn(0);
3830273d9f13SBarry Smith }
3831273d9f13SBarry Smith 
3832846b4da1SFande Kong 
3833846b4da1SFande Kong PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
3834846b4da1SFande Kong {
3835846b4da1SFande Kong   Mat_SeqAIJ     *a;
3836a5bbaf83SFande Kong   PetscInt       i;
3837846b4da1SFande Kong   PetscErrorCode ierr;
3838846b4da1SFande Kong 
3839846b4da1SFande Kong   PetscFunctionBegin;
3840846b4da1SFande Kong   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3841846b4da1SFande Kong   a = (Mat_SeqAIJ*)A->data;
38422c814fdeSFande Kong   /* if no saved info, we error out */
38432c814fdeSFande Kong   if (!a->ipre) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_NULL,"No saved preallocation info \n");
38442c814fdeSFande Kong 
38452c814fdeSFande Kong   if (!a->i || !a->j || !a->a || !a->imax || !a->ilen) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_NULL,"Memory info is incomplete, and can not reset preallocation \n");
38462c814fdeSFande Kong 
3847846b4da1SFande Kong   ierr = PetscMemcpy(a->imax,a->ipre,A->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3848846b4da1SFande Kong   ierr = PetscMemzero(a->ilen,A->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3849846b4da1SFande Kong   a->i[0] = 0;
3850846b4da1SFande Kong   for (i=1; i<A->rmap->n+1; i++) {
3851846b4da1SFande Kong     a->i[i] = a->i[i-1] + a->imax[i-1];
3852846b4da1SFande Kong   }
3853846b4da1SFande Kong   A->preallocated     = PETSC_TRUE;
3854846b4da1SFande Kong   a->nz               = 0;
3855846b4da1SFande Kong   a->maxnz            = a->i[A->rmap->n];
3856846b4da1SFande Kong   A->info.nz_unneeded = (double)a->maxnz;
3857846b4da1SFande Kong   A->was_assembled    = PETSC_FALSE;
3858846b4da1SFande Kong   A->assembled        = PETSC_FALSE;
3859846b4da1SFande Kong   PetscFunctionReturn(0);
3860846b4da1SFande Kong }
3861846b4da1SFande Kong 
386258d36128SBarry Smith /*@
3863a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3864a1661176SMatthew Knepley 
3865a1661176SMatthew Knepley    Input Parameters:
3866a1661176SMatthew Knepley +  B - the matrix
3867a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3868a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3869a1661176SMatthew Knepley -  v - optional values in the matrix
3870a1661176SMatthew Knepley 
3871a1661176SMatthew Knepley    Level: developer
3872a1661176SMatthew Knepley 
387358d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
387458d36128SBarry Smith 
3875a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3876a1661176SMatthew Knepley 
3877c1c1d628SHong Zhang .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ
3878a1661176SMatthew Knepley @*/
3879a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3880a1661176SMatthew Knepley {
3881a1661176SMatthew Knepley   PetscErrorCode ierr;
3882a1661176SMatthew Knepley 
3883a1661176SMatthew Knepley   PetscFunctionBegin;
38840700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
38856ba663aaSJed Brown   PetscValidType(B,1);
38864ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3887a1661176SMatthew Knepley   PetscFunctionReturn(0);
3888a1661176SMatthew Knepley }
3889a1661176SMatthew Knepley 
38907087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3891a1661176SMatthew Knepley {
3892a1661176SMatthew Knepley   PetscInt       i;
3893a1661176SMatthew Knepley   PetscInt       m,n;
3894a1661176SMatthew Knepley   PetscInt       nz;
3895a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3896a1661176SMatthew Knepley   PetscScalar    *values;
3897a1661176SMatthew Knepley   PetscErrorCode ierr;
3898a1661176SMatthew Knepley 
3899a1661176SMatthew Knepley   PetscFunctionBegin;
390065e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3901779a8d59SSatish Balay 
3902779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3903779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3904779a8d59SSatish Balay 
3905779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3906854ce69bSBarry Smith   ierr = PetscMalloc1(m+1, &nnz);CHKERRQ(ierr);
3907a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3908b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3909a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
391065e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3911a1661176SMatthew Knepley     nnz[i] = nz;
3912a1661176SMatthew Knepley   }
3913a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3914a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3915a1661176SMatthew Knepley 
3916a1661176SMatthew Knepley   if (v) {
3917a1661176SMatthew Knepley     values = (PetscScalar*) v;
3918a1661176SMatthew Knepley   } else {
39191795a4d1SJed Brown     ierr = PetscCalloc1(nz_max, &values);CHKERRQ(ierr);
3920a1661176SMatthew Knepley   }
3921a1661176SMatthew Knepley 
3922a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3923b7940d39SSatish Balay     nz   = Ii[i+1] - Ii[i];
3924b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3925a1661176SMatthew Knepley   }
3926a1661176SMatthew Knepley 
3927a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3928a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3929a1661176SMatthew Knepley 
3930a1661176SMatthew Knepley   if (!v) {
3931a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3932a1661176SMatthew Knepley   }
39337827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3934a1661176SMatthew Knepley   PetscFunctionReturn(0);
3935a1661176SMatthew Knepley }
3936a1661176SMatthew Knepley 
3937c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3938af0996ceSBarry Smith #include <petsc/private/kernels/petscaxpy.h>
3939170fe5c8SBarry Smith 
3940170fe5c8SBarry Smith /*
3941170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3942170fe5c8SBarry Smith 
3943170fe5c8SBarry Smith                n                       p                          p
3944170fe5c8SBarry Smith         (              )       (              )         (                  )
3945170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3946170fe5c8SBarry Smith         (              )       (              )         (                  )
3947170fe5c8SBarry Smith 
3948170fe5c8SBarry Smith */
3949170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3950170fe5c8SBarry Smith {
3951170fe5c8SBarry Smith   PetscErrorCode    ierr;
3952170fe5c8SBarry Smith   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3953170fe5c8SBarry Smith   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3954170fe5c8SBarry Smith   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
39551de00fd4SBarry Smith   PetscInt          i,n,m,q,p;
3956170fe5c8SBarry Smith   const PetscInt    *ii,*idx;
3957170fe5c8SBarry Smith   const PetscScalar *b,*a,*a_q;
3958170fe5c8SBarry Smith   PetscScalar       *c,*c_q;
3959170fe5c8SBarry Smith 
3960170fe5c8SBarry Smith   PetscFunctionBegin;
3961d0f46423SBarry Smith   m    = A->rmap->n;
3962d0f46423SBarry Smith   n    = A->cmap->n;
3963d0f46423SBarry Smith   p    = B->cmap->n;
3964170fe5c8SBarry Smith   a    = sub_a->v;
3965170fe5c8SBarry Smith   b    = sub_b->a;
3966170fe5c8SBarry Smith   c    = sub_c->v;
3967170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3968170fe5c8SBarry Smith 
3969170fe5c8SBarry Smith   ii  = sub_b->i;
3970170fe5c8SBarry Smith   idx = sub_b->j;
3971170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3972170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3973170fe5c8SBarry Smith     while (q-->0) {
3974170fe5c8SBarry Smith       c_q = c + m*(*idx);
3975170fe5c8SBarry Smith       a_q = a + m*i;
3976854c7f52SBarry Smith       PetscKernelAXPY(c_q,*b,a_q,m);
3977170fe5c8SBarry Smith       idx++;
3978170fe5c8SBarry Smith       b++;
3979170fe5c8SBarry Smith     }
3980170fe5c8SBarry Smith   }
3981170fe5c8SBarry Smith   PetscFunctionReturn(0);
3982170fe5c8SBarry Smith }
3983170fe5c8SBarry Smith 
3984170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3985170fe5c8SBarry Smith {
3986170fe5c8SBarry Smith   PetscErrorCode ierr;
3987d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3988170fe5c8SBarry Smith   Mat            Cmat;
3989170fe5c8SBarry Smith 
3990170fe5c8SBarry Smith   PetscFunctionBegin;
399160e0710aSBarry 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);
3992ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
3993170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
399433d57670SJed Brown   ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr);
3995170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
39960298fd71SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
3997d73949e8SHong Zhang 
3998d73949e8SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
39992205254eSKarl Rupp 
4000170fe5c8SBarry Smith   *C = Cmat;
4001170fe5c8SBarry Smith   PetscFunctionReturn(0);
4002170fe5c8SBarry Smith }
4003170fe5c8SBarry Smith 
4004170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
4005150d2497SBarry Smith PETSC_INTERN PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
4006170fe5c8SBarry Smith {
4007170fe5c8SBarry Smith   PetscErrorCode ierr;
4008170fe5c8SBarry Smith 
4009170fe5c8SBarry Smith   PetscFunctionBegin;
4010170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
40113ff4c91cSHong Zhang     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
4012170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
40133ff4c91cSHong Zhang     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
4014170fe5c8SBarry Smith   }
40153ff4c91cSHong Zhang   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
4016170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
40173ff4c91cSHong Zhang   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
4018170fe5c8SBarry Smith   PetscFunctionReturn(0);
4019170fe5c8SBarry Smith }
4020170fe5c8SBarry Smith 
4021170fe5c8SBarry Smith 
40220bad9183SKris Buschelman /*MC
4023fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
40240bad9183SKris Buschelman    based on compressed sparse row format.
40250bad9183SKris Buschelman 
40260bad9183SKris Buschelman    Options Database Keys:
40270bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
40280bad9183SKris Buschelman 
40290bad9183SKris Buschelman   Level: beginner
40300bad9183SKris Buschelman 
4031f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
40320bad9183SKris Buschelman M*/
40330bad9183SKris Buschelman 
4034ccd284c7SBarry Smith /*MC
4035ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
4036ccd284c7SBarry Smith 
4037ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
4038ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
4039ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
4040ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
4041ccd284c7SBarry Smith   the above preallocation routines for simplicity.
4042ccd284c7SBarry Smith 
4043ccd284c7SBarry Smith    Options Database Keys:
4044ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
4045ccd284c7SBarry Smith 
404695452b02SPatrick Sanan   Developer Notes:
4047ca9cdca7SRichard Tran Mills     Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
4048ccd284c7SBarry Smith    enough exist.
4049ccd284c7SBarry Smith 
4050ccd284c7SBarry Smith   Level: beginner
4051ccd284c7SBarry Smith 
4052ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
4053ccd284c7SBarry Smith M*/
4054ccd284c7SBarry Smith 
4055ccd284c7SBarry Smith /*MC
4056ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
4057ccd284c7SBarry Smith 
4058ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
4059ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
4060ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
4061ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
4062ccd284c7SBarry Smith   the above preallocation routines for simplicity.
4063ccd284c7SBarry Smith 
4064ccd284c7SBarry Smith    Options Database Keys:
4065ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
4066ccd284c7SBarry Smith 
4067ccd284c7SBarry Smith   Level: beginner
4068ccd284c7SBarry Smith 
4069ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4070ccd284c7SBarry Smith M*/
4071ccd284c7SBarry Smith 
40727906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
40737906f579SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
40747906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
40757906f579SHong Zhang #endif
40767906f579SHong Zhang #if defined(PETSC_HAVE_HYPRE)
40777906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*);
40787906f579SHong Zhang PETSC_INTERN PetscErrorCode MatMatMatMult_Transpose_AIJ_AIJ(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
40797906f579SHong Zhang #endif
40807906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);
40817906f579SHong Zhang 
4082d4002b98SHong Zhang PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*);
4083c9225affSStefano Zampini PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
408475d48cdbSStefano Zampini PETSC_INTERN PetscErrorCode MatPtAP_IS_XAIJ(Mat,Mat,MatReuse,PetscReal,Mat*);
40857906f579SHong Zhang 
40868c778c55SBarry Smith /*@C
40878397e458SBarry Smith    MatSeqAIJGetArray - gives access to the array where the data for a MATSEQAIJ matrix is stored
40888c778c55SBarry Smith 
40898c778c55SBarry Smith    Not Collective
40908c778c55SBarry Smith 
40918c778c55SBarry Smith    Input Parameter:
4092579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
40938c778c55SBarry Smith 
40948c778c55SBarry Smith    Output Parameter:
40958c778c55SBarry Smith .   array - pointer to the data
40968c778c55SBarry Smith 
40978c778c55SBarry Smith    Level: intermediate
40988c778c55SBarry Smith 
4099774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
41008c778c55SBarry Smith @*/
41018c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
41028c778c55SBarry Smith {
41038c778c55SBarry Smith   PetscErrorCode ierr;
41048c778c55SBarry Smith 
41058c778c55SBarry Smith   PetscFunctionBegin;
41068c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
41078c778c55SBarry Smith   PetscFunctionReturn(0);
41088c778c55SBarry Smith }
41098c778c55SBarry Smith 
411021e72a00SBarry Smith /*@C
411121e72a00SBarry Smith    MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
411221e72a00SBarry Smith 
411321e72a00SBarry Smith    Not Collective
411421e72a00SBarry Smith 
411521e72a00SBarry Smith    Input Parameter:
4116579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
411721e72a00SBarry Smith 
411821e72a00SBarry Smith    Output Parameter:
411921e72a00SBarry Smith .   nz - the maximum number of nonzeros in any row
412021e72a00SBarry Smith 
412121e72a00SBarry Smith    Level: intermediate
412221e72a00SBarry Smith 
412321e72a00SBarry Smith .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
412421e72a00SBarry Smith @*/
412521e72a00SBarry Smith PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
412621e72a00SBarry Smith {
412721e72a00SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
412821e72a00SBarry Smith 
412921e72a00SBarry Smith   PetscFunctionBegin;
413021e72a00SBarry Smith   *nz = aij->rmax;
413121e72a00SBarry Smith   PetscFunctionReturn(0);
413221e72a00SBarry Smith }
413321e72a00SBarry Smith 
41348c778c55SBarry Smith /*@C
4135579dbff0SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray()
41368c778c55SBarry Smith 
41378c778c55SBarry Smith    Not Collective
41388c778c55SBarry Smith 
41398c778c55SBarry Smith    Input Parameters:
4140579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
41418c778c55SBarry Smith .  array - pointer to the data
41428c778c55SBarry Smith 
41438c778c55SBarry Smith    Level: intermediate
41448c778c55SBarry Smith 
4145774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
41468c778c55SBarry Smith @*/
41478c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
41488c778c55SBarry Smith {
41498c778c55SBarry Smith   PetscErrorCode ierr;
41508c778c55SBarry Smith 
41518c778c55SBarry Smith   PetscFunctionBegin;
41528c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
41538c778c55SBarry Smith   PetscFunctionReturn(0);
41548c778c55SBarry Smith }
41558c778c55SBarry Smith 
415634b5b067SBarry Smith #if defined(PETSC_HAVE_CUDA)
415702fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat);
415802fe1965SBarry Smith #endif
415902fe1965SBarry Smith 
41608cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4161273d9f13SBarry Smith {
4162273d9f13SBarry Smith   Mat_SeqAIJ     *b;
4163dfbe8321SBarry Smith   PetscErrorCode ierr;
416438baddfdSBarry Smith   PetscMPIInt    size;
4165273d9f13SBarry Smith 
4166273d9f13SBarry Smith   PetscFunctionBegin;
4167ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
4168e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
4169273d9f13SBarry Smith 
4170b00a9115SJed Brown   ierr = PetscNewLog(B,&b);CHKERRQ(ierr);
41712205254eSKarl Rupp 
4172b0a32e0cSBarry Smith   B->data = (void*)b;
41732205254eSKarl Rupp 
4174549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
41752205254eSKarl Rupp 
4176416022c9SBarry Smith   b->row                = 0;
4177416022c9SBarry Smith   b->col                = 0;
417882bf6240SBarry Smith   b->icol               = 0;
4179b810aeb4SBarry Smith   b->reallocs           = 0;
418036db0b34SBarry Smith   b->ignorezeroentries  = PETSC_FALSE;
4181f1e2ffcdSBarry Smith   b->roworiented        = PETSC_TRUE;
4182416022c9SBarry Smith   b->nonew              = 0;
4183416022c9SBarry Smith   b->diag               = 0;
4184416022c9SBarry Smith   b->solve_work         = 0;
41852a1b7f2aSHong Zhang   B->spptr              = 0;
4186be6bf707SBarry Smith   b->saved_values       = 0;
4187d7f994e1SBarry Smith   b->idiag              = 0;
418871f1c65dSBarry Smith   b->mdiag              = 0;
418971f1c65dSBarry Smith   b->ssor_work          = 0;
419071f1c65dSBarry Smith   b->omega              = 1.0;
419171f1c65dSBarry Smith   b->fshift             = 0.0;
419271f1c65dSBarry Smith   b->idiagvalid         = PETSC_FALSE;
4193bbead8a2SBarry Smith   b->ibdiagvalid        = PETSC_FALSE;
4194a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
419517ab2063SBarry Smith 
419635d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
4197bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
4198bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
41998c778c55SBarry Smith 
4200b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4201bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
4202bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
4203b3866ffcSBarry Smith #endif
420417f1a0eaSHong Zhang 
4205bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
4206bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
4207bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
4208bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
4209bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
4210bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
42114dfdc2d9SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijsell_C",MatConvert_SeqAIJ_SeqAIJSELL);CHKERRQ(ierr);
42129779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE)
42134a2a386eSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr);
4214191b95cbSRichard Tran Mills #endif
421534b5b067SBarry Smith #if defined(PETSC_HAVE_CUDA)
421602fe1965SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcusparse_C",MatConvert_SeqAIJ_SeqAIJCUSPARSE);CHKERRQ(ierr);
421702fe1965SBarry Smith #endif
4218bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
4219af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
4220af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);CHKERRQ(ierr);
4221af8000cdSHong Zhang #endif
422263c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE)
422363c07aadSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);CHKERRQ(ierr);
42243dad0653Sstefano_zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMatMult_transpose_seqaij_seqaij_C",MatMatMatMult_Transpose_AIJ_AIJ);CHKERRQ(ierr);
422563c07aadSStefano Zampini #endif
4226b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);CHKERRQ(ierr);
4227d4002b98SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);CHKERRQ(ierr);
4228c9225affSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_is_C",MatConvert_XAIJ_IS);CHKERRQ(ierr);
4229bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4230bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4231bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
4232846b4da1SFande Kong   ierr = PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);CHKERRQ(ierr);
4233bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
4234bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
4235bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
4236bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
4237bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
423875d48cdbSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_seqaij_C",MatPtAP_IS_XAIJ);CHKERRQ(ierr);
42394108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
424017667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
42414099cc6bSBarry Smith   ierr = MatSeqAIJSetTypeFromOptions(B);CHKERRQ(ierr);  /* this allows changing the matrix subtype to say MATSEQAIJPERM */
42423a40ed3dSBarry Smith   PetscFunctionReturn(0);
424317ab2063SBarry Smith }
424417ab2063SBarry Smith 
4245b24902e0SBarry Smith /*
4246b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4247b24902e0SBarry Smith */
4248ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
424917ab2063SBarry Smith {
4250416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
42516849ba73SBarry Smith   PetscErrorCode ierr;
4252d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
425317ab2063SBarry Smith 
42543a40ed3dSBarry Smith   PetscFunctionBegin;
4255273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4256273d9f13SBarry Smith 
4257d5f3da31SBarry Smith   C->factortype = A->factortype;
4258416022c9SBarry Smith   c->row        = 0;
4259416022c9SBarry Smith   c->col        = 0;
426082bf6240SBarry Smith   c->icol       = 0;
42616ad4291fSHong Zhang   c->reallocs   = 0;
426217ab2063SBarry Smith 
42636ad4291fSHong Zhang   C->assembled = PETSC_TRUE;
426417ab2063SBarry Smith 
4265aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4266aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4267eec197d1SBarry Smith 
4268dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&c->imax,m,&c->ilen);CHKERRQ(ierr);
42693bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
427017ab2063SBarry Smith   for (i=0; i<m; i++) {
4271416022c9SBarry Smith     c->imax[i] = a->imax[i];
4272416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
427317ab2063SBarry Smith   }
427417ab2063SBarry Smith 
427517ab2063SBarry Smith   /* allocate the matrix space */
4276f77e22a1SHong Zhang   if (mallocmatspace) {
4277dcca6d9dSJed Brown     ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr);
42783bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
42792205254eSKarl Rupp 
4280f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
42812205254eSKarl Rupp 
428297f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
428317ab2063SBarry Smith     if (m > 0) {
428497f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4285be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4286bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4287be6bf707SBarry Smith       } else {
4288bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
428917ab2063SBarry Smith       }
429008480c60SBarry Smith     }
4291f77e22a1SHong Zhang   }
429217ab2063SBarry Smith 
42936ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4294416022c9SBarry Smith   c->roworiented       = a->roworiented;
4295416022c9SBarry Smith   c->nonew             = a->nonew;
4296416022c9SBarry Smith   if (a->diag) {
4297854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&c->diag);CHKERRQ(ierr);
42983bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
429917ab2063SBarry Smith     for (i=0; i<m; i++) {
4300416022c9SBarry Smith       c->diag[i] = a->diag[i];
430117ab2063SBarry Smith     }
43023a40ed3dSBarry Smith   } else c->diag = 0;
43032205254eSKarl Rupp 
43046ad4291fSHong Zhang   c->solve_work         = 0;
43056ad4291fSHong Zhang   c->saved_values       = 0;
43066ad4291fSHong Zhang   c->idiag              = 0;
430771f1c65dSBarry Smith   c->ssor_work          = 0;
4308a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
4309e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
4310e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
43116ad4291fSHong Zhang 
4312893ad86cSHong Zhang   c->rmax         = a->rmax;
4313416022c9SBarry Smith   c->nz           = a->nz;
43148ed568f8SMatthew G Knepley   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4315273d9f13SBarry Smith   C->preallocated = PETSC_TRUE;
4316754ec7b1SSatish Balay 
43176ad4291fSHong Zhang   c->compressedrow.use   = a->compressedrow.use;
43186ad4291fSHong Zhang   c->compressedrow.nrows = a->compressedrow.nrows;
4319cd6b891eSBarry Smith   if (a->compressedrow.use) {
43206ad4291fSHong Zhang     i    = a->compressedrow.nrows;
4321dcca6d9dSJed Brown     ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr);
43226ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
43236ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
432427ea64f8SHong Zhang   } else {
432527ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
43260298fd71SBarry Smith     c->compressedrow.i      = NULL;
43270298fd71SBarry Smith     c->compressedrow.rindex = NULL;
43286ad4291fSHong Zhang   }
4329ea632784SBarry Smith   c->nonzerorowcnt = a->nonzerorowcnt;
4330e56f5c9eSBarry Smith   C->nonzerostate  = A->nonzerostate;
43314846f1f5SKris Buschelman 
43322205254eSKarl Rupp   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4333140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
43343a40ed3dSBarry Smith   PetscFunctionReturn(0);
433517ab2063SBarry Smith }
433617ab2063SBarry Smith 
4337b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4338b24902e0SBarry Smith {
4339b24902e0SBarry Smith   PetscErrorCode ierr;
4340b24902e0SBarry Smith 
4341b24902e0SBarry Smith   PetscFunctionBegin;
4342ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
43434b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4344cfd3f464SBarry Smith   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
434533d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
4346cfd3f464SBarry Smith   }
4347a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4348f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4349b24902e0SBarry Smith   PetscFunctionReturn(0);
4350b24902e0SBarry Smith }
4351b24902e0SBarry Smith 
4352112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4353fbdbba38SShri Abhyankar {
435452f91c60SVaclav Hapla   PetscBool      isbinary, ishdf5;
435552f91c60SVaclav Hapla   PetscErrorCode ierr;
435652f91c60SVaclav Hapla 
435752f91c60SVaclav Hapla   PetscFunctionBegin;
435852f91c60SVaclav Hapla   PetscValidHeaderSpecific(newMat,MAT_CLASSID,1);
435952f91c60SVaclav Hapla   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4360c27b3999SVaclav Hapla   /* force binary viewer to load .info file if it has not yet done so */
4361c27b3999SVaclav Hapla   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
436252f91c60SVaclav Hapla   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
436352f91c60SVaclav Hapla   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
436452f91c60SVaclav Hapla   if (isbinary) {
436552f91c60SVaclav Hapla     ierr = MatLoad_SeqAIJ_Binary(newMat,viewer);CHKERRQ(ierr);
436652f91c60SVaclav Hapla   } else if (ishdf5) {
436752f91c60SVaclav Hapla #if defined(PETSC_HAVE_HDF5)
436852f91c60SVaclav Hapla     ierr = MatLoad_AIJ_HDF5(newMat,viewer);CHKERRQ(ierr);
436952f91c60SVaclav Hapla #else
437052f91c60SVaclav Hapla     SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
437152f91c60SVaclav Hapla #endif
437252f91c60SVaclav Hapla   } else {
437352f91c60SVaclav Hapla     SETERRQ2(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"Viewer type %s not yet supported for reading %s matrices",((PetscObject)viewer)->type_name,((PetscObject)newMat)->type_name);
437452f91c60SVaclav Hapla   }
437552f91c60SVaclav Hapla   PetscFunctionReturn(0);
437652f91c60SVaclav Hapla }
437752f91c60SVaclav Hapla 
437852f91c60SVaclav Hapla PetscErrorCode MatLoad_SeqAIJ_Binary(Mat newMat, PetscViewer viewer)
437952f91c60SVaclav Hapla {
4380fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4381fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4382fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4383fbdbba38SShri Abhyankar   int            fd;
4384fbdbba38SShri Abhyankar   PetscMPIInt    size;
4385fbdbba38SShri Abhyankar   MPI_Comm       comm;
43863059b6faSBarry Smith   PetscInt       bs = newMat->rmap->bs;
4387fbdbba38SShri Abhyankar 
4388fbdbba38SShri Abhyankar   PetscFunctionBegin;
4389fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4390fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4391fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4392bbead8a2SBarry Smith 
43930298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
43940298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
4395bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
43963059b6faSBarry Smith   if (bs < 0) bs = 1;
43973059b6faSBarry Smith   ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);
4398bbead8a2SBarry Smith 
4399fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4400fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4401fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4402fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4403fbdbba38SShri Abhyankar 
4404bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4405fbdbba38SShri Abhyankar 
4406fbdbba38SShri Abhyankar   /* read in row lengths */
4407785e854fSJed Brown   ierr = PetscMalloc1(M,&rowlengths);CHKERRQ(ierr);
4408fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4409fbdbba38SShri Abhyankar 
4410fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4411fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
441260e0710aSBarry 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);
4413fbdbba38SShri Abhyankar 
4414fbdbba38SShri Abhyankar   /* set global size if not set already*/
4415f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4416fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4417aabbc4fbSShri Abhyankar   } else {
44189d36ed5fSBarry Smith     /* if sizes and type are already set, check if the matrix  global sizes are correct */
4419fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
44204c5b953cSHong Zhang     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
44214c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
44224c5b953cSHong Zhang     }
442360e0710aSBarry 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);
4424aabbc4fbSShri Abhyankar   }
4425fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4426fbdbba38SShri Abhyankar   a    = (Mat_SeqAIJ*)newMat->data;
4427fbdbba38SShri Abhyankar 
4428fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4429fbdbba38SShri Abhyankar 
4430fbdbba38SShri Abhyankar   /* read in nonzero values */
4431fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4432fbdbba38SShri Abhyankar 
4433fbdbba38SShri Abhyankar   /* set matrix "i" values */
4434fbdbba38SShri Abhyankar   a->i[0] = 0;
4435fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4436fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4437fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4438fbdbba38SShri Abhyankar   }
4439fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4440fbdbba38SShri Abhyankar 
4441fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4442fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4443fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4444fbdbba38SShri Abhyankar }
4445fbdbba38SShri Abhyankar 
4446ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
44477264ac53SSatish Balay {
44487264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4449dfbe8321SBarry Smith   PetscErrorCode ierr;
4450eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4451eeffb40dSHong Zhang   PetscInt k;
4452eeffb40dSHong Zhang #endif
44537264ac53SSatish Balay 
44543a40ed3dSBarry Smith   PetscFunctionBegin;
4455bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4456d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4457ca44d042SBarry Smith     *flg = PETSC_FALSE;
4458ca44d042SBarry Smith     PetscFunctionReturn(0);
4459bcd2baecSBarry Smith   }
44607264ac53SSatish Balay 
44617264ac53SSatish Balay   /* if the a->i are the same */
4462d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4463abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
44647264ac53SSatish Balay 
44657264ac53SSatish Balay   /* if a->j are the same */
446697f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4467abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4468bcd2baecSBarry Smith 
4469bcd2baecSBarry Smith   /* if a->a are the same */
4470eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4471eeffb40dSHong Zhang   for (k=0; k<a->nz; k++) {
4472eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4473eeffb40dSHong Zhang       *flg = PETSC_FALSE;
44743a40ed3dSBarry Smith       PetscFunctionReturn(0);
4475eeffb40dSHong Zhang     }
4476eeffb40dSHong Zhang   }
4477eeffb40dSHong Zhang #else
4478eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4479eeffb40dSHong Zhang #endif
4480eeffb40dSHong Zhang   PetscFunctionReturn(0);
44817264ac53SSatish Balay }
448236db0b34SBarry Smith 
448305869f15SSatish Balay /*@
448436db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
448536db0b34SBarry Smith               provided by the user.
448636db0b34SBarry Smith 
4487c75a6043SHong Zhang       Collective on MPI_Comm
448836db0b34SBarry Smith 
448936db0b34SBarry Smith    Input Parameters:
449036db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
449136db0b34SBarry Smith .   m - number of rows
449236db0b34SBarry Smith .   n - number of columns
4493483a2f95SBarry Smith .   i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
449436db0b34SBarry Smith .   j - column indices
449536db0b34SBarry Smith -   a - matrix values
449636db0b34SBarry Smith 
449736db0b34SBarry Smith    Output Parameter:
449836db0b34SBarry Smith .   mat - the matrix
449936db0b34SBarry Smith 
450036db0b34SBarry Smith    Level: intermediate
450136db0b34SBarry Smith 
450236db0b34SBarry Smith    Notes:
45030551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4504292fb18eSBarry Smith     once the matrix is destroyed and not before
450536db0b34SBarry Smith 
450636db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
450736db0b34SBarry Smith 
4508bfeeae90SHong Zhang        The i and j indices are 0 based
450936db0b34SBarry Smith 
4510a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4511a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
45128eef79e4SBarry Smith     as shown
4513a4552177SSatish Balay 
45148eef79e4SBarry Smith $        1 0 0
45158eef79e4SBarry Smith $        2 0 3
45168eef79e4SBarry Smith $        4 5 6
45178eef79e4SBarry Smith $
45188eef79e4SBarry Smith $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
45198eef79e4SBarry Smith $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
45208eef79e4SBarry Smith $        v =  {1,2,3,4,5,6}  [size = 6]
4521a4552177SSatish Balay 
45229985e31cSBarry Smith 
452369b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
452436db0b34SBarry Smith 
452536db0b34SBarry Smith @*/
4526c3c607ccSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat)
452736db0b34SBarry Smith {
4528dfbe8321SBarry Smith   PetscErrorCode ierr;
4529cbcfb4deSHong Zhang   PetscInt       ii;
453036db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4531cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4532cbcfb4deSHong Zhang   PetscInt jj;
4533cbcfb4deSHong Zhang #endif
453436db0b34SBarry Smith 
453536db0b34SBarry Smith   PetscFunctionBegin;
453641096f02SStefano Zampini   if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4537f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4538f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4539a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4540ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4541ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4542ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4543dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&aij->imax,m,&aij->ilen);CHKERRQ(ierr);
4544ab93d7beSBarry Smith 
454536db0b34SBarry Smith   aij->i            = i;
454636db0b34SBarry Smith   aij->j            = j;
454736db0b34SBarry Smith   aij->a            = a;
454836db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
454936db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4550e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4551e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
455236db0b34SBarry Smith 
455336db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
455436db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
45552515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
455660e0710aSBarry 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]);
45579985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4558a061629eSStefano Zampini       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
4559a061629eSStefano Zampini       if (j[jj] == j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
45609985e31cSBarry Smith     }
456136db0b34SBarry Smith #endif
456236db0b34SBarry Smith   }
45632515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
456436db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
456560e0710aSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
456660e0710aSBarry 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]);
456736db0b34SBarry Smith   }
456836db0b34SBarry Smith #endif
456936db0b34SBarry Smith 
4570b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4571b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
457236db0b34SBarry Smith   PetscFunctionReturn(0);
457336db0b34SBarry Smith }
457480ef6e79SMatthew G Knepley /*@C
4575d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
45768a0b0e6bSVictor Minden               provided by the user.
45778a0b0e6bSVictor Minden 
45788a0b0e6bSVictor Minden       Collective on MPI_Comm
45798a0b0e6bSVictor Minden 
45808a0b0e6bSVictor Minden    Input Parameters:
45818a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
45828a0b0e6bSVictor Minden .   m   - number of rows
45838a0b0e6bSVictor Minden .   n   - number of columns
45848a0b0e6bSVictor Minden .   i   - row indices
45858a0b0e6bSVictor Minden .   j   - column indices
45861230e6d1SVictor Minden .   a   - matrix values
45871230e6d1SVictor Minden .   nz  - number of nonzeros
45881230e6d1SVictor Minden -   idx - 0 or 1 based
45898a0b0e6bSVictor Minden 
45908a0b0e6bSVictor Minden    Output Parameter:
45918a0b0e6bSVictor Minden .   mat - the matrix
45928a0b0e6bSVictor Minden 
45938a0b0e6bSVictor Minden    Level: intermediate
45948a0b0e6bSVictor Minden 
45958a0b0e6bSVictor Minden    Notes:
45968a0b0e6bSVictor Minden        The i and j indices are 0 based
45978a0b0e6bSVictor Minden 
45988a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
45998a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
46008a0b0e6bSVictor Minden     as shown:
46018a0b0e6bSVictor Minden 
46028a0b0e6bSVictor Minden         1 0 0
46038a0b0e6bSVictor Minden         2 0 3
46048a0b0e6bSVictor Minden         4 5 6
46058a0b0e6bSVictor Minden 
46068a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
46078a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
46088a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
46098a0b0e6bSVictor Minden 
46108a0b0e6bSVictor Minden 
461169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
46128a0b0e6bSVictor Minden 
46138a0b0e6bSVictor Minden @*/
4614c3c607ccSBarry Smith PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat,PetscInt nz,PetscBool idx)
46158a0b0e6bSVictor Minden {
46168a0b0e6bSVictor Minden   PetscErrorCode ierr;
4617d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
46188a0b0e6bSVictor Minden 
46198a0b0e6bSVictor Minden 
46208a0b0e6bSVictor Minden   PetscFunctionBegin;
46211795a4d1SJed Brown   ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr);
46221230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
4623c8d679ebSHong Zhang     nnz[i[ii] - !!idx] += 1;
46241230e6d1SVictor Minden   }
46258a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
46268a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
46278a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
46281230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
46291230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
46301230e6d1SVictor Minden     if (idx) {
46311230e6d1SVictor Minden       row = i[ii] - 1;
46321230e6d1SVictor Minden       col = j[ii] - 1;
46331230e6d1SVictor Minden     } else {
46341230e6d1SVictor Minden       row = i[ii];
46351230e6d1SVictor Minden       col = j[ii];
46368a0b0e6bSVictor Minden     }
46371230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
46388a0b0e6bSVictor Minden   }
46398a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
46408a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4641d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
46428a0b0e6bSVictor Minden   PetscFunctionReturn(0);
46438a0b0e6bSVictor Minden }
464436db0b34SBarry Smith 
4645acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4646acf2f550SJed Brown {
4647acf2f550SJed Brown   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4648acf2f550SJed Brown   PetscErrorCode ierr;
4649acf2f550SJed Brown 
4650acf2f550SJed Brown   PetscFunctionBegin;
4651acf2f550SJed Brown   a->idiagvalid  = PETSC_FALSE;
4652acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
46532205254eSKarl Rupp 
4654acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4655acf2f550SJed Brown   PetscFunctionReturn(0);
4656acf2f550SJed Brown }
4657acf2f550SJed Brown 
46589c8f2541SHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
46599c8f2541SHong Zhang {
46609c8f2541SHong Zhang   PetscErrorCode ierr;
46618761c3d6SHong Zhang   PetscMPIInt    size;
46629c8f2541SHong Zhang 
46639c8f2541SHong Zhang   PetscFunctionBegin;
46648761c3d6SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
46657bbdc51dSHong Zhang   if (size == 1) {
46667bbdc51dSHong Zhang     if (scall == MAT_INITIAL_MATRIX) {
46677bbdc51dSHong Zhang       ierr = MatDuplicate(inmat,MAT_COPY_VALUES,outmat);CHKERRQ(ierr);
46687bbdc51dSHong Zhang     } else {
46698761c3d6SHong Zhang       ierr = MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
46707bbdc51dSHong Zhang     }
46718761c3d6SHong Zhang   } else {
46729c8f2541SHong Zhang     ierr = MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr);
46738761c3d6SHong Zhang   }
46749c8f2541SHong Zhang   PetscFunctionReturn(0);
46759c8f2541SHong Zhang }
46769c8f2541SHong Zhang 
467781824310SBarry Smith /*
467853dd7562SDmitry Karpeev  Permute A into C's *local* index space using rowemb,colemb.
467953dd7562SDmitry Karpeev  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
468053dd7562SDmitry Karpeev  of [0,m), colemb is in [0,n).
468153dd7562SDmitry Karpeev  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
468253dd7562SDmitry Karpeev  */
468353dd7562SDmitry Karpeev PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
468453dd7562SDmitry Karpeev {
468553dd7562SDmitry Karpeev   /* If making this function public, change the error returned in this function away from _PLIB. */
468653dd7562SDmitry Karpeev   PetscErrorCode ierr;
468753dd7562SDmitry Karpeev   Mat_SeqAIJ     *Baij;
468853dd7562SDmitry Karpeev   PetscBool      seqaij;
468953dd7562SDmitry Karpeev   PetscInt       m,n,*nz,i,j,count;
469053dd7562SDmitry Karpeev   PetscScalar    v;
469153dd7562SDmitry Karpeev   const PetscInt *rowindices,*colindices;
469253dd7562SDmitry Karpeev 
469353dd7562SDmitry Karpeev   PetscFunctionBegin;
469453dd7562SDmitry Karpeev   if (!B) PetscFunctionReturn(0);
469553dd7562SDmitry Karpeev   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
46964099cc6bSBarry Smith   ierr = PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);CHKERRQ(ierr);
469753dd7562SDmitry Karpeev   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
469853dd7562SDmitry Karpeev   if (rowemb) {
469953dd7562SDmitry Karpeev     ierr = ISGetLocalSize(rowemb,&m);CHKERRQ(ierr);
470053dd7562SDmitry Karpeev     if (m != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Row IS of size %D is incompatible with matrix row size %D",m,B->rmap->n);
470153dd7562SDmitry Karpeev   } else {
47026c4ed002SBarry Smith     if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
470353dd7562SDmitry Karpeev   }
470453dd7562SDmitry Karpeev   if (colemb) {
470553dd7562SDmitry Karpeev     ierr = ISGetLocalSize(colemb,&n);CHKERRQ(ierr);
470653dd7562SDmitry Karpeev     if (n != B->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Diag col IS of size %D is incompatible with input matrix col size %D",n,B->cmap->n);
470753dd7562SDmitry Karpeev   } else {
470853dd7562SDmitry Karpeev     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
470953dd7562SDmitry Karpeev   }
471053dd7562SDmitry Karpeev 
471153dd7562SDmitry Karpeev   Baij = (Mat_SeqAIJ*)(B->data);
471253dd7562SDmitry Karpeev   if (pattern == DIFFERENT_NONZERO_PATTERN) {
471353dd7562SDmitry Karpeev     ierr = PetscMalloc1(B->rmap->n,&nz);CHKERRQ(ierr);
471453dd7562SDmitry Karpeev     for (i=0; i<B->rmap->n; i++) {
471553dd7562SDmitry Karpeev       nz[i] = Baij->i[i+1] - Baij->i[i];
471653dd7562SDmitry Karpeev     }
471753dd7562SDmitry Karpeev     ierr = MatSeqAIJSetPreallocation(C,0,nz);CHKERRQ(ierr);
471853dd7562SDmitry Karpeev     ierr = PetscFree(nz);CHKERRQ(ierr);
471953dd7562SDmitry Karpeev   }
472053dd7562SDmitry Karpeev   if (pattern == SUBSET_NONZERO_PATTERN) {
472153dd7562SDmitry Karpeev     ierr = MatZeroEntries(C);CHKERRQ(ierr);
472253dd7562SDmitry Karpeev   }
472353dd7562SDmitry Karpeev   count = 0;
472453dd7562SDmitry Karpeev   rowindices = NULL;
472553dd7562SDmitry Karpeev   colindices = NULL;
472653dd7562SDmitry Karpeev   if (rowemb) {
472753dd7562SDmitry Karpeev     ierr = ISGetIndices(rowemb,&rowindices);CHKERRQ(ierr);
472853dd7562SDmitry Karpeev   }
472953dd7562SDmitry Karpeev   if (colemb) {
473053dd7562SDmitry Karpeev     ierr = ISGetIndices(colemb,&colindices);CHKERRQ(ierr);
473153dd7562SDmitry Karpeev   }
473253dd7562SDmitry Karpeev   for (i=0; i<B->rmap->n; i++) {
473353dd7562SDmitry Karpeev     PetscInt row;
473453dd7562SDmitry Karpeev     row = i;
473553dd7562SDmitry Karpeev     if (rowindices) row = rowindices[i];
473653dd7562SDmitry Karpeev     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
473753dd7562SDmitry Karpeev       PetscInt col;
473853dd7562SDmitry Karpeev       col  = Baij->j[count];
473953dd7562SDmitry Karpeev       if (colindices) col = colindices[col];
474053dd7562SDmitry Karpeev       v    = Baij->a[count];
474153dd7562SDmitry Karpeev       ierr = MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);CHKERRQ(ierr);
474253dd7562SDmitry Karpeev       ++count;
474353dd7562SDmitry Karpeev     }
474453dd7562SDmitry Karpeev   }
474553dd7562SDmitry Karpeev   /* FIXME: set C's nonzerostate correctly. */
474653dd7562SDmitry Karpeev   /* Assembly for C is necessary. */
474753dd7562SDmitry Karpeev   C->preallocated = PETSC_TRUE;
474853dd7562SDmitry Karpeev   C->assembled     = PETSC_TRUE;
474953dd7562SDmitry Karpeev   C->was_assembled = PETSC_FALSE;
475053dd7562SDmitry Karpeev   PetscFunctionReturn(0);
475153dd7562SDmitry Karpeev }
475253dd7562SDmitry Karpeev 
47534099cc6bSBarry Smith PetscFunctionList MatSeqAIJList = NULL;
47544099cc6bSBarry Smith 
47554099cc6bSBarry Smith /*@C
47564099cc6bSBarry Smith    MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype
47574099cc6bSBarry Smith 
47584099cc6bSBarry Smith    Collective on Mat
47594099cc6bSBarry Smith 
47604099cc6bSBarry Smith    Input Parameters:
47614099cc6bSBarry Smith +  mat      - the matrix object
47624099cc6bSBarry Smith -  matype   - matrix type
47634099cc6bSBarry Smith 
47644099cc6bSBarry Smith    Options Database Key:
47654099cc6bSBarry Smith .  -mat_seqai_type  <method> - for example seqaijcrl
47664099cc6bSBarry Smith 
47674099cc6bSBarry Smith 
47684099cc6bSBarry Smith   Level: intermediate
47694099cc6bSBarry Smith 
47704099cc6bSBarry Smith .keywords: Mat, MatType, set, method
47714099cc6bSBarry Smith 
47724099cc6bSBarry Smith .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
47734099cc6bSBarry Smith @*/
47744099cc6bSBarry Smith PetscErrorCode  MatSeqAIJSetType(Mat mat, MatType matype)
47754099cc6bSBarry Smith {
4776fd9d3c67SJed Brown   PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*);
47774099cc6bSBarry Smith   PetscBool      sametype;
47784099cc6bSBarry Smith 
47794099cc6bSBarry Smith   PetscFunctionBegin;
47804099cc6bSBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
47814099cc6bSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
47824099cc6bSBarry Smith   if (sametype) PetscFunctionReturn(0);
47834099cc6bSBarry Smith 
47844099cc6bSBarry Smith   ierr =  PetscFunctionListFind(MatSeqAIJList,matype,&r);CHKERRQ(ierr);
47854099cc6bSBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
47864099cc6bSBarry Smith   ierr = (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);CHKERRQ(ierr);
47874099cc6bSBarry Smith   PetscFunctionReturn(0);
47884099cc6bSBarry Smith }
47894099cc6bSBarry Smith 
47904099cc6bSBarry Smith 
47914099cc6bSBarry Smith /*@C
47924099cc6bSBarry Smith   MatSeqAIJRegister -  - Adds a new sub-matrix type for sequential AIJ matrices
47934099cc6bSBarry Smith 
47944099cc6bSBarry Smith    Not Collective
47954099cc6bSBarry Smith 
47964099cc6bSBarry Smith    Input Parameters:
47974099cc6bSBarry Smith +  name - name of a new user-defined matrix type, for example MATSEQAIJCRL
47984099cc6bSBarry Smith -  function - routine to convert to subtype
47994099cc6bSBarry Smith 
48004099cc6bSBarry Smith    Notes:
48014099cc6bSBarry Smith    MatSeqAIJRegister() may be called multiple times to add several user-defined solvers.
48024099cc6bSBarry Smith 
48034099cc6bSBarry Smith 
48044099cc6bSBarry Smith    Then, your matrix can be chosen with the procedural interface at runtime via the option
48054099cc6bSBarry Smith $     -mat_seqaij_type my_mat
48064099cc6bSBarry Smith 
48074099cc6bSBarry Smith    Level: advanced
48084099cc6bSBarry Smith 
48094099cc6bSBarry Smith .keywords: Mat, register
48104099cc6bSBarry Smith 
48114099cc6bSBarry Smith .seealso: MatSeqAIJRegisterAll()
48124099cc6bSBarry Smith 
48134099cc6bSBarry Smith 
48144099cc6bSBarry Smith   Level: advanced
48154099cc6bSBarry Smith @*/
4816388d47a6SSatish Balay PetscErrorCode  MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *))
48174099cc6bSBarry Smith {
48184099cc6bSBarry Smith   PetscErrorCode ierr;
48194099cc6bSBarry Smith 
48204099cc6bSBarry Smith   PetscFunctionBegin;
48219cc31a68SJed Brown   ierr = MatInitializePackage();CHKERRQ(ierr);
48224099cc6bSBarry Smith   ierr = PetscFunctionListAdd(&MatSeqAIJList,sname,function);CHKERRQ(ierr);
48234099cc6bSBarry Smith   PetscFunctionReturn(0);
48244099cc6bSBarry Smith }
48254099cc6bSBarry Smith 
48264099cc6bSBarry Smith PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;
48274099cc6bSBarry Smith 
48284099cc6bSBarry Smith /*@C
48294099cc6bSBarry Smith   MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ
48304099cc6bSBarry Smith 
48314099cc6bSBarry Smith   Not Collective
48324099cc6bSBarry Smith 
48334099cc6bSBarry Smith   Level: advanced
48344099cc6bSBarry Smith 
48354099cc6bSBarry Smith   Developers Note: CUSP and CUSPARSE do not yet support the  MatConvert_SeqAIJ..() paradigm and thus cannot be registered here
48364099cc6bSBarry Smith 
48374099cc6bSBarry Smith .keywords: KSP, register, all
48384099cc6bSBarry Smith 
48394099cc6bSBarry Smith .seealso:  MatRegisterAll(), MatSeqAIJRegister()
48404099cc6bSBarry Smith @*/
48414099cc6bSBarry Smith PetscErrorCode  MatSeqAIJRegisterAll(void)
48424099cc6bSBarry Smith {
48434099cc6bSBarry Smith   PetscErrorCode ierr;
48444099cc6bSBarry Smith 
48454099cc6bSBarry Smith   PetscFunctionBegin;
48464099cc6bSBarry Smith   if (MatSeqAIJRegisterAllCalled) PetscFunctionReturn(0);
48474099cc6bSBarry Smith   MatSeqAIJRegisterAllCalled = PETSC_TRUE;
48484099cc6bSBarry Smith 
48494099cc6bSBarry Smith   ierr = MatSeqAIJRegister(MATSEQAIJCRL,      MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
48504099cc6bSBarry Smith   ierr = MatSeqAIJRegister(MATSEQAIJPERM,     MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
48514dfdc2d9SRichard Tran Mills   ierr = MatSeqAIJRegister(MATSEQAIJSELL,     MatConvert_SeqAIJ_SeqAIJSELL);CHKERRQ(ierr);
48529779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE)
48536b62b571SRichard Tran Mills   ierr = MatSeqAIJRegister(MATSEQAIJMKL,      MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr);
4854485f9817SRichard Tran Mills #endif
48554099cc6bSBarry Smith #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
48564099cc6bSBarry Smith   ierr = MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);CHKERRQ(ierr);
48574099cc6bSBarry Smith #endif
48584099cc6bSBarry Smith   PetscFunctionReturn(0);
48594099cc6bSBarry Smith }
486053dd7562SDmitry Karpeev 
486153dd7562SDmitry Karpeev /*
486281824310SBarry Smith     Special version for direct calls from Fortran
486381824310SBarry Smith */
4864af0996ceSBarry Smith #include <petsc/private/fortranimpl.h>
486581824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
486681824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
486781824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
486881824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
486981824310SBarry Smith #endif
487081824310SBarry Smith 
487181824310SBarry Smith /* Change these macros so can be used in void function */
487281824310SBarry Smith #undef CHKERRQ
4873ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
487481824310SBarry Smith #undef SETERRQ2
4875e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
48764994cf47SJed Brown #undef SETERRQ3
48774994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
487881824310SBarry Smith 
48798cc058d9SJed 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)
488081824310SBarry Smith {
488181824310SBarry Smith   Mat            A  = *AA;
488281824310SBarry Smith   PetscInt       m  = *mm, n = *nn;
488381824310SBarry Smith   InsertMode     is = *isis;
488481824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
488581824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
488681824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
488781824310SBarry Smith   PetscErrorCode ierr;
488881824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
488954f21887SBarry Smith   MatScalar      *ap,value,*aa;
4890ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4891ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
489281824310SBarry Smith 
489381824310SBarry Smith   PetscFunctionBegin;
48944994cf47SJed Brown   MatCheckPreallocated(A,1);
489581824310SBarry Smith   imax  = a->imax;
489681824310SBarry Smith   ai    = a->i;
489781824310SBarry Smith   ailen = a->ilen;
489881824310SBarry Smith   aj    = a->j;
489981824310SBarry Smith   aa    = a->a;
490081824310SBarry Smith 
490181824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
490281824310SBarry Smith     row = im[k];
490381824310SBarry Smith     if (row < 0) continue;
490481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4905ce94432eSBarry Smith     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
490681824310SBarry Smith #endif
490781824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
490881824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
490981824310SBarry Smith     low  = 0;
491081824310SBarry Smith     high = nrow;
491181824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
491281824310SBarry Smith       if (in[l] < 0) continue;
491381824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4914ce94432eSBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
491581824310SBarry Smith #endif
491681824310SBarry Smith       col = in[l];
49172205254eSKarl Rupp       if (roworiented) value = v[l + k*n];
49182205254eSKarl Rupp       else value = v[k + l*m];
49192205254eSKarl Rupp 
492081824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
492181824310SBarry Smith 
49222205254eSKarl Rupp       if (col <= lastcol) low = 0;
49232205254eSKarl Rupp       else high = nrow;
492481824310SBarry Smith       lastcol = col;
492581824310SBarry Smith       while (high-low > 5) {
492681824310SBarry Smith         t = (low+high)/2;
492781824310SBarry Smith         if (rp[t] > col) high = t;
492881824310SBarry Smith         else             low  = t;
492981824310SBarry Smith       }
493081824310SBarry Smith       for (i=low; i<high; i++) {
493181824310SBarry Smith         if (rp[i] > col) break;
493281824310SBarry Smith         if (rp[i] == col) {
493381824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
493481824310SBarry Smith           else                  ap[i] = value;
493581824310SBarry Smith           goto noinsert;
493681824310SBarry Smith         }
493781824310SBarry Smith       }
493881824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
493981824310SBarry Smith       if (nonew == 1) goto noinsert;
4940ce94432eSBarry Smith       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4941fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
494281824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
494381824310SBarry Smith       /* shift up all the later entries in this row */
494481824310SBarry Smith       for (ii=N; ii>=i; ii--) {
494581824310SBarry Smith         rp[ii+1] = rp[ii];
494681824310SBarry Smith         ap[ii+1] = ap[ii];
494781824310SBarry Smith       }
494881824310SBarry Smith       rp[i] = col;
494981824310SBarry Smith       ap[i] = value;
4950e56f5c9eSBarry Smith       A->nonzerostate++;
495181824310SBarry Smith noinsert:;
495281824310SBarry Smith       low = i + 1;
495381824310SBarry Smith     }
495481824310SBarry Smith     ailen[row] = nrow;
495581824310SBarry Smith   }
495681824310SBarry Smith   PetscFunctionReturnVoid();
495781824310SBarry Smith }
4958