xref: /petsc/src/mat/impls/aij/seq/aij.c (revision fdc842d1a43d22858d8041cc45d2f5b4ad073a0b)
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) {
4680c0d7e18SFande Kong             if (is == ADD_VALUES) {
4690c0d7e18SFande Kong               ap[i] += value;
4700c0d7e18SFande Kong               (void)PetscLogFlops(1.0);
4710c0d7e18SFande Kong             }
47217ab2063SBarry Smith             else ap[i] = value;
473720833daSHong Zhang           }
474e44c0bd4SBarry Smith           low = i + 1;
47517ab2063SBarry Smith           goto noinsert;
47617ab2063SBarry Smith         }
47717ab2063SBarry Smith       }
478dcd36c23SBarry Smith       if (value == 0.0 && ignorezeroentries && row != col) goto noinsert;
479c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
480e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
481720833daSHong Zhang       if (A->structure_only) {
482876c6284SHong Zhang         MatSeqXAIJReallocateAIJ_structure_only(A,A->rmap->n,1,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar);
483720833daSHong Zhang       } else {
484fef13f97SBarry Smith         MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
485720833daSHong Zhang       }
486c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
487416022c9SBarry Smith       /* shift up all the later entries in this row */
488416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
48917ab2063SBarry Smith         rp[ii+1] = rp[ii];
490876c6284SHong Zhang         if (!A->structure_only) ap[ii+1] = ap[ii];
491720833daSHong Zhang       }
49217ab2063SBarry Smith       rp[i] = col;
493876c6284SHong Zhang       if (!A->structure_only) ap[i] = value;
494416022c9SBarry Smith       low   = i + 1;
495e56f5c9eSBarry Smith       A->nonzerostate++;
496e44c0bd4SBarry Smith noinsert:;
49717ab2063SBarry Smith     }
49817ab2063SBarry Smith     ailen[row] = nrow;
49917ab2063SBarry Smith   }
5003a40ed3dSBarry Smith   PetscFunctionReturn(0);
50117ab2063SBarry Smith }
50217ab2063SBarry Smith 
50381824310SBarry Smith 
504a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
5057eb43aa7SLois Curfman McInnes {
5067eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
50797f1f81fSBarry Smith   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
50897f1f81fSBarry Smith   PetscInt   *ai = a->i,*ailen = a->ilen;
50954f21887SBarry Smith   MatScalar  *ap,*aa = a->a;
5107eb43aa7SLois Curfman McInnes 
5113a40ed3dSBarry Smith   PetscFunctionBegin;
5127eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
5137eb43aa7SLois Curfman McInnes     row = im[k];
514e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
515e32f2f54SBarry 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);
516bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
5177eb43aa7SLois Curfman McInnes     nrow = ailen[row];
5187eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
519e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
520e32f2f54SBarry 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);
521bfeeae90SHong Zhang       col  = in[l];
5227eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
5237eb43aa7SLois Curfman McInnes       while (high-low > 5) {
5247eb43aa7SLois Curfman McInnes         t = (low+high)/2;
5257eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
5267eb43aa7SLois Curfman McInnes         else low = t;
5277eb43aa7SLois Curfman McInnes       }
5287eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
5297eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
5307eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
531b49de8d1SLois Curfman McInnes           *v++ = ap[i];
5327eb43aa7SLois Curfman McInnes           goto finished;
5337eb43aa7SLois Curfman McInnes         }
5347eb43aa7SLois Curfman McInnes       }
53597e567efSBarry Smith       *v++ = 0.0;
5367eb43aa7SLois Curfman McInnes finished:;
5377eb43aa7SLois Curfman McInnes     }
5387eb43aa7SLois Curfman McInnes   }
5393a40ed3dSBarry Smith   PetscFunctionReturn(0);
5407eb43aa7SLois Curfman McInnes }
5417eb43aa7SLois Curfman McInnes 
54217ab2063SBarry Smith 
543dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
54417ab2063SBarry Smith {
545416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5466849ba73SBarry Smith   PetscErrorCode ierr;
5476f69ff64SBarry Smith   PetscInt       i,*col_lens;
5486f69ff64SBarry Smith   int            fd;
549b37d52dbSMark F. Adams   FILE           *file;
55017ab2063SBarry Smith 
5513a40ed3dSBarry Smith   PetscFunctionBegin;
552b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
553854ce69bSBarry Smith   ierr = PetscMalloc1(4+A->rmap->n,&col_lens);CHKERRQ(ierr);
5542205254eSKarl Rupp 
5550700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
556d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
557d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
558416022c9SBarry Smith   col_lens[3] = a->nz;
559416022c9SBarry Smith 
560416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
561d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
562416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
56317ab2063SBarry Smith   }
564d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
565606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
566416022c9SBarry Smith 
567416022c9SBarry Smith   /* store column indices (zero start index) */
5686f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
569416022c9SBarry Smith 
570416022c9SBarry Smith   /* store nonzero values */
5716f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
572b37d52dbSMark F. Adams 
573b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
574b37d52dbSMark F. Adams   if (file) {
57533d57670SJed Brown     fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs));
576b37d52dbSMark F. Adams   }
5773a40ed3dSBarry Smith   PetscFunctionReturn(0);
57817ab2063SBarry Smith }
579416022c9SBarry Smith 
5807dc0baabSHong Zhang static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer)
5817dc0baabSHong Zhang {
5827dc0baabSHong Zhang   PetscErrorCode ierr;
5837dc0baabSHong Zhang   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5847dc0baabSHong Zhang   PetscInt       i,k,m=A->rmap->N;
5857dc0baabSHong Zhang 
5867dc0baabSHong Zhang   PetscFunctionBegin;
5877dc0baabSHong Zhang   ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5887dc0baabSHong Zhang   for (i=0; i<m; i++) {
5897dc0baabSHong Zhang     ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
5907dc0baabSHong Zhang     for (k=a->i[i]; k<a->i[i+1]; k++) {
5917dc0baabSHong Zhang       ierr = PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);CHKERRQ(ierr);
5927dc0baabSHong Zhang     }
5937dc0baabSHong Zhang     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
5947dc0baabSHong Zhang   }
5957dc0baabSHong Zhang   ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5967dc0baabSHong Zhang   PetscFunctionReturn(0);
5977dc0baabSHong Zhang }
5987dc0baabSHong Zhang 
59909573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
600cd155464SBarry Smith 
601dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
602416022c9SBarry Smith {
603416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
604dfbe8321SBarry Smith   PetscErrorCode    ierr;
60560e0710aSBarry Smith   PetscInt          i,j,m = A->rmap->n;
606e060cb09SBarry Smith   const char        *name;
607f3ef73ceSBarry Smith   PetscViewerFormat format;
60817ab2063SBarry Smith 
6093a40ed3dSBarry Smith   PetscFunctionBegin;
6107dc0baabSHong Zhang   if (A->structure_only) {
6117dc0baabSHong Zhang     ierr = MatView_SeqAIJ_ASCII_structonly(A,viewer);CHKERRQ(ierr);
6127dc0baabSHong Zhang     PetscFunctionReturn(0);
6137dc0baabSHong Zhang   }
61443e49210SHong Zhang 
615b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
61671c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
61797f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
61860e0710aSBarry Smith     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
619c337ccceSJed Brown       /* Need a dummy value to ensure the dimension of the matrix. */
620d00d2cf4SBarry Smith       nofinalvalue = 1;
621d00d2cf4SBarry Smith     }
622d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
623d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
62477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
625fbfe6fa7SJed Brown #if defined(PETSC_USE_COMPLEX)
626fbfe6fa7SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
627fbfe6fa7SJed Brown #else
62877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
629fbfe6fa7SJed Brown #endif
630b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
63117ab2063SBarry Smith 
63217ab2063SBarry Smith     for (i=0; i<m; i++) {
63360e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
634aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
635a9bf72d8SJed 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);
63617ab2063SBarry Smith #else
63760e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr);
63817ab2063SBarry Smith #endif
63917ab2063SBarry Smith       }
64017ab2063SBarry Smith     }
641d00d2cf4SBarry Smith     if (nofinalvalue) {
642c337ccceSJed Brown #if defined(PETSC_USE_COMPLEX)
643c337ccceSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr);
644c337ccceSJed Brown #else
645d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
646c337ccceSJed Brown #endif
647d00d2cf4SBarry Smith     }
648317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
649fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
650d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
65168369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
652cd155464SBarry Smith     PetscFunctionReturn(0);
653fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
654d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
65544cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
65677431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
65760e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
658aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
65936db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
66060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
66136db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
66260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
66336db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
66460e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
6656831982aSBarry Smith         }
66644cd7ae7SLois Curfman McInnes #else
66760e0710aSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);}
66844cd7ae7SLois Curfman McInnes #endif
66944cd7ae7SLois Curfman McInnes       }
670b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
67144cd7ae7SLois Curfman McInnes     }
672d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
673fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
67497f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
675d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
676854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&sptr);CHKERRQ(ierr);
677496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
678496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
67960e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
680496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
681aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
68236db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
683496be53dSLois Curfman McInnes #else
684496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
685496be53dSLois Curfman McInnes #endif
686496be53dSLois Curfman McInnes         }
687496be53dSLois Curfman McInnes       }
688496be53dSLois Curfman McInnes     }
6892e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
69077431f27SBarry Smith     ierr    = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
6912e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
6922205254eSKarl Rupp       if (i+4<m) {
6932205254eSKarl 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);
6942205254eSKarl Rupp       } else if (i+3<m) {
6952205254eSKarl 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);
6962205254eSKarl Rupp       } else if (i+2<m) {
6972205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);
6982205254eSKarl Rupp       } else if (i+1<m) {
6992205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);
7002205254eSKarl Rupp       } else if (i<m) {
7012205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);
7022205254eSKarl Rupp       } else {
7032205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);
7042205254eSKarl Rupp       }
705496be53dSLois Curfman McInnes     }
706b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
707606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
708496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
70960e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
71077431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
711496be53dSLois Curfman McInnes       }
712b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
713496be53dSLois Curfman McInnes     }
714b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
715496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
71660e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
717496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
718aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
71936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
72060e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
7216831982aSBarry Smith           }
722496be53dSLois Curfman McInnes #else
72360e0710aSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);}
724496be53dSLois Curfman McInnes #endif
725496be53dSLois Curfman McInnes         }
726496be53dSLois Curfman McInnes       }
727b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
728496be53dSLois Curfman McInnes     }
729d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
730fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
73197f1f81fSBarry Smith     PetscInt    cnt = 0,jcnt;
73287828ca2SBarry Smith     PetscScalar value;
73368f1ed48SBarry Smith #if defined(PETSC_USE_COMPLEX)
73468f1ed48SBarry Smith     PetscBool   realonly = PETSC_TRUE;
73568f1ed48SBarry Smith 
73668f1ed48SBarry Smith     for (i=0; i<a->i[m]; i++) {
73768f1ed48SBarry Smith       if (PetscImaginaryPart(a->a[i]) != 0.0) {
73868f1ed48SBarry Smith         realonly = PETSC_FALSE;
73968f1ed48SBarry Smith         break;
74068f1ed48SBarry Smith       }
74168f1ed48SBarry Smith     }
74268f1ed48SBarry Smith #endif
74302594712SBarry Smith 
744d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
74502594712SBarry Smith     for (i=0; i<m; i++) {
74602594712SBarry Smith       jcnt = 0;
747d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
748e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
74902594712SBarry Smith           value = a->a[cnt++];
750e24b481bSBarry Smith           jcnt++;
75102594712SBarry Smith         } else {
75202594712SBarry Smith           value = 0.0;
75302594712SBarry Smith         }
754aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
75568f1ed48SBarry Smith         if (realonly) {
75660e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr);
75768f1ed48SBarry Smith         } else {
75860e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr);
75968f1ed48SBarry Smith         }
76002594712SBarry Smith #else
76160e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr);
76202594712SBarry Smith #endif
76302594712SBarry Smith       }
764b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
76502594712SBarry Smith     }
766d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7673c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
768150b93efSMatthew G. Knepley     PetscInt fshift=1;
769d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
7703c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
77119303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");CHKERRQ(ierr);
7723c215bfdSMatthew Knepley #else
77319303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");CHKERRQ(ierr);
7743c215bfdSMatthew Knepley #endif
775d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
7763c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
77760e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
7783c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
779a9a0e077SKarl 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);
7803c215bfdSMatthew Knepley #else
781150b93efSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr);
7823c215bfdSMatthew Knepley #endif
7833c215bfdSMatthew Knepley       }
7843c215bfdSMatthew Knepley     }
785d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7863a40ed3dSBarry Smith   } else {
787d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
788d5f3da31SBarry Smith     if (A->factortype) {
78916cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
79016cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
79116cd7e1dSShri Abhyankar         /* L part */
79260e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
79316cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
79416cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
79560e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
79616cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
7976712e2f1SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
79816cd7e1dSShri Abhyankar           } else {
79960e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
80016cd7e1dSShri Abhyankar           }
80116cd7e1dSShri Abhyankar #else
80260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
80316cd7e1dSShri Abhyankar #endif
80416cd7e1dSShri Abhyankar         }
80516cd7e1dSShri Abhyankar         /* diagonal */
80616cd7e1dSShri Abhyankar         j = a->diag[i];
80716cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
80816cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
80960e0710aSBarry 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);
81016cd7e1dSShri Abhyankar         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
8116712e2f1SBarry 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);
81216cd7e1dSShri Abhyankar         } else {
81360e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
81416cd7e1dSShri Abhyankar         }
81516cd7e1dSShri Abhyankar #else
81660e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr);
81716cd7e1dSShri Abhyankar #endif
81816cd7e1dSShri Abhyankar 
81916cd7e1dSShri Abhyankar         /* U part */
82060e0710aSBarry Smith         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
82116cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
82216cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
82360e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
82416cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
82522ab088eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
82616cd7e1dSShri Abhyankar           } else {
82760e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
82816cd7e1dSShri Abhyankar           }
82916cd7e1dSShri Abhyankar #else
83060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
83116cd7e1dSShri Abhyankar #endif
83216cd7e1dSShri Abhyankar         }
83316cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
83416cd7e1dSShri Abhyankar       }
83516cd7e1dSShri Abhyankar     } else {
83617ab2063SBarry Smith       for (i=0; i<m; i++) {
83777431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
83860e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
839aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
84036db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
84160e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
84236db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
84360e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
8443a40ed3dSBarry Smith           } else {
84560e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
84617ab2063SBarry Smith           }
84717ab2063SBarry Smith #else
84860e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
84917ab2063SBarry Smith #endif
85017ab2063SBarry Smith         }
851b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
85217ab2063SBarry Smith       }
85316cd7e1dSShri Abhyankar     }
854d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
85517ab2063SBarry Smith   }
856b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8573a40ed3dSBarry Smith   PetscFunctionReturn(0);
858416022c9SBarry Smith }
859416022c9SBarry Smith 
8609804daf3SBarry Smith #include <petscdraw.h>
861dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
862416022c9SBarry Smith {
863480ef9eaSBarry Smith   Mat               A  = (Mat) Aa;
864416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
865dfbe8321SBarry Smith   PetscErrorCode    ierr;
866383922c3SLisandro Dalcin   PetscInt          i,j,m = A->rmap->n;
867383922c3SLisandro Dalcin   int               color;
868b05fc000SLisandro Dalcin   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
869b0a32e0cSBarry Smith   PetscViewer       viewer;
870f3ef73ceSBarry Smith   PetscViewerFormat format;
871cddf8d76SBarry Smith 
8723a40ed3dSBarry Smith   PetscFunctionBegin;
873480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
874b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
875b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
876383922c3SLisandro Dalcin 
877416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
8780513a670SBarry Smith 
879fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
880383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
8810513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
882b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
883416022c9SBarry Smith     for (i=0; i<m; i++) {
884cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
885bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
886bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
88736db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
888b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
889cddf8d76SBarry Smith       }
890cddf8d76SBarry Smith     }
891b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
892cddf8d76SBarry Smith     for (i=0; i<m; i++) {
893cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
894bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
895bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
896cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
897b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
898cddf8d76SBarry Smith       }
899cddf8d76SBarry Smith     }
900b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
901cddf8d76SBarry Smith     for (i=0; i<m; i++) {
902cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
903bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
904bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
90536db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
906b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
907416022c9SBarry Smith       }
908416022c9SBarry Smith     }
909383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
9100513a670SBarry Smith   } else {
9110513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
9120513a670SBarry Smith     /* first determine max of all nonzero values */
913b05fc000SLisandro Dalcin     PetscReal minv = 0.0, maxv = 0.0;
914383922c3SLisandro Dalcin     PetscInt  nz = a->nz, count = 0;
915b0a32e0cSBarry Smith     PetscDraw popup;
9160513a670SBarry Smith 
9170513a670SBarry Smith     for (i=0; i<nz; i++) {
9180513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
9190513a670SBarry Smith     }
920383922c3SLisandro Dalcin     if (minv >= maxv) maxv = minv + PETSC_SMALL;
921b0a32e0cSBarry Smith     ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
92245f3bb6eSLisandro Dalcin     ierr = PetscDrawScalePopup(popup,minv,maxv);CHKERRQ(ierr);
923383922c3SLisandro Dalcin 
924383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
9250513a670SBarry Smith     for (i=0; i<m; i++) {
926383922c3SLisandro Dalcin       y_l = m - i - 1.0;
927383922c3SLisandro Dalcin       y_r = y_l + 1.0;
928bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
929383922c3SLisandro Dalcin         x_l = a->j[j];
930383922c3SLisandro Dalcin         x_r = x_l + 1.0;
931b05fc000SLisandro Dalcin         color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
932b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
9330513a670SBarry Smith         count++;
9340513a670SBarry Smith       }
9350513a670SBarry Smith     }
936383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
9370513a670SBarry Smith   }
938480ef9eaSBarry Smith   PetscFunctionReturn(0);
939480ef9eaSBarry Smith }
940cddf8d76SBarry Smith 
9419804daf3SBarry Smith #include <petscdraw.h>
942dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
943480ef9eaSBarry Smith {
944dfbe8321SBarry Smith   PetscErrorCode ierr;
945b0a32e0cSBarry Smith   PetscDraw      draw;
94636db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
947ace3abfcSBarry Smith   PetscBool      isnull;
948480ef9eaSBarry Smith 
949480ef9eaSBarry Smith   PetscFunctionBegin;
950b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
951b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
952480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
953480ef9eaSBarry Smith 
954d0f46423SBarry Smith   xr   = A->cmap->n; yr  = A->rmap->n; h = yr/10.0; w = xr/10.0;
955480ef9eaSBarry Smith   xr  += w;          yr += h;         xl = -w;     yl = -h;
956b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
957832b7cebSLisandro Dalcin   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
958b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
9590298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
960832b7cebSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
9613a40ed3dSBarry Smith   PetscFunctionReturn(0);
962416022c9SBarry Smith }
963416022c9SBarry Smith 
964dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
965416022c9SBarry Smith {
966dfbe8321SBarry Smith   PetscErrorCode ierr;
967ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
968416022c9SBarry Smith 
9693a40ed3dSBarry Smith   PetscFunctionBegin;
970251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
971251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
972251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
973c45a1595SBarry Smith   if (iascii) {
9743a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
9750f5bd95cSBarry Smith   } else if (isbinary) {
9763a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
9770f5bd95cSBarry Smith   } else if (isdraw) {
9783a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
97911aeaf0aSBarry Smith   }
9804108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
9813a40ed3dSBarry Smith   PetscFunctionReturn(0);
98217ab2063SBarry Smith }
98319bcc07fSBarry Smith 
984dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
98517ab2063SBarry Smith {
986416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9876849ba73SBarry Smith   PetscErrorCode ierr;
98897f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
989d0f46423SBarry Smith   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
99054f21887SBarry Smith   MatScalar      *aa    = a->a,*ap;
9913447b6efSHong Zhang   PetscReal      ratio  = 0.6;
99217ab2063SBarry Smith 
9933a40ed3dSBarry Smith   PetscFunctionBegin;
9943a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
99517ab2063SBarry Smith 
99643ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
99717ab2063SBarry Smith   for (i=1; i<m; i++) {
998416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
99917ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
100094a9d846SBarry Smith     rmax    = PetscMax(rmax,ailen[i]);
100117ab2063SBarry Smith     if (fshift) {
1002bfeeae90SHong Zhang       ip = aj + ai[i];
1003bfeeae90SHong Zhang       ap = aa + ai[i];
100417ab2063SBarry Smith       N  = ailen[i];
100517ab2063SBarry Smith       for (j=0; j<N; j++) {
100617ab2063SBarry Smith         ip[j-fshift] = ip[j];
1007876c6284SHong Zhang         if (!A->structure_only) ap[j-fshift] = ap[j];
100817ab2063SBarry Smith       }
100917ab2063SBarry Smith     }
101017ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
101117ab2063SBarry Smith   }
101217ab2063SBarry Smith   if (m) {
101317ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
101417ab2063SBarry Smith     ai[m]   = ai[m-1] + ailen[m-1];
101517ab2063SBarry Smith   }
10167b083b7cSBarry Smith 
101717ab2063SBarry Smith   /* reset ilen and imax for each row */
10187b083b7cSBarry Smith   a->nonzerorowcnt = 0;
1019396832f4SHong Zhang   if (A->structure_only) {
1020396832f4SHong Zhang     ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
1021396832f4SHong Zhang   } else { /* !A->structure_only */
102217ab2063SBarry Smith     for (i=0; i<m; i++) {
102317ab2063SBarry Smith       ailen[i] = imax[i] = ai[i+1] - ai[i];
10247b083b7cSBarry Smith       a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
102517ab2063SBarry Smith     }
1026396832f4SHong Zhang   }
1027bfeeae90SHong Zhang   a->nz = ai[m];
102865e19b50SBarry 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);
102917ab2063SBarry Smith 
103009f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1031d0f46423SBarry 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);
1032ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
1033ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
10342205254eSKarl Rupp 
10358e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
1036dd5f02e7SSatish Balay   a->reallocs         = 0;
10376712e2f1SBarry Smith   A->info.nz_unneeded = (PetscReal)fshift;
103836db0b34SBarry Smith   a->rmax             = rmax;
10394e220ebcSLois Curfman McInnes 
1040396832f4SHong Zhang   if (!A->structure_only) {
104111e456e1SBarry Smith     ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
1042396832f4SHong Zhang   }
10434108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
1044acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10453a40ed3dSBarry Smith   PetscFunctionReturn(0);
104617ab2063SBarry Smith }
104717ab2063SBarry Smith 
104899cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
104999cafbc1SBarry Smith {
105099cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
105199cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
105254f21887SBarry Smith   MatScalar      *aa = a->a;
1053acf2f550SJed Brown   PetscErrorCode ierr;
105499cafbc1SBarry Smith 
105599cafbc1SBarry Smith   PetscFunctionBegin;
105699cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1057acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
105899cafbc1SBarry Smith   PetscFunctionReturn(0);
105999cafbc1SBarry Smith }
106099cafbc1SBarry Smith 
106199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
106299cafbc1SBarry Smith {
106399cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
106499cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
106554f21887SBarry Smith   MatScalar      *aa = a->a;
1066acf2f550SJed Brown   PetscErrorCode ierr;
106799cafbc1SBarry Smith 
106899cafbc1SBarry Smith   PetscFunctionBegin;
106999cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1070acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
107199cafbc1SBarry Smith   PetscFunctionReturn(0);
107299cafbc1SBarry Smith }
107399cafbc1SBarry Smith 
1074dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
107517ab2063SBarry Smith {
1076416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1077dfbe8321SBarry Smith   PetscErrorCode ierr;
10783a40ed3dSBarry Smith 
10793a40ed3dSBarry Smith   PetscFunctionBegin;
1080d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
1081acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
10823a40ed3dSBarry Smith   PetscFunctionReturn(0);
108317ab2063SBarry Smith }
1084416022c9SBarry Smith 
1085dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
108617ab2063SBarry Smith {
1087416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1088dfbe8321SBarry Smith   PetscErrorCode ierr;
1089d5d45c9bSBarry Smith 
10903a40ed3dSBarry Smith   PetscFunctionBegin;
1091aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1092d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
109317ab2063SBarry Smith #endif
1094e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
10956bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
10966bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
109705b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
1098d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
109905b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
1100846b4da1SFande Kong   ierr = PetscFree(a->ipre);CHKERRQ(ierr);
110171f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
110205b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
11036bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
110405b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
11056bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
1106cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
11070b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
1108a30b2313SHong Zhang 
11094108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
1110bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
1111901853e0SKris Buschelman 
1112dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
1113bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
1114bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1115bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1116bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr);
1117bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr);
1118bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr);
1119af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
1120af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);CHKERRQ(ierr);
1121af8000cdSHong Zhang #endif
112263c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE)
112363c07aadSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);CHKERRQ(ierr);
11243dad0653Sstefano_zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMatMatMult_transpose_seqaij_seqaij_C",NULL);CHKERRQ(ierr);
112563c07aadSStefano Zampini #endif
1126b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);CHKERRQ(ierr);
1127c9225affSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsell_C",NULL);CHKERRQ(ierr);
1128c9225affSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_is_C",NULL);CHKERRQ(ierr);
1129bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1130bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1131846b4da1SFande Kong   ierr = PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);CHKERRQ(ierr);
1132bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1133bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr);
113475d48cdbSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatPtAP_is_seqaij_C",NULL);CHKERRQ(ierr);
11353a40ed3dSBarry Smith   PetscFunctionReturn(0);
113617ab2063SBarry Smith }
113717ab2063SBarry Smith 
1138ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
113917ab2063SBarry Smith {
1140416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11414846f1f5SKris Buschelman   PetscErrorCode ierr;
11423a40ed3dSBarry Smith 
11433a40ed3dSBarry Smith   PetscFunctionBegin;
1144a65d3064SKris Buschelman   switch (op) {
1145a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
11464e0d8c25SBarry Smith     a->roworiented = flg;
1147a65d3064SKris Buschelman     break;
1148a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1149a9817697SBarry Smith     a->keepnonzeropattern = flg;
1150a65d3064SKris Buschelman     break;
1151512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1152512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1153a65d3064SKris Buschelman     break;
1154a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
11554e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1156a65d3064SKris Buschelman     break;
1157a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
11584e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1159a65d3064SKris Buschelman     break;
116028b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
116128b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
116228b2fa4aSMatthew Knepley     break;
1163a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11644e0d8c25SBarry Smith     a->ignorezeroentries = flg;
11650df259c2SBarry Smith     break;
11663d472b54SHong Zhang   case MAT_SPD:
1167b1646e73SJed Brown   case MAT_SYMMETRIC:
1168b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1169b1646e73SJed Brown   case MAT_HERMITIAN:
1170b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
1171957cac9fSHong Zhang   case MAT_STRUCTURE_ONLY:
11725021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
11735021d80fSJed Brown     break;
11744e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1175a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1176a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1177290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1178a65d3064SKris Buschelman     break;
1179b87ac2d8SJed Brown   case MAT_USE_INODES:
1180b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1181b87ac2d8SJed Brown     break;
1182c10200c1SHong Zhang   case MAT_SUBMAT_SINGLEIS:
1183c10200c1SHong Zhang     A->submat_singleis = flg;
1184c10200c1SHong Zhang     break;
1185a65d3064SKris Buschelman   default:
1186e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1187a65d3064SKris Buschelman   }
11884108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
11893a40ed3dSBarry Smith   PetscFunctionReturn(0);
119017ab2063SBarry Smith }
119117ab2063SBarry Smith 
1192dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
119317ab2063SBarry Smith {
1194416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
11956849ba73SBarry Smith   PetscErrorCode ierr;
1196*fdc842d1SBarry Smith   PetscInt       i,j,n,*ai=a->i,*aj=a->j;
1197*fdc842d1SBarry Smith   PetscScalar    *aa=a->a,*x;
119817ab2063SBarry Smith 
11993a40ed3dSBarry Smith   PetscFunctionBegin;
1200d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1201e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
120235e7444dSHong Zhang 
1203d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1204d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
1205*fdc842d1SBarry Smith     ierr = VecGetArrayWrite(v,&x);CHKERRQ(ierr);
12062c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
1207*fdc842d1SBarry Smith     ierr = VecRestoreArrayWrite(v,&x);CHKERRQ(ierr);
120835e7444dSHong Zhang     PetscFunctionReturn(0);
120935e7444dSHong Zhang   }
121035e7444dSHong Zhang 
1211*fdc842d1SBarry Smith   ierr = VecGetArrayWrite(v,&x);CHKERRQ(ierr);
121235e7444dSHong Zhang   for (i=0; i<n; i++) {
1213*fdc842d1SBarry Smith     x[i] = 0.0;
121435e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++) {
121535e7444dSHong Zhang       if (aj[j] == i) {
121635e7444dSHong Zhang         x[i] = aa[j];
121717ab2063SBarry Smith         break;
121817ab2063SBarry Smith       }
121917ab2063SBarry Smith     }
122017ab2063SBarry Smith   }
1221*fdc842d1SBarry Smith   ierr = VecRestoreArrayWrite(v,&x);CHKERRQ(ierr);
12223a40ed3dSBarry Smith   PetscFunctionReturn(0);
122317ab2063SBarry Smith }
122417ab2063SBarry Smith 
1225c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1226dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
122717ab2063SBarry Smith {
1228416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1229d9ca1df4SBarry Smith   PetscScalar       *y;
1230d9ca1df4SBarry Smith   const PetscScalar *x;
1231dfbe8321SBarry Smith   PetscErrorCode    ierr;
1232d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
12335c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1234d9ca1df4SBarry Smith   const MatScalar   *v;
1235a77337e4SBarry Smith   PetscScalar       alpha;
1236d9ca1df4SBarry Smith   PetscInt          n,i,j;
1237d9ca1df4SBarry Smith   const PetscInt    *idx,*ii,*ridx=NULL;
12383447b6efSHong Zhang   Mat_CompressedRow cprow    = a->compressedrow;
1239ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
12405c897100SBarry Smith #endif
124117ab2063SBarry Smith 
12423a40ed3dSBarry Smith   PetscFunctionBegin;
12432e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
1244d9ca1df4SBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12451ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
12465c897100SBarry Smith 
12475c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1248bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
12495c897100SBarry Smith #else
12503447b6efSHong Zhang   if (usecprow) {
12513447b6efSHong Zhang     m    = cprow.nrows;
12523447b6efSHong Zhang     ii   = cprow.i;
12537b2bb3b9SHong Zhang     ridx = cprow.rindex;
12543447b6efSHong Zhang   } else {
12553447b6efSHong Zhang     ii = a->i;
12563447b6efSHong Zhang   }
125717ab2063SBarry Smith   for (i=0; i<m; i++) {
12583447b6efSHong Zhang     idx = a->j + ii[i];
12593447b6efSHong Zhang     v   = a->a + ii[i];
12603447b6efSHong Zhang     n   = ii[i+1] - ii[i];
12613447b6efSHong Zhang     if (usecprow) {
12627b2bb3b9SHong Zhang       alpha = x[ridx[i]];
12633447b6efSHong Zhang     } else {
126417ab2063SBarry Smith       alpha = x[i];
12653447b6efSHong Zhang     }
126604fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
126717ab2063SBarry Smith   }
12685c897100SBarry Smith #endif
1269dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1270d9ca1df4SBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12711ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
12723a40ed3dSBarry Smith   PetscFunctionReturn(0);
127317ab2063SBarry Smith }
127417ab2063SBarry Smith 
1275dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
12765c897100SBarry Smith {
1277dfbe8321SBarry Smith   PetscErrorCode ierr;
12785c897100SBarry Smith 
12795c897100SBarry Smith   PetscFunctionBegin;
1280170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
12815c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
12825c897100SBarry Smith   PetscFunctionReturn(0);
12835c897100SBarry Smith }
12845c897100SBarry Smith 
1285c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
128678b84d54SShri Abhyankar 
1287dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
128817ab2063SBarry Smith {
1289416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1290d9fead3dSBarry Smith   PetscScalar       *y;
129154f21887SBarry Smith   const PetscScalar *x;
129254f21887SBarry Smith   const MatScalar   *aa;
1293dfbe8321SBarry Smith   PetscErrorCode    ierr;
1294003131ecSBarry Smith   PetscInt          m=A->rmap->n;
12950298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
12967b083b7cSBarry Smith   PetscInt          n,i;
1297362ced78SSatish Balay   PetscScalar       sum;
1298ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
129917ab2063SBarry Smith 
1300b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
130197952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1302fee21e36SBarry Smith #endif
1303fee21e36SBarry Smith 
13043a40ed3dSBarry Smith   PetscFunctionBegin;
13053649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
13061ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1307416022c9SBarry Smith   ii   = a->i;
13084eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
13094f390cb1SBarry Smith     ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr);
131097952fefSHong Zhang     m    = a->compressedrow.nrows;
131197952fefSHong Zhang     ii   = a->compressedrow.i;
131297952fefSHong Zhang     ridx = a->compressedrow.rindex;
131397952fefSHong Zhang     for (i=0; i<m; i++) {
131497952fefSHong Zhang       n           = ii[i+1] - ii[i];
131597952fefSHong Zhang       aj          = a->j + ii[i];
131697952fefSHong Zhang       aa          = a->a + ii[i];
131797952fefSHong Zhang       sum         = 0.0;
1318003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1319003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
132097952fefSHong Zhang       y[*ridx++] = sum;
132197952fefSHong Zhang     }
132297952fefSHong Zhang   } else { /* do not use compressed row format */
1323b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
13243d3eaba7SBarry Smith     aj   = a->j;
13253d3eaba7SBarry Smith     aa   = a->a;
1326b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1327b05257ddSBarry Smith #else
132817ab2063SBarry Smith     for (i=0; i<m; i++) {
1329003131ecSBarry Smith       n           = ii[i+1] - ii[i];
1330003131ecSBarry Smith       aj          = a->j + ii[i];
1331003131ecSBarry Smith       aa          = a->a + ii[i];
133217ab2063SBarry Smith       sum         = 0.0;
1333003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
133417ab2063SBarry Smith       y[i] = sum;
133517ab2063SBarry Smith     }
13368d195f9aSBarry Smith #endif
1337b05257ddSBarry Smith   }
13387b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
13393649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13401ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13413a40ed3dSBarry Smith   PetscFunctionReturn(0);
134217ab2063SBarry Smith }
134317ab2063SBarry Smith 
1344b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1345b434eb95SMatthew G. Knepley {
1346b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1347b434eb95SMatthew G. Knepley   PetscScalar       *y;
1348b434eb95SMatthew G. Knepley   const PetscScalar *x;
1349b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1350b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1351b434eb95SMatthew G. Knepley   PetscInt          m=A->rmap->n;
1352b434eb95SMatthew G. Knepley   const PetscInt    *aj,*ii,*ridx=NULL;
1353b434eb95SMatthew G. Knepley   PetscInt          n,i,nonzerorow=0;
1354b434eb95SMatthew G. Knepley   PetscScalar       sum;
1355b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1356b434eb95SMatthew G. Knepley 
1357b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1358b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa)
1359b434eb95SMatthew G. Knepley #endif
1360b434eb95SMatthew G. Knepley 
1361b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1362b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1363b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1364b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1365b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1366b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1367b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1368b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1369b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1370b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1371b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1372b434eb95SMatthew G. Knepley       sum         = 0.0;
1373b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1374b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1375b434eb95SMatthew G. Knepley       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1376b434eb95SMatthew G. Knepley       y[*ridx++] = sum;
1377b434eb95SMatthew G. Knepley     }
1378b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
13793d3eaba7SBarry Smith     ii = a->i;
1380b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1381b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1382b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1383b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1384b434eb95SMatthew G. Knepley       sum         = 0.0;
1385b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1386b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1387b434eb95SMatthew G. Knepley       y[i] = sum;
1388b434eb95SMatthew G. Knepley     }
1389b434eb95SMatthew G. Knepley   }
1390b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1391b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1392b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1393b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1394b434eb95SMatthew G. Knepley }
1395b434eb95SMatthew G. Knepley 
1396b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1397b434eb95SMatthew G. Knepley {
1398b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1399b434eb95SMatthew G. Knepley   PetscScalar       *y,*z;
1400b434eb95SMatthew G. Knepley   const PetscScalar *x;
1401b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1402b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1403b434eb95SMatthew G. Knepley   PetscInt          m = A->rmap->n,*aj,*ii;
1404b434eb95SMatthew G. Knepley   PetscInt          n,i,*ridx=NULL;
1405b434eb95SMatthew G. Knepley   PetscScalar       sum;
1406b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1407b434eb95SMatthew G. Knepley 
1408b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1409b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1410d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1411b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1412b434eb95SMatthew G. Knepley     if (zz != yy) {
1413b434eb95SMatthew G. Knepley       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
1414b434eb95SMatthew G. Knepley     }
1415b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1416b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1417b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1418b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1419b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1420b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1421b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1422b434eb95SMatthew G. Knepley       sum = y[*ridx];
1423b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1424b434eb95SMatthew G. Knepley       z[*ridx++] = sum;
1425b434eb95SMatthew G. Knepley     }
1426b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
14273d3eaba7SBarry Smith     ii = a->i;
1428b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1429b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1430b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1431b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1432b434eb95SMatthew G. Knepley       sum = y[i];
1433b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1434b434eb95SMatthew G. Knepley       z[i] = sum;
1435b434eb95SMatthew G. Knepley     }
1436b434eb95SMatthew G. Knepley   }
1437b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1438b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1439d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1440b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1441b434eb95SMatthew G. Knepley }
1442b434eb95SMatthew G. Knepley 
1443c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1444dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
144517ab2063SBarry Smith {
1446416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1447f15663dcSBarry Smith   PetscScalar       *y,*z;
1448f15663dcSBarry Smith   const PetscScalar *x;
144954f21887SBarry Smith   const MatScalar   *aa;
1450dfbe8321SBarry Smith   PetscErrorCode    ierr;
1451d9ca1df4SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
1452d9ca1df4SBarry Smith   PetscInt          m = A->rmap->n,n,i;
1453362ced78SSatish Balay   PetscScalar       sum;
1454ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
14559ea0dfa2SSatish Balay 
14563a40ed3dSBarry Smith   PetscFunctionBegin;
1457f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1458d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
14594eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
14604eb6d288SHong Zhang     if (zz != yy) {
14614eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
14624eb6d288SHong Zhang     }
146397952fefSHong Zhang     m    = a->compressedrow.nrows;
146497952fefSHong Zhang     ii   = a->compressedrow.i;
146597952fefSHong Zhang     ridx = a->compressedrow.rindex;
146697952fefSHong Zhang     for (i=0; i<m; i++) {
146797952fefSHong Zhang       n   = ii[i+1] - ii[i];
146897952fefSHong Zhang       aj  = a->j + ii[i];
146997952fefSHong Zhang       aa  = a->a + ii[i];
147097952fefSHong Zhang       sum = y[*ridx];
1471f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
147297952fefSHong Zhang       z[*ridx++] = sum;
147397952fefSHong Zhang     }
147497952fefSHong Zhang   } else { /* do not use compressed row format */
14753d3eaba7SBarry Smith     ii = a->i;
1476f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
14773d3eaba7SBarry Smith     aj = a->j;
14783d3eaba7SBarry Smith     aa = a->a;
1479f15663dcSBarry Smith     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1480f15663dcSBarry Smith #else
148117ab2063SBarry Smith     for (i=0; i<m; i++) {
1482f15663dcSBarry Smith       n   = ii[i+1] - ii[i];
1483f15663dcSBarry Smith       aj  = a->j + ii[i];
1484f15663dcSBarry Smith       aa  = a->a + ii[i];
148517ab2063SBarry Smith       sum = y[i];
1486f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
148717ab2063SBarry Smith       z[i] = sum;
148817ab2063SBarry Smith     }
148902ab625aSSatish Balay #endif
1490f15663dcSBarry Smith   }
1491dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1492f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1493d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
14943a40ed3dSBarry Smith   PetscFunctionReturn(0);
149517ab2063SBarry Smith }
149617ab2063SBarry Smith 
149717ab2063SBarry Smith /*
149817ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
149917ab2063SBarry Smith */
1500dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
150117ab2063SBarry Smith {
1502416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15036849ba73SBarry Smith   PetscErrorCode ierr;
1504d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
150517ab2063SBarry Smith 
15063a40ed3dSBarry Smith   PetscFunctionBegin;
150709f38230SBarry Smith   if (!a->diag) {
1508785e854fSJed Brown     ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr);
15093bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr);
151009f38230SBarry Smith   }
1511d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
151209f38230SBarry Smith     a->diag[i] = a->i[i+1];
1513bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1514bfeeae90SHong Zhang       if (a->j[j] == i) {
151509f38230SBarry Smith         a->diag[i] = j;
151617ab2063SBarry Smith         break;
151717ab2063SBarry Smith       }
151817ab2063SBarry Smith     }
151917ab2063SBarry Smith   }
15203a40ed3dSBarry Smith   PetscFunctionReturn(0);
152117ab2063SBarry Smith }
152217ab2063SBarry Smith 
152361ecd0c6SBarry Smith PetscErrorCode MatShift_SeqAIJ(Mat A,PetscScalar v)
152461ecd0c6SBarry Smith {
152561ecd0c6SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
152661ecd0c6SBarry Smith   const PetscInt    *diag = (const PetscInt*)a->diag;
152761ecd0c6SBarry Smith   const PetscInt    *ii = (const PetscInt*) a->i;
152861ecd0c6SBarry Smith   PetscInt          i,*mdiag = NULL;
152961ecd0c6SBarry Smith   PetscErrorCode    ierr;
153061ecd0c6SBarry Smith   PetscInt          cnt = 0; /* how many diagonals are missing */
153161ecd0c6SBarry Smith 
153261ecd0c6SBarry Smith   PetscFunctionBegin;
153361ecd0c6SBarry Smith   if (!A->preallocated || !a->nz) {
153461ecd0c6SBarry Smith     ierr = MatSeqAIJSetPreallocation(A,1,NULL);CHKERRQ(ierr);
153561ecd0c6SBarry Smith     ierr = MatShift_Basic(A,v);CHKERRQ(ierr);
153661ecd0c6SBarry Smith     PetscFunctionReturn(0);
153761ecd0c6SBarry Smith   }
153861ecd0c6SBarry Smith 
153961ecd0c6SBarry Smith   if (a->diagonaldense) {
154061ecd0c6SBarry Smith     cnt = 0;
154161ecd0c6SBarry Smith   } else {
154261ecd0c6SBarry Smith     ierr = PetscCalloc1(A->rmap->n,&mdiag);CHKERRQ(ierr);
154361ecd0c6SBarry Smith     for (i=0; i<A->rmap->n; i++) {
154461ecd0c6SBarry Smith       if (diag[i] >= ii[i+1]) {
154561ecd0c6SBarry Smith         cnt++;
154661ecd0c6SBarry Smith         mdiag[i] = 1;
154761ecd0c6SBarry Smith       }
154861ecd0c6SBarry Smith     }
154961ecd0c6SBarry Smith   }
155061ecd0c6SBarry Smith   if (!cnt) {
155161ecd0c6SBarry Smith     ierr = MatShift_Basic(A,v);CHKERRQ(ierr);
155261ecd0c6SBarry Smith   } else {
1553b6f2aa54SBarry Smith     PetscScalar *olda = a->a;  /* preserve pointers to current matrix nonzeros structure and values */
1554b6f2aa54SBarry Smith     PetscInt    *oldj = a->j, *oldi = a->i;
155561ecd0c6SBarry Smith     PetscBool   singlemalloc = a->singlemalloc,free_a = a->free_a,free_ij = a->free_ij;
155661ecd0c6SBarry Smith 
155761ecd0c6SBarry Smith     a->a = NULL;
155861ecd0c6SBarry Smith     a->j = NULL;
155961ecd0c6SBarry Smith     a->i = NULL;
156061ecd0c6SBarry Smith     /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
156161ecd0c6SBarry Smith     for (i=0; i<A->rmap->n; i++) {
156261ecd0c6SBarry Smith       a->imax[i] += mdiag[i];
1563447d62f5SStefano Zampini       a->imax[i] = PetscMin(a->imax[i],A->cmap->n);
156461ecd0c6SBarry Smith     }
156561ecd0c6SBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,0,a->imax);CHKERRQ(ierr);
156661ecd0c6SBarry Smith 
156761ecd0c6SBarry Smith     /* copy old values into new matrix data structure */
156861ecd0c6SBarry Smith     for (i=0; i<A->rmap->n; i++) {
156961ecd0c6SBarry Smith       ierr = MatSetValues(A,1,&i,a->imax[i] - mdiag[i],&oldj[oldi[i]],&olda[oldi[i]],ADD_VALUES);CHKERRQ(ierr);
1570447d62f5SStefano Zampini       if (i < A->cmap->n) {
157161ecd0c6SBarry Smith         ierr = MatSetValue(A,i,i,v,ADD_VALUES);CHKERRQ(ierr);
157261ecd0c6SBarry Smith       }
1573447d62f5SStefano Zampini     }
157461ecd0c6SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
157561ecd0c6SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
157661ecd0c6SBarry Smith     if (singlemalloc) {
157761ecd0c6SBarry Smith       ierr = PetscFree3(olda,oldj,oldi);CHKERRQ(ierr);
157861ecd0c6SBarry Smith     } else {
157961ecd0c6SBarry Smith       if (free_a)  {ierr = PetscFree(olda);CHKERRQ(ierr);}
158061ecd0c6SBarry Smith       if (free_ij) {ierr = PetscFree(oldj);CHKERRQ(ierr);}
158161ecd0c6SBarry Smith       if (free_ij) {ierr = PetscFree(oldi);CHKERRQ(ierr);}
158261ecd0c6SBarry Smith     }
158361ecd0c6SBarry Smith   }
158461ecd0c6SBarry Smith   ierr = PetscFree(mdiag);CHKERRQ(ierr);
158561ecd0c6SBarry Smith   a->diagonaldense = PETSC_TRUE;
158661ecd0c6SBarry Smith   PetscFunctionReturn(0);
158761ecd0c6SBarry Smith }
158861ecd0c6SBarry Smith 
1589be5855fcSBarry Smith /*
1590be5855fcSBarry Smith      Checks for missing diagonals
1591be5855fcSBarry Smith */
1592ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1593be5855fcSBarry Smith {
1594be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15957734d3b5SMatthew G. Knepley   PetscInt       *diag,*ii = a->i,i;
1596994fe344SLisandro Dalcin   PetscErrorCode ierr;
1597be5855fcSBarry Smith 
1598be5855fcSBarry Smith   PetscFunctionBegin;
159909f38230SBarry Smith   *missing = PETSC_FALSE;
16007734d3b5SMatthew G. Knepley   if (A->rmap->n > 0 && !ii) {
160109f38230SBarry Smith     *missing = PETSC_TRUE;
160209f38230SBarry Smith     if (d) *d = 0;
1603994fe344SLisandro Dalcin     ierr = PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");CHKERRQ(ierr);
160409f38230SBarry Smith   } else {
1605f1e2ffcdSBarry Smith     diag = a->diag;
1606d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
16077734d3b5SMatthew G. Knepley       if (diag[i] >= ii[i+1]) {
160809f38230SBarry Smith         *missing = PETSC_TRUE;
160909f38230SBarry Smith         if (d) *d = i;
1610994fe344SLisandro Dalcin         ierr = PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);CHKERRQ(ierr);
1611358d2f5dSShri Abhyankar         break;
161209f38230SBarry Smith       }
1613be5855fcSBarry Smith     }
1614be5855fcSBarry Smith   }
1615be5855fcSBarry Smith   PetscFunctionReturn(0);
1616be5855fcSBarry Smith }
1617be5855fcSBarry Smith 
16180da83c2eSBarry Smith #include <petscblaslapack.h>
16190da83c2eSBarry Smith #include <petsc/private/kernels/blockinvert.h>
16200da83c2eSBarry Smith 
16210da83c2eSBarry Smith /*
16220da83c2eSBarry Smith     Note that values is allocated externally by the PC and then passed into this routine
16230da83c2eSBarry Smith */
16240da83c2eSBarry Smith PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag)
16250da83c2eSBarry Smith {
16260da83c2eSBarry Smith   PetscErrorCode  ierr;
16270da83c2eSBarry Smith   PetscInt        n = A->rmap->n, i, ncnt = 0, *indx,j,bsizemax = 0,*v_pivots;
16280da83c2eSBarry Smith   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;
16290da83c2eSBarry Smith   const PetscReal shift = 0.0;
16300da83c2eSBarry Smith   PetscInt        ipvt[5];
16310da83c2eSBarry Smith   PetscScalar     work[25],*v_work;
16320da83c2eSBarry Smith 
16330da83c2eSBarry Smith   PetscFunctionBegin;
16340da83c2eSBarry Smith   allowzeropivot = PetscNot(A->erroriffailure);
16350da83c2eSBarry Smith   for (i=0; i<nblocks; i++) ncnt += bsizes[i];
16360da83c2eSBarry Smith   if (ncnt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Total blocksizes %D doesn't match number matrix rows %D",ncnt,n);
16370da83c2eSBarry Smith   for (i=0; i<nblocks; i++) {
16380da83c2eSBarry Smith     bsizemax = PetscMax(bsizemax,bsizes[i]);
16390da83c2eSBarry Smith   }
16400da83c2eSBarry Smith   ierr = PetscMalloc1(bsizemax,&indx);CHKERRQ(ierr);
16410da83c2eSBarry Smith   if (bsizemax > 7) {
16420da83c2eSBarry Smith     ierr = PetscMalloc2(bsizemax,&v_work,bsizemax,&v_pivots);CHKERRQ(ierr);
16430da83c2eSBarry Smith   }
16440da83c2eSBarry Smith   ncnt = 0;
16450da83c2eSBarry Smith   for (i=0; i<nblocks; i++) {
16460da83c2eSBarry Smith     for (j=0; j<bsizes[i]; j++) indx[j] = ncnt+j;
16470da83c2eSBarry Smith     ierr    = MatGetValues(A,bsizes[i],indx,bsizes[i],indx,diag);CHKERRQ(ierr);
16480da83c2eSBarry Smith     switch (bsizes[i]) {
16490da83c2eSBarry Smith     case 1:
16500da83c2eSBarry Smith       *diag = 1.0/(*diag);
16510da83c2eSBarry Smith       break;
16520da83c2eSBarry Smith     case 2:
16530da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16540da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16550da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
16560da83c2eSBarry Smith       break;
16570da83c2eSBarry Smith     case 3:
16580da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16590da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16600da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
16610da83c2eSBarry Smith       break;
16620da83c2eSBarry Smith     case 4:
16630da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16640da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16650da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
16660da83c2eSBarry Smith       break;
16670da83c2eSBarry Smith     case 5:
16680da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16690da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16700da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
16710da83c2eSBarry Smith       break;
16720da83c2eSBarry Smith     case 6:
16730da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16740da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16750da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
16760da83c2eSBarry Smith       break;
16770da83c2eSBarry Smith     case 7:
16780da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16790da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16800da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
16810da83c2eSBarry Smith       break;
16820da83c2eSBarry Smith     default:
16830da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A(bsizes[i],diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
16840da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
16850da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bsizes[i]);CHKERRQ(ierr);
16860da83c2eSBarry Smith     }
16870da83c2eSBarry Smith     ncnt   += bsizes[i];
16880da83c2eSBarry Smith     diag += bsizes[i]*bsizes[i];
16890da83c2eSBarry Smith   }
16900da83c2eSBarry Smith   if (bsizemax > 7) {
16910da83c2eSBarry Smith     ierr = PetscFree2(v_work,v_pivots);CHKERRQ(ierr);
16920da83c2eSBarry Smith   }
16930da83c2eSBarry Smith   ierr = PetscFree(indx);CHKERRQ(ierr);
16940da83c2eSBarry Smith   PetscFunctionReturn(0);
16950da83c2eSBarry Smith }
16960da83c2eSBarry Smith 
1697422a814eSBarry Smith /*
1698422a814eSBarry Smith    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1699422a814eSBarry Smith */
17007087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
170171f1c65dSBarry Smith {
170271f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
170371f1c65dSBarry Smith   PetscErrorCode ierr;
1704d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
170554f21887SBarry Smith   MatScalar      *v = a->a;
170654f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
170771f1c65dSBarry Smith 
170871f1c65dSBarry Smith   PetscFunctionBegin;
170971f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
171071f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
171171f1c65dSBarry Smith   diag = a->diag;
171271f1c65dSBarry Smith   if (!a->idiag) {
1713dcca6d9dSJed Brown     ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr);
17143bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
171571f1c65dSBarry Smith     v    = a->a;
171671f1c65dSBarry Smith   }
171771f1c65dSBarry Smith   mdiag = a->mdiag;
171871f1c65dSBarry Smith   idiag = a->idiag;
171971f1c65dSBarry Smith 
1720422a814eSBarry Smith   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
172171f1c65dSBarry Smith     for (i=0; i<m; i++) {
172271f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1723899639b0SHong Zhang       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1724899639b0SHong Zhang         if (PetscRealPart(fshift)) {
1725899639b0SHong Zhang           ierr = PetscInfo1(A,"Zero diagonal on row %D\n",i);CHKERRQ(ierr);
17267b6c816cSBarry Smith           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
17277b6c816cSBarry Smith           A->factorerror_zeropivot_value = 0.0;
17287b6c816cSBarry Smith           A->factorerror_zeropivot_row   = i;
1729a6fa060aSHong Zhang         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1730899639b0SHong Zhang       }
173171f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
173271f1c65dSBarry Smith     }
173371f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
173471f1c65dSBarry Smith   } else {
173571f1c65dSBarry Smith     for (i=0; i<m; i++) {
173671f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
173771f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
173871f1c65dSBarry Smith     }
1739dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
174071f1c65dSBarry Smith   }
174171f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
174271f1c65dSBarry Smith   PetscFunctionReturn(0);
174371f1c65dSBarry Smith }
174471f1c65dSBarry Smith 
1745c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
174641f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
174717ab2063SBarry Smith {
1748416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1749e6d1f457SBarry Smith   PetscScalar       *x,d,sum,*t,scale;
17503d3eaba7SBarry Smith   const MatScalar   *v,*idiag=0,*mdiag;
175154f21887SBarry Smith   const PetscScalar *b, *bs,*xb, *ts;
1752dfbe8321SBarry Smith   PetscErrorCode    ierr;
17533d3eaba7SBarry Smith   PetscInt          n,m = A->rmap->n,i;
175497f1f81fSBarry Smith   const PetscInt    *idx,*diag;
175517ab2063SBarry Smith 
17563a40ed3dSBarry Smith   PetscFunctionBegin;
1757b965ef7fSBarry Smith   its = its*lits;
175891723122SBarry Smith 
175971f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
176071f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
176171f1c65dSBarry Smith   a->fshift = fshift;
176271f1c65dSBarry Smith   a->omega  = omega;
1763ed480e8bSBarry Smith 
176471f1c65dSBarry Smith   diag  = a->diag;
176571f1c65dSBarry Smith   t     = a->ssor_work;
1766ed480e8bSBarry Smith   idiag = a->idiag;
176771f1c65dSBarry Smith   mdiag = a->mdiag;
1768ed480e8bSBarry Smith 
17691ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
17703649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1771ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
177217ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
177317ab2063SBarry Smith     /* apply (U + D/omega) to the vector */
1774ed480e8bSBarry Smith     bs = b;
177517ab2063SBarry Smith     for (i=0; i<m; i++) {
177671f1c65dSBarry Smith       d   = fshift + mdiag[i];
1777416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1778ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1779ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
178017ab2063SBarry Smith       sum = b[i]*d/omega;
1781003131ecSBarry Smith       PetscSparseDensePlusDot(sum,bs,v,idx,n);
178217ab2063SBarry Smith       x[i] = sum;
178317ab2063SBarry Smith     }
17841ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
17853649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1786efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
17873a40ed3dSBarry Smith     PetscFunctionReturn(0);
178817ab2063SBarry Smith   }
1789c783ea89SBarry Smith 
17902205254eSKarl Rupp   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
17912205254eSKarl Rupp   else if (flag & SOR_EISENSTAT) {
179217ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1793887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
179417ab2063SBarry Smith 
179517ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
179617ab2063SBarry Smith 
1797887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
179817ab2063SBarry Smith     */
179917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
180017ab2063SBarry Smith 
180117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
180217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1803416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1804ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1805ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
180617ab2063SBarry Smith       sum = b[i];
1807e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1808ed480e8bSBarry Smith       x[i] = sum*idiag[i];
180917ab2063SBarry Smith     }
181017ab2063SBarry Smith 
181117ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1812416022c9SBarry Smith     v = a->a;
18132205254eSKarl Rupp     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
181417ab2063SBarry Smith 
181517ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1816ed480e8bSBarry Smith     ts   = t;
1817416022c9SBarry Smith     diag = a->diag;
181817ab2063SBarry Smith     for (i=0; i<m; i++) {
1819416022c9SBarry Smith       n   = diag[i] - a->i[i];
1820ed480e8bSBarry Smith       idx = a->j + a->i[i];
1821ed480e8bSBarry Smith       v   = a->a + a->i[i];
182217ab2063SBarry Smith       sum = t[i];
1823003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1824ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1825733d66baSBarry Smith       /*  x = x + t */
1826733d66baSBarry Smith       x[i] += t[i];
182717ab2063SBarry Smith     }
182817ab2063SBarry Smith 
1829dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
18301ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
18313649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
18323a40ed3dSBarry Smith     PetscFunctionReturn(0);
183317ab2063SBarry Smith   }
183417ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
183517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
183617ab2063SBarry Smith       for (i=0; i<m; i++) {
1837416022c9SBarry Smith         n   = diag[i] - a->i[i];
1838ed480e8bSBarry Smith         idx = a->j + a->i[i];
1839ed480e8bSBarry Smith         v   = a->a + a->i[i];
184017ab2063SBarry Smith         sum = b[i];
1841e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
18425c99c7daSBarry Smith         t[i] = sum;
1843ed480e8bSBarry Smith         x[i] = sum*idiag[i];
184417ab2063SBarry Smith       }
18455c99c7daSBarry Smith       xb   = t;
1846efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18473a40ed3dSBarry Smith     } else xb = b;
184817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
184917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1850416022c9SBarry Smith         n   = a->i[i+1] - diag[i] - 1;
1851ed480e8bSBarry Smith         idx = a->j + diag[i] + 1;
1852ed480e8bSBarry Smith         v   = a->a + diag[i] + 1;
185317ab2063SBarry Smith         sum = xb[i];
1854e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
18555c99c7daSBarry Smith         if (xb == b) {
1856ed480e8bSBarry Smith           x[i] = sum*idiag[i];
18575c99c7daSBarry Smith         } else {
1858b19a5dc2SMark Adams           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
185917ab2063SBarry Smith         }
18605c99c7daSBarry Smith       }
1861b19a5dc2SMark Adams       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
186217ab2063SBarry Smith     }
186317ab2063SBarry Smith     its--;
186417ab2063SBarry Smith   }
186517ab2063SBarry Smith   while (its--) {
186617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
186717ab2063SBarry Smith       for (i=0; i<m; i++) {
1868b19a5dc2SMark Adams         /* lower */
1869b19a5dc2SMark Adams         n   = diag[i] - a->i[i];
1870ed480e8bSBarry Smith         idx = a->j + a->i[i];
1871ed480e8bSBarry Smith         v   = a->a + a->i[i];
187217ab2063SBarry Smith         sum = b[i];
1873e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1874b19a5dc2SMark Adams         t[i] = sum;             /* save application of the lower-triangular part */
1875b19a5dc2SMark Adams         /* upper */
1876b19a5dc2SMark Adams         n   = a->i[i+1] - diag[i] - 1;
1877b19a5dc2SMark Adams         idx = a->j + diag[i] + 1;
1878b19a5dc2SMark Adams         v   = a->a + diag[i] + 1;
1879b19a5dc2SMark Adams         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1880b19a5dc2SMark Adams         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
188117ab2063SBarry Smith       }
1882b19a5dc2SMark Adams       xb   = t;
18839f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1884b19a5dc2SMark Adams     } else xb = b;
188517ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
188617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1887b19a5dc2SMark Adams         sum = xb[i];
1888b19a5dc2SMark Adams         if (xb == b) {
1889b19a5dc2SMark Adams           /* whole matrix (no checkpointing available) */
1890416022c9SBarry Smith           n   = a->i[i+1] - a->i[i];
1891ed480e8bSBarry Smith           idx = a->j + a->i[i];
1892ed480e8bSBarry Smith           v   = a->a + a->i[i];
1893e6d1f457SBarry Smith           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1894ed480e8bSBarry Smith           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1895b19a5dc2SMark Adams         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1896b19a5dc2SMark Adams           n   = a->i[i+1] - diag[i] - 1;
1897b19a5dc2SMark Adams           idx = a->j + diag[i] + 1;
1898b19a5dc2SMark Adams           v   = a->a + diag[i] + 1;
1899b19a5dc2SMark Adams           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1900b19a5dc2SMark Adams           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
190117ab2063SBarry Smith         }
1902b19a5dc2SMark Adams       }
1903b19a5dc2SMark Adams       if (xb == b) {
19049f863219SBarry Smith         ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1905b19a5dc2SMark Adams       } else {
1906b19a5dc2SMark Adams         ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1907b19a5dc2SMark Adams       }
190817ab2063SBarry Smith     }
190917ab2063SBarry Smith   }
19101ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
19113649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1912365a8a9eSBarry Smith   PetscFunctionReturn(0);
191317ab2063SBarry Smith }
191417ab2063SBarry Smith 
19152af78befSBarry Smith 
1916dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
191717ab2063SBarry Smith {
1918416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
19194e220ebcSLois Curfman McInnes 
19203a40ed3dSBarry Smith   PetscFunctionBegin;
19214e220ebcSLois Curfman McInnes   info->block_size   = 1.0;
19224e220ebcSLois Curfman McInnes   info->nz_allocated = (double)a->maxnz;
19234e220ebcSLois Curfman McInnes   info->nz_used      = (double)a->nz;
19244e220ebcSLois Curfman McInnes   info->nz_unneeded  = (double)(a->maxnz - a->nz);
19254e220ebcSLois Curfman McInnes   info->assemblies   = (double)A->num_ass;
19268e58a170SBarry Smith   info->mallocs      = (double)A->info.mallocs;
19277adad957SLisandro Dalcin   info->memory       = ((PetscObject)A)->mem;
1928d5f3da31SBarry Smith   if (A->factortype) {
19294e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
19304e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
19314e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
19324e220ebcSLois Curfman McInnes   } else {
19334e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
19344e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
19354e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
19364e220ebcSLois Curfman McInnes   }
19373a40ed3dSBarry Smith   PetscFunctionReturn(0);
193817ab2063SBarry Smith }
193917ab2063SBarry Smith 
19402b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
194117ab2063SBarry Smith {
1942416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1943c7da8527SEric Chamberland   PetscInt          i,m = A->rmap->n - 1;
19446849ba73SBarry Smith   PetscErrorCode    ierr;
194597b48c8fSBarry Smith   const PetscScalar *xx;
194697b48c8fSBarry Smith   PetscScalar       *bb;
1947c7da8527SEric Chamberland   PetscInt          d = 0;
194817ab2063SBarry Smith 
19493a40ed3dSBarry Smith   PetscFunctionBegin;
195097b48c8fSBarry Smith   if (x && b) {
195197b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
195297b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
195397b48c8fSBarry Smith     for (i=0; i<N; i++) {
195497b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1955447d62f5SStefano Zampini       if (rows[i] >= A->cmap->n) continue;
195697b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
195797b48c8fSBarry Smith     }
195897b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
195997b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
196097b48c8fSBarry Smith   }
196197b48c8fSBarry Smith 
1962a9817697SBarry Smith   if (a->keepnonzeropattern) {
1963f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1964e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1965bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1966f1e2ffcdSBarry Smith     }
1967f4df32b1SMatthew Knepley     if (diag != 0.0) {
1968c7da8527SEric Chamberland       for (i=0; i<N; i++) {
1969c7da8527SEric Chamberland         d = rows[i];
1970447d62f5SStefano Zampini         if (rows[i] >= A->cmap->n) continue;
1971c7da8527SEric 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);
1972c7da8527SEric Chamberland       }
1973f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1974447d62f5SStefano Zampini         if (rows[i] >= A->cmap->n) continue;
1975f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1976f1e2ffcdSBarry Smith       }
1977f1e2ffcdSBarry Smith     }
1978f1e2ffcdSBarry Smith   } else {
1979f4df32b1SMatthew Knepley     if (diag != 0.0) {
198017ab2063SBarry Smith       for (i=0; i<N; i++) {
1981e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
19827ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1983447d62f5SStefano Zampini 	  if (rows[i] >= A->cmap->n) {
1984447d62f5SStefano Zampini             a->ilen[rows[i]] = 0;
1985447d62f5SStefano Zampini           } else {
1986416022c9SBarry Smith             a->ilen[rows[i]]    = 1;
1987f4df32b1SMatthew Knepley             a->a[a->i[rows[i]]] = diag;
1988bfeeae90SHong Zhang             a->j[a->i[rows[i]]] = rows[i];
1989447d62f5SStefano Zampini           }
1990447d62f5SStefano Zampini         } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
1991f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
199217ab2063SBarry Smith         }
199317ab2063SBarry Smith       }
19943a40ed3dSBarry Smith     } else {
199517ab2063SBarry Smith       for (i=0; i<N; i++) {
1996e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1997416022c9SBarry Smith         a->ilen[rows[i]] = 0;
199817ab2063SBarry Smith       }
199917ab2063SBarry Smith     }
2000e56f5c9eSBarry Smith     A->nonzerostate++;
2001f1e2ffcdSBarry Smith   }
20024099cc6bSBarry Smith   ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20033a40ed3dSBarry Smith   PetscFunctionReturn(0);
200417ab2063SBarry Smith }
200517ab2063SBarry Smith 
20066e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
20076e169961SBarry Smith {
20086e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
20096e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
20106e169961SBarry Smith   PetscErrorCode    ierr;
20112b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
20126e169961SBarry Smith   const PetscScalar *xx;
20136e169961SBarry Smith   PetscScalar       *bb;
20146e169961SBarry Smith 
20156e169961SBarry Smith   PetscFunctionBegin;
20166e169961SBarry Smith   if (x && b) {
20176e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
20186e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
20192b40b63fSBarry Smith     vecs = PETSC_TRUE;
20206e169961SBarry Smith   }
20211795a4d1SJed Brown   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
20226e169961SBarry Smith   for (i=0; i<N; i++) {
20236e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
20246e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
20252205254eSKarl Rupp 
20266e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
20276e169961SBarry Smith   }
20286e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
20296e169961SBarry Smith     if (!zeroed[i]) {
20306e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
20314cf107fdSStefano Zampini         if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
20322b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
20336e169961SBarry Smith           a->a[j] = 0.0;
20346e169961SBarry Smith         }
20356e169961SBarry Smith       }
20364cf107fdSStefano Zampini     } else if (vecs && i < A->cmap->N) bb[i] = diag*xx[i];
20376e169961SBarry Smith   }
20386e169961SBarry Smith   if (x && b) {
20396e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
20406e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
20416e169961SBarry Smith   }
20426e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
20436e169961SBarry Smith   if (diag != 0.0) {
20446e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
20451d5a398dSstefano_zampini     if (missing) {
20461d5a398dSstefano_zampini       for (i=0; i<N; i++) {
20474cf107fdSStefano Zampini         if (rows[i] >= A->cmap->N) continue;
20484cf107fdSStefano 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]);
20491d5a398dSstefano_zampini         ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
20501d5a398dSstefano_zampini       }
20511d5a398dSstefano_zampini     } else {
20526e169961SBarry Smith       for (i=0; i<N; i++) {
20536e169961SBarry Smith         a->a[a->diag[rows[i]]] = diag;
20546e169961SBarry Smith       }
20556e169961SBarry Smith     }
20561d5a398dSstefano_zampini   }
20574099cc6bSBarry Smith   ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20586e169961SBarry Smith   PetscFunctionReturn(0);
20596e169961SBarry Smith }
20606e169961SBarry Smith 
2061a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
206217ab2063SBarry Smith {
2063416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
206497f1f81fSBarry Smith   PetscInt   *itmp;
206517ab2063SBarry Smith 
20663a40ed3dSBarry Smith   PetscFunctionBegin;
2067e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
206817ab2063SBarry Smith 
2069416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
2070bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
207117ab2063SBarry Smith   if (idx) {
2072bfeeae90SHong Zhang     itmp = a->j + a->i[row];
207326fbe8dcSKarl Rupp     if (*nz) *idx = itmp;
207417ab2063SBarry Smith     else *idx = 0;
207517ab2063SBarry Smith   }
20763a40ed3dSBarry Smith   PetscFunctionReturn(0);
207717ab2063SBarry Smith }
207817ab2063SBarry Smith 
2079bfeeae90SHong Zhang /* remove this function? */
2080a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
208117ab2063SBarry Smith {
20823a40ed3dSBarry Smith   PetscFunctionBegin;
20833a40ed3dSBarry Smith   PetscFunctionReturn(0);
208417ab2063SBarry Smith }
208517ab2063SBarry Smith 
2086dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
208717ab2063SBarry Smith {
2088416022c9SBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
208954f21887SBarry Smith   MatScalar      *v  = a->a;
209036db0b34SBarry Smith   PetscReal      sum = 0.0;
20916849ba73SBarry Smith   PetscErrorCode ierr;
209297f1f81fSBarry Smith   PetscInt       i,j;
209317ab2063SBarry Smith 
20943a40ed3dSBarry Smith   PetscFunctionBegin;
209517ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
2096570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FP16)
2097570b7f6dSBarry Smith     PetscBLASInt one = 1,nz = a->nz;
2098570b7f6dSBarry Smith     *nrm = BLASnrm2_(&nz,v,&one);
2099570b7f6dSBarry Smith #else
2100416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
210136db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
210217ab2063SBarry Smith     }
21038f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
2104570b7f6dSBarry Smith #endif
210551f70360SJed Brown     ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
21063a40ed3dSBarry Smith   } else if (type == NORM_1) {
210736db0b34SBarry Smith     PetscReal *tmp;
210897f1f81fSBarry Smith     PetscInt  *jj = a->j;
21091795a4d1SJed Brown     ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr);
2110064f8208SBarry Smith     *nrm = 0.0;
2111416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
2112bfeeae90SHong Zhang       tmp[*jj++] += PetscAbsScalar(*v);  v++;
211317ab2063SBarry Smith     }
2114d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
2115064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
211617ab2063SBarry Smith     }
2117606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
211851f70360SJed Brown     ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr);
21193a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
2120064f8208SBarry Smith     *nrm = 0.0;
2121d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
2122bfeeae90SHong Zhang       v   = a->a + a->i[j];
212317ab2063SBarry Smith       sum = 0.0;
2124416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2125cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
212617ab2063SBarry Smith       }
2127064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
212817ab2063SBarry Smith     }
212951f70360SJed Brown     ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr);
2130f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
21313a40ed3dSBarry Smith   PetscFunctionReturn(0);
213217ab2063SBarry Smith }
213317ab2063SBarry Smith 
21344e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
21354e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
21364e938277SHong Zhang {
21374e938277SHong Zhang   PetscErrorCode ierr;
21384e938277SHong Zhang   PetscInt       i,j,anzj;
21394e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
21404e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
21414e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
21424e938277SHong Zhang 
21434e938277SHong Zhang   PetscFunctionBegin;
21444e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
2145854ce69bSBarry Smith   ierr = PetscCalloc1(an+1,&ati);CHKERRQ(ierr);
2146785e854fSJed Brown   ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr);
2147785e854fSJed Brown   ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr);
21484e938277SHong Zhang 
21494e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
21504e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
215126fbe8dcSKarl Rupp   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
21524e938277SHong Zhang   /* Form ati for csr format of A^T. */
215326fbe8dcSKarl Rupp   for (i=0;i<an;i++) ati[i+1] += ati[i];
21544e938277SHong Zhang 
21554e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
21564e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
21574e938277SHong Zhang 
21584e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
21594e938277SHong Zhang   for (i=0;i<am;i++) {
21604e938277SHong Zhang     anzj = ai[i+1] - ai[i];
21614e938277SHong Zhang     for (j=0;j<anzj;j++) {
21624e938277SHong Zhang       atj[atfill[*aj]] = i;
21634e938277SHong Zhang       atfill[*aj++]   += 1;
21644e938277SHong Zhang     }
21654e938277SHong Zhang   }
21664e938277SHong Zhang 
21674e938277SHong Zhang   /* Clean up temporary space and complete requests. */
21684e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
2169ce94432eSBarry Smith   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
217033d57670SJed Brown   ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
2171a2f3521dSMark F. Adams 
21724e938277SHong Zhang   b          = (Mat_SeqAIJ*)((*B)->data);
21734e938277SHong Zhang   b->free_a  = PETSC_FALSE;
21744e938277SHong Zhang   b->free_ij = PETSC_TRUE;
21754e938277SHong Zhang   b->nonew   = 0;
21764e938277SHong Zhang   PetscFunctionReturn(0);
21774e938277SHong Zhang }
21784e938277SHong Zhang 
21797087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2180cd0d46ebSvictorle {
21813d3eaba7SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
218254f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
218354f21887SBarry Smith   MatScalar      *va,*vb;
21846849ba73SBarry Smith   PetscErrorCode ierr;
218597f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
2186cd0d46ebSvictorle 
2187cd0d46ebSvictorle   PetscFunctionBegin;
2188cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2189cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
21905485867bSBarry Smith   if (ma!=nb || na!=mb) {
21915485867bSBarry Smith     *f = PETSC_FALSE;
21925485867bSBarry Smith     PetscFunctionReturn(0);
21935485867bSBarry Smith   }
2194cd0d46ebSvictorle   aii  = aij->i; bii = bij->i;
2195cd0d46ebSvictorle   adx  = aij->j; bdx = bij->j;
2196cd0d46ebSvictorle   va   = aij->a; vb = bij->a;
2197785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2198785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
2199cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
2200cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
2201cd0d46ebSvictorle 
2202cd0d46ebSvictorle   *f = PETSC_TRUE;
2203cd0d46ebSvictorle   for (i=0; i<ma; i++) {
2204cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
220597f1f81fSBarry Smith       PetscInt    idc,idr;
22065485867bSBarry Smith       PetscScalar vc,vr;
2207cd0d46ebSvictorle       /* column/row index/value */
22085485867bSBarry Smith       idc = adx[aptr[i]];
22095485867bSBarry Smith       idr = bdx[bptr[idc]];
22105485867bSBarry Smith       vc  = va[aptr[i]];
22115485867bSBarry Smith       vr  = vb[bptr[idc]];
22125485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
22135485867bSBarry Smith         *f = PETSC_FALSE;
22145485867bSBarry Smith         goto done;
2215cd0d46ebSvictorle       } else {
22165485867bSBarry Smith         aptr[i]++;
22175485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
2218cd0d46ebSvictorle       }
2219cd0d46ebSvictorle     }
2220cd0d46ebSvictorle   }
2221cd0d46ebSvictorle done:
2222cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
22233aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
2224cd0d46ebSvictorle   PetscFunctionReturn(0);
2225cd0d46ebSvictorle }
2226cd0d46ebSvictorle 
22277087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
22281cbb95d3SBarry Smith {
22293d3eaba7SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
223054f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
223154f21887SBarry Smith   MatScalar      *va,*vb;
22321cbb95d3SBarry Smith   PetscErrorCode ierr;
22331cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
22341cbb95d3SBarry Smith 
22351cbb95d3SBarry Smith   PetscFunctionBegin;
22361cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
22371cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
22381cbb95d3SBarry Smith   if (ma!=nb || na!=mb) {
22391cbb95d3SBarry Smith     *f = PETSC_FALSE;
22401cbb95d3SBarry Smith     PetscFunctionReturn(0);
22411cbb95d3SBarry Smith   }
22421cbb95d3SBarry Smith   aii  = aij->i; bii = bij->i;
22431cbb95d3SBarry Smith   adx  = aij->j; bdx = bij->j;
22441cbb95d3SBarry Smith   va   = aij->a; vb = bij->a;
2245785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2246785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
22471cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
22481cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
22491cbb95d3SBarry Smith 
22501cbb95d3SBarry Smith   *f = PETSC_TRUE;
22511cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
22521cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
22531cbb95d3SBarry Smith       PetscInt    idc,idr;
22541cbb95d3SBarry Smith       PetscScalar vc,vr;
22551cbb95d3SBarry Smith       /* column/row index/value */
22561cbb95d3SBarry Smith       idc = adx[aptr[i]];
22571cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
22581cbb95d3SBarry Smith       vc  = va[aptr[i]];
22591cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
22601cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
22611cbb95d3SBarry Smith         *f = PETSC_FALSE;
22621cbb95d3SBarry Smith         goto done;
22631cbb95d3SBarry Smith       } else {
22641cbb95d3SBarry Smith         aptr[i]++;
22651cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
22661cbb95d3SBarry Smith       }
22671cbb95d3SBarry Smith     }
22681cbb95d3SBarry Smith   }
22691cbb95d3SBarry Smith done:
22701cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
22711cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
22721cbb95d3SBarry Smith   PetscFunctionReturn(0);
22731cbb95d3SBarry Smith }
22741cbb95d3SBarry Smith 
2275ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
22769e29f15eSvictorle {
2277dfbe8321SBarry Smith   PetscErrorCode ierr;
22786e111a19SKarl Rupp 
22799e29f15eSvictorle   PetscFunctionBegin;
22805485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
22819e29f15eSvictorle   PetscFunctionReturn(0);
22829e29f15eSvictorle }
22839e29f15eSvictorle 
2284ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
22851cbb95d3SBarry Smith {
22861cbb95d3SBarry Smith   PetscErrorCode ierr;
22876e111a19SKarl Rupp 
22881cbb95d3SBarry Smith   PetscFunctionBegin;
22891cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
22901cbb95d3SBarry Smith   PetscFunctionReturn(0);
22911cbb95d3SBarry Smith }
22921cbb95d3SBarry Smith 
2293dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
229417ab2063SBarry Smith {
2295416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2296fff8e43fSBarry Smith   const PetscScalar *l,*r;
2297fff8e43fSBarry Smith   PetscScalar       x;
229854f21887SBarry Smith   MatScalar         *v;
2299dfbe8321SBarry Smith   PetscErrorCode    ierr;
2300fff8e43fSBarry Smith   PetscInt          i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz;
2301fff8e43fSBarry Smith   const PetscInt    *jj;
230217ab2063SBarry Smith 
23033a40ed3dSBarry Smith   PetscFunctionBegin;
230417ab2063SBarry Smith   if (ll) {
23053ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
23063ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2307e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2308e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
2309fff8e43fSBarry Smith     ierr = VecGetArrayRead(ll,&l);CHKERRQ(ierr);
2310416022c9SBarry Smith     v    = a->a;
231117ab2063SBarry Smith     for (i=0; i<m; i++) {
231217ab2063SBarry Smith       x = l[i];
2313416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
23142205254eSKarl Rupp       for (j=0; j<M; j++) (*v++) *= x;
231517ab2063SBarry Smith     }
2316fff8e43fSBarry Smith     ierr = VecRestoreArrayRead(ll,&l);CHKERRQ(ierr);
2317efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
231817ab2063SBarry Smith   }
231917ab2063SBarry Smith   if (rr) {
2320e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2321e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
2322fff8e43fSBarry Smith     ierr = VecGetArrayRead(rr,&r);CHKERRQ(ierr);
2323416022c9SBarry Smith     v    = a->a; jj = a->j;
23242205254eSKarl Rupp     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
2325fff8e43fSBarry Smith     ierr = VecRestoreArrayRead(rr,&r);CHKERRQ(ierr);
2326efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
232717ab2063SBarry Smith   }
2328acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
23293a40ed3dSBarry Smith   PetscFunctionReturn(0);
233017ab2063SBarry Smith }
233117ab2063SBarry Smith 
23327dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
233317ab2063SBarry Smith {
2334db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
23356849ba73SBarry Smith   PetscErrorCode ierr;
2336d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
233797f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
23385d0c19d7SBarry Smith   const PetscInt *irow,*icol;
23395d0c19d7SBarry Smith   PetscInt       nrows,ncols;
234097f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
234154f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2342416022c9SBarry Smith   Mat            C;
2343cdc6f3adSToby Isaac   PetscBool      stride;
234417ab2063SBarry Smith 
23453a40ed3dSBarry Smith   PetscFunctionBegin;
234699141d43SSatish Balay 
234717ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2348b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2349b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
235017ab2063SBarry Smith 
2351251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2352ff718158SBarry Smith   if (stride) {
2353ff718158SBarry Smith     ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2354ff718158SBarry Smith   } else {
2355ff718158SBarry Smith     first = 0;
2356ff718158SBarry Smith     step  = 0;
2357ff718158SBarry Smith   }
2358fee21e36SBarry Smith   if (stride && step == 1) {
235902834360SBarry Smith     /* special case of contiguous rows */
2360dcca6d9dSJed Brown     ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr);
236102834360SBarry Smith     /* loop over new rows determining lens and starting points */
236202834360SBarry Smith     for (i=0; i<nrows; i++) {
2363bfeeae90SHong Zhang       kstart = ai[irow[i]];
2364a2744918SBarry Smith       kend   = kstart + ailen[irow[i]];
2365a91a9bebSLisandro Dalcin       starts[i] = kstart;
236602834360SBarry Smith       for (k=kstart; k<kend; k++) {
2367bfeeae90SHong Zhang         if (aj[k] >= first) {
236802834360SBarry Smith           starts[i] = k;
236902834360SBarry Smith           break;
237002834360SBarry Smith         }
237102834360SBarry Smith       }
2372a2744918SBarry Smith       sum = 0;
237302834360SBarry Smith       while (k < kend) {
2374bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2375a2744918SBarry Smith         sum++;
237602834360SBarry Smith       }
2377a2744918SBarry Smith       lens[i] = sum;
237802834360SBarry Smith     }
237902834360SBarry Smith     /* create submatrix */
2380cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
238197f1f81fSBarry Smith       PetscInt n_cols,n_rows;
238208480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2383e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2384d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
238508480c60SBarry Smith       C    = *B;
23863a40ed3dSBarry Smith     } else {
23873bef6203SJed Brown       PetscInt rbs,cbs;
2388ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2389f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
23903bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
23913bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
23923bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
23937adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2394ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
239508480c60SBarry Smith     }
2396db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2397db02288aSLois Curfman McInnes 
239802834360SBarry Smith     /* loop over rows inserting into submatrix */
2399db02288aSLois Curfman McInnes     a_new = c->a;
2400db02288aSLois Curfman McInnes     j_new = c->j;
2401db02288aSLois Curfman McInnes     i_new = c->i;
2402bfeeae90SHong Zhang 
240302834360SBarry Smith     for (i=0; i<nrows; i++) {
2404a2744918SBarry Smith       ii    = starts[i];
2405a2744918SBarry Smith       lensi = lens[i];
2406a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2407a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
240802834360SBarry Smith       }
240987828ca2SBarry Smith       ierr       = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2410a2744918SBarry Smith       a_new     += lensi;
2411a2744918SBarry Smith       i_new[i+1] = i_new[i] + lensi;
2412a2744918SBarry Smith       c->ilen[i] = lensi;
241302834360SBarry Smith     }
24140e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
24153a40ed3dSBarry Smith   } else {
241602834360SBarry Smith     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
24171795a4d1SJed Brown     ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr);
2418854ce69bSBarry Smith     ierr = PetscMalloc1(1+nrows,&lens);CHKERRQ(ierr);
24194dcab191SBarry Smith     for (i=0; i<ncols; i++) {
24204dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
24214dcab191SBarry 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);
24224dcab191SBarry Smith #endif
24234dcab191SBarry Smith       smap[icol[i]] = i+1;
24244dcab191SBarry Smith     }
24254dcab191SBarry Smith 
242602834360SBarry Smith     /* determine lens of each row */
242702834360SBarry Smith     for (i=0; i<nrows; i++) {
2428bfeeae90SHong Zhang       kstart  = ai[irow[i]];
242902834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
243002834360SBarry Smith       lens[i] = 0;
243102834360SBarry Smith       for (k=kstart; k<kend; k++) {
2432bfeeae90SHong Zhang         if (smap[aj[k]]) {
243302834360SBarry Smith           lens[i]++;
243402834360SBarry Smith         }
243502834360SBarry Smith       }
243602834360SBarry Smith     }
243717ab2063SBarry Smith     /* Create and fill new matrix */
2438a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2439ace3abfcSBarry Smith       PetscBool equal;
24400f5bd95cSBarry Smith 
244199141d43SSatish Balay       c = (Mat_SeqAIJ*)((*B)->data);
2442e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2443d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
2444f23aa3ddSBarry Smith       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2445d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
244608480c60SBarry Smith       C    = *B;
24473a40ed3dSBarry Smith     } else {
24483bef6203SJed Brown       PetscInt rbs,cbs;
2449ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2450f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
24513bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
24523bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
24533bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
24547adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2455ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
245608480c60SBarry Smith     }
245799141d43SSatish Balay     c = (Mat_SeqAIJ*)(C->data);
245817ab2063SBarry Smith     for (i=0; i<nrows; i++) {
245999141d43SSatish Balay       row      = irow[i];
2460bfeeae90SHong Zhang       kstart   = ai[row];
246199141d43SSatish Balay       kend     = kstart + a->ilen[row];
2462bfeeae90SHong Zhang       mat_i    = c->i[i];
246399141d43SSatish Balay       mat_j    = c->j + mat_i;
246499141d43SSatish Balay       mat_a    = c->a + mat_i;
246599141d43SSatish Balay       mat_ilen = c->ilen + i;
246617ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2467bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2468ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
246999141d43SSatish Balay           *mat_a++ = a->a[k];
247099141d43SSatish Balay           (*mat_ilen)++;
247199141d43SSatish Balay 
247217ab2063SBarry Smith         }
247317ab2063SBarry Smith       }
247417ab2063SBarry Smith     }
247502834360SBarry Smith     /* Free work space */
247602834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2477606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2478606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
2479cdc6f3adSToby Isaac     /* sort */
2480cdc6f3adSToby Isaac     for (i = 0; i < nrows; i++) {
2481cdc6f3adSToby Isaac       PetscInt ilen;
2482cdc6f3adSToby Isaac 
2483cdc6f3adSToby Isaac       mat_i = c->i[i];
2484cdc6f3adSToby Isaac       mat_j = c->j + mat_i;
2485cdc6f3adSToby Isaac       mat_a = c->a + mat_i;
2486cdc6f3adSToby Isaac       ilen  = c->ilen[i];
2487390e1bf2SBarry Smith       ierr  = PetscSortIntWithScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr);
2488cdc6f3adSToby Isaac     }
248902834360SBarry Smith   }
24906d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24916d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
249217ab2063SBarry Smith 
249317ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2494416022c9SBarry Smith   *B   = C;
24953a40ed3dSBarry Smith   PetscFunctionReturn(0);
249617ab2063SBarry Smith }
249717ab2063SBarry Smith 
2498fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
249982d44351SHong Zhang {
250082d44351SHong Zhang   PetscErrorCode ierr;
250182d44351SHong Zhang   Mat            B;
250282d44351SHong Zhang 
250382d44351SHong Zhang   PetscFunctionBegin;
2504c2d650bdSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
250582d44351SHong Zhang     ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
250682d44351SHong Zhang     ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
250733d57670SJed Brown     ierr    = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr);
250882d44351SHong Zhang     ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
250982d44351SHong Zhang     ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
251082d44351SHong Zhang     *subMat = B;
2511c2d650bdSHong Zhang   } else {
2512c2d650bdSHong Zhang     ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
2513c2d650bdSHong Zhang   }
251482d44351SHong Zhang   PetscFunctionReturn(0);
251582d44351SHong Zhang }
251682d44351SHong Zhang 
25179a625307SHong Zhang PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2518a871dcd8SBarry Smith {
251963b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2520dfbe8321SBarry Smith   PetscErrorCode ierr;
252163b91edcSBarry Smith   Mat            outA;
2522ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
252363b91edcSBarry Smith 
25243a40ed3dSBarry Smith   PetscFunctionBegin;
2525e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
25261df811f5SHong Zhang 
2527b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2528b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2529a871dcd8SBarry Smith 
253063b91edcSBarry Smith   outA             = inA;
2531d5f3da31SBarry Smith   outA->factortype = MAT_FACTOR_LU;
2532f6224b95SHong Zhang   ierr = PetscFree(inA->solvertype);CHKERRQ(ierr);
2533f6224b95SHong Zhang   ierr = PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);CHKERRQ(ierr);
25342205254eSKarl Rupp 
2535c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
25366bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
25372205254eSKarl Rupp 
2538c3122656SLisandro Dalcin   a->row = row;
25392205254eSKarl Rupp 
2540c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
25416bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
25422205254eSKarl Rupp 
2543c3122656SLisandro Dalcin   a->col = col;
254463b91edcSBarry Smith 
254536db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
25466bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
25474c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
25483bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
2549f0ec6fceSSatish Balay 
255094a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2551854ce69bSBarry Smith     ierr = PetscMalloc1(inA->rmap->n+1,&a->solve_work);CHKERRQ(ierr);
25523bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
255394a9d846SBarry Smith   }
255463b91edcSBarry Smith 
2555f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2556137fb511SHong Zhang   if (row_identity && col_identity) {
2557ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2558137fb511SHong Zhang   } else {
2559719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2560137fb511SHong Zhang   }
25613a40ed3dSBarry Smith   PetscFunctionReturn(0);
2562a871dcd8SBarry Smith }
2563a871dcd8SBarry Smith 
2564f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2565f0b747eeSBarry Smith {
2566f0b747eeSBarry Smith   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2567f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2568efee365bSSatish Balay   PetscErrorCode ierr;
2569c5df96a5SBarry Smith   PetscBLASInt   one = 1,bnz;
25703a40ed3dSBarry Smith 
25713a40ed3dSBarry Smith   PetscFunctionBegin;
2572c5df96a5SBarry Smith   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
25738b83055fSJed Brown   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2574efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2575acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
25763a40ed3dSBarry Smith   PetscFunctionReturn(0);
2577f0b747eeSBarry Smith }
2578f0b747eeSBarry Smith 
2579f68bb481SHong Zhang PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
258016b64355SHong Zhang {
258116b64355SHong Zhang   PetscErrorCode ierr;
258216b64355SHong Zhang   PetscInt       i;
258316b64355SHong Zhang 
258416b64355SHong Zhang   PetscFunctionBegin;
258516b64355SHong Zhang   if (!submatj->id) { /* delete data that are linked only to submats[id=0] */
258616b64355SHong Zhang     ierr = PetscFree4(submatj->sbuf1,submatj->ptr,submatj->tmp,submatj->ctr);CHKERRQ(ierr);
258716b64355SHong Zhang 
258816b64355SHong Zhang     for (i=0; i<submatj->nrqr; ++i) {
258916b64355SHong Zhang       ierr = PetscFree(submatj->sbuf2[i]);CHKERRQ(ierr);
259016b64355SHong Zhang     }
259116b64355SHong Zhang     ierr = PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);CHKERRQ(ierr);
259216b64355SHong Zhang 
259316b64355SHong Zhang     if (submatj->rbuf1) {
259416b64355SHong Zhang       ierr = PetscFree(submatj->rbuf1[0]);CHKERRQ(ierr);
259516b64355SHong Zhang       ierr = PetscFree(submatj->rbuf1);CHKERRQ(ierr);
259616b64355SHong Zhang     }
259716b64355SHong Zhang 
259816b64355SHong Zhang     for (i=0; i<submatj->nrqs; ++i) {
259916b64355SHong Zhang       ierr = PetscFree(submatj->rbuf3[i]);CHKERRQ(ierr);
260016b64355SHong Zhang     }
260116b64355SHong Zhang     ierr = PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);CHKERRQ(ierr);
260216b64355SHong Zhang     ierr = PetscFree(submatj->pa);CHKERRQ(ierr);
260316b64355SHong Zhang   }
260416b64355SHong Zhang 
260516b64355SHong Zhang #if defined(PETSC_USE_CTABLE)
260616b64355SHong Zhang   ierr = PetscTableDestroy((PetscTable*)&submatj->rmap);CHKERRQ(ierr);
260716b64355SHong Zhang   if (submatj->cmap_loc) {ierr = PetscFree(submatj->cmap_loc);CHKERRQ(ierr);}
260816b64355SHong Zhang   ierr = PetscFree(submatj->rmap_loc);CHKERRQ(ierr);
260916b64355SHong Zhang #else
261016b64355SHong Zhang   ierr = PetscFree(submatj->rmap);CHKERRQ(ierr);
261116b64355SHong Zhang #endif
261216b64355SHong Zhang 
261316b64355SHong Zhang   if (!submatj->allcolumns) {
261416b64355SHong Zhang #if defined(PETSC_USE_CTABLE)
261516b64355SHong Zhang     ierr = PetscTableDestroy((PetscTable*)&submatj->cmap);CHKERRQ(ierr);
261616b64355SHong Zhang #else
261716b64355SHong Zhang     ierr = PetscFree(submatj->cmap);CHKERRQ(ierr);
261816b64355SHong Zhang #endif
261916b64355SHong Zhang   }
262016b64355SHong Zhang   ierr = PetscFree(submatj->row2proc);CHKERRQ(ierr);
262116b64355SHong Zhang 
262216b64355SHong Zhang   ierr = PetscFree(submatj);CHKERRQ(ierr);
262316b64355SHong Zhang   PetscFunctionReturn(0);
262416b64355SHong Zhang }
262516b64355SHong Zhang 
26260fb991dcSHong Zhang PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
262716b64355SHong Zhang {
262816b64355SHong Zhang   PetscErrorCode ierr;
262916b64355SHong Zhang   Mat_SeqAIJ     *c = (Mat_SeqAIJ*)C->data;
26305c39f6d9SHong Zhang   Mat_SubSppt    *submatj = c->submatis1;
263116b64355SHong Zhang 
263216b64355SHong Zhang   PetscFunctionBegin;
263334136279SStefano Zampini   ierr = (*submatj->destroy)(C);CHKERRQ(ierr);
2634f68bb481SHong Zhang   ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr);
263516b64355SHong Zhang   PetscFunctionReturn(0);
263616b64355SHong Zhang }
263716b64355SHong Zhang 
26382d033e1fSHong Zhang PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[])
26392d033e1fSHong Zhang {
26402d033e1fSHong Zhang   PetscErrorCode ierr;
26412d033e1fSHong Zhang   PetscInt       i;
26420fb991dcSHong Zhang   Mat            C;
26430fb991dcSHong Zhang   Mat_SeqAIJ     *c;
26440fb991dcSHong Zhang   Mat_SubSppt    *submatj;
26452d033e1fSHong Zhang 
26462d033e1fSHong Zhang   PetscFunctionBegin;
26472d033e1fSHong Zhang   for (i=0; i<n; i++) {
26480fb991dcSHong Zhang     C       = (*mat)[i];
26490fb991dcSHong Zhang     c       = (Mat_SeqAIJ*)C->data;
26500fb991dcSHong Zhang     submatj = c->submatis1;
26512d033e1fSHong Zhang     if (submatj) {
2652682e4c99SStefano Zampini       if (--((PetscObject)C)->refct <= 0) {
265334136279SStefano Zampini         ierr = (*submatj->destroy)(C);CHKERRQ(ierr);
2654f68bb481SHong Zhang         ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr);
265534136279SStefano Zampini         ierr = PetscFree(C->defaultvectype);CHKERRQ(ierr);
26562d033e1fSHong Zhang         ierr = PetscLayoutDestroy(&C->rmap);CHKERRQ(ierr);
26572d033e1fSHong Zhang         ierr = PetscLayoutDestroy(&C->cmap);CHKERRQ(ierr);
26582d033e1fSHong Zhang         ierr = PetscHeaderDestroy(&C);CHKERRQ(ierr);
2659682e4c99SStefano Zampini       }
26602d033e1fSHong Zhang     } else {
26612d033e1fSHong Zhang       ierr = MatDestroy(&C);CHKERRQ(ierr);
26622d033e1fSHong Zhang     }
26632d033e1fSHong Zhang   }
266486e85357SHong Zhang 
266563a75b2aSHong Zhang   /* Destroy Dummy submatrices created for reuse */
266663a75b2aSHong Zhang   ierr = MatDestroySubMatrices_Dummy(n,mat);CHKERRQ(ierr);
266763a75b2aSHong Zhang 
26682d033e1fSHong Zhang   ierr = PetscFree(*mat);CHKERRQ(ierr);
26692d033e1fSHong Zhang   PetscFunctionReturn(0);
26702d033e1fSHong Zhang }
26712d033e1fSHong Zhang 
26727dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2673cddf8d76SBarry Smith {
2674dfbe8321SBarry Smith   PetscErrorCode ierr;
267597f1f81fSBarry Smith   PetscInt       i;
2676cddf8d76SBarry Smith 
26773a40ed3dSBarry Smith   PetscFunctionBegin;
2678cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2679df750dc8SHong Zhang     ierr = PetscCalloc1(n+1,B);CHKERRQ(ierr);
2680cddf8d76SBarry Smith   }
2681cddf8d76SBarry Smith 
2682cddf8d76SBarry Smith   for (i=0; i<n; i++) {
26837dae84e0SHong Zhang     ierr = MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2684cddf8d76SBarry Smith   }
26853a40ed3dSBarry Smith   PetscFunctionReturn(0);
2686cddf8d76SBarry Smith }
2687cddf8d76SBarry Smith 
268897f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
26894dcbc457SBarry Smith {
2690e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
26916849ba73SBarry Smith   PetscErrorCode ierr;
26925d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
26935d0c19d7SBarry Smith   const PetscInt *idx;
269497f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2695f1af5d2fSBarry Smith   PetscBT        table;
2696bbd702dbSSatish Balay 
26973a40ed3dSBarry Smith   PetscFunctionBegin;
2698d0f46423SBarry Smith   m  = A->rmap->n;
2699e4d965acSSatish Balay   ai = a->i;
2700bfeeae90SHong Zhang   aj = a->j;
27018a047759SSatish Balay 
2702e32f2f54SBarry Smith   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
270306763907SSatish Balay 
2704854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&nidx);CHKERRQ(ierr);
270553b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
270606763907SSatish Balay 
2707e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2708b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2709e4d965acSSatish Balay     isz  = 0;
27106831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2711e4d965acSSatish Balay 
2712e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
27134dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2714b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2715e4d965acSSatish Balay 
2716dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2717e4d965acSSatish Balay     for (j=0; j<n; ++j) {
27182205254eSKarl Rupp       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
27194dcbc457SBarry Smith     }
272006763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
27216bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2722e4d965acSSatish Balay 
272304a348a9SBarry Smith     k = 0;
272404a348a9SBarry Smith     for (j=0; j<ov; j++) { /* for each overlap */
272504a348a9SBarry Smith       n = isz;
272606763907SSatish Balay       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2727e4d965acSSatish Balay         row   = nidx[k];
2728e4d965acSSatish Balay         start = ai[row];
2729e4d965acSSatish Balay         end   = ai[row+1];
273004a348a9SBarry Smith         for (l = start; l<end; l++) {
2731efb16452SHong Zhang           val = aj[l];
27322205254eSKarl Rupp           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2733e4d965acSSatish Balay         }
2734e4d965acSSatish Balay       }
2735e4d965acSSatish Balay     }
273670b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2737e4d965acSSatish Balay   }
273894bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2739606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
27403a40ed3dSBarry Smith   PetscFunctionReturn(0);
27414dcbc457SBarry Smith }
274217ab2063SBarry Smith 
27430513a670SBarry Smith /* -------------------------------------------------------------- */
2744dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
27450513a670SBarry Smith {
27460513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
27476849ba73SBarry Smith   PetscErrorCode ierr;
27483b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
27495d0c19d7SBarry Smith   const PetscInt *row,*col;
27505d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
275156cd22aeSBarry Smith   IS             icolp,irowp;
27520298fd71SBarry Smith   PetscInt       *cwork = NULL;
27530298fd71SBarry Smith   PetscScalar    *vwork = NULL;
27540513a670SBarry Smith 
27553a40ed3dSBarry Smith   PetscFunctionBegin;
27564c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
275756cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
27584c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
275956cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
27600513a670SBarry Smith 
27610513a670SBarry Smith   /* determine lengths of permuted rows */
2762854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&lens);CHKERRQ(ierr);
27632205254eSKarl Rupp   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2764ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2765f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
276633d57670SJed Brown   ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
27677adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2768ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2769606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
27700513a670SBarry Smith 
2771785e854fSJed Brown   ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr);
27720513a670SBarry Smith   for (i=0; i<m; i++) {
277332ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
27742205254eSKarl Rupp     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2775cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
277632ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
27770513a670SBarry Smith   }
2778606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
27792205254eSKarl Rupp 
27803c7d62e4SBarry Smith   (*B)->assembled = PETSC_FALSE;
27812205254eSKarl Rupp 
27820513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27830513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
278456cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
278556cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
27866bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
27876bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
27883a40ed3dSBarry Smith   PetscFunctionReturn(0);
27890513a670SBarry Smith }
27900513a670SBarry Smith 
2791dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2792cb5b572fSBarry Smith {
2793dfbe8321SBarry Smith   PetscErrorCode ierr;
2794cb5b572fSBarry Smith 
2795cb5b572fSBarry Smith   PetscFunctionBegin;
279633f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
279733f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2798be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2799be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2800be6bf707SBarry Smith 
2801700c5bfcSBarry 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");
2802d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2803cdc753b6SBarry Smith     ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr);
2804cb5b572fSBarry Smith   } else {
2805cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2806cb5b572fSBarry Smith   }
2807cb5b572fSBarry Smith   PetscFunctionReturn(0);
2808cb5b572fSBarry Smith }
2809cb5b572fSBarry Smith 
28104994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2811273d9f13SBarry Smith {
2812dfbe8321SBarry Smith   PetscErrorCode ierr;
2813273d9f13SBarry Smith 
2814273d9f13SBarry Smith   PetscFunctionBegin;
2815ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2816273d9f13SBarry Smith   PetscFunctionReturn(0);
2817273d9f13SBarry Smith }
2818273d9f13SBarry Smith 
28198c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
28206c0721eeSBarry Smith {
28216c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
28226e111a19SKarl Rupp 
28236c0721eeSBarry Smith   PetscFunctionBegin;
28246c0721eeSBarry Smith   *array = a->a;
28256c0721eeSBarry Smith   PetscFunctionReturn(0);
28266c0721eeSBarry Smith }
28276c0721eeSBarry Smith 
28288c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
28296c0721eeSBarry Smith {
28306c0721eeSBarry Smith   PetscFunctionBegin;
28316c0721eeSBarry Smith   PetscFunctionReturn(0);
28326c0721eeSBarry Smith }
2833273d9f13SBarry Smith 
28348229c054SShri Abhyankar /*
28358229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
28368229c054SShri Abhyankar    have different nonzero structure.
28378229c054SShri Abhyankar */
2838b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2839ec7775f6SShri Abhyankar {
2840b264fe52SHong Zhang   PetscInt       i,j,k,nzx,nzy;
2841ec7775f6SShri Abhyankar 
2842ec7775f6SShri Abhyankar   PetscFunctionBegin;
2843ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2844ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
2845b264fe52SHong Zhang     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2846b264fe52SHong Zhang     nzx = xi[i+1] - xi[i];
2847b264fe52SHong Zhang     nzy = yi[i+1] - yi[i];
28488af7cee1SJed Brown     nnz[i] = 0;
28498af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2850b264fe52SHong Zhang       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2851b264fe52SHong Zhang       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
28528af7cee1SJed Brown       nnz[i]++;
28538af7cee1SJed Brown     }
28548af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2855ec7775f6SShri Abhyankar   }
2856ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2857ec7775f6SShri Abhyankar }
2858ec7775f6SShri Abhyankar 
2859b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2860b264fe52SHong Zhang {
2861b264fe52SHong Zhang   PetscInt       m = Y->rmap->N;
2862b264fe52SHong Zhang   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2863b264fe52SHong Zhang   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;
2864b264fe52SHong Zhang   PetscErrorCode ierr;
2865b264fe52SHong Zhang 
2866b264fe52SHong Zhang   PetscFunctionBegin;
2867b264fe52SHong Zhang   /* Set the number of nonzeros in the new matrix */
2868b264fe52SHong Zhang   ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr);
2869b264fe52SHong Zhang   PetscFunctionReturn(0);
2870b264fe52SHong Zhang }
2871b264fe52SHong Zhang 
2872f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2873ac90fabeSBarry Smith {
2874dfbe8321SBarry Smith   PetscErrorCode ierr;
2875ac90fabeSBarry Smith   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2876c5df96a5SBarry Smith   PetscBLASInt   one=1,bnz;
2877ac90fabeSBarry Smith 
2878ac90fabeSBarry Smith   PetscFunctionBegin;
2879c5df96a5SBarry Smith   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2880ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2881f4df32b1SMatthew Knepley     PetscScalar alpha = a;
28828b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2883acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2884a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2885ab784542SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2886ab784542SHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2887ac90fabeSBarry Smith   } else {
28888229c054SShri Abhyankar     Mat      B;
28898229c054SShri Abhyankar     PetscInt *nnz;
2890785e854fSJed Brown     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
2891ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2892bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
28934aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
289433d57670SJed Brown     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
2895176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
28968229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2897ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2898ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
289928be2f97SBarry Smith     ierr = MatHeaderReplace(Y,&B);CHKERRQ(ierr);
29008229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2901ac90fabeSBarry Smith   }
2902ac90fabeSBarry Smith   PetscFunctionReturn(0);
2903ac90fabeSBarry Smith }
2904ac90fabeSBarry Smith 
29057087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2906354c94deSBarry Smith {
2907354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2908354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2909354c94deSBarry Smith   PetscInt    i,nz;
2910354c94deSBarry Smith   PetscScalar *a;
2911354c94deSBarry Smith 
2912354c94deSBarry Smith   PetscFunctionBegin;
2913354c94deSBarry Smith   nz = aij->nz;
2914354c94deSBarry Smith   a  = aij->a;
29152205254eSKarl Rupp   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2916354c94deSBarry Smith #else
2917354c94deSBarry Smith   PetscFunctionBegin;
2918354c94deSBarry Smith #endif
2919354c94deSBarry Smith   PetscFunctionReturn(0);
2920354c94deSBarry Smith }
2921354c94deSBarry Smith 
2922985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2923e34fafa9SBarry Smith {
2924e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2925e34fafa9SBarry Smith   PetscErrorCode ierr;
2926d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2927e34fafa9SBarry Smith   PetscReal      atmp;
2928985db425SBarry Smith   PetscScalar    *x;
2929e34fafa9SBarry Smith   MatScalar      *aa;
2930e34fafa9SBarry Smith 
2931e34fafa9SBarry Smith   PetscFunctionBegin;
2932e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2933e34fafa9SBarry Smith   aa = a->a;
2934e34fafa9SBarry Smith   ai = a->i;
2935e34fafa9SBarry Smith   aj = a->j;
2936e34fafa9SBarry Smith 
2937985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2938e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2939e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2940e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2941e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2942e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
29439189402eSHong Zhang     x[i]  = 0.0;
2944e34fafa9SBarry Smith     for (j=0; j<ncols; j++) {
2945985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2946985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2947985db425SBarry Smith       aa++; aj++;
2948985db425SBarry Smith     }
2949985db425SBarry Smith   }
2950985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2951985db425SBarry Smith   PetscFunctionReturn(0);
2952985db425SBarry Smith }
2953985db425SBarry Smith 
2954985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2955985db425SBarry Smith {
2956985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2957985db425SBarry Smith   PetscErrorCode ierr;
2958d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2959985db425SBarry Smith   PetscScalar    *x;
2960985db425SBarry Smith   MatScalar      *aa;
2961985db425SBarry Smith 
2962985db425SBarry Smith   PetscFunctionBegin;
2963e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2964985db425SBarry Smith   aa = a->a;
2965985db425SBarry Smith   ai = a->i;
2966985db425SBarry Smith   aj = a->j;
2967985db425SBarry Smith 
2968985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2969985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2970985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2971e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2972985db425SBarry Smith   for (i=0; i<m; i++) {
2973985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2974d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2975985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2976985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2977985db425SBarry Smith       x[i] = 0.0;
2978985db425SBarry Smith       if (idx) {
2979985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2980985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2981985db425SBarry Smith           if (aj[j] > j) {
2982985db425SBarry Smith             idx[i] = j;
2983985db425SBarry Smith             break;
2984985db425SBarry Smith           }
2985985db425SBarry Smith         }
2986985db425SBarry Smith       }
2987985db425SBarry Smith     }
2988985db425SBarry Smith     for (j=0; j<ncols; j++) {
2989985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2990985db425SBarry Smith       aa++; aj++;
2991985db425SBarry Smith     }
2992985db425SBarry Smith   }
2993985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2994985db425SBarry Smith   PetscFunctionReturn(0);
2995985db425SBarry Smith }
2996985db425SBarry Smith 
2997c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2998c87e5d42SMatthew Knepley {
2999c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3000c87e5d42SMatthew Knepley   PetscErrorCode ierr;
3001c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3002c87e5d42SMatthew Knepley   PetscReal      atmp;
3003c87e5d42SMatthew Knepley   PetscScalar    *x;
3004c87e5d42SMatthew Knepley   MatScalar      *aa;
3005c87e5d42SMatthew Knepley 
3006c87e5d42SMatthew Knepley   PetscFunctionBegin;
3007e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3008c87e5d42SMatthew Knepley   aa = a->a;
3009c87e5d42SMatthew Knepley   ai = a->i;
3010c87e5d42SMatthew Knepley   aj = a->j;
3011c87e5d42SMatthew Knepley 
3012c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3013c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3014c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
301560e0710aSBarry 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);
3016c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
3017c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
3018289a08f5SMatthew Knepley     if (ncols) {
3019289a08f5SMatthew Knepley       /* Get first nonzero */
3020289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
3021289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
30222205254eSKarl Rupp         if (atmp > 1.0e-12) {
30232205254eSKarl Rupp           x[i] = atmp;
30242205254eSKarl Rupp           if (idx) idx[i] = aj[j];
30252205254eSKarl Rupp           break;
30262205254eSKarl Rupp         }
3027289a08f5SMatthew Knepley       }
302812431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
3029289a08f5SMatthew Knepley     } else {
3030289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
3031289a08f5SMatthew Knepley     }
3032c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
3033c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
3034289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3035c87e5d42SMatthew Knepley       aa++; aj++;
3036c87e5d42SMatthew Knepley     }
3037c87e5d42SMatthew Knepley   }
3038c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3039c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
3040c87e5d42SMatthew Knepley }
3041c87e5d42SMatthew Knepley 
3042985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3043985db425SBarry Smith {
3044985db425SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3045985db425SBarry Smith   PetscErrorCode  ierr;
3046d9ca1df4SBarry Smith   PetscInt        i,j,m = A->rmap->n,ncols,n;
3047d9ca1df4SBarry Smith   const PetscInt  *ai,*aj;
3048985db425SBarry Smith   PetscScalar     *x;
3049d9ca1df4SBarry Smith   const MatScalar *aa;
3050985db425SBarry Smith 
3051985db425SBarry Smith   PetscFunctionBegin;
3052e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3053985db425SBarry Smith   aa = a->a;
3054985db425SBarry Smith   ai = a->i;
3055985db425SBarry Smith   aj = a->j;
3056985db425SBarry Smith 
3057985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3058985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3059985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
3060e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3061985db425SBarry Smith   for (i=0; i<m; i++) {
3062985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
3063d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
3064985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
3065985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3066985db425SBarry Smith       x[i] = 0.0;
3067985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
3068985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
3069985db425SBarry Smith         for (j=0; j<ncols; j++) {
3070985db425SBarry Smith           if (aj[j] > j) {
3071985db425SBarry Smith             idx[i] = j;
3072985db425SBarry Smith             break;
3073985db425SBarry Smith           }
3074985db425SBarry Smith         }
3075985db425SBarry Smith       }
3076985db425SBarry Smith     }
3077985db425SBarry Smith     for (j=0; j<ncols; j++) {
3078985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3079985db425SBarry Smith       aa++; aj++;
3080e34fafa9SBarry Smith     }
3081e34fafa9SBarry Smith   }
3082e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3083e34fafa9SBarry Smith   PetscFunctionReturn(0);
3084e34fafa9SBarry Smith }
3085bbead8a2SBarry Smith 
3086713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3087bbead8a2SBarry Smith {
3088bbead8a2SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*) A->data;
3089bbead8a2SBarry Smith   PetscErrorCode  ierr;
309033d57670SJed Brown   PetscInt        i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3091bbead8a2SBarry Smith   MatScalar       *diag,work[25],*v_work;
30920da83c2eSBarry Smith   const PetscReal shift = 0.0;
30931a9391e3SHong Zhang   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;
3094bbead8a2SBarry Smith 
3095bbead8a2SBarry Smith   PetscFunctionBegin;
3096a455e926SHong Zhang   allowzeropivot = PetscNot(A->erroriffailure);
30974a0d0026SBarry Smith   if (a->ibdiagvalid) {
30984a0d0026SBarry Smith     if (values) *values = a->ibdiag;
30994a0d0026SBarry Smith     PetscFunctionReturn(0);
31004a0d0026SBarry Smith   }
3101bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
3102bbead8a2SBarry Smith   if (!a->ibdiag) {
3103785e854fSJed Brown     ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr);
31043bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
3105bbead8a2SBarry Smith   }
3106bbead8a2SBarry Smith   diag = a->ibdiag;
3107bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
3108bbead8a2SBarry Smith   /* factor and invert each block */
3109bbead8a2SBarry Smith   switch (bs) {
3110bbead8a2SBarry Smith   case 1:
3111bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3112bbead8a2SBarry Smith       ierr = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
3113ec1892c8SHong Zhang       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3114ec1892c8SHong Zhang         if (allowzeropivot) {
31157b6c816cSBarry Smith           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
31167b6c816cSBarry Smith           A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
31177b6c816cSBarry Smith           A->factorerror_zeropivot_row   = i;
31187b6c816cSBarry Smith           ierr = PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);CHKERRQ(ierr);
31197b6c816cSBarry 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);
3120ec1892c8SHong Zhang       }
3121bbead8a2SBarry Smith       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3122bbead8a2SBarry Smith     }
3123bbead8a2SBarry Smith     break;
3124bbead8a2SBarry Smith   case 2:
3125bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3126bbead8a2SBarry Smith       ij[0] = 2*i; ij[1] = 2*i + 1;
3127bbead8a2SBarry Smith       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
3128a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31297b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
313096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
3131bbead8a2SBarry Smith       diag += 4;
3132bbead8a2SBarry Smith     }
3133bbead8a2SBarry Smith     break;
3134bbead8a2SBarry Smith   case 3:
3135bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3136bbead8a2SBarry Smith       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3137bbead8a2SBarry Smith       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
3138a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31397b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
314096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
3141bbead8a2SBarry Smith       diag += 9;
3142bbead8a2SBarry Smith     }
3143bbead8a2SBarry Smith     break;
3144bbead8a2SBarry Smith   case 4:
3145bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3146bbead8a2SBarry Smith       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3147bbead8a2SBarry Smith       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
3148a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31497b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
315096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
3151bbead8a2SBarry Smith       diag += 16;
3152bbead8a2SBarry Smith     }
3153bbead8a2SBarry Smith     break;
3154bbead8a2SBarry Smith   case 5:
3155bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3156bbead8a2SBarry 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;
3157bbead8a2SBarry Smith       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
3158a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31597b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
316096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
3161bbead8a2SBarry Smith       diag += 25;
3162bbead8a2SBarry Smith     }
3163bbead8a2SBarry Smith     break;
3164bbead8a2SBarry Smith   case 6:
3165bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3166bbead8a2SBarry 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;
3167bbead8a2SBarry Smith       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
3168a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31697b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
317096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3171bbead8a2SBarry Smith       diag += 36;
3172bbead8a2SBarry Smith     }
3173bbead8a2SBarry Smith     break;
3174bbead8a2SBarry Smith   case 7:
3175bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3176bbead8a2SBarry 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;
3177bbead8a2SBarry Smith       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
3178a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31797b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
318096b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3181bbead8a2SBarry Smith       diag += 49;
3182bbead8a2SBarry Smith     }
3183bbead8a2SBarry Smith     break;
3184bbead8a2SBarry Smith   default:
3185dcca6d9dSJed Brown     ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr);
3186bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3187bbead8a2SBarry Smith       for (j=0; j<bs; j++) {
3188bbead8a2SBarry Smith         IJ[j] = bs*i + j;
3189bbead8a2SBarry Smith       }
3190bbead8a2SBarry Smith       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
31915f8bbccaSHong Zhang       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
31927b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
319396b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3194bbead8a2SBarry Smith       diag += bs2;
3195bbead8a2SBarry Smith     }
3196bbead8a2SBarry Smith     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3197bbead8a2SBarry Smith   }
3198bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3199bbead8a2SBarry Smith   PetscFunctionReturn(0);
3200bbead8a2SBarry Smith }
3201bbead8a2SBarry Smith 
320273a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
320373a71a0fSBarry Smith {
320473a71a0fSBarry Smith   PetscErrorCode ierr;
320573a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
320673a71a0fSBarry Smith   PetscScalar    a;
320773a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
320873a71a0fSBarry Smith 
320973a71a0fSBarry Smith   PetscFunctionBegin;
321073a71a0fSBarry Smith   if (!x->assembled) {
321173a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
321273a71a0fSBarry Smith     for (i=0; i<m; i++) {
321373a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
321473a71a0fSBarry Smith         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
321573a71a0fSBarry Smith         col  = (PetscInt)(n*PetscRealPart(a));
321673a71a0fSBarry Smith         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
321773a71a0fSBarry Smith       }
321873a71a0fSBarry Smith     }
3219e2ce353bSJunchao Zhang   } else {
3220e2ce353bSJunchao Zhang     for (i=0; i<aij->nz; i++) {ierr = PetscRandomGetValue(rctx,aij->a+i);CHKERRQ(ierr);}
3221e2ce353bSJunchao Zhang   }
322273a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
322373a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
322473a71a0fSBarry Smith   PetscFunctionReturn(0);
322573a71a0fSBarry Smith }
322673a71a0fSBarry Smith 
3227679944adSJunchao Zhang /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3228679944adSJunchao Zhang PetscErrorCode  MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x,PetscInt low,PetscInt high,PetscRandom rctx)
3229679944adSJunchao Zhang {
3230679944adSJunchao Zhang   PetscErrorCode ierr;
3231679944adSJunchao Zhang   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3232679944adSJunchao Zhang   PetscScalar    a;
3233679944adSJunchao Zhang   PetscInt       m,n,i,j,col,nskip;
3234679944adSJunchao Zhang 
3235679944adSJunchao Zhang   PetscFunctionBegin;
3236679944adSJunchao Zhang   nskip = high - low;
3237679944adSJunchao Zhang   ierr  = MatGetSize(x,&m,&n);CHKERRQ(ierr);
3238679944adSJunchao Zhang   n    -= nskip; /* shrink number of columns where nonzeros can be set */
3239679944adSJunchao Zhang   for (i=0; i<m; i++) {
3240679944adSJunchao Zhang     for (j=0; j<aij->imax[i]; j++) {
3241679944adSJunchao Zhang       ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
3242679944adSJunchao Zhang       col  = (PetscInt)(n*PetscRealPart(a));
3243679944adSJunchao Zhang       if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3244679944adSJunchao Zhang       ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
3245679944adSJunchao Zhang     }
3246e2ce353bSJunchao Zhang   }
3247679944adSJunchao Zhang   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3248679944adSJunchao Zhang   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3249679944adSJunchao Zhang   PetscFunctionReturn(0);
3250679944adSJunchao Zhang }
3251679944adSJunchao Zhang 
3252679944adSJunchao Zhang 
3253682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
32540a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3255cb5b572fSBarry Smith                                         MatGetRow_SeqAIJ,
3256cb5b572fSBarry Smith                                         MatRestoreRow_SeqAIJ,
3257cb5b572fSBarry Smith                                         MatMult_SeqAIJ,
325897304618SKris Buschelman                                 /*  4*/ MatMultAdd_SeqAIJ,
32597c922b88SBarry Smith                                         MatMultTranspose_SeqAIJ,
32607c922b88SBarry Smith                                         MatMultTransposeAdd_SeqAIJ,
3261db4efbfdSBarry Smith                                         0,
3262db4efbfdSBarry Smith                                         0,
3263db4efbfdSBarry Smith                                         0,
3264db4efbfdSBarry Smith                                 /* 10*/ 0,
3265cb5b572fSBarry Smith                                         MatLUFactor_SeqAIJ,
3266cb5b572fSBarry Smith                                         0,
326741f059aeSBarry Smith                                         MatSOR_SeqAIJ,
326891e9d3e2SHong Zhang                                         MatTranspose_SeqAIJ,
326997304618SKris Buschelman                                 /*1 5*/ MatGetInfo_SeqAIJ,
3270cb5b572fSBarry Smith                                         MatEqual_SeqAIJ,
3271cb5b572fSBarry Smith                                         MatGetDiagonal_SeqAIJ,
3272cb5b572fSBarry Smith                                         MatDiagonalScale_SeqAIJ,
3273cb5b572fSBarry Smith                                         MatNorm_SeqAIJ,
327497304618SKris Buschelman                                 /* 20*/ 0,
3275cb5b572fSBarry Smith                                         MatAssemblyEnd_SeqAIJ,
3276cb5b572fSBarry Smith                                         MatSetOption_SeqAIJ,
3277cb5b572fSBarry Smith                                         MatZeroEntries_SeqAIJ,
3278d519adbfSMatthew Knepley                                 /* 24*/ MatZeroRows_SeqAIJ,
3279db4efbfdSBarry Smith                                         0,
3280db4efbfdSBarry Smith                                         0,
3281db4efbfdSBarry Smith                                         0,
3282db4efbfdSBarry Smith                                         0,
32834994cf47SJed Brown                                 /* 29*/ MatSetUp_SeqAIJ,
3284db4efbfdSBarry Smith                                         0,
3285db4efbfdSBarry Smith                                         0,
32868c778c55SBarry Smith                                         0,
32878c778c55SBarry Smith                                         0,
3288d519adbfSMatthew Knepley                                 /* 34*/ MatDuplicate_SeqAIJ,
3289cb5b572fSBarry Smith                                         0,
3290cb5b572fSBarry Smith                                         0,
3291cb5b572fSBarry Smith                                         MatILUFactor_SeqAIJ,
3292cb5b572fSBarry Smith                                         0,
3293d519adbfSMatthew Knepley                                 /* 39*/ MatAXPY_SeqAIJ,
32947dae84e0SHong Zhang                                         MatCreateSubMatrices_SeqAIJ,
3295cb5b572fSBarry Smith                                         MatIncreaseOverlap_SeqAIJ,
3296cb5b572fSBarry Smith                                         MatGetValues_SeqAIJ,
3297cb5b572fSBarry Smith                                         MatCopy_SeqAIJ,
3298d519adbfSMatthew Knepley                                 /* 44*/ MatGetRowMax_SeqAIJ,
3299cb5b572fSBarry Smith                                         MatScale_SeqAIJ,
33007d68702bSBarry Smith                                         MatShift_SeqAIJ,
330179299369SBarry Smith                                         MatDiagonalSet_SeqAIJ,
33026e169961SBarry Smith                                         MatZeroRowsColumns_SeqAIJ,
330373a71a0fSBarry Smith                                 /* 49*/ MatSetRandom_SeqAIJ,
33043b2fbd54SBarry Smith                                         MatGetRowIJ_SeqAIJ,
33053b2fbd54SBarry Smith                                         MatRestoreRowIJ_SeqAIJ,
33063b2fbd54SBarry Smith                                         MatGetColumnIJ_SeqAIJ,
3307a93ec695SBarry Smith                                         MatRestoreColumnIJ_SeqAIJ,
330893dfae19SHong Zhang                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3309b9617806SBarry Smith                                         0,
33100513a670SBarry Smith                                         0,
3311cda55fadSBarry Smith                                         MatPermute_SeqAIJ,
3312cda55fadSBarry Smith                                         0,
3313d519adbfSMatthew Knepley                                 /* 59*/ 0,
3314b9b97703SBarry Smith                                         MatDestroy_SeqAIJ,
3315b9b97703SBarry Smith                                         MatView_SeqAIJ,
3316357abbc8SBarry Smith                                         0,
3317321b30b9SSatish Balay                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3318321b30b9SSatish Balay                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3319321b30b9SSatish Balay                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3320ee4f033dSBarry Smith                                         0,
3321ee4f033dSBarry Smith                                         0,
3322ee4f033dSBarry Smith                                         0,
3323d519adbfSMatthew Knepley                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3324c87e5d42SMatthew Knepley                                         MatGetRowMinAbs_SeqAIJ,
3325ee4f033dSBarry Smith                                         0,
3326dcf5cc72SBarry Smith                                         0,
33272c93a97aSBarry Smith                                         0,
33282c93a97aSBarry Smith                                 /* 74*/ 0,
33293acb8795SBarry Smith                                         MatFDColoringApply_AIJ,
333097304618SKris Buschelman                                         0,
333197304618SKris Buschelman                                         0,
333297304618SKris Buschelman                                         0,
33336ce1633cSBarry Smith                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
333497304618SKris Buschelman                                         0,
333597304618SKris Buschelman                                         0,
333697304618SKris Buschelman                                         0,
3337bc011b1eSHong Zhang                                         MatLoad_SeqAIJ,
3338d519adbfSMatthew Knepley                                 /* 84*/ MatIsSymmetric_SeqAIJ,
33391cbb95d3SBarry Smith                                         MatIsHermitian_SeqAIJ,
33406284ec50SHong Zhang                                         0,
33416284ec50SHong Zhang                                         0,
3342bc011b1eSHong Zhang                                         0,
3343d519adbfSMatthew Knepley                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
334426be0446SHong Zhang                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
334526be0446SHong Zhang                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
334665e8a0caSHong Zhang                                         MatPtAP_SeqAIJ_SeqAIJ,
33478fa4b5a6SHong Zhang                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy,
33488fa4b5a6SHong Zhang                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
33496fc122caSHong Zhang                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
33506fc122caSHong Zhang                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
33516fc122caSHong Zhang                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
33522121bac1SHong Zhang                                         0,
33532121bac1SHong Zhang                                 /* 99*/ 0,
3354609c6c4dSKris Buschelman                                         0,
3355609c6c4dSKris Buschelman                                         0,
335687d4246cSBarry Smith                                         MatConjugate_SeqAIJ,
335787d4246cSBarry Smith                                         0,
3358d519adbfSMatthew Knepley                                 /*104*/ MatSetValuesRow_SeqAIJ,
335999cafbc1SBarry Smith                                         MatRealPart_SeqAIJ,
3360f5edf698SHong Zhang                                         MatImaginaryPart_SeqAIJ,
3361f5edf698SHong Zhang                                         0,
33622bebee5dSHong Zhang                                         0,
3363cbd44569SHong Zhang                                 /*109*/ MatMatSolve_SeqAIJ,
3364985db425SBarry Smith                                         0,
33652af78befSBarry Smith                                         MatGetRowMin_SeqAIJ,
33662af78befSBarry Smith                                         0,
3367599ef60dSHong Zhang                                         MatMissingDiagonal_SeqAIJ,
3368d519adbfSMatthew Knepley                                 /*114*/ 0,
3369599ef60dSHong Zhang                                         0,
33703c2a7987SHong Zhang                                         0,
3371fe97e370SBarry Smith                                         0,
3372fbdbba38SShri Abhyankar                                         0,
3373fbdbba38SShri Abhyankar                                 /*119*/ 0,
3374fbdbba38SShri Abhyankar                                         0,
3375fbdbba38SShri Abhyankar                                         0,
337682d44351SHong Zhang                                         0,
3377b3a44c85SBarry Smith                                         MatGetMultiProcBlock_SeqAIJ,
33780716a85fSBarry Smith                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3379bbead8a2SBarry Smith                                         MatGetColumnNorms_SeqAIJ,
338037868618SMatthew G Knepley                                         MatInvertBlockDiagonal_SeqAIJ,
33810da83c2eSBarry Smith                                         MatInvertVariableBlockDiagonal_SeqAIJ,
338237868618SMatthew G Knepley                                         0,
33835df89d91SHong Zhang                                 /*129*/ 0,
338475648e8dSHong Zhang                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
338575648e8dSHong Zhang                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
338675648e8dSHong Zhang                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3387b9af6bddSHong Zhang                                         MatTransposeColoringCreate_SeqAIJ,
3388b9af6bddSHong Zhang                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
33892b8ad9a3SHong Zhang                                         MatTransColoringApplyDenToSp_SeqAIJ,
33902b8ad9a3SHong Zhang                                         MatRARt_SeqAIJ_SeqAIJ,
33912b8ad9a3SHong Zhang                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
33923964eb88SJed Brown                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
33933964eb88SJed Brown                                  /*139*/0,
3394f9426fe0SMark Adams                                         0,
33951919a2e2SJed Brown                                         0,
33963a062f41SBarry Smith                                         MatFDColoringSetUp_SeqXAIJ,
33979c8f2541SHong Zhang                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
33982d033e1fSHong Zhang                                  /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
33992d033e1fSHong Zhang                                         MatDestroySubMatrices_SeqAIJ
34009e29f15eSvictorle };
340117ab2063SBarry Smith 
34027087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3403bef8e0ddSBarry Smith {
3404bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
340597f1f81fSBarry Smith   PetscInt   i,nz,n;
3406bef8e0ddSBarry Smith 
3407bef8e0ddSBarry Smith   PetscFunctionBegin;
3408bef8e0ddSBarry Smith   nz = aij->maxnz;
3409d0f46423SBarry Smith   n  = mat->rmap->n;
3410bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3411bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3412bef8e0ddSBarry Smith   }
3413bef8e0ddSBarry Smith   aij->nz = nz;
3414bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3415bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3416bef8e0ddSBarry Smith   }
3417bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3418bef8e0ddSBarry Smith }
3419bef8e0ddSBarry Smith 
3420a3bb6f32SFande Kong /*
3421e8b528d9SFande Kong  * When a sparse matrix has many zero columns, we should compact them out to save the space
3422a3bb6f32SFande Kong  * This happens in MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3423a3bb6f32SFande Kong  * */
3424a3bb6f32SFande Kong PetscErrorCode  MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3425a3bb6f32SFande Kong {
3426a3bb6f32SFande Kong   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3427a3bb6f32SFande Kong   PetscTable         gid1_lid1;
3428a3bb6f32SFande Kong   PetscTablePosition tpos;
3429a3bb6f32SFande Kong   PetscInt           gid,lid,i,j,ncols,ec;
3430a3bb6f32SFande Kong   PetscInt           *garray;
3431a3bb6f32SFande Kong   PetscErrorCode  ierr;
3432a3bb6f32SFande Kong 
3433a3bb6f32SFande Kong   PetscFunctionBegin;
3434a3bb6f32SFande Kong   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3435a3bb6f32SFande Kong   PetscValidPointer(mapping,2);
3436a3bb6f32SFande Kong   /* use a table */
3437a3bb6f32SFande Kong   ierr = PetscTableCreate(mat->rmap->n,mat->cmap->N+1,&gid1_lid1);CHKERRQ(ierr);
3438a3bb6f32SFande Kong   ec = 0;
3439a3bb6f32SFande Kong   for (i=0; i<mat->rmap->n; i++) {
3440a3bb6f32SFande Kong     ncols = aij->i[i+1] - aij->i[i];
3441a3bb6f32SFande Kong     for (j=0; j<ncols; j++) {
3442a3bb6f32SFande Kong       PetscInt data,gid1 = aij->j[aij->i[i] + j] + 1;
3443a3bb6f32SFande Kong       ierr = PetscTableFind(gid1_lid1,gid1,&data);CHKERRQ(ierr);
3444a3bb6f32SFande Kong       if (!data) {
3445a3bb6f32SFande Kong         /* one based table */
3446a3bb6f32SFande Kong         ierr = PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);CHKERRQ(ierr);
3447a3bb6f32SFande Kong       }
3448a3bb6f32SFande Kong     }
3449a3bb6f32SFande Kong   }
3450a3bb6f32SFande Kong   /* form array of columns we need */
3451a3bb6f32SFande Kong   ierr = PetscMalloc1(ec+1,&garray);CHKERRQ(ierr);
3452a3bb6f32SFande Kong   ierr = PetscTableGetHeadPosition(gid1_lid1,&tpos);CHKERRQ(ierr);
3453a3bb6f32SFande Kong   while (tpos) {
3454a3bb6f32SFande Kong     ierr = PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);CHKERRQ(ierr);
3455a3bb6f32SFande Kong     gid--;
3456a3bb6f32SFande Kong     lid--;
3457a3bb6f32SFande Kong     garray[lid] = gid;
3458a3bb6f32SFande Kong   }
3459a3bb6f32SFande Kong   ierr = PetscSortInt(ec,garray);CHKERRQ(ierr); /* sort, and rebuild */
3460a3bb6f32SFande Kong   ierr = PetscTableRemoveAll(gid1_lid1);CHKERRQ(ierr);
3461a3bb6f32SFande Kong   for (i=0; i<ec; i++) {
3462a3bb6f32SFande Kong     ierr = PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr);
3463a3bb6f32SFande Kong   }
3464a3bb6f32SFande Kong   /* compact out the extra columns in B */
3465a3bb6f32SFande Kong   for (i=0; i<mat->rmap->n; i++) {
3466a3bb6f32SFande Kong 	ncols = aij->i[i+1] - aij->i[i];
3467a3bb6f32SFande Kong     for (j=0; j<ncols; j++) {
3468a3bb6f32SFande Kong       PetscInt gid1 = aij->j[aij->i[i] + j] + 1;
3469a3bb6f32SFande Kong       ierr = PetscTableFind(gid1_lid1,gid1,&lid);CHKERRQ(ierr);
3470a3bb6f32SFande Kong       lid--;
3471a3bb6f32SFande Kong       aij->j[aij->i[i] + j] = lid;
3472a3bb6f32SFande Kong     }
3473a3bb6f32SFande Kong   }
3474a3bb6f32SFande Kong   mat->cmap->n = mat->cmap->N = ec;
3475a3bb6f32SFande Kong   mat->cmap->bs = 1;
3476a3bb6f32SFande Kong 
3477a3bb6f32SFande Kong   ierr = PetscTableDestroy(&gid1_lid1);CHKERRQ(ierr);
3478a3bb6f32SFande Kong   ierr = PetscLayoutSetUp((mat->cmap));CHKERRQ(ierr);
3479a3bb6f32SFande Kong   ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,mat->cmap->bs,mat->cmap->n,garray,PETSC_OWN_POINTER,mapping);CHKERRQ(ierr);
3480a3bb6f32SFande Kong   ierr = ISLocalToGlobalMappingSetType(*mapping,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
3481a3bb6f32SFande Kong   PetscFunctionReturn(0);
3482a3bb6f32SFande Kong }
3483a3bb6f32SFande Kong 
3484bef8e0ddSBarry Smith /*@
3485bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3486bef8e0ddSBarry Smith        in the matrix.
3487bef8e0ddSBarry Smith 
3488bef8e0ddSBarry Smith   Input Parameters:
3489bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3490bef8e0ddSBarry Smith -  indices - the column indices
3491bef8e0ddSBarry Smith 
349215091d37SBarry Smith   Level: advanced
349315091d37SBarry Smith 
3494bef8e0ddSBarry Smith   Notes:
3495bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3496bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3497bef8e0ddSBarry Smith   of the MatSetValues() operation.
3498bef8e0ddSBarry Smith 
3499bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3500d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3501bef8e0ddSBarry Smith 
3502bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3503bef8e0ddSBarry Smith 
3504b9617806SBarry Smith     The indices should start with zero, not one.
3505b9617806SBarry Smith 
3506bef8e0ddSBarry Smith @*/
35077087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3508bef8e0ddSBarry Smith {
35094ac538c5SBarry Smith   PetscErrorCode ierr;
3510bef8e0ddSBarry Smith 
3511bef8e0ddSBarry Smith   PetscFunctionBegin;
35120700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
35134482741eSBarry Smith   PetscValidPointer(indices,2);
35144ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3515bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3516bef8e0ddSBarry Smith }
3517bef8e0ddSBarry Smith 
3518be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3519be6bf707SBarry Smith 
35207087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3521be6bf707SBarry Smith {
3522be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
35236849ba73SBarry Smith   PetscErrorCode ierr;
3524d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3525be6bf707SBarry Smith 
3526be6bf707SBarry Smith   PetscFunctionBegin;
3527169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3528be6bf707SBarry Smith 
3529be6bf707SBarry Smith   /* allocate space for values if not already there */
3530be6bf707SBarry Smith   if (!aij->saved_values) {
3531854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr);
35323bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3533be6bf707SBarry Smith   }
3534be6bf707SBarry Smith 
3535be6bf707SBarry Smith   /* copy values over */
353687828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3537be6bf707SBarry Smith   PetscFunctionReturn(0);
3538be6bf707SBarry Smith }
3539be6bf707SBarry Smith 
3540be6bf707SBarry Smith /*@
3541be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3542be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3543be6bf707SBarry Smith        nonlinear portion.
3544be6bf707SBarry Smith 
3545be6bf707SBarry Smith    Collect on Mat
3546be6bf707SBarry Smith 
3547be6bf707SBarry Smith   Input Parameters:
35480e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3549be6bf707SBarry Smith 
355015091d37SBarry Smith   Level: advanced
355115091d37SBarry Smith 
3552be6bf707SBarry Smith   Common Usage, with SNESSolve():
3553be6bf707SBarry Smith $    Create Jacobian matrix
3554be6bf707SBarry Smith $    Set linear terms into matrix
3555be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3556be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3557be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3558512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3559be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3560be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3561be6bf707SBarry Smith $    In your Jacobian routine
3562be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3563be6bf707SBarry Smith $      Set nonlinear terms in matrix
3564be6bf707SBarry Smith 
3565be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3566be6bf707SBarry Smith $    // build linear portion of Jacobian
3567512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3568be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3569be6bf707SBarry Smith $    loop over nonlinear iterations
3570be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3571be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3572be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3573be6bf707SBarry Smith $       Solve linear system with Jacobian
3574be6bf707SBarry Smith $    endloop
3575be6bf707SBarry Smith 
3576be6bf707SBarry Smith   Notes:
3577be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3578512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3579be6bf707SBarry Smith     calling this routine.
3580be6bf707SBarry Smith 
35810c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
35820c468ba9SBarry Smith     and does not allocated additional space.
35830c468ba9SBarry Smith 
3584be6bf707SBarry Smith .seealso: MatRetrieveValues()
3585be6bf707SBarry Smith 
3586be6bf707SBarry Smith @*/
35877087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3588be6bf707SBarry Smith {
35894ac538c5SBarry Smith   PetscErrorCode ierr;
3590be6bf707SBarry Smith 
3591be6bf707SBarry Smith   PetscFunctionBegin;
35920700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3593e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3594e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
35954ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3596be6bf707SBarry Smith   PetscFunctionReturn(0);
3597be6bf707SBarry Smith }
3598be6bf707SBarry Smith 
35997087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3600be6bf707SBarry Smith {
3601be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
36026849ba73SBarry Smith   PetscErrorCode ierr;
3603d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3604be6bf707SBarry Smith 
3605be6bf707SBarry Smith   PetscFunctionBegin;
3606169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3607f23aa3ddSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3608be6bf707SBarry Smith   /* copy values over */
360987828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3610be6bf707SBarry Smith   PetscFunctionReturn(0);
3611be6bf707SBarry Smith }
3612be6bf707SBarry Smith 
3613be6bf707SBarry Smith /*@
3614be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3615be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3616be6bf707SBarry Smith        nonlinear portion.
3617be6bf707SBarry Smith 
3618be6bf707SBarry Smith    Collect on Mat
3619be6bf707SBarry Smith 
3620be6bf707SBarry Smith   Input Parameters:
3621386f7cf9SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3622be6bf707SBarry Smith 
362315091d37SBarry Smith   Level: advanced
362415091d37SBarry Smith 
3625be6bf707SBarry Smith .seealso: MatStoreValues()
3626be6bf707SBarry Smith 
3627be6bf707SBarry Smith @*/
36287087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3629be6bf707SBarry Smith {
36304ac538c5SBarry Smith   PetscErrorCode ierr;
3631be6bf707SBarry Smith 
3632be6bf707SBarry Smith   PetscFunctionBegin;
36330700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3634e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3635e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
36364ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3637be6bf707SBarry Smith   PetscFunctionReturn(0);
3638be6bf707SBarry Smith }
3639be6bf707SBarry Smith 
3640f83d6046SBarry Smith 
3641be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
364217ab2063SBarry Smith /*@C
3643682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
36440d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
36456e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
364651c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
36472bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
364817ab2063SBarry Smith 
3649d083f849SBarry Smith    Collective
3650db81eaa0SLois Curfman McInnes 
365117ab2063SBarry Smith    Input Parameters:
3652db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
365317ab2063SBarry Smith .  m - number of rows
365417ab2063SBarry Smith .  n - number of columns
365517ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
365651c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
36570298fd71SBarry Smith          (possibly different for each row) or NULL
365817ab2063SBarry Smith 
365917ab2063SBarry Smith    Output Parameter:
3660416022c9SBarry Smith .  A - the matrix
366117ab2063SBarry Smith 
3662175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3663ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3664175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3665175b88e8SBarry Smith 
3666b259b22eSLois Curfman McInnes    Notes:
366749a6f317SBarry Smith    If nnz is given then nz is ignored
366849a6f317SBarry Smith 
366917ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
367017ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
36710002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
367244cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
367317ab2063SBarry Smith 
367417ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
36750298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
36763d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
36776da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
367817ab2063SBarry Smith 
3679682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
36804fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3681682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
36826c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
36836c7ebb05SLois Curfman McInnes 
36846c7ebb05SLois Curfman McInnes    Options Database Keys:
3685698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
36869db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
368717ab2063SBarry Smith 
3688027ccd11SLois Curfman McInnes    Level: intermediate
3689027ccd11SLois Curfman McInnes 
369069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
369136db0b34SBarry Smith 
369217ab2063SBarry Smith @*/
36937087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
369417ab2063SBarry Smith {
3695dfbe8321SBarry Smith   PetscErrorCode ierr;
36966945ee14SBarry Smith 
36973a40ed3dSBarry Smith   PetscFunctionBegin;
3698f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3699117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3700c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3701d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3702273d9f13SBarry Smith   PetscFunctionReturn(0);
3703273d9f13SBarry Smith }
3704273d9f13SBarry Smith 
3705273d9f13SBarry Smith /*@C
3706273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3707273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3708273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3709273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3710273d9f13SBarry Smith 
3711d083f849SBarry Smith    Collective
3712273d9f13SBarry Smith 
3713273d9f13SBarry Smith    Input Parameters:
37141c4f3114SJed Brown +  B - The matrix
3715273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3716273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
37170298fd71SBarry Smith          (possibly different for each row) or NULL
3718273d9f13SBarry Smith 
3719273d9f13SBarry Smith    Notes:
372049a6f317SBarry Smith      If nnz is given then nz is ignored
372149a6f317SBarry Smith 
3722273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3723273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3724273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3725273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3726273d9f13SBarry Smith 
3727273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
37280298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3729273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3730273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3731273d9f13SBarry Smith 
3732aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3733aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3734aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3735aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3736aa95bbe8SBarry Smith 
3737a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3738a96a251dSBarry Smith    entries or columns indices
3739a96a251dSBarry Smith 
3740273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3741273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3742273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3743273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3744273d9f13SBarry Smith 
3745273d9f13SBarry Smith    Options Database Keys:
3746698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
374747b2e64bSBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3748273d9f13SBarry Smith 
3749273d9f13SBarry Smith    Level: intermediate
3750273d9f13SBarry Smith 
375169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3752273d9f13SBarry Smith 
3753273d9f13SBarry Smith @*/
37547087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3755273d9f13SBarry Smith {
37564ac538c5SBarry Smith   PetscErrorCode ierr;
3757a23d5eceSKris Buschelman 
3758a23d5eceSKris Buschelman   PetscFunctionBegin;
37596ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
37606ba663aaSJed Brown   PetscValidType(B,1);
37614ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3762a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3763a23d5eceSKris Buschelman }
3764a23d5eceSKris Buschelman 
37657087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3766a23d5eceSKris Buschelman {
3767273d9f13SBarry Smith   Mat_SeqAIJ     *b;
37682576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
37696849ba73SBarry Smith   PetscErrorCode ierr;
377097f1f81fSBarry Smith   PetscInt       i;
3771273d9f13SBarry Smith 
3772273d9f13SBarry Smith   PetscFunctionBegin;
37732576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3774a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3775c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3776c461c341SBarry Smith     nz             = 0;
3777c461c341SBarry Smith   }
377826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
377926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3780899cda47SBarry Smith 
3781435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
378260e0710aSBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3783b73539f3SBarry Smith   if (nnz) {
3784d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
378560e0710aSBarry 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]);
378660e0710aSBarry 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);
3787b73539f3SBarry Smith     }
3788b73539f3SBarry Smith   }
3789b73539f3SBarry Smith 
3790273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
37912205254eSKarl Rupp 
3792273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3793273d9f13SBarry Smith 
3794ab93d7beSBarry Smith   if (!skipallocation) {
37952ee49352SLisandro Dalcin     if (!b->imax) {
3796dcca6d9dSJed Brown       ierr = PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);CHKERRQ(ierr);
37973bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
37982ee49352SLisandro Dalcin     }
3799846b4da1SFande Kong     if (!b->ipre) {
3800846b4da1SFande Kong       ierr = PetscMalloc1(B->rmap->n,&b->ipre);CHKERRQ(ierr);
3801846b4da1SFande Kong       ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3802846b4da1SFande Kong     }
3803273d9f13SBarry Smith     if (!nnz) {
3804435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3805c62bd62aSJed Brown       else if (nz < 0) nz = 1;
38065d2a9ed1SStefano Zampini       nz = PetscMin(nz,B->cmap->n);
3807d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3808d0f46423SBarry Smith       nz = nz*B->rmap->n;
3809273d9f13SBarry Smith     } else {
3810273d9f13SBarry Smith       nz = 0;
3811d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3812273d9f13SBarry Smith     }
3813ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
38142205254eSKarl Rupp     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;
3815ab93d7beSBarry Smith 
3816273d9f13SBarry Smith     /* allocate the matrix space */
381753dd7562SDmitry Karpeev     /* FIXME: should B's old memory be unlogged? */
38182ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3819396832f4SHong Zhang     if (B->structure_only) {
38205848002fSHong Zhang       ierr = PetscMalloc1(nz,&b->j);CHKERRQ(ierr);
38215848002fSHong Zhang       ierr = PetscMalloc1(B->rmap->n+1,&b->i);CHKERRQ(ierr);
3822396832f4SHong Zhang       ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));CHKERRQ(ierr);
3823396832f4SHong Zhang     } else {
3824dcca6d9dSJed Brown       ierr = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr);
38253bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3826396832f4SHong Zhang     }
3827bfeeae90SHong Zhang     b->i[0] = 0;
3828d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
38295da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
38305da197adSKris Buschelman     }
3831396832f4SHong Zhang     if (B->structure_only) {
3832396832f4SHong Zhang       b->singlemalloc = PETSC_FALSE;
3833396832f4SHong Zhang       b->free_a       = PETSC_FALSE;
3834396832f4SHong Zhang     } else {
3835273d9f13SBarry Smith       b->singlemalloc = PETSC_TRUE;
3836e6b907acSBarry Smith       b->free_a       = PETSC_TRUE;
3837396832f4SHong Zhang     }
3838e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3839c461c341SBarry Smith   } else {
3840e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
3841e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
3842c461c341SBarry Smith   }
3843273d9f13SBarry Smith 
3844846b4da1SFande Kong   if (b->ipre && nnz != b->ipre  && b->imax) {
3845846b4da1SFande Kong     /* reserve user-requested sparsity */
3846846b4da1SFande Kong     ierr = PetscMemcpy(b->ipre,b->imax,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3847846b4da1SFande Kong   }
3848846b4da1SFande Kong 
3849846b4da1SFande Kong 
3850273d9f13SBarry Smith   b->nz               = 0;
3851273d9f13SBarry Smith   b->maxnz            = nz;
3852273d9f13SBarry Smith   B->info.nz_unneeded = (double)b->maxnz;
38532205254eSKarl Rupp   if (realalloc) {
38542205254eSKarl Rupp     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
38552205254eSKarl Rupp   }
3856cb7b82ddSBarry Smith   B->was_assembled = PETSC_FALSE;
3857cb7b82ddSBarry Smith   B->assembled     = PETSC_FALSE;
3858273d9f13SBarry Smith   PetscFunctionReturn(0);
3859273d9f13SBarry Smith }
3860273d9f13SBarry Smith 
3861846b4da1SFande Kong 
3862846b4da1SFande Kong PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
3863846b4da1SFande Kong {
3864846b4da1SFande Kong   Mat_SeqAIJ     *a;
3865a5bbaf83SFande Kong   PetscInt       i;
3866846b4da1SFande Kong   PetscErrorCode ierr;
3867846b4da1SFande Kong 
3868846b4da1SFande Kong   PetscFunctionBegin;
3869846b4da1SFande Kong   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3870846b4da1SFande Kong   a = (Mat_SeqAIJ*)A->data;
38712c814fdeSFande Kong   /* if no saved info, we error out */
38722c814fdeSFande Kong   if (!a->ipre) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_NULL,"No saved preallocation info \n");
38732c814fdeSFande Kong 
38742c814fdeSFande 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");
38752c814fdeSFande Kong 
3876846b4da1SFande Kong   ierr = PetscMemcpy(a->imax,a->ipre,A->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3877846b4da1SFande Kong   ierr = PetscMemzero(a->ilen,A->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3878846b4da1SFande Kong   a->i[0] = 0;
3879846b4da1SFande Kong   for (i=1; i<A->rmap->n+1; i++) {
3880846b4da1SFande Kong     a->i[i] = a->i[i-1] + a->imax[i-1];
3881846b4da1SFande Kong   }
3882846b4da1SFande Kong   A->preallocated     = PETSC_TRUE;
3883846b4da1SFande Kong   a->nz               = 0;
3884846b4da1SFande Kong   a->maxnz            = a->i[A->rmap->n];
3885846b4da1SFande Kong   A->info.nz_unneeded = (double)a->maxnz;
3886846b4da1SFande Kong   A->was_assembled    = PETSC_FALSE;
3887846b4da1SFande Kong   A->assembled        = PETSC_FALSE;
3888846b4da1SFande Kong   PetscFunctionReturn(0);
3889846b4da1SFande Kong }
3890846b4da1SFande Kong 
389158d36128SBarry Smith /*@
3892a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3893a1661176SMatthew Knepley 
3894a1661176SMatthew Knepley    Input Parameters:
3895a1661176SMatthew Knepley +  B - the matrix
3896a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3897a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3898a1661176SMatthew Knepley -  v - optional values in the matrix
3899a1661176SMatthew Knepley 
3900a1661176SMatthew Knepley    Level: developer
3901a1661176SMatthew Knepley 
390258d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
390358d36128SBarry Smith 
3904c1c1d628SHong Zhang .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ
3905a1661176SMatthew Knepley @*/
3906a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3907a1661176SMatthew Knepley {
3908a1661176SMatthew Knepley   PetscErrorCode ierr;
3909a1661176SMatthew Knepley 
3910a1661176SMatthew Knepley   PetscFunctionBegin;
39110700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
39126ba663aaSJed Brown   PetscValidType(B,1);
39134ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3914a1661176SMatthew Knepley   PetscFunctionReturn(0);
3915a1661176SMatthew Knepley }
3916a1661176SMatthew Knepley 
39177087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3918a1661176SMatthew Knepley {
3919a1661176SMatthew Knepley   PetscInt       i;
3920a1661176SMatthew Knepley   PetscInt       m,n;
3921a1661176SMatthew Knepley   PetscInt       nz;
3922a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3923a1661176SMatthew Knepley   PetscScalar    *values;
3924a1661176SMatthew Knepley   PetscErrorCode ierr;
3925a1661176SMatthew Knepley 
3926a1661176SMatthew Knepley   PetscFunctionBegin;
392765e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3928779a8d59SSatish Balay 
3929779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3930779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3931779a8d59SSatish Balay 
3932779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3933854ce69bSBarry Smith   ierr = PetscMalloc1(m+1, &nnz);CHKERRQ(ierr);
3934a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3935b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3936a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
393765e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3938a1661176SMatthew Knepley     nnz[i] = nz;
3939a1661176SMatthew Knepley   }
3940a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3941a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3942a1661176SMatthew Knepley 
3943a1661176SMatthew Knepley   if (v) {
3944a1661176SMatthew Knepley     values = (PetscScalar*) v;
3945a1661176SMatthew Knepley   } else {
39461795a4d1SJed Brown     ierr = PetscCalloc1(nz_max, &values);CHKERRQ(ierr);
3947a1661176SMatthew Knepley   }
3948a1661176SMatthew Knepley 
3949a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
3950b7940d39SSatish Balay     nz   = Ii[i+1] - Ii[i];
3951b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3952a1661176SMatthew Knepley   }
3953a1661176SMatthew Knepley 
3954a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3955a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3956a1661176SMatthew Knepley 
3957a1661176SMatthew Knepley   if (!v) {
3958a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3959a1661176SMatthew Knepley   }
39607827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3961a1661176SMatthew Knepley   PetscFunctionReturn(0);
3962a1661176SMatthew Knepley }
3963a1661176SMatthew Knepley 
3964c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3965af0996ceSBarry Smith #include <petsc/private/kernels/petscaxpy.h>
3966170fe5c8SBarry Smith 
3967170fe5c8SBarry Smith /*
3968170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3969170fe5c8SBarry Smith 
3970170fe5c8SBarry Smith                n                       p                          p
3971170fe5c8SBarry Smith         (              )       (              )         (                  )
3972170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3973170fe5c8SBarry Smith         (              )       (              )         (                  )
3974170fe5c8SBarry Smith 
3975170fe5c8SBarry Smith */
3976170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3977170fe5c8SBarry Smith {
3978170fe5c8SBarry Smith   PetscErrorCode    ierr;
3979170fe5c8SBarry Smith   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3980170fe5c8SBarry Smith   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3981170fe5c8SBarry Smith   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
39821de00fd4SBarry Smith   PetscInt          i,n,m,q,p;
3983170fe5c8SBarry Smith   const PetscInt    *ii,*idx;
3984170fe5c8SBarry Smith   const PetscScalar *b,*a,*a_q;
3985170fe5c8SBarry Smith   PetscScalar       *c,*c_q;
3986170fe5c8SBarry Smith 
3987170fe5c8SBarry Smith   PetscFunctionBegin;
3988d0f46423SBarry Smith   m    = A->rmap->n;
3989d0f46423SBarry Smith   n    = A->cmap->n;
3990d0f46423SBarry Smith   p    = B->cmap->n;
3991170fe5c8SBarry Smith   a    = sub_a->v;
3992170fe5c8SBarry Smith   b    = sub_b->a;
3993170fe5c8SBarry Smith   c    = sub_c->v;
3994170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3995170fe5c8SBarry Smith 
3996170fe5c8SBarry Smith   ii  = sub_b->i;
3997170fe5c8SBarry Smith   idx = sub_b->j;
3998170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3999170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
4000170fe5c8SBarry Smith     while (q-->0) {
4001170fe5c8SBarry Smith       c_q = c + m*(*idx);
4002170fe5c8SBarry Smith       a_q = a + m*i;
4003854c7f52SBarry Smith       PetscKernelAXPY(c_q,*b,a_q,m);
4004170fe5c8SBarry Smith       idx++;
4005170fe5c8SBarry Smith       b++;
4006170fe5c8SBarry Smith     }
4007170fe5c8SBarry Smith   }
4008170fe5c8SBarry Smith   PetscFunctionReturn(0);
4009170fe5c8SBarry Smith }
4010170fe5c8SBarry Smith 
4011170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
4012170fe5c8SBarry Smith {
4013170fe5c8SBarry Smith   PetscErrorCode ierr;
4014d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
4015170fe5c8SBarry Smith   Mat            Cmat;
4016170fe5c8SBarry Smith 
4017170fe5c8SBarry Smith   PetscFunctionBegin;
401860e0710aSBarry 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);
4019ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
4020170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
402133d57670SJed Brown   ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr);
4022170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
40230298fd71SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
4024d73949e8SHong Zhang 
4025d73949e8SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
40262205254eSKarl Rupp 
4027170fe5c8SBarry Smith   *C = Cmat;
4028170fe5c8SBarry Smith   PetscFunctionReturn(0);
4029170fe5c8SBarry Smith }
4030170fe5c8SBarry Smith 
4031170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
4032150d2497SBarry Smith PETSC_INTERN PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
4033170fe5c8SBarry Smith {
4034170fe5c8SBarry Smith   PetscErrorCode ierr;
4035170fe5c8SBarry Smith 
4036170fe5c8SBarry Smith   PetscFunctionBegin;
4037170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
40383ff4c91cSHong Zhang     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
4039170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
40403ff4c91cSHong Zhang     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
4041170fe5c8SBarry Smith   }
40423ff4c91cSHong Zhang   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
4043170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
40443ff4c91cSHong Zhang   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
4045170fe5c8SBarry Smith   PetscFunctionReturn(0);
4046170fe5c8SBarry Smith }
4047170fe5c8SBarry Smith 
4048170fe5c8SBarry Smith 
40490bad9183SKris Buschelman /*MC
4050fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
40510bad9183SKris Buschelman    based on compressed sparse row format.
40520bad9183SKris Buschelman 
40530bad9183SKris Buschelman    Options Database Keys:
40540bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
40550bad9183SKris Buschelman 
40560bad9183SKris Buschelman   Level: beginner
40570bad9183SKris Buschelman 
4058f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
40590bad9183SKris Buschelman M*/
40600bad9183SKris Buschelman 
4061ccd284c7SBarry Smith /*MC
4062ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
4063ccd284c7SBarry Smith 
4064ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
4065ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
4066ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
4067ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
4068ccd284c7SBarry Smith   the above preallocation routines for simplicity.
4069ccd284c7SBarry Smith 
4070ccd284c7SBarry Smith    Options Database Keys:
4071ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
4072ccd284c7SBarry Smith 
407395452b02SPatrick Sanan   Developer Notes:
4074ca9cdca7SRichard Tran Mills     Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
4075ccd284c7SBarry Smith    enough exist.
4076ccd284c7SBarry Smith 
4077ccd284c7SBarry Smith   Level: beginner
4078ccd284c7SBarry Smith 
4079ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
4080ccd284c7SBarry Smith M*/
4081ccd284c7SBarry Smith 
4082ccd284c7SBarry Smith /*MC
4083ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
4084ccd284c7SBarry Smith 
4085ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
4086ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
4087ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
4088ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
4089ccd284c7SBarry Smith   the above preallocation routines for simplicity.
4090ccd284c7SBarry Smith 
4091ccd284c7SBarry Smith    Options Database Keys:
4092ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
4093ccd284c7SBarry Smith 
4094ccd284c7SBarry Smith   Level: beginner
4095ccd284c7SBarry Smith 
4096ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4097ccd284c7SBarry Smith M*/
4098ccd284c7SBarry Smith 
40997906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
41007906f579SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
41017906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
41027906f579SHong Zhang #endif
41037906f579SHong Zhang #if defined(PETSC_HAVE_HYPRE)
41047906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*);
41057906f579SHong Zhang PETSC_INTERN PetscErrorCode MatMatMatMult_Transpose_AIJ_AIJ(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
41067906f579SHong Zhang #endif
41077906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);
41087906f579SHong Zhang 
4109d4002b98SHong Zhang PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*);
4110c9225affSStefano Zampini PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
411175d48cdbSStefano Zampini PETSC_INTERN PetscErrorCode MatPtAP_IS_XAIJ(Mat,Mat,MatReuse,PetscReal,Mat*);
41127906f579SHong Zhang 
41138c778c55SBarry Smith /*@C
41148397e458SBarry Smith    MatSeqAIJGetArray - gives access to the array where the data for a MATSEQAIJ matrix is stored
41158c778c55SBarry Smith 
41168c778c55SBarry Smith    Not Collective
41178c778c55SBarry Smith 
41188c778c55SBarry Smith    Input Parameter:
4119579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
41208c778c55SBarry Smith 
41218c778c55SBarry Smith    Output Parameter:
41228c778c55SBarry Smith .   array - pointer to the data
41238c778c55SBarry Smith 
41248c778c55SBarry Smith    Level: intermediate
41258c778c55SBarry Smith 
4126774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
41278c778c55SBarry Smith @*/
41288c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
41298c778c55SBarry Smith {
41308c778c55SBarry Smith   PetscErrorCode ierr;
41318c778c55SBarry Smith 
41328c778c55SBarry Smith   PetscFunctionBegin;
41338c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
41348c778c55SBarry Smith   PetscFunctionReturn(0);
41358c778c55SBarry Smith }
41368c778c55SBarry Smith 
413721e72a00SBarry Smith /*@C
413821e72a00SBarry Smith    MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
413921e72a00SBarry Smith 
414021e72a00SBarry Smith    Not Collective
414121e72a00SBarry Smith 
414221e72a00SBarry Smith    Input Parameter:
4143579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
414421e72a00SBarry Smith 
414521e72a00SBarry Smith    Output Parameter:
414621e72a00SBarry Smith .   nz - the maximum number of nonzeros in any row
414721e72a00SBarry Smith 
414821e72a00SBarry Smith    Level: intermediate
414921e72a00SBarry Smith 
415021e72a00SBarry Smith .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
415121e72a00SBarry Smith @*/
415221e72a00SBarry Smith PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
415321e72a00SBarry Smith {
415421e72a00SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
415521e72a00SBarry Smith 
415621e72a00SBarry Smith   PetscFunctionBegin;
415721e72a00SBarry Smith   *nz = aij->rmax;
415821e72a00SBarry Smith   PetscFunctionReturn(0);
415921e72a00SBarry Smith }
416021e72a00SBarry Smith 
41618c778c55SBarry Smith /*@C
4162579dbff0SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray()
41638c778c55SBarry Smith 
41648c778c55SBarry Smith    Not Collective
41658c778c55SBarry Smith 
41668c778c55SBarry Smith    Input Parameters:
4167579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
41688c778c55SBarry Smith .  array - pointer to the data
41698c778c55SBarry Smith 
41708c778c55SBarry Smith    Level: intermediate
41718c778c55SBarry Smith 
4172774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
41738c778c55SBarry Smith @*/
41748c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
41758c778c55SBarry Smith {
41768c778c55SBarry Smith   PetscErrorCode ierr;
41778c778c55SBarry Smith 
41788c778c55SBarry Smith   PetscFunctionBegin;
41798c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
41808c778c55SBarry Smith   PetscFunctionReturn(0);
41818c778c55SBarry Smith }
41828c778c55SBarry Smith 
418334b5b067SBarry Smith #if defined(PETSC_HAVE_CUDA)
418402fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat);
418502fe1965SBarry Smith #endif
418602fe1965SBarry Smith 
41878cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4188273d9f13SBarry Smith {
4189273d9f13SBarry Smith   Mat_SeqAIJ     *b;
4190dfbe8321SBarry Smith   PetscErrorCode ierr;
419138baddfdSBarry Smith   PetscMPIInt    size;
4192273d9f13SBarry Smith 
4193273d9f13SBarry Smith   PetscFunctionBegin;
4194ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
4195e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
4196273d9f13SBarry Smith 
4197b00a9115SJed Brown   ierr = PetscNewLog(B,&b);CHKERRQ(ierr);
41982205254eSKarl Rupp 
4199b0a32e0cSBarry Smith   B->data = (void*)b;
42002205254eSKarl Rupp 
4201549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
42022205254eSKarl Rupp 
4203416022c9SBarry Smith   b->row                = 0;
4204416022c9SBarry Smith   b->col                = 0;
420582bf6240SBarry Smith   b->icol               = 0;
4206b810aeb4SBarry Smith   b->reallocs           = 0;
420736db0b34SBarry Smith   b->ignorezeroentries  = PETSC_FALSE;
4208f1e2ffcdSBarry Smith   b->roworiented        = PETSC_TRUE;
4209416022c9SBarry Smith   b->nonew              = 0;
4210416022c9SBarry Smith   b->diag               = 0;
4211416022c9SBarry Smith   b->solve_work         = 0;
42122a1b7f2aSHong Zhang   B->spptr              = 0;
4213be6bf707SBarry Smith   b->saved_values       = 0;
4214d7f994e1SBarry Smith   b->idiag              = 0;
421571f1c65dSBarry Smith   b->mdiag              = 0;
421671f1c65dSBarry Smith   b->ssor_work          = 0;
421771f1c65dSBarry Smith   b->omega              = 1.0;
421871f1c65dSBarry Smith   b->fshift             = 0.0;
421971f1c65dSBarry Smith   b->idiagvalid         = PETSC_FALSE;
4220bbead8a2SBarry Smith   b->ibdiagvalid        = PETSC_FALSE;
4221a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
422217ab2063SBarry Smith 
422335d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
4224bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
4225bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
42268c778c55SBarry Smith 
4227b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4228bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
4229bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
4230b3866ffcSBarry Smith #endif
423117f1a0eaSHong Zhang 
4232bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
4233bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
4234bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
4235bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
4236bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
4237bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
42384dfdc2d9SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijsell_C",MatConvert_SeqAIJ_SeqAIJSELL);CHKERRQ(ierr);
42399779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE)
42404a2a386eSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr);
4241191b95cbSRichard Tran Mills #endif
424234b5b067SBarry Smith #if defined(PETSC_HAVE_CUDA)
424302fe1965SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcusparse_C",MatConvert_SeqAIJ_SeqAIJCUSPARSE);CHKERRQ(ierr);
424402fe1965SBarry Smith #endif
4245bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
4246af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
4247af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);CHKERRQ(ierr);
4248af8000cdSHong Zhang #endif
424963c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE)
425063c07aadSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);CHKERRQ(ierr);
42513dad0653Sstefano_zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMatMult_transpose_seqaij_seqaij_C",MatMatMatMult_Transpose_AIJ_AIJ);CHKERRQ(ierr);
425263c07aadSStefano Zampini #endif
4253b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);CHKERRQ(ierr);
4254d4002b98SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);CHKERRQ(ierr);
4255c9225affSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_is_C",MatConvert_XAIJ_IS);CHKERRQ(ierr);
4256bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4257bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4258bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
4259846b4da1SFande Kong   ierr = PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);CHKERRQ(ierr);
4260bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
4261bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
4262bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
4263bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
4264bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
426575d48cdbSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_seqaij_C",MatPtAP_IS_XAIJ);CHKERRQ(ierr);
42664108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
426717667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
42684099cc6bSBarry Smith   ierr = MatSeqAIJSetTypeFromOptions(B);CHKERRQ(ierr);  /* this allows changing the matrix subtype to say MATSEQAIJPERM */
42693a40ed3dSBarry Smith   PetscFunctionReturn(0);
427017ab2063SBarry Smith }
427117ab2063SBarry Smith 
4272b24902e0SBarry Smith /*
4273b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4274b24902e0SBarry Smith */
4275ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
427617ab2063SBarry Smith {
4277416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
42786849ba73SBarry Smith   PetscErrorCode ierr;
4279d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
428017ab2063SBarry Smith 
42813a40ed3dSBarry Smith   PetscFunctionBegin;
4282273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4283273d9f13SBarry Smith 
4284d5f3da31SBarry Smith   C->factortype = A->factortype;
4285416022c9SBarry Smith   c->row        = 0;
4286416022c9SBarry Smith   c->col        = 0;
428782bf6240SBarry Smith   c->icol       = 0;
42886ad4291fSHong Zhang   c->reallocs   = 0;
428917ab2063SBarry Smith 
42906ad4291fSHong Zhang   C->assembled = PETSC_TRUE;
429117ab2063SBarry Smith 
4292aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4293aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4294eec197d1SBarry Smith 
4295dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&c->imax,m,&c->ilen);CHKERRQ(ierr);
42963bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
429717ab2063SBarry Smith   for (i=0; i<m; i++) {
4298416022c9SBarry Smith     c->imax[i] = a->imax[i];
4299416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
430017ab2063SBarry Smith   }
430117ab2063SBarry Smith 
430217ab2063SBarry Smith   /* allocate the matrix space */
4303f77e22a1SHong Zhang   if (mallocmatspace) {
4304dcca6d9dSJed Brown     ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr);
43053bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
43062205254eSKarl Rupp 
4307f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
43082205254eSKarl Rupp 
430997f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
431017ab2063SBarry Smith     if (m > 0) {
431197f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4312be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4313bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4314be6bf707SBarry Smith       } else {
4315bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
431617ab2063SBarry Smith       }
431708480c60SBarry Smith     }
4318f77e22a1SHong Zhang   }
431917ab2063SBarry Smith 
43206ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4321416022c9SBarry Smith   c->roworiented       = a->roworiented;
4322416022c9SBarry Smith   c->nonew             = a->nonew;
4323416022c9SBarry Smith   if (a->diag) {
4324854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&c->diag);CHKERRQ(ierr);
43253bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
432617ab2063SBarry Smith     for (i=0; i<m; i++) {
4327416022c9SBarry Smith       c->diag[i] = a->diag[i];
432817ab2063SBarry Smith     }
43293a40ed3dSBarry Smith   } else c->diag = 0;
43302205254eSKarl Rupp 
43316ad4291fSHong Zhang   c->solve_work         = 0;
43326ad4291fSHong Zhang   c->saved_values       = 0;
43336ad4291fSHong Zhang   c->idiag              = 0;
433471f1c65dSBarry Smith   c->ssor_work          = 0;
4335a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
4336e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
4337e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
43386ad4291fSHong Zhang 
4339893ad86cSHong Zhang   c->rmax         = a->rmax;
4340416022c9SBarry Smith   c->nz           = a->nz;
43418ed568f8SMatthew G Knepley   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4342273d9f13SBarry Smith   C->preallocated = PETSC_TRUE;
4343754ec7b1SSatish Balay 
43446ad4291fSHong Zhang   c->compressedrow.use   = a->compressedrow.use;
43456ad4291fSHong Zhang   c->compressedrow.nrows = a->compressedrow.nrows;
4346cd6b891eSBarry Smith   if (a->compressedrow.use) {
43476ad4291fSHong Zhang     i    = a->compressedrow.nrows;
4348dcca6d9dSJed Brown     ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr);
43496ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
43506ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
435127ea64f8SHong Zhang   } else {
435227ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
43530298fd71SBarry Smith     c->compressedrow.i      = NULL;
43540298fd71SBarry Smith     c->compressedrow.rindex = NULL;
43556ad4291fSHong Zhang   }
4356ea632784SBarry Smith   c->nonzerorowcnt = a->nonzerorowcnt;
4357e56f5c9eSBarry Smith   C->nonzerostate  = A->nonzerostate;
43584846f1f5SKris Buschelman 
43592205254eSKarl Rupp   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4360140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
43613a40ed3dSBarry Smith   PetscFunctionReturn(0);
436217ab2063SBarry Smith }
436317ab2063SBarry Smith 
4364b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4365b24902e0SBarry Smith {
4366b24902e0SBarry Smith   PetscErrorCode ierr;
4367b24902e0SBarry Smith 
4368b24902e0SBarry Smith   PetscFunctionBegin;
4369ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
43704b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4371cfd3f464SBarry Smith   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
437233d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
4373cfd3f464SBarry Smith   }
4374a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4375f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4376b24902e0SBarry Smith   PetscFunctionReturn(0);
4377b24902e0SBarry Smith }
4378b24902e0SBarry Smith 
4379112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4380fbdbba38SShri Abhyankar {
438152f91c60SVaclav Hapla   PetscBool      isbinary, ishdf5;
438252f91c60SVaclav Hapla   PetscErrorCode ierr;
438352f91c60SVaclav Hapla 
438452f91c60SVaclav Hapla   PetscFunctionBegin;
438552f91c60SVaclav Hapla   PetscValidHeaderSpecific(newMat,MAT_CLASSID,1);
438652f91c60SVaclav Hapla   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4387c27b3999SVaclav Hapla   /* force binary viewer to load .info file if it has not yet done so */
4388c27b3999SVaclav Hapla   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
438952f91c60SVaclav Hapla   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
439052f91c60SVaclav Hapla   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
439152f91c60SVaclav Hapla   if (isbinary) {
439252f91c60SVaclav Hapla     ierr = MatLoad_SeqAIJ_Binary(newMat,viewer);CHKERRQ(ierr);
439352f91c60SVaclav Hapla   } else if (ishdf5) {
439452f91c60SVaclav Hapla #if defined(PETSC_HAVE_HDF5)
439552f91c60SVaclav Hapla     ierr = MatLoad_AIJ_HDF5(newMat,viewer);CHKERRQ(ierr);
439652f91c60SVaclav Hapla #else
439752f91c60SVaclav Hapla     SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
439852f91c60SVaclav Hapla #endif
439952f91c60SVaclav Hapla   } else {
440052f91c60SVaclav 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);
440152f91c60SVaclav Hapla   }
440252f91c60SVaclav Hapla   PetscFunctionReturn(0);
440352f91c60SVaclav Hapla }
440452f91c60SVaclav Hapla 
440552f91c60SVaclav Hapla PetscErrorCode MatLoad_SeqAIJ_Binary(Mat newMat, PetscViewer viewer)
440652f91c60SVaclav Hapla {
4407fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4408fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4409fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4410fbdbba38SShri Abhyankar   int            fd;
4411fbdbba38SShri Abhyankar   PetscMPIInt    size;
4412fbdbba38SShri Abhyankar   MPI_Comm       comm;
44133059b6faSBarry Smith   PetscInt       bs = newMat->rmap->bs;
4414fbdbba38SShri Abhyankar 
4415fbdbba38SShri Abhyankar   PetscFunctionBegin;
4416fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4417fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4418fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4419bbead8a2SBarry Smith 
44200298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
44210298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
4422bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
44233059b6faSBarry Smith   if (bs < 0) bs = 1;
44243059b6faSBarry Smith   ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);
4425bbead8a2SBarry Smith 
4426fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
44279860990eSLisandro Dalcin   ierr = PetscBinaryRead(fd,header,4,NULL,PETSC_INT);CHKERRQ(ierr);
4428fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4429fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4430fbdbba38SShri Abhyankar 
4431bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4432fbdbba38SShri Abhyankar 
4433fbdbba38SShri Abhyankar   /* read in row lengths */
4434785e854fSJed Brown   ierr = PetscMalloc1(M,&rowlengths);CHKERRQ(ierr);
44359860990eSLisandro Dalcin   ierr = PetscBinaryRead(fd,rowlengths,M,NULL,PETSC_INT);CHKERRQ(ierr);
4436fbdbba38SShri Abhyankar 
4437fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4438fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
443960e0710aSBarry 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);
4440fbdbba38SShri Abhyankar 
4441fbdbba38SShri Abhyankar   /* set global size if not set already*/
4442f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4443fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4444aabbc4fbSShri Abhyankar   } else {
44459d36ed5fSBarry Smith     /* if sizes and type are already set, check if the matrix  global sizes are correct */
4446fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
44474c5b953cSHong Zhang     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
44484c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
44494c5b953cSHong Zhang     }
445060e0710aSBarry 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);
4451aabbc4fbSShri Abhyankar   }
4452fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4453fbdbba38SShri Abhyankar   a    = (Mat_SeqAIJ*)newMat->data;
4454fbdbba38SShri Abhyankar 
44559860990eSLisandro Dalcin   ierr = PetscBinaryRead(fd,a->j,nz,NULL,PETSC_INT);CHKERRQ(ierr);
4456fbdbba38SShri Abhyankar 
4457fbdbba38SShri Abhyankar   /* read in nonzero values */
44589860990eSLisandro Dalcin   ierr = PetscBinaryRead(fd,a->a,nz,NULL,PETSC_SCALAR);CHKERRQ(ierr);
4459fbdbba38SShri Abhyankar 
4460fbdbba38SShri Abhyankar   /* set matrix "i" values */
4461fbdbba38SShri Abhyankar   a->i[0] = 0;
4462fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4463fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4464fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4465fbdbba38SShri Abhyankar   }
4466fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4467fbdbba38SShri Abhyankar 
4468fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4469fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4470fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4471fbdbba38SShri Abhyankar }
4472fbdbba38SShri Abhyankar 
4473ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
44747264ac53SSatish Balay {
44757264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4476dfbe8321SBarry Smith   PetscErrorCode ierr;
4477eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4478eeffb40dSHong Zhang   PetscInt k;
4479eeffb40dSHong Zhang #endif
44807264ac53SSatish Balay 
44813a40ed3dSBarry Smith   PetscFunctionBegin;
4482bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4483d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4484ca44d042SBarry Smith     *flg = PETSC_FALSE;
4485ca44d042SBarry Smith     PetscFunctionReturn(0);
4486bcd2baecSBarry Smith   }
44877264ac53SSatish Balay 
44887264ac53SSatish Balay   /* if the a->i are the same */
4489d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4490abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
44917264ac53SSatish Balay 
44927264ac53SSatish Balay   /* if a->j are the same */
449397f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4494abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4495bcd2baecSBarry Smith 
4496bcd2baecSBarry Smith   /* if a->a are the same */
4497eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4498eeffb40dSHong Zhang   for (k=0; k<a->nz; k++) {
4499eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4500eeffb40dSHong Zhang       *flg = PETSC_FALSE;
45013a40ed3dSBarry Smith       PetscFunctionReturn(0);
4502eeffb40dSHong Zhang     }
4503eeffb40dSHong Zhang   }
4504eeffb40dSHong Zhang #else
4505eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4506eeffb40dSHong Zhang #endif
4507eeffb40dSHong Zhang   PetscFunctionReturn(0);
45087264ac53SSatish Balay }
450936db0b34SBarry Smith 
451005869f15SSatish Balay /*@
451136db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
451236db0b34SBarry Smith               provided by the user.
451336db0b34SBarry Smith 
4514d083f849SBarry Smith       Collective
451536db0b34SBarry Smith 
451636db0b34SBarry Smith    Input Parameters:
451736db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
451836db0b34SBarry Smith .   m - number of rows
451936db0b34SBarry Smith .   n - number of columns
4520483a2f95SBarry Smith .   i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
452136db0b34SBarry Smith .   j - column indices
452236db0b34SBarry Smith -   a - matrix values
452336db0b34SBarry Smith 
452436db0b34SBarry Smith    Output Parameter:
452536db0b34SBarry Smith .   mat - the matrix
452636db0b34SBarry Smith 
452736db0b34SBarry Smith    Level: intermediate
452836db0b34SBarry Smith 
452936db0b34SBarry Smith    Notes:
45300551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4531292fb18eSBarry Smith     once the matrix is destroyed and not before
453236db0b34SBarry Smith 
453336db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
453436db0b34SBarry Smith 
4535bfeeae90SHong Zhang        The i and j indices are 0 based
453636db0b34SBarry Smith 
4537a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4538a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
45398eef79e4SBarry Smith     as shown
4540a4552177SSatish Balay 
45418eef79e4SBarry Smith $        1 0 0
45428eef79e4SBarry Smith $        2 0 3
45438eef79e4SBarry Smith $        4 5 6
45448eef79e4SBarry Smith $
45458eef79e4SBarry Smith $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
45468eef79e4SBarry Smith $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
45478eef79e4SBarry Smith $        v =  {1,2,3,4,5,6}  [size = 6]
4548a4552177SSatish Balay 
45499985e31cSBarry Smith 
455069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
455136db0b34SBarry Smith 
455236db0b34SBarry Smith @*/
4553c3c607ccSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat)
455436db0b34SBarry Smith {
4555dfbe8321SBarry Smith   PetscErrorCode ierr;
4556cbcfb4deSHong Zhang   PetscInt       ii;
455736db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4558cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4559cbcfb4deSHong Zhang   PetscInt jj;
4560cbcfb4deSHong Zhang #endif
456136db0b34SBarry Smith 
456236db0b34SBarry Smith   PetscFunctionBegin;
456341096f02SStefano Zampini   if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4564f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4565f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4566a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4567ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4568ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4569ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4570dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&aij->imax,m,&aij->ilen);CHKERRQ(ierr);
4571ab93d7beSBarry Smith 
457236db0b34SBarry Smith   aij->i            = i;
457336db0b34SBarry Smith   aij->j            = j;
457436db0b34SBarry Smith   aij->a            = a;
457536db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
457636db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4577e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4578e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
457936db0b34SBarry Smith 
458036db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
458136db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
45822515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
458360e0710aSBarry 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]);
45849985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4585a061629eSStefano 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);
4586a061629eSStefano 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);
45879985e31cSBarry Smith     }
458836db0b34SBarry Smith #endif
458936db0b34SBarry Smith   }
45902515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
459136db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
459260e0710aSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
459360e0710aSBarry 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]);
459436db0b34SBarry Smith   }
459536db0b34SBarry Smith #endif
459636db0b34SBarry Smith 
4597b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4598b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
459936db0b34SBarry Smith   PetscFunctionReturn(0);
460036db0b34SBarry Smith }
460180ef6e79SMatthew G Knepley /*@C
4602d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
46038a0b0e6bSVictor Minden               provided by the user.
46048a0b0e6bSVictor Minden 
4605d083f849SBarry Smith       Collective
46068a0b0e6bSVictor Minden 
46078a0b0e6bSVictor Minden    Input Parameters:
46088a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
46098a0b0e6bSVictor Minden .   m   - number of rows
46108a0b0e6bSVictor Minden .   n   - number of columns
46118a0b0e6bSVictor Minden .   i   - row indices
46128a0b0e6bSVictor Minden .   j   - column indices
46131230e6d1SVictor Minden .   a   - matrix values
46141230e6d1SVictor Minden .   nz  - number of nonzeros
46151230e6d1SVictor Minden -   idx - 0 or 1 based
46168a0b0e6bSVictor Minden 
46178a0b0e6bSVictor Minden    Output Parameter:
46188a0b0e6bSVictor Minden .   mat - the matrix
46198a0b0e6bSVictor Minden 
46208a0b0e6bSVictor Minden    Level: intermediate
46218a0b0e6bSVictor Minden 
46228a0b0e6bSVictor Minden    Notes:
46238a0b0e6bSVictor Minden        The i and j indices are 0 based
46248a0b0e6bSVictor Minden 
46258a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
46268a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
46278a0b0e6bSVictor Minden     as shown:
46288a0b0e6bSVictor Minden 
46298a0b0e6bSVictor Minden         1 0 0
46308a0b0e6bSVictor Minden         2 0 3
46318a0b0e6bSVictor Minden         4 5 6
46328a0b0e6bSVictor Minden 
46338a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
46348a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
46358a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
46368a0b0e6bSVictor Minden 
46378a0b0e6bSVictor Minden 
463869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
46398a0b0e6bSVictor Minden 
46408a0b0e6bSVictor Minden @*/
4641c3c607ccSBarry Smith PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat,PetscInt nz,PetscBool idx)
46428a0b0e6bSVictor Minden {
46438a0b0e6bSVictor Minden   PetscErrorCode ierr;
4644d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
46458a0b0e6bSVictor Minden 
46468a0b0e6bSVictor Minden 
46478a0b0e6bSVictor Minden   PetscFunctionBegin;
46481795a4d1SJed Brown   ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr);
46491230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
4650c8d679ebSHong Zhang     nnz[i[ii] - !!idx] += 1;
46511230e6d1SVictor Minden   }
46528a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
46538a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
46548a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
46551230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
46561230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
46571230e6d1SVictor Minden     if (idx) {
46581230e6d1SVictor Minden       row = i[ii] - 1;
46591230e6d1SVictor Minden       col = j[ii] - 1;
46601230e6d1SVictor Minden     } else {
46611230e6d1SVictor Minden       row = i[ii];
46621230e6d1SVictor Minden       col = j[ii];
46638a0b0e6bSVictor Minden     }
46641230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
46658a0b0e6bSVictor Minden   }
46668a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
46678a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4668d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
46698a0b0e6bSVictor Minden   PetscFunctionReturn(0);
46708a0b0e6bSVictor Minden }
467136db0b34SBarry Smith 
4672acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4673acf2f550SJed Brown {
4674acf2f550SJed Brown   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4675acf2f550SJed Brown   PetscErrorCode ierr;
4676acf2f550SJed Brown 
4677acf2f550SJed Brown   PetscFunctionBegin;
4678acf2f550SJed Brown   a->idiagvalid  = PETSC_FALSE;
4679acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
46802205254eSKarl Rupp 
4681acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4682acf2f550SJed Brown   PetscFunctionReturn(0);
4683acf2f550SJed Brown }
4684acf2f550SJed Brown 
46859c8f2541SHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
46869c8f2541SHong Zhang {
46879c8f2541SHong Zhang   PetscErrorCode ierr;
46888761c3d6SHong Zhang   PetscMPIInt    size;
46899c8f2541SHong Zhang 
46909c8f2541SHong Zhang   PetscFunctionBegin;
46918761c3d6SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
46927bbdc51dSHong Zhang   if (size == 1) {
46937bbdc51dSHong Zhang     if (scall == MAT_INITIAL_MATRIX) {
46947bbdc51dSHong Zhang       ierr = MatDuplicate(inmat,MAT_COPY_VALUES,outmat);CHKERRQ(ierr);
46957bbdc51dSHong Zhang     } else {
46968761c3d6SHong Zhang       ierr = MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
46977bbdc51dSHong Zhang     }
46988761c3d6SHong Zhang   } else {
46999c8f2541SHong Zhang     ierr = MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr);
47008761c3d6SHong Zhang   }
47019c8f2541SHong Zhang   PetscFunctionReturn(0);
47029c8f2541SHong Zhang }
47039c8f2541SHong Zhang 
470481824310SBarry Smith /*
470553dd7562SDmitry Karpeev  Permute A into C's *local* index space using rowemb,colemb.
470653dd7562SDmitry Karpeev  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
470753dd7562SDmitry Karpeev  of [0,m), colemb is in [0,n).
470853dd7562SDmitry Karpeev  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
470953dd7562SDmitry Karpeev  */
471053dd7562SDmitry Karpeev PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
471153dd7562SDmitry Karpeev {
471253dd7562SDmitry Karpeev   /* If making this function public, change the error returned in this function away from _PLIB. */
471353dd7562SDmitry Karpeev   PetscErrorCode ierr;
471453dd7562SDmitry Karpeev   Mat_SeqAIJ     *Baij;
471553dd7562SDmitry Karpeev   PetscBool      seqaij;
471653dd7562SDmitry Karpeev   PetscInt       m,n,*nz,i,j,count;
471753dd7562SDmitry Karpeev   PetscScalar    v;
471853dd7562SDmitry Karpeev   const PetscInt *rowindices,*colindices;
471953dd7562SDmitry Karpeev 
472053dd7562SDmitry Karpeev   PetscFunctionBegin;
472153dd7562SDmitry Karpeev   if (!B) PetscFunctionReturn(0);
472253dd7562SDmitry Karpeev   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
47234099cc6bSBarry Smith   ierr = PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);CHKERRQ(ierr);
472453dd7562SDmitry Karpeev   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
472553dd7562SDmitry Karpeev   if (rowemb) {
472653dd7562SDmitry Karpeev     ierr = ISGetLocalSize(rowemb,&m);CHKERRQ(ierr);
472753dd7562SDmitry 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);
472853dd7562SDmitry Karpeev   } else {
47296c4ed002SBarry Smith     if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
473053dd7562SDmitry Karpeev   }
473153dd7562SDmitry Karpeev   if (colemb) {
473253dd7562SDmitry Karpeev     ierr = ISGetLocalSize(colemb,&n);CHKERRQ(ierr);
473353dd7562SDmitry 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);
473453dd7562SDmitry Karpeev   } else {
473553dd7562SDmitry Karpeev     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
473653dd7562SDmitry Karpeev   }
473753dd7562SDmitry Karpeev 
473853dd7562SDmitry Karpeev   Baij = (Mat_SeqAIJ*)(B->data);
473953dd7562SDmitry Karpeev   if (pattern == DIFFERENT_NONZERO_PATTERN) {
474053dd7562SDmitry Karpeev     ierr = PetscMalloc1(B->rmap->n,&nz);CHKERRQ(ierr);
474153dd7562SDmitry Karpeev     for (i=0; i<B->rmap->n; i++) {
474253dd7562SDmitry Karpeev       nz[i] = Baij->i[i+1] - Baij->i[i];
474353dd7562SDmitry Karpeev     }
474453dd7562SDmitry Karpeev     ierr = MatSeqAIJSetPreallocation(C,0,nz);CHKERRQ(ierr);
474553dd7562SDmitry Karpeev     ierr = PetscFree(nz);CHKERRQ(ierr);
474653dd7562SDmitry Karpeev   }
474753dd7562SDmitry Karpeev   if (pattern == SUBSET_NONZERO_PATTERN) {
474853dd7562SDmitry Karpeev     ierr = MatZeroEntries(C);CHKERRQ(ierr);
474953dd7562SDmitry Karpeev   }
475053dd7562SDmitry Karpeev   count = 0;
475153dd7562SDmitry Karpeev   rowindices = NULL;
475253dd7562SDmitry Karpeev   colindices = NULL;
475353dd7562SDmitry Karpeev   if (rowemb) {
475453dd7562SDmitry Karpeev     ierr = ISGetIndices(rowemb,&rowindices);CHKERRQ(ierr);
475553dd7562SDmitry Karpeev   }
475653dd7562SDmitry Karpeev   if (colemb) {
475753dd7562SDmitry Karpeev     ierr = ISGetIndices(colemb,&colindices);CHKERRQ(ierr);
475853dd7562SDmitry Karpeev   }
475953dd7562SDmitry Karpeev   for (i=0; i<B->rmap->n; i++) {
476053dd7562SDmitry Karpeev     PetscInt row;
476153dd7562SDmitry Karpeev     row = i;
476253dd7562SDmitry Karpeev     if (rowindices) row = rowindices[i];
476353dd7562SDmitry Karpeev     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
476453dd7562SDmitry Karpeev       PetscInt col;
476553dd7562SDmitry Karpeev       col  = Baij->j[count];
476653dd7562SDmitry Karpeev       if (colindices) col = colindices[col];
476753dd7562SDmitry Karpeev       v    = Baij->a[count];
476853dd7562SDmitry Karpeev       ierr = MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);CHKERRQ(ierr);
476953dd7562SDmitry Karpeev       ++count;
477053dd7562SDmitry Karpeev     }
477153dd7562SDmitry Karpeev   }
477253dd7562SDmitry Karpeev   /* FIXME: set C's nonzerostate correctly. */
477353dd7562SDmitry Karpeev   /* Assembly for C is necessary. */
477453dd7562SDmitry Karpeev   C->preallocated = PETSC_TRUE;
477553dd7562SDmitry Karpeev   C->assembled     = PETSC_TRUE;
477653dd7562SDmitry Karpeev   C->was_assembled = PETSC_FALSE;
477753dd7562SDmitry Karpeev   PetscFunctionReturn(0);
477853dd7562SDmitry Karpeev }
477953dd7562SDmitry Karpeev 
47804099cc6bSBarry Smith PetscFunctionList MatSeqAIJList = NULL;
47814099cc6bSBarry Smith 
47824099cc6bSBarry Smith /*@C
47834099cc6bSBarry Smith    MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype
47844099cc6bSBarry Smith 
47854099cc6bSBarry Smith    Collective on Mat
47864099cc6bSBarry Smith 
47874099cc6bSBarry Smith    Input Parameters:
47884099cc6bSBarry Smith +  mat      - the matrix object
47894099cc6bSBarry Smith -  matype   - matrix type
47904099cc6bSBarry Smith 
47914099cc6bSBarry Smith    Options Database Key:
47924099cc6bSBarry Smith .  -mat_seqai_type  <method> - for example seqaijcrl
47934099cc6bSBarry Smith 
47944099cc6bSBarry Smith 
47954099cc6bSBarry Smith   Level: intermediate
47964099cc6bSBarry Smith 
47974099cc6bSBarry Smith .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
47984099cc6bSBarry Smith @*/
47994099cc6bSBarry Smith PetscErrorCode  MatSeqAIJSetType(Mat mat, MatType matype)
48004099cc6bSBarry Smith {
4801fd9d3c67SJed Brown   PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*);
48024099cc6bSBarry Smith   PetscBool      sametype;
48034099cc6bSBarry Smith 
48044099cc6bSBarry Smith   PetscFunctionBegin;
48054099cc6bSBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
48064099cc6bSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
48074099cc6bSBarry Smith   if (sametype) PetscFunctionReturn(0);
48084099cc6bSBarry Smith 
48094099cc6bSBarry Smith   ierr =  PetscFunctionListFind(MatSeqAIJList,matype,&r);CHKERRQ(ierr);
48104099cc6bSBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
48114099cc6bSBarry Smith   ierr = (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);CHKERRQ(ierr);
48124099cc6bSBarry Smith   PetscFunctionReturn(0);
48134099cc6bSBarry Smith }
48144099cc6bSBarry Smith 
48154099cc6bSBarry Smith 
48164099cc6bSBarry Smith /*@C
48174099cc6bSBarry Smith   MatSeqAIJRegister -  - Adds a new sub-matrix type for sequential AIJ matrices
48184099cc6bSBarry Smith 
48194099cc6bSBarry Smith    Not Collective
48204099cc6bSBarry Smith 
48214099cc6bSBarry Smith    Input Parameters:
48224099cc6bSBarry Smith +  name - name of a new user-defined matrix type, for example MATSEQAIJCRL
48234099cc6bSBarry Smith -  function - routine to convert to subtype
48244099cc6bSBarry Smith 
48254099cc6bSBarry Smith    Notes:
48264099cc6bSBarry Smith    MatSeqAIJRegister() may be called multiple times to add several user-defined solvers.
48274099cc6bSBarry Smith 
48284099cc6bSBarry Smith 
48294099cc6bSBarry Smith    Then, your matrix can be chosen with the procedural interface at runtime via the option
48304099cc6bSBarry Smith $     -mat_seqaij_type my_mat
48314099cc6bSBarry Smith 
48324099cc6bSBarry Smith    Level: advanced
48334099cc6bSBarry Smith 
48344099cc6bSBarry Smith .seealso: MatSeqAIJRegisterAll()
48354099cc6bSBarry Smith 
48364099cc6bSBarry Smith 
48374099cc6bSBarry Smith   Level: advanced
48384099cc6bSBarry Smith @*/
4839388d47a6SSatish Balay PetscErrorCode  MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *))
48404099cc6bSBarry Smith {
48414099cc6bSBarry Smith   PetscErrorCode ierr;
48424099cc6bSBarry Smith 
48434099cc6bSBarry Smith   PetscFunctionBegin;
48449cc31a68SJed Brown   ierr = MatInitializePackage();CHKERRQ(ierr);
48454099cc6bSBarry Smith   ierr = PetscFunctionListAdd(&MatSeqAIJList,sname,function);CHKERRQ(ierr);
48464099cc6bSBarry Smith   PetscFunctionReturn(0);
48474099cc6bSBarry Smith }
48484099cc6bSBarry Smith 
48494099cc6bSBarry Smith PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;
48504099cc6bSBarry Smith 
48514099cc6bSBarry Smith /*@C
48524099cc6bSBarry Smith   MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ
48534099cc6bSBarry Smith 
48544099cc6bSBarry Smith   Not Collective
48554099cc6bSBarry Smith 
48564099cc6bSBarry Smith   Level: advanced
48574099cc6bSBarry Smith 
48584099cc6bSBarry Smith   Developers Note: CUSP and CUSPARSE do not yet support the  MatConvert_SeqAIJ..() paradigm and thus cannot be registered here
48594099cc6bSBarry Smith 
48604099cc6bSBarry Smith .seealso:  MatRegisterAll(), MatSeqAIJRegister()
48614099cc6bSBarry Smith @*/
48624099cc6bSBarry Smith PetscErrorCode  MatSeqAIJRegisterAll(void)
48634099cc6bSBarry Smith {
48644099cc6bSBarry Smith   PetscErrorCode ierr;
48654099cc6bSBarry Smith 
48664099cc6bSBarry Smith   PetscFunctionBegin;
48674099cc6bSBarry Smith   if (MatSeqAIJRegisterAllCalled) PetscFunctionReturn(0);
48684099cc6bSBarry Smith   MatSeqAIJRegisterAllCalled = PETSC_TRUE;
48694099cc6bSBarry Smith 
48704099cc6bSBarry Smith   ierr = MatSeqAIJRegister(MATSEQAIJCRL,      MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
48714099cc6bSBarry Smith   ierr = MatSeqAIJRegister(MATSEQAIJPERM,     MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
48724dfdc2d9SRichard Tran Mills   ierr = MatSeqAIJRegister(MATSEQAIJSELL,     MatConvert_SeqAIJ_SeqAIJSELL);CHKERRQ(ierr);
48739779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE)
48746b62b571SRichard Tran Mills   ierr = MatSeqAIJRegister(MATSEQAIJMKL,      MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr);
4875485f9817SRichard Tran Mills #endif
48764099cc6bSBarry Smith #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
48774099cc6bSBarry Smith   ierr = MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);CHKERRQ(ierr);
48784099cc6bSBarry Smith #endif
48794099cc6bSBarry Smith   PetscFunctionReturn(0);
48804099cc6bSBarry Smith }
488153dd7562SDmitry Karpeev 
488253dd7562SDmitry Karpeev /*
488381824310SBarry Smith     Special version for direct calls from Fortran
488481824310SBarry Smith */
4885af0996ceSBarry Smith #include <petsc/private/fortranimpl.h>
488681824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
488781824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
488881824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
488981824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
489081824310SBarry Smith #endif
489181824310SBarry Smith 
489281824310SBarry Smith /* Change these macros so can be used in void function */
489381824310SBarry Smith #undef CHKERRQ
4894ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
489581824310SBarry Smith #undef SETERRQ2
4896e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
48974994cf47SJed Brown #undef SETERRQ3
48984994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
489981824310SBarry Smith 
49008cc058d9SJed 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)
490181824310SBarry Smith {
490281824310SBarry Smith   Mat            A  = *AA;
490381824310SBarry Smith   PetscInt       m  = *mm, n = *nn;
490481824310SBarry Smith   InsertMode     is = *isis;
490581824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
490681824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
490781824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
490881824310SBarry Smith   PetscErrorCode ierr;
490981824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
491054f21887SBarry Smith   MatScalar      *ap,value,*aa;
4911ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4912ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
491381824310SBarry Smith 
491481824310SBarry Smith   PetscFunctionBegin;
49154994cf47SJed Brown   MatCheckPreallocated(A,1);
491681824310SBarry Smith   imax  = a->imax;
491781824310SBarry Smith   ai    = a->i;
491881824310SBarry Smith   ailen = a->ilen;
491981824310SBarry Smith   aj    = a->j;
492081824310SBarry Smith   aa    = a->a;
492181824310SBarry Smith 
492281824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
492381824310SBarry Smith     row = im[k];
492481824310SBarry Smith     if (row < 0) continue;
492581824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4926ce94432eSBarry Smith     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
492781824310SBarry Smith #endif
492881824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
492981824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
493081824310SBarry Smith     low  = 0;
493181824310SBarry Smith     high = nrow;
493281824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
493381824310SBarry Smith       if (in[l] < 0) continue;
493481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4935ce94432eSBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
493681824310SBarry Smith #endif
493781824310SBarry Smith       col = in[l];
49382205254eSKarl Rupp       if (roworiented) value = v[l + k*n];
49392205254eSKarl Rupp       else value = v[k + l*m];
49402205254eSKarl Rupp 
494181824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
494281824310SBarry Smith 
49432205254eSKarl Rupp       if (col <= lastcol) low = 0;
49442205254eSKarl Rupp       else high = nrow;
494581824310SBarry Smith       lastcol = col;
494681824310SBarry Smith       while (high-low > 5) {
494781824310SBarry Smith         t = (low+high)/2;
494881824310SBarry Smith         if (rp[t] > col) high = t;
494981824310SBarry Smith         else             low  = t;
495081824310SBarry Smith       }
495181824310SBarry Smith       for (i=low; i<high; i++) {
495281824310SBarry Smith         if (rp[i] > col) break;
495381824310SBarry Smith         if (rp[i] == col) {
495481824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
495581824310SBarry Smith           else                  ap[i] = value;
495681824310SBarry Smith           goto noinsert;
495781824310SBarry Smith         }
495881824310SBarry Smith       }
495981824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
496081824310SBarry Smith       if (nonew == 1) goto noinsert;
4961ce94432eSBarry Smith       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4962fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
496381824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
496481824310SBarry Smith       /* shift up all the later entries in this row */
496581824310SBarry Smith       for (ii=N; ii>=i; ii--) {
496681824310SBarry Smith         rp[ii+1] = rp[ii];
496781824310SBarry Smith         ap[ii+1] = ap[ii];
496881824310SBarry Smith       }
496981824310SBarry Smith       rp[i] = col;
497081824310SBarry Smith       ap[i] = value;
4971e56f5c9eSBarry Smith       A->nonzerostate++;
497281824310SBarry Smith noinsert:;
497381824310SBarry Smith       low = i + 1;
497481824310SBarry Smith     }
497581824310SBarry Smith     ailen[row] = nrow;
497681824310SBarry Smith   }
497781824310SBarry Smith   PetscFunctionReturnVoid();
497881824310SBarry Smith }
4979