xref: /petsc/src/mat/impls/aij/seq/aij.c (revision cf9c20a2d58da010f7c4712defbcdf61cc8f72b5)
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);
37580bdb30SBarry Smith   ierr = PetscArrayzero(norms,n);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;
177837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
178837a59e1SRichard Tran Mills   PetscBool         inserted = PETSC_FALSE;
179837a59e1SRichard Tran Mills #endif
18079299369SBarry Smith 
18179299369SBarry Smith   PetscFunctionBegin;
18209f38230SBarry Smith   if (Y->assembled) {
1830298fd71SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);CHKERRQ(ierr);
18409f38230SBarry Smith     if (!missing) {
18579299369SBarry Smith       diag = aij->diag;
18699e65526SBarry Smith       ierr = VecGetArrayRead(D,&v);CHKERRQ(ierr);
18779299369SBarry Smith       if (is == INSERT_VALUES) {
188837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
189837a59e1SRichard Tran Mills         inserted = PETSC_TRUE;
190837a59e1SRichard Tran Mills #endif
19179299369SBarry Smith         for (i=0; i<m; i++) {
19279299369SBarry Smith           aa[diag[i]] = v[i];
19379299369SBarry Smith         }
19479299369SBarry Smith       } else {
19579299369SBarry Smith         for (i=0; i<m; i++) {
196837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
197837a59e1SRichard Tran Mills           if (v[i] != 0.0) inserted = PETSC_TRUE;
198837a59e1SRichard Tran Mills #endif
19979299369SBarry Smith           aa[diag[i]] += v[i];
20079299369SBarry Smith         }
20179299369SBarry Smith       }
202837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
203837a59e1SRichard Tran Mills       if (inserted) Y->offloadmask = PETSC_OFFLOAD_CPU;
204837a59e1SRichard Tran Mills #endif
20599e65526SBarry Smith       ierr = VecRestoreArrayRead(D,&v);CHKERRQ(ierr);
20679299369SBarry Smith       PetscFunctionReturn(0);
20779299369SBarry Smith     }
208acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
20909f38230SBarry Smith   }
21009f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
21109f38230SBarry Smith   PetscFunctionReturn(0);
21209f38230SBarry Smith }
21379299369SBarry Smith 
2141a83f524SJed Brown PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
21517ab2063SBarry Smith {
216416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
217dfbe8321SBarry Smith   PetscErrorCode ierr;
21897f1f81fSBarry Smith   PetscInt       i,ishift;
21917ab2063SBarry Smith 
2203a40ed3dSBarry Smith   PetscFunctionBegin;
221d0f46423SBarry Smith   *m = A->rmap->n;
2223a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
223bfeeae90SHong Zhang   ishift = 0;
22453e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
2252462f5fdSStefano Zampini     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
226bfeeae90SHong Zhang   } else if (oshift == 1) {
2271a83f524SJed Brown     PetscInt *tia;
228d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
2293b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
230854ce69bSBarry Smith     ierr = PetscMalloc1(A->rmap->n+1,&tia);CHKERRQ(ierr);
2311a83f524SJed Brown     for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1;
2321a83f524SJed Brown     *ia = tia;
233ecc77c7aSBarry Smith     if (ja) {
2341a83f524SJed Brown       PetscInt *tja;
235854ce69bSBarry Smith       ierr = PetscMalloc1(nz+1,&tja);CHKERRQ(ierr);
2361a83f524SJed Brown       for (i=0; i<nz; i++) tja[i] = a->j[i] + 1;
2371a83f524SJed Brown       *ja = tja;
238ecc77c7aSBarry Smith     }
2396945ee14SBarry Smith   } else {
240ecc77c7aSBarry Smith     *ia = a->i;
241ecc77c7aSBarry Smith     if (ja) *ja = a->j;
242a2ce50c7SBarry Smith   }
2433a40ed3dSBarry Smith   PetscFunctionReturn(0);
244a2744918SBarry Smith }
245a2744918SBarry Smith 
2461a83f524SJed Brown PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2476945ee14SBarry Smith {
248dfbe8321SBarry Smith   PetscErrorCode ierr;
2496945ee14SBarry Smith 
2503a40ed3dSBarry Smith   PetscFunctionBegin;
2513a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
252bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
253606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
254ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
255bcd2baecSBarry Smith   }
2563a40ed3dSBarry Smith   PetscFunctionReturn(0);
25717ab2063SBarry Smith }
25817ab2063SBarry Smith 
2591a83f524SJed Brown PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
2603b2fbd54SBarry Smith {
2613b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
262dfbe8321SBarry Smith   PetscErrorCode ierr;
263d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
26497f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
2653b2fbd54SBarry Smith 
2663a40ed3dSBarry Smith   PetscFunctionBegin;
267899cda47SBarry Smith   *nn = n;
2683a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2693b2fbd54SBarry Smith   if (symmetric) {
2702462f5fdSStefano Zampini     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,0,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr);
2713b2fbd54SBarry Smith   } else {
272b9e7e5c1SBarry Smith     ierr = PetscCalloc1(n,&collengths);CHKERRQ(ierr);
273854ce69bSBarry Smith     ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr);
274b9e7e5c1SBarry Smith     ierr = PetscMalloc1(nz,&cja);CHKERRQ(ierr);
2753b2fbd54SBarry Smith     jj   = a->j;
2763b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
277bfeeae90SHong Zhang       collengths[jj[i]]++;
2783b2fbd54SBarry Smith     }
2793b2fbd54SBarry Smith     cia[0] = oshift;
2803b2fbd54SBarry Smith     for (i=0; i<n; i++) {
2813b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
2823b2fbd54SBarry Smith     }
283580bdb30SBarry Smith     ierr = PetscArrayzero(collengths,n);CHKERRQ(ierr);
2843b2fbd54SBarry Smith     jj   = a->j;
285a93ec695SBarry Smith     for (row=0; row<m; row++) {
286a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
287a93ec695SBarry Smith       for (i=0; i<mr; i++) {
288bfeeae90SHong Zhang         col = *jj++;
2892205254eSKarl Rupp 
2903b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2913b2fbd54SBarry Smith       }
2923b2fbd54SBarry Smith     }
293606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2943b2fbd54SBarry Smith     *ia  = cia; *ja = cja;
2953b2fbd54SBarry Smith   }
2963a40ed3dSBarry Smith   PetscFunctionReturn(0);
2973b2fbd54SBarry Smith }
2983b2fbd54SBarry Smith 
2991a83f524SJed Brown PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
3003b2fbd54SBarry Smith {
301dfbe8321SBarry Smith   PetscErrorCode ierr;
302606d414cSSatish Balay 
3033a40ed3dSBarry Smith   PetscFunctionBegin;
3043a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
3053b2fbd54SBarry Smith 
306606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
307606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
3083a40ed3dSBarry Smith   PetscFunctionReturn(0);
3093b2fbd54SBarry Smith }
3103b2fbd54SBarry Smith 
3117cee066cSHong Zhang /*
3127cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
3137cee066cSHong Zhang  MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
314040ebd07SHong Zhang  spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
3157cee066cSHong Zhang */
3167cee066cSHong 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)
3177cee066cSHong Zhang {
3187cee066cSHong Zhang   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3197cee066cSHong Zhang   PetscErrorCode ierr;
3207cee066cSHong Zhang   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
321071fcb05SBarry Smith   PetscInt       nz = a->i[m],row,mr,col,tmp;
3227cee066cSHong Zhang   PetscInt       *cspidx;
323071fcb05SBarry Smith   const PetscInt *jj;
3247cee066cSHong Zhang 
3257cee066cSHong Zhang   PetscFunctionBegin;
3267cee066cSHong Zhang   *nn = n;
3277cee066cSHong Zhang   if (!ia) PetscFunctionReturn(0);
328625f6d37SHong Zhang 
329b9e7e5c1SBarry Smith   ierr = PetscCalloc1(n,&collengths);CHKERRQ(ierr);
330854ce69bSBarry Smith   ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr);
331b9e7e5c1SBarry Smith   ierr = PetscMalloc1(nz,&cja);CHKERRQ(ierr);
332b9e7e5c1SBarry Smith   ierr = PetscMalloc1(nz,&cspidx);CHKERRQ(ierr);
3337cee066cSHong Zhang   jj   = a->j;
3347cee066cSHong Zhang   for (i=0; i<nz; i++) {
3357cee066cSHong Zhang     collengths[jj[i]]++;
3367cee066cSHong Zhang   }
3377cee066cSHong Zhang   cia[0] = oshift;
3387cee066cSHong Zhang   for (i=0; i<n; i++) {
3397cee066cSHong Zhang     cia[i+1] = cia[i] + collengths[i];
3407cee066cSHong Zhang   }
341580bdb30SBarry Smith   ierr = PetscArrayzero(collengths,n);CHKERRQ(ierr);
3427cee066cSHong Zhang   jj   = a->j;
3437cee066cSHong Zhang   for (row=0; row<m; row++) {
3447cee066cSHong Zhang     mr = a->i[row+1] - a->i[row];
3457cee066cSHong Zhang     for (i=0; i<mr; i++) {
3467cee066cSHong Zhang       col         = *jj++;
347071fcb05SBarry Smith       tmp         = cia[col] + collengths[col]++ - oshift;
348071fcb05SBarry Smith       cspidx[tmp] = a->i[row] + i; /* index of a->j */
349071fcb05SBarry Smith       cja[tmp]    = row + oshift;
3507cee066cSHong Zhang     }
3517cee066cSHong Zhang   }
3527cee066cSHong Zhang   ierr   = PetscFree(collengths);CHKERRQ(ierr);
353071fcb05SBarry Smith   *ia    = cia;
354071fcb05SBarry Smith   *ja    = cja;
3557cee066cSHong Zhang   *spidx = cspidx;
3567cee066cSHong Zhang   PetscFunctionReturn(0);
3577cee066cSHong Zhang }
3587cee066cSHong Zhang 
3597cee066cSHong 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)
3607cee066cSHong Zhang {
3617cee066cSHong Zhang   PetscErrorCode ierr;
3627cee066cSHong Zhang 
3637cee066cSHong Zhang   PetscFunctionBegin;
3645243ef75SHong Zhang   ierr = MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
3657cee066cSHong Zhang   ierr = PetscFree(*spidx);CHKERRQ(ierr);
3667cee066cSHong Zhang   PetscFunctionReturn(0);
3677cee066cSHong Zhang }
3687cee066cSHong Zhang 
36987d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
37087d4246cSBarry Smith {
37187d4246cSBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
37287d4246cSBarry Smith   PetscInt       *ai = a->i;
37387d4246cSBarry Smith   PetscErrorCode ierr;
37487d4246cSBarry Smith 
37587d4246cSBarry Smith   PetscFunctionBegin;
376580bdb30SBarry Smith   ierr = PetscArraycpy(a->a+ai[row],v,ai[row+1]-ai[row]);CHKERRQ(ierr);
377e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
378c70f7ee4SJunchao Zhang   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && ai[row+1]-ai[row]) A->offloadmask = PETSC_OFFLOAD_CPU;
379e2cf4d64SStefano Zampini #endif
38087d4246cSBarry Smith   PetscFunctionReturn(0);
38187d4246cSBarry Smith }
38287d4246cSBarry Smith 
383bd04181cSBarry Smith /*
384bd04181cSBarry Smith     MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions
385bd04181cSBarry Smith 
386bd04181cSBarry Smith       -   a single row of values is set with each call
387bd04181cSBarry Smith       -   no row or column indices are negative or (in error) larger than the number of rows or columns
388bd04181cSBarry Smith       -   the values are always added to the matrix, not set
389bd04181cSBarry Smith       -   no new locations are introduced in the nonzero structure of the matrix
390bd04181cSBarry Smith 
3911f763a69SBarry Smith      This does NOT assume the global column indices are sorted
392bd04181cSBarry Smith 
3931f763a69SBarry Smith */
394bd04181cSBarry Smith 
395af0996ceSBarry Smith #include <petsc/private/isimpl.h>
396189e4007SBarry Smith PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
397189e4007SBarry Smith {
398189e4007SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3991f763a69SBarry Smith   PetscInt       low,high,t,row,nrow,i,col,l;
4001f763a69SBarry Smith   const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j;
4011f763a69SBarry Smith   PetscInt       lastcol = -1;
402189e4007SBarry Smith   MatScalar      *ap,value,*aa = a->a;
403189e4007SBarry Smith   const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices;
404189e4007SBarry Smith 
405f38dd0b8SBarry Smith   row  = ridx[im[0]];
4061f763a69SBarry Smith   rp   = aj + ai[row];
4071f763a69SBarry Smith   ap   = aa + ai[row];
4081f763a69SBarry Smith   nrow = ailen[row];
409189e4007SBarry Smith   low  = 0;
410189e4007SBarry Smith   high = nrow;
411189e4007SBarry Smith   for (l=0; l<n; l++) { /* loop over added columns */
412189e4007SBarry Smith     col = cidx[in[l]];
413f38dd0b8SBarry Smith     value = v[l];
414189e4007SBarry Smith 
415189e4007SBarry Smith     if (col <= lastcol) low = 0;
416189e4007SBarry Smith     else high = nrow;
417189e4007SBarry Smith     lastcol = col;
418189e4007SBarry Smith     while (high-low > 5) {
419189e4007SBarry Smith       t = (low+high)/2;
420189e4007SBarry Smith       if (rp[t] > col) high = t;
421189e4007SBarry Smith       else low = t;
422189e4007SBarry Smith     }
423189e4007SBarry Smith     for (i=low; i<high; i++) {
424189e4007SBarry Smith       if (rp[i] == col) {
4251f763a69SBarry Smith         ap[i] += value;
426189e4007SBarry Smith         low = i + 1;
4271f763a69SBarry Smith         break;
428189e4007SBarry Smith       }
429189e4007SBarry Smith     }
430189e4007SBarry Smith   }
431e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
432c70f7ee4SJunchao Zhang   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU;
433e2cf4d64SStefano Zampini #endif
434f38dd0b8SBarry Smith   return 0;
435189e4007SBarry Smith }
436189e4007SBarry Smith 
43797f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
43817ab2063SBarry Smith {
439416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
440e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
44197f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
4426849ba73SBarry Smith   PetscErrorCode ierr;
443e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
444d8cdefa3SHong Zhang   MatScalar      *ap=NULL,value=0.0,*aa = a->a;
445ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
446ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
447e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
448e2cf4d64SStefano Zampini   PetscBool      inserted          = PETSC_FALSE;
449e2cf4d64SStefano Zampini #endif
45017ab2063SBarry Smith 
4513a40ed3dSBarry Smith   PetscFunctionBegin;
45217ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
453416022c9SBarry Smith     row = im[k];
4545ef9f2a5SBarry Smith     if (row < 0) continue;
455*cf9c20a2SJed Brown     if (PetscUnlikelyDebug(row >= A->rmap->n)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
456720833daSHong Zhang     rp   = aj + ai[row];
457876c6284SHong Zhang     if (!A->structure_only) ap = aa + ai[row];
45817ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
459416022c9SBarry Smith     low  = 0;
460c71e6ed7SBarry Smith     high = nrow;
46117ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
4625ef9f2a5SBarry Smith       if (in[l] < 0) continue;
463*cf9c20a2SJed Brown       if (PetscUnlikelyDebug(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);
464bfeeae90SHong Zhang       col = in[l];
465071fcb05SBarry Smith       if (v && !A->structure_only) value = roworiented ? v[l + k*n] : v[k + l*m];
466071fcb05SBarry Smith       if (!A->structure_only && value == 0.0 && ignorezeroentries && is == ADD_VALUES && row != col) continue;
46736db0b34SBarry Smith 
4682205254eSKarl Rupp       if (col <= lastcol) low = 0;
4692205254eSKarl Rupp       else high = nrow;
470e2ee6c50SBarry Smith       lastcol = col;
471416022c9SBarry Smith       while (high-low > 5) {
472416022c9SBarry Smith         t = (low+high)/2;
473416022c9SBarry Smith         if (rp[t] > col) high = t;
474416022c9SBarry Smith         else low = t;
47517ab2063SBarry Smith       }
476416022c9SBarry Smith       for (i=low; i<high; i++) {
47717ab2063SBarry Smith         if (rp[i] > col) break;
47817ab2063SBarry Smith         if (rp[i] == col) {
479876c6284SHong Zhang           if (!A->structure_only) {
4800c0d7e18SFande Kong             if (is == ADD_VALUES) {
4810c0d7e18SFande Kong               ap[i] += value;
4820c0d7e18SFande Kong               (void)PetscLogFlops(1.0);
4830c0d7e18SFande Kong             }
48417ab2063SBarry Smith             else ap[i] = value;
485e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
486e2cf4d64SStefano Zampini             inserted = PETSC_TRUE;
487e2cf4d64SStefano Zampini #endif
488720833daSHong Zhang           }
489e44c0bd4SBarry Smith           low = i + 1;
49017ab2063SBarry Smith           goto noinsert;
49117ab2063SBarry Smith         }
49217ab2063SBarry Smith       }
493dcd36c23SBarry Smith       if (value == 0.0 && ignorezeroentries && row != col) goto noinsert;
494c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
495e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
496720833daSHong Zhang       if (A->structure_only) {
497876c6284SHong Zhang         MatSeqXAIJReallocateAIJ_structure_only(A,A->rmap->n,1,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar);
498720833daSHong Zhang       } else {
499fef13f97SBarry Smith         MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
500720833daSHong Zhang       }
501c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
502416022c9SBarry Smith       /* shift up all the later entries in this row */
503580bdb30SBarry Smith       ierr  = PetscArraymove(rp+i+1,rp+i,N-i+1);CHKERRQ(ierr);
50417ab2063SBarry Smith       rp[i] = col;
505580bdb30SBarry Smith       if (!A->structure_only){
506580bdb30SBarry Smith         ierr  = PetscArraymove(ap+i+1,ap+i,N-i+1);CHKERRQ(ierr);
507580bdb30SBarry Smith         ap[i] = value;
508580bdb30SBarry Smith       }
509416022c9SBarry Smith       low = i + 1;
510e56f5c9eSBarry Smith       A->nonzerostate++;
511e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
512e2cf4d64SStefano Zampini       inserted = PETSC_TRUE;
513e2cf4d64SStefano Zampini #endif
514e44c0bd4SBarry Smith noinsert:;
51517ab2063SBarry Smith     }
51617ab2063SBarry Smith     ailen[row] = nrow;
51717ab2063SBarry Smith   }
518e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
519c70f7ee4SJunchao Zhang   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) A->offloadmask = PETSC_OFFLOAD_CPU;
520e2cf4d64SStefano Zampini #endif
5213a40ed3dSBarry Smith   PetscFunctionReturn(0);
52217ab2063SBarry Smith }
52317ab2063SBarry Smith 
524071fcb05SBarry Smith PetscErrorCode MatSetValues_SeqAIJ_SortedFull(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
525071fcb05SBarry Smith {
526071fcb05SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
527071fcb05SBarry Smith   PetscInt       *rp,k,row;
528071fcb05SBarry Smith   PetscInt       *ai = a->i,*ailen = a->ilen;
529071fcb05SBarry Smith   PetscErrorCode ierr;
530071fcb05SBarry Smith   PetscInt       *aj = a->j;
531071fcb05SBarry Smith   MatScalar      *aa = a->a,*ap;
532071fcb05SBarry Smith 
533071fcb05SBarry Smith   PetscFunctionBegin;
534071fcb05SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
535071fcb05SBarry Smith     row  = im[k];
536071fcb05SBarry Smith     rp   = aj + ai[row];
537071fcb05SBarry Smith     ap   = aa + ai[row];
538071fcb05SBarry Smith     if (!A->was_assembled) {
539071fcb05SBarry Smith       ierr = PetscMemcpy(rp,in,n*sizeof(PetscInt));CHKERRQ(ierr);
540071fcb05SBarry Smith     }
541071fcb05SBarry Smith     if (!A->structure_only) {
542071fcb05SBarry Smith       if (v) {
543071fcb05SBarry Smith         ierr = PetscMemcpy(ap,v,n*sizeof(PetscScalar));CHKERRQ(ierr);
544071fcb05SBarry Smith         v   += n;
545071fcb05SBarry Smith       } else {
546071fcb05SBarry Smith         ierr = PetscMemzero(ap,n*sizeof(PetscScalar));CHKERRQ(ierr);
547071fcb05SBarry Smith       }
548071fcb05SBarry Smith     }
549071fcb05SBarry Smith     ailen[row] = n;
550071fcb05SBarry Smith     a->nz      += n;
551071fcb05SBarry Smith   }
552e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
553c70f7ee4SJunchao Zhang   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU;
554e2cf4d64SStefano Zampini #endif
555071fcb05SBarry Smith   PetscFunctionReturn(0);
556071fcb05SBarry Smith }
557071fcb05SBarry Smith 
55881824310SBarry Smith 
559a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
5607eb43aa7SLois Curfman McInnes {
5617eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
56297f1f81fSBarry Smith   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
56397f1f81fSBarry Smith   PetscInt   *ai = a->i,*ailen = a->ilen;
56454f21887SBarry Smith   MatScalar  *ap,*aa = a->a;
5657eb43aa7SLois Curfman McInnes 
5663a40ed3dSBarry Smith   PetscFunctionBegin;
5677eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
5687eb43aa7SLois Curfman McInnes     row = im[k];
569e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
570e32f2f54SBarry 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);
571bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
5727eb43aa7SLois Curfman McInnes     nrow = ailen[row];
5737eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
574e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
575e32f2f54SBarry 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);
576bfeeae90SHong Zhang       col  = in[l];
5777eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
5787eb43aa7SLois Curfman McInnes       while (high-low > 5) {
5797eb43aa7SLois Curfman McInnes         t = (low+high)/2;
5807eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
5817eb43aa7SLois Curfman McInnes         else low = t;
5827eb43aa7SLois Curfman McInnes       }
5837eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
5847eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
5857eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
586b49de8d1SLois Curfman McInnes           *v++ = ap[i];
5877eb43aa7SLois Curfman McInnes           goto finished;
5887eb43aa7SLois Curfman McInnes         }
5897eb43aa7SLois Curfman McInnes       }
59097e567efSBarry Smith       *v++ = 0.0;
5917eb43aa7SLois Curfman McInnes finished:;
5927eb43aa7SLois Curfman McInnes     }
5937eb43aa7SLois Curfman McInnes   }
5943a40ed3dSBarry Smith   PetscFunctionReturn(0);
5957eb43aa7SLois Curfman McInnes }
5967eb43aa7SLois Curfman McInnes 
5973ea6fe3dSLisandro Dalcin PetscErrorCode MatView_SeqAIJ_Binary(Mat mat,PetscViewer viewer)
59817ab2063SBarry Smith {
5993ea6fe3dSLisandro Dalcin   Mat_SeqAIJ     *A = (Mat_SeqAIJ*)mat->data;
6003ea6fe3dSLisandro Dalcin   PetscInt       header[4],M,N,m,nz,i;
6013ea6fe3dSLisandro Dalcin   PetscInt       *rowlens;
6026849ba73SBarry Smith   PetscErrorCode ierr;
60317ab2063SBarry Smith 
6043a40ed3dSBarry Smith   PetscFunctionBegin;
6053ea6fe3dSLisandro Dalcin   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
6062205254eSKarl Rupp 
6073ea6fe3dSLisandro Dalcin   M  = mat->rmap->N;
6083ea6fe3dSLisandro Dalcin   N  = mat->cmap->N;
6093ea6fe3dSLisandro Dalcin   m  = mat->rmap->n;
6103ea6fe3dSLisandro Dalcin   nz = A->nz;
611416022c9SBarry Smith 
6123ea6fe3dSLisandro Dalcin   /* write matrix header */
6133ea6fe3dSLisandro Dalcin   header[0] = MAT_FILE_CLASSID;
6143ea6fe3dSLisandro Dalcin   header[1] = M; header[2] = N; header[3] = nz;
6153ea6fe3dSLisandro Dalcin   ierr = PetscViewerBinaryWrite(viewer,header,4,PETSC_INT);CHKERRQ(ierr);
616416022c9SBarry Smith 
6173ea6fe3dSLisandro Dalcin   /* fill in and store row lengths */
6183ea6fe3dSLisandro Dalcin   ierr = PetscMalloc1(m,&rowlens);CHKERRQ(ierr);
6193ea6fe3dSLisandro Dalcin   for (i=0; i<m; i++) rowlens[i] = A->i[i+1] - A->i[i];
6203ea6fe3dSLisandro Dalcin   ierr = PetscViewerBinaryWrite(viewer,rowlens,m,PETSC_INT);CHKERRQ(ierr);
6213ea6fe3dSLisandro Dalcin   ierr = PetscFree(rowlens);CHKERRQ(ierr);
6223ea6fe3dSLisandro Dalcin   /* store column indices */
6233ea6fe3dSLisandro Dalcin   ierr = PetscViewerBinaryWrite(viewer,A->j,nz,PETSC_INT);CHKERRQ(ierr);
624416022c9SBarry Smith   /* store nonzero values */
6253ea6fe3dSLisandro Dalcin   ierr = PetscViewerBinaryWrite(viewer,A->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
626b37d52dbSMark F. Adams 
6273ea6fe3dSLisandro Dalcin   /* write block size option to the viewer's .info file */
6283ea6fe3dSLisandro Dalcin   ierr = MatView_Binary_BlockSizes(mat,viewer);CHKERRQ(ierr);
6293a40ed3dSBarry Smith   PetscFunctionReturn(0);
63017ab2063SBarry Smith }
631416022c9SBarry Smith 
6327dc0baabSHong Zhang static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer)
6337dc0baabSHong Zhang {
6347dc0baabSHong Zhang   PetscErrorCode ierr;
6357dc0baabSHong Zhang   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6367dc0baabSHong Zhang   PetscInt       i,k,m=A->rmap->N;
6377dc0baabSHong Zhang 
6387dc0baabSHong Zhang   PetscFunctionBegin;
6397dc0baabSHong Zhang   ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
6407dc0baabSHong Zhang   for (i=0; i<m; i++) {
6417dc0baabSHong Zhang     ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
6427dc0baabSHong Zhang     for (k=a->i[i]; k<a->i[i+1]; k++) {
6437dc0baabSHong Zhang       ierr = PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);CHKERRQ(ierr);
6447dc0baabSHong Zhang     }
6457dc0baabSHong Zhang     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
6467dc0baabSHong Zhang   }
6477dc0baabSHong Zhang   ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
6487dc0baabSHong Zhang   PetscFunctionReturn(0);
6497dc0baabSHong Zhang }
6507dc0baabSHong Zhang 
65109573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
652cd155464SBarry Smith 
653dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
654416022c9SBarry Smith {
655416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
656dfbe8321SBarry Smith   PetscErrorCode    ierr;
65760e0710aSBarry Smith   PetscInt          i,j,m = A->rmap->n;
658e060cb09SBarry Smith   const char        *name;
659f3ef73ceSBarry Smith   PetscViewerFormat format;
66017ab2063SBarry Smith 
6613a40ed3dSBarry Smith   PetscFunctionBegin;
6627dc0baabSHong Zhang   if (A->structure_only) {
6637dc0baabSHong Zhang     ierr = MatView_SeqAIJ_ASCII_structonly(A,viewer);CHKERRQ(ierr);
6647dc0baabSHong Zhang     PetscFunctionReturn(0);
6657dc0baabSHong Zhang   }
66643e49210SHong Zhang 
667b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
66871c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
66997f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
67060e0710aSBarry Smith     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
671c337ccceSJed Brown       /* Need a dummy value to ensure the dimension of the matrix. */
672d00d2cf4SBarry Smith       nofinalvalue = 1;
673d00d2cf4SBarry Smith     }
674d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
675d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
67677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
677fbfe6fa7SJed Brown #if defined(PETSC_USE_COMPLEX)
678fbfe6fa7SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
679fbfe6fa7SJed Brown #else
68077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
681fbfe6fa7SJed Brown #endif
682b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
68317ab2063SBarry Smith 
68417ab2063SBarry Smith     for (i=0; i<m; i++) {
68560e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
686aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
687a9bf72d8SJed 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);
68817ab2063SBarry Smith #else
68960e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr);
69017ab2063SBarry Smith #endif
69117ab2063SBarry Smith       }
69217ab2063SBarry Smith     }
693d00d2cf4SBarry Smith     if (nofinalvalue) {
694c337ccceSJed Brown #if defined(PETSC_USE_COMPLEX)
695c337ccceSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr);
696c337ccceSJed Brown #else
697d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
698c337ccceSJed Brown #endif
699d00d2cf4SBarry Smith     }
700317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
701fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
702d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
7032950ac48SStefano Zampini   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
704cd155464SBarry Smith     PetscFunctionReturn(0);
705fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
706d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
70744cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
70877431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
70960e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
710aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
71136db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
71260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
71336db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
71460e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
71536db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
71660e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
7176831982aSBarry Smith         }
71844cd7ae7SLois Curfman McInnes #else
71960e0710aSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);}
72044cd7ae7SLois Curfman McInnes #endif
72144cd7ae7SLois Curfman McInnes       }
722b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
72344cd7ae7SLois Curfman McInnes     }
724d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
725fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
72697f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
727d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
728854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&sptr);CHKERRQ(ierr);
729496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
730496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
73160e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
732496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
733aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
73436db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
735496be53dSLois Curfman McInnes #else
736496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
737496be53dSLois Curfman McInnes #endif
738496be53dSLois Curfman McInnes         }
739496be53dSLois Curfman McInnes       }
740496be53dSLois Curfman McInnes     }
7412e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
74277431f27SBarry Smith     ierr    = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
7432e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
7442205254eSKarl Rupp       if (i+4<m) {
7452205254eSKarl 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);
7462205254eSKarl Rupp       } else if (i+3<m) {
7472205254eSKarl 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);
7482205254eSKarl Rupp       } else if (i+2<m) {
7492205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);
7502205254eSKarl Rupp       } else if (i+1<m) {
7512205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);
7522205254eSKarl Rupp       } else if (i<m) {
7532205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);
7542205254eSKarl Rupp       } else {
7552205254eSKarl Rupp         ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);
7562205254eSKarl Rupp       }
757496be53dSLois Curfman McInnes     }
758b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
759606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
760496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
76160e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
76277431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
763496be53dSLois Curfman McInnes       }
764b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
765496be53dSLois Curfman McInnes     }
766b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
767496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
76860e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
769496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
770aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
77136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
77260e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
7736831982aSBarry Smith           }
774496be53dSLois Curfman McInnes #else
77560e0710aSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);}
776496be53dSLois Curfman McInnes #endif
777496be53dSLois Curfman McInnes         }
778496be53dSLois Curfman McInnes       }
779b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
780496be53dSLois Curfman McInnes     }
781d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
782fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
78397f1f81fSBarry Smith     PetscInt    cnt = 0,jcnt;
78487828ca2SBarry Smith     PetscScalar value;
78568f1ed48SBarry Smith #if defined(PETSC_USE_COMPLEX)
78668f1ed48SBarry Smith     PetscBool   realonly = PETSC_TRUE;
78768f1ed48SBarry Smith 
78868f1ed48SBarry Smith     for (i=0; i<a->i[m]; i++) {
78968f1ed48SBarry Smith       if (PetscImaginaryPart(a->a[i]) != 0.0) {
79068f1ed48SBarry Smith         realonly = PETSC_FALSE;
79168f1ed48SBarry Smith         break;
79268f1ed48SBarry Smith       }
79368f1ed48SBarry Smith     }
79468f1ed48SBarry Smith #endif
79502594712SBarry Smith 
796d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
79702594712SBarry Smith     for (i=0; i<m; i++) {
79802594712SBarry Smith       jcnt = 0;
799d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
800e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
80102594712SBarry Smith           value = a->a[cnt++];
802e24b481bSBarry Smith           jcnt++;
80302594712SBarry Smith         } else {
80402594712SBarry Smith           value = 0.0;
80502594712SBarry Smith         }
806aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
80768f1ed48SBarry Smith         if (realonly) {
80860e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr);
80968f1ed48SBarry Smith         } else {
81060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr);
81168f1ed48SBarry Smith         }
81202594712SBarry Smith #else
81360e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr);
81402594712SBarry Smith #endif
81502594712SBarry Smith       }
816b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
81702594712SBarry Smith     }
818d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
8193c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
820150b93efSMatthew G. Knepley     PetscInt fshift=1;
821d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
8223c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
82319303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");CHKERRQ(ierr);
8243c215bfdSMatthew Knepley #else
82519303e72SJonathan Guyer     ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");CHKERRQ(ierr);
8263c215bfdSMatthew Knepley #endif
827d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
8283c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
82960e0710aSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
8303c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
831a9a0e077SKarl 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);
8323c215bfdSMatthew Knepley #else
833150b93efSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr);
8343c215bfdSMatthew Knepley #endif
8353c215bfdSMatthew Knepley       }
8363c215bfdSMatthew Knepley     }
837d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
8383a40ed3dSBarry Smith   } else {
839d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
840d5f3da31SBarry Smith     if (A->factortype) {
84116cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
84216cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
84316cd7e1dSShri Abhyankar         /* L part */
84460e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
84516cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
84616cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
84760e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
84816cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
8496712e2f1SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
85016cd7e1dSShri Abhyankar           } else {
85160e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
85216cd7e1dSShri Abhyankar           }
85316cd7e1dSShri Abhyankar #else
85460e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
85516cd7e1dSShri Abhyankar #endif
85616cd7e1dSShri Abhyankar         }
85716cd7e1dSShri Abhyankar         /* diagonal */
85816cd7e1dSShri Abhyankar         j = a->diag[i];
85916cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
86016cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
86160e0710aSBarry 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);
86216cd7e1dSShri Abhyankar         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
8636712e2f1SBarry 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);
86416cd7e1dSShri Abhyankar         } else {
86560e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
86616cd7e1dSShri Abhyankar         }
86716cd7e1dSShri Abhyankar #else
86860e0710aSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr);
86916cd7e1dSShri Abhyankar #endif
87016cd7e1dSShri Abhyankar 
87116cd7e1dSShri Abhyankar         /* U part */
87260e0710aSBarry Smith         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
87316cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
87416cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
87560e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
87616cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
87722ab088eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr);
87816cd7e1dSShri Abhyankar           } else {
87960e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
88016cd7e1dSShri Abhyankar           }
88116cd7e1dSShri Abhyankar #else
88260e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
88316cd7e1dSShri Abhyankar #endif
88416cd7e1dSShri Abhyankar         }
88516cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
88616cd7e1dSShri Abhyankar       }
88716cd7e1dSShri Abhyankar     } else {
88817ab2063SBarry Smith       for (i=0; i<m; i++) {
88977431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
89060e0710aSBarry Smith         for (j=a->i[i]; j<a->i[i+1]; j++) {
891aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
89236db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
89360e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
89436db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
89560e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
8963a40ed3dSBarry Smith           } else {
89760e0710aSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr);
89817ab2063SBarry Smith           }
89917ab2063SBarry Smith #else
90060e0710aSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);
90117ab2063SBarry Smith #endif
90217ab2063SBarry Smith         }
903b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
90417ab2063SBarry Smith       }
90516cd7e1dSShri Abhyankar     }
906d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
90717ab2063SBarry Smith   }
908b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
9093a40ed3dSBarry Smith   PetscFunctionReturn(0);
910416022c9SBarry Smith }
911416022c9SBarry Smith 
9129804daf3SBarry Smith #include <petscdraw.h>
913dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
914416022c9SBarry Smith {
915480ef9eaSBarry Smith   Mat               A  = (Mat) Aa;
916416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
917dfbe8321SBarry Smith   PetscErrorCode    ierr;
918383922c3SLisandro Dalcin   PetscInt          i,j,m = A->rmap->n;
919383922c3SLisandro Dalcin   int               color;
920b05fc000SLisandro Dalcin   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
921b0a32e0cSBarry Smith   PetscViewer       viewer;
922f3ef73ceSBarry Smith   PetscViewerFormat format;
923cddf8d76SBarry Smith 
9243a40ed3dSBarry Smith   PetscFunctionBegin;
925480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
926b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
927b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
928383922c3SLisandro Dalcin 
929416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
9300513a670SBarry Smith 
931fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
932383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
9330513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
934b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
935416022c9SBarry Smith     for (i=0; i<m; i++) {
936cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
937bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
938bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
93936db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
940b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
941cddf8d76SBarry Smith       }
942cddf8d76SBarry Smith     }
943b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
944cddf8d76SBarry Smith     for (i=0; i<m; i++) {
945cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
946bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
947bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
948cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
949b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
950cddf8d76SBarry Smith       }
951cddf8d76SBarry Smith     }
952b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
953cddf8d76SBarry Smith     for (i=0; i<m; i++) {
954cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
955bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
956bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
95736db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
958b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
959416022c9SBarry Smith       }
960416022c9SBarry Smith     }
961383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
9620513a670SBarry Smith   } else {
9630513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
9640513a670SBarry Smith     /* first determine max of all nonzero values */
965b05fc000SLisandro Dalcin     PetscReal minv = 0.0, maxv = 0.0;
966383922c3SLisandro Dalcin     PetscInt  nz = a->nz, count = 0;
967b0a32e0cSBarry Smith     PetscDraw popup;
9680513a670SBarry Smith 
9690513a670SBarry Smith     for (i=0; i<nz; i++) {
9700513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
9710513a670SBarry Smith     }
972383922c3SLisandro Dalcin     if (minv >= maxv) maxv = minv + PETSC_SMALL;
973b0a32e0cSBarry Smith     ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
97445f3bb6eSLisandro Dalcin     ierr = PetscDrawScalePopup(popup,minv,maxv);CHKERRQ(ierr);
975383922c3SLisandro Dalcin 
976383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
9770513a670SBarry Smith     for (i=0; i<m; i++) {
978383922c3SLisandro Dalcin       y_l = m - i - 1.0;
979383922c3SLisandro Dalcin       y_r = y_l + 1.0;
980bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
981383922c3SLisandro Dalcin         x_l = a->j[j];
982383922c3SLisandro Dalcin         x_r = x_l + 1.0;
983b05fc000SLisandro Dalcin         color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
984b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
9850513a670SBarry Smith         count++;
9860513a670SBarry Smith       }
9870513a670SBarry Smith     }
988383922c3SLisandro Dalcin     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
9890513a670SBarry Smith   }
990480ef9eaSBarry Smith   PetscFunctionReturn(0);
991480ef9eaSBarry Smith }
992cddf8d76SBarry Smith 
9939804daf3SBarry Smith #include <petscdraw.h>
994dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
995480ef9eaSBarry Smith {
996dfbe8321SBarry Smith   PetscErrorCode ierr;
997b0a32e0cSBarry Smith   PetscDraw      draw;
99836db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
999ace3abfcSBarry Smith   PetscBool      isnull;
1000480ef9eaSBarry Smith 
1001480ef9eaSBarry Smith   PetscFunctionBegin;
1002b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
1003b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
1004480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
1005480ef9eaSBarry Smith 
1006d0f46423SBarry Smith   xr   = A->cmap->n; yr  = A->rmap->n; h = yr/10.0; w = xr/10.0;
1007480ef9eaSBarry Smith   xr  += w;          yr += h;         xl = -w;     yl = -h;
1008b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
1009832b7cebSLisandro Dalcin   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
1010b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
10110298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
1012832b7cebSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
10133a40ed3dSBarry Smith   PetscFunctionReturn(0);
1014416022c9SBarry Smith }
1015416022c9SBarry Smith 
1016dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
1017416022c9SBarry Smith {
1018dfbe8321SBarry Smith   PetscErrorCode ierr;
1019ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
1020416022c9SBarry Smith 
10213a40ed3dSBarry Smith   PetscFunctionBegin;
1022251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1023251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
1024251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1025c45a1595SBarry Smith   if (iascii) {
10263a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
10270f5bd95cSBarry Smith   } else if (isbinary) {
10283a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
10290f5bd95cSBarry Smith   } else if (isdraw) {
10303a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
103111aeaf0aSBarry Smith   }
10324108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
10333a40ed3dSBarry Smith   PetscFunctionReturn(0);
103417ab2063SBarry Smith }
103519bcc07fSBarry Smith 
1036dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
103717ab2063SBarry Smith {
1038416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10396849ba73SBarry Smith   PetscErrorCode ierr;
1040580bdb30SBarry Smith   PetscInt       fshift = 0,i,*ai = a->i,*aj = a->j,*imax = a->imax;
1041d0f46423SBarry Smith   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
104254f21887SBarry Smith   MatScalar      *aa    = a->a,*ap;
10433447b6efSHong Zhang   PetscReal      ratio  = 0.6;
104417ab2063SBarry Smith 
10453a40ed3dSBarry Smith   PetscFunctionBegin;
10463a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
1047071fcb05SBarry Smith   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
1048071fcb05SBarry Smith   if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) PetscFunctionReturn(0);
104917ab2063SBarry Smith 
105043ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
105117ab2063SBarry Smith   for (i=1; i<m; i++) {
1052416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
105317ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
105494a9d846SBarry Smith     rmax    = PetscMax(rmax,ailen[i]);
105517ab2063SBarry Smith     if (fshift) {
1056bfeeae90SHong Zhang       ip = aj + ai[i];
1057bfeeae90SHong Zhang       ap = aa + ai[i];
105817ab2063SBarry Smith       N  = ailen[i];
1059580bdb30SBarry Smith       ierr = PetscArraymove(ip-fshift,ip,N);CHKERRQ(ierr);
1060580bdb30SBarry Smith       if (!A->structure_only) {
1061580bdb30SBarry Smith         ierr = PetscArraymove(ap-fshift,ap,N);CHKERRQ(ierr);
106217ab2063SBarry Smith       }
106317ab2063SBarry Smith     }
106417ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
106517ab2063SBarry Smith   }
106617ab2063SBarry Smith   if (m) {
106717ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
106817ab2063SBarry Smith     ai[m]   = ai[m-1] + ailen[m-1];
106917ab2063SBarry Smith   }
10707b083b7cSBarry Smith 
107117ab2063SBarry Smith   /* reset ilen and imax for each row */
10727b083b7cSBarry Smith   a->nonzerorowcnt = 0;
1073396832f4SHong Zhang   if (A->structure_only) {
1074071fcb05SBarry Smith     ierr = PetscFree(a->imax);CHKERRQ(ierr);
1075071fcb05SBarry Smith     ierr = PetscFree(a->ilen);CHKERRQ(ierr);
1076396832f4SHong Zhang   } else { /* !A->structure_only */
107717ab2063SBarry Smith     for (i=0; i<m; i++) {
107817ab2063SBarry Smith       ailen[i] = imax[i] = ai[i+1] - ai[i];
10797b083b7cSBarry Smith       a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
108017ab2063SBarry Smith     }
1081396832f4SHong Zhang   }
1082bfeeae90SHong Zhang   a->nz = ai[m];
108365e19b50SBarry 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);
108417ab2063SBarry Smith 
108509f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1086d0f46423SBarry 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);
1087ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
1088ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
10892205254eSKarl Rupp 
10908e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
1091dd5f02e7SSatish Balay   a->reallocs         = 0;
10926712e2f1SBarry Smith   A->info.nz_unneeded = (PetscReal)fshift;
109336db0b34SBarry Smith   a->rmax             = rmax;
10944e220ebcSLois Curfman McInnes 
1095396832f4SHong Zhang   if (!A->structure_only) {
109611e456e1SBarry Smith     ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
1097396832f4SHong Zhang   }
10984108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
10993a40ed3dSBarry Smith   PetscFunctionReturn(0);
110017ab2063SBarry Smith }
110117ab2063SBarry Smith 
110299cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
110399cafbc1SBarry Smith {
110499cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
110599cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
110654f21887SBarry Smith   MatScalar      *aa = a->a;
1107acf2f550SJed Brown   PetscErrorCode ierr;
110899cafbc1SBarry Smith 
110999cafbc1SBarry Smith   PetscFunctionBegin;
111099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1111acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
1112e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1113c70f7ee4SJunchao Zhang   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1114e2cf4d64SStefano Zampini #endif
111599cafbc1SBarry Smith   PetscFunctionReturn(0);
111699cafbc1SBarry Smith }
111799cafbc1SBarry Smith 
111899cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
111999cafbc1SBarry Smith {
112099cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
112199cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
112254f21887SBarry Smith   MatScalar      *aa = a->a;
1123acf2f550SJed Brown   PetscErrorCode ierr;
112499cafbc1SBarry Smith 
112599cafbc1SBarry Smith   PetscFunctionBegin;
112699cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1127acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
1128e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1129c70f7ee4SJunchao Zhang   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1130e2cf4d64SStefano Zampini #endif
113199cafbc1SBarry Smith   PetscFunctionReturn(0);
113299cafbc1SBarry Smith }
113399cafbc1SBarry Smith 
1134dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
113517ab2063SBarry Smith {
1136416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1137dfbe8321SBarry Smith   PetscErrorCode ierr;
11383a40ed3dSBarry Smith 
11393a40ed3dSBarry Smith   PetscFunctionBegin;
1140580bdb30SBarry Smith   ierr = PetscArrayzero(a->a,a->i[A->rmap->n]);CHKERRQ(ierr);
1141acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
1142e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1143c70f7ee4SJunchao Zhang   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1144e2cf4d64SStefano Zampini #endif
11453a40ed3dSBarry Smith   PetscFunctionReturn(0);
114617ab2063SBarry Smith }
1147416022c9SBarry Smith 
1148dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
114917ab2063SBarry Smith {
1150416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1151dfbe8321SBarry Smith   PetscErrorCode ierr;
1152d5d45c9bSBarry Smith 
11533a40ed3dSBarry Smith   PetscFunctionBegin;
1154aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1155d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
115617ab2063SBarry Smith #endif
1157e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
11586bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
11596bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
116005b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
1161d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
1162071fcb05SBarry Smith   ierr = PetscFree(a->imax);CHKERRQ(ierr);
1163071fcb05SBarry Smith   ierr = PetscFree(a->ilen);CHKERRQ(ierr);
1164846b4da1SFande Kong   ierr = PetscFree(a->ipre);CHKERRQ(ierr);
116571f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
116605b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
11676bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
116805b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
11696bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
1170cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
11710b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
1172a30b2313SHong Zhang 
11734108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
1174bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
1175901853e0SKris Buschelman 
1176dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
1177bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
1178bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1179bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1180bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr);
1181bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr);
1182bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr);
11834222ddf1SHong Zhang 
11844222ddf1SHong Zhang #if defined(PETSC_HAVE_CUDA)
11854222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijcusparse_C",NULL);CHKERRQ(ierr);
11864222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSetFromOptions_seqaijcusparse_seqaij_C",NULL);CHKERRQ(ierr);
11874222ddf1SHong Zhang #endif
11884222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijcrl_C",NULL);CHKERRQ(ierr);
1189af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
1190af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);CHKERRQ(ierr);
1191af8000cdSHong Zhang #endif
119263c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE)
119363c07aadSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);CHKERRQ(ierr);
11944222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_transpose_seqaij_seqaij_C",NULL);CHKERRQ(ierr);
119563c07aadSStefano Zampini #endif
1196b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);CHKERRQ(ierr);
1197c9225affSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsell_C",NULL);CHKERRQ(ierr);
1198c9225affSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_is_C",NULL);CHKERRQ(ierr);
1199bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1200bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1201846b4da1SFande Kong   ierr = PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);CHKERRQ(ierr);
1202bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1203bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr);
12044222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_is_seqaij_C",NULL);CHKERRQ(ierr);
12054222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqdense_seqaij_C",NULL);CHKERRQ(ierr);
12064222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaij_seqaij_C",NULL);CHKERRQ(ierr);
12073a40ed3dSBarry Smith   PetscFunctionReturn(0);
120817ab2063SBarry Smith }
120917ab2063SBarry Smith 
1210ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
121117ab2063SBarry Smith {
1212416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12134846f1f5SKris Buschelman   PetscErrorCode ierr;
12143a40ed3dSBarry Smith 
12153a40ed3dSBarry Smith   PetscFunctionBegin;
1216a65d3064SKris Buschelman   switch (op) {
1217a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
12184e0d8c25SBarry Smith     a->roworiented = flg;
1219a65d3064SKris Buschelman     break;
1220a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1221a9817697SBarry Smith     a->keepnonzeropattern = flg;
1222a65d3064SKris Buschelman     break;
1223512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1224512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1225a65d3064SKris Buschelman     break;
1226a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
12274e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1228a65d3064SKris Buschelman     break;
1229a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
12304e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1231a65d3064SKris Buschelman     break;
123228b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
123328b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
123428b2fa4aSMatthew Knepley     break;
1235a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
12364e0d8c25SBarry Smith     a->ignorezeroentries = flg;
12370df259c2SBarry Smith     break;
12383d472b54SHong Zhang   case MAT_SPD:
1239b1646e73SJed Brown   case MAT_SYMMETRIC:
1240b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1241b1646e73SJed Brown   case MAT_HERMITIAN:
1242b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
1243957cac9fSHong Zhang   case MAT_STRUCTURE_ONLY:
12445021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
12455021d80fSJed Brown     break;
12464e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1247a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1248a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1249290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1250a65d3064SKris Buschelman     break;
1251b87ac2d8SJed Brown   case MAT_USE_INODES:
1252b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1253b87ac2d8SJed Brown     break;
1254c10200c1SHong Zhang   case MAT_SUBMAT_SINGLEIS:
1255c10200c1SHong Zhang     A->submat_singleis = flg;
1256c10200c1SHong Zhang     break;
1257071fcb05SBarry Smith   case MAT_SORTED_FULL:
1258071fcb05SBarry Smith     if (flg) A->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
1259071fcb05SBarry Smith     else     A->ops->setvalues = MatSetValues_SeqAIJ;
1260071fcb05SBarry Smith     break;
1261a65d3064SKris Buschelman   default:
1262e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1263a65d3064SKris Buschelman   }
12644108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
12653a40ed3dSBarry Smith   PetscFunctionReturn(0);
126617ab2063SBarry Smith }
126717ab2063SBarry Smith 
1268dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
126917ab2063SBarry Smith {
1270416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12716849ba73SBarry Smith   PetscErrorCode ierr;
1272fdc842d1SBarry Smith   PetscInt       i,j,n,*ai=a->i,*aj=a->j;
1273fdc842d1SBarry Smith   PetscScalar    *aa=a->a,*x;
127417ab2063SBarry Smith 
12753a40ed3dSBarry Smith   PetscFunctionBegin;
1276d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1277e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
127835e7444dSHong Zhang 
1279d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1280d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
1281fdc842d1SBarry Smith     ierr = VecGetArrayWrite(v,&x);CHKERRQ(ierr);
12822c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
1283fdc842d1SBarry Smith     ierr = VecRestoreArrayWrite(v,&x);CHKERRQ(ierr);
128435e7444dSHong Zhang     PetscFunctionReturn(0);
128535e7444dSHong Zhang   }
128635e7444dSHong Zhang 
1287fdc842d1SBarry Smith   ierr = VecGetArrayWrite(v,&x);CHKERRQ(ierr);
128835e7444dSHong Zhang   for (i=0; i<n; i++) {
1289fdc842d1SBarry Smith     x[i] = 0.0;
129035e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++) {
129135e7444dSHong Zhang       if (aj[j] == i) {
129235e7444dSHong Zhang         x[i] = aa[j];
129317ab2063SBarry Smith         break;
129417ab2063SBarry Smith       }
129517ab2063SBarry Smith     }
129617ab2063SBarry Smith   }
1297fdc842d1SBarry Smith   ierr = VecRestoreArrayWrite(v,&x);CHKERRQ(ierr);
12983a40ed3dSBarry Smith   PetscFunctionReturn(0);
129917ab2063SBarry Smith }
130017ab2063SBarry Smith 
1301c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1302dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
130317ab2063SBarry Smith {
1304416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1305d9ca1df4SBarry Smith   PetscScalar       *y;
1306d9ca1df4SBarry Smith   const PetscScalar *x;
1307dfbe8321SBarry Smith   PetscErrorCode    ierr;
1308d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
13095c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1310d9ca1df4SBarry Smith   const MatScalar   *v;
1311a77337e4SBarry Smith   PetscScalar       alpha;
1312d9ca1df4SBarry Smith   PetscInt          n,i,j;
1313d9ca1df4SBarry Smith   const PetscInt    *idx,*ii,*ridx=NULL;
13143447b6efSHong Zhang   Mat_CompressedRow cprow    = a->compressedrow;
1315ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
13165c897100SBarry Smith #endif
131717ab2063SBarry Smith 
13183a40ed3dSBarry Smith   PetscFunctionBegin;
13192e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
1320d9ca1df4SBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
13211ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
13225c897100SBarry Smith 
13235c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1324bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
13255c897100SBarry Smith #else
13263447b6efSHong Zhang   if (usecprow) {
13273447b6efSHong Zhang     m    = cprow.nrows;
13283447b6efSHong Zhang     ii   = cprow.i;
13297b2bb3b9SHong Zhang     ridx = cprow.rindex;
13303447b6efSHong Zhang   } else {
13313447b6efSHong Zhang     ii = a->i;
13323447b6efSHong Zhang   }
133317ab2063SBarry Smith   for (i=0; i<m; i++) {
13343447b6efSHong Zhang     idx = a->j + ii[i];
13353447b6efSHong Zhang     v   = a->a + ii[i];
13363447b6efSHong Zhang     n   = ii[i+1] - ii[i];
13373447b6efSHong Zhang     if (usecprow) {
13387b2bb3b9SHong Zhang       alpha = x[ridx[i]];
13393447b6efSHong Zhang     } else {
134017ab2063SBarry Smith       alpha = x[i];
13413447b6efSHong Zhang     }
134204fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
134317ab2063SBarry Smith   }
13445c897100SBarry Smith #endif
1345dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1346d9ca1df4SBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13471ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13483a40ed3dSBarry Smith   PetscFunctionReturn(0);
134917ab2063SBarry Smith }
135017ab2063SBarry Smith 
1351dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
13525c897100SBarry Smith {
1353dfbe8321SBarry Smith   PetscErrorCode ierr;
13545c897100SBarry Smith 
13555c897100SBarry Smith   PetscFunctionBegin;
1356170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
13575c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
13585c897100SBarry Smith   PetscFunctionReturn(0);
13595c897100SBarry Smith }
13605c897100SBarry Smith 
1361c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
136278b84d54SShri Abhyankar 
1363dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
136417ab2063SBarry Smith {
1365416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1366d9fead3dSBarry Smith   PetscScalar       *y;
136754f21887SBarry Smith   const PetscScalar *x;
136854f21887SBarry Smith   const MatScalar   *aa;
1369dfbe8321SBarry Smith   PetscErrorCode    ierr;
1370003131ecSBarry Smith   PetscInt          m=A->rmap->n;
13710298fd71SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
13727b083b7cSBarry Smith   PetscInt          n,i;
1373362ced78SSatish Balay   PetscScalar       sum;
1374ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
137517ab2063SBarry Smith 
1376b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
137797952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1378fee21e36SBarry Smith #endif
1379fee21e36SBarry Smith 
13803a40ed3dSBarry Smith   PetscFunctionBegin;
13813649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
13821ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1383416022c9SBarry Smith   ii   = a->i;
13844eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
1385580bdb30SBarry Smith     ierr = PetscArrayzero(y,m);CHKERRQ(ierr);
138697952fefSHong Zhang     m    = a->compressedrow.nrows;
138797952fefSHong Zhang     ii   = a->compressedrow.i;
138897952fefSHong Zhang     ridx = a->compressedrow.rindex;
138997952fefSHong Zhang     for (i=0; i<m; i++) {
139097952fefSHong Zhang       n           = ii[i+1] - ii[i];
139197952fefSHong Zhang       aj          = a->j + ii[i];
139297952fefSHong Zhang       aa          = a->a + ii[i];
139397952fefSHong Zhang       sum         = 0.0;
1394003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1395003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
139697952fefSHong Zhang       y[*ridx++] = sum;
139797952fefSHong Zhang     }
139897952fefSHong Zhang   } else { /* do not use compressed row format */
1399b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
14003d3eaba7SBarry Smith     aj   = a->j;
14013d3eaba7SBarry Smith     aa   = a->a;
1402b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1403b05257ddSBarry Smith #else
140417ab2063SBarry Smith     for (i=0; i<m; i++) {
1405003131ecSBarry Smith       n           = ii[i+1] - ii[i];
1406003131ecSBarry Smith       aj          = a->j + ii[i];
1407003131ecSBarry Smith       aa          = a->a + ii[i];
140817ab2063SBarry Smith       sum         = 0.0;
1409003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
141017ab2063SBarry Smith       y[i] = sum;
141117ab2063SBarry Smith     }
14128d195f9aSBarry Smith #endif
1413b05257ddSBarry Smith   }
14147b083b7cSBarry Smith   ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr);
14153649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
14161ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
14173a40ed3dSBarry Smith   PetscFunctionReturn(0);
141817ab2063SBarry Smith }
141917ab2063SBarry Smith 
1420b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1421b434eb95SMatthew G. Knepley {
1422b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1423b434eb95SMatthew G. Knepley   PetscScalar       *y;
1424b434eb95SMatthew G. Knepley   const PetscScalar *x;
1425b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1426b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1427b434eb95SMatthew G. Knepley   PetscInt          m=A->rmap->n;
1428b434eb95SMatthew G. Knepley   const PetscInt    *aj,*ii,*ridx=NULL;
1429b434eb95SMatthew G. Knepley   PetscInt          n,i,nonzerorow=0;
1430b434eb95SMatthew G. Knepley   PetscScalar       sum;
1431b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1432b434eb95SMatthew G. Knepley 
1433b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1434b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa)
1435b434eb95SMatthew G. Knepley #endif
1436b434eb95SMatthew G. Knepley 
1437b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1438b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1439b434eb95SMatthew G. Knepley   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
1440b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1441b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1442b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1443b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1444b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1445b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1446b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1447b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1448b434eb95SMatthew G. Knepley       sum         = 0.0;
1449b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1450b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1451b434eb95SMatthew G. Knepley       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1452b434eb95SMatthew G. Knepley       y[*ridx++] = sum;
1453b434eb95SMatthew G. Knepley     }
1454b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
14553d3eaba7SBarry Smith     ii = a->i;
1456b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1457b434eb95SMatthew G. Knepley       n           = ii[i+1] - ii[i];
1458b434eb95SMatthew G. Knepley       aj          = a->j + ii[i];
1459b434eb95SMatthew G. Knepley       aa          = a->a + ii[i];
1460b434eb95SMatthew G. Knepley       sum         = 0.0;
1461b434eb95SMatthew G. Knepley       nonzerorow += (n>0);
1462b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1463b434eb95SMatthew G. Knepley       y[i] = sum;
1464b434eb95SMatthew G. Knepley     }
1465b434eb95SMatthew G. Knepley   }
1466b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
1467b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1468b434eb95SMatthew G. Knepley   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
1469b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1470b434eb95SMatthew G. Knepley }
1471b434eb95SMatthew G. Knepley 
1472b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1473b434eb95SMatthew G. Knepley {
1474b434eb95SMatthew G. Knepley   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1475b434eb95SMatthew G. Knepley   PetscScalar       *y,*z;
1476b434eb95SMatthew G. Knepley   const PetscScalar *x;
1477b434eb95SMatthew G. Knepley   const MatScalar   *aa;
1478b434eb95SMatthew G. Knepley   PetscErrorCode    ierr;
1479b434eb95SMatthew G. Knepley   PetscInt          m = A->rmap->n,*aj,*ii;
1480b434eb95SMatthew G. Knepley   PetscInt          n,i,*ridx=NULL;
1481b434eb95SMatthew G. Knepley   PetscScalar       sum;
1482b434eb95SMatthew G. Knepley   PetscBool         usecprow=a->compressedrow.use;
1483b434eb95SMatthew G. Knepley 
1484b434eb95SMatthew G. Knepley   PetscFunctionBegin;
1485b434eb95SMatthew G. Knepley   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1486d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1487b434eb95SMatthew G. Knepley   if (usecprow) { /* use compressed row format */
1488b434eb95SMatthew G. Knepley     if (zz != yy) {
1489580bdb30SBarry Smith       ierr = PetscArraycpy(z,y,m);CHKERRQ(ierr);
1490b434eb95SMatthew G. Knepley     }
1491b434eb95SMatthew G. Knepley     m    = a->compressedrow.nrows;
1492b434eb95SMatthew G. Knepley     ii   = a->compressedrow.i;
1493b434eb95SMatthew G. Knepley     ridx = a->compressedrow.rindex;
1494b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1495b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1496b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1497b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1498b434eb95SMatthew G. Knepley       sum = y[*ridx];
1499b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1500b434eb95SMatthew G. Knepley       z[*ridx++] = sum;
1501b434eb95SMatthew G. Knepley     }
1502b434eb95SMatthew G. Knepley   } else { /* do not use compressed row format */
15033d3eaba7SBarry Smith     ii = a->i;
1504b434eb95SMatthew G. Knepley     for (i=0; i<m; i++) {
1505b434eb95SMatthew G. Knepley       n   = ii[i+1] - ii[i];
1506b434eb95SMatthew G. Knepley       aj  = a->j + ii[i];
1507b434eb95SMatthew G. Knepley       aa  = a->a + ii[i];
1508b434eb95SMatthew G. Knepley       sum = y[i];
1509b434eb95SMatthew G. Knepley       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1510b434eb95SMatthew G. Knepley       z[i] = sum;
1511b434eb95SMatthew G. Knepley     }
1512b434eb95SMatthew G. Knepley   }
1513b434eb95SMatthew G. Knepley   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1514b434eb95SMatthew G. Knepley   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1515d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
1516b434eb95SMatthew G. Knepley   PetscFunctionReturn(0);
1517b434eb95SMatthew G. Knepley }
1518b434eb95SMatthew G. Knepley 
1519c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1520dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
152117ab2063SBarry Smith {
1522416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1523f15663dcSBarry Smith   PetscScalar       *y,*z;
1524f15663dcSBarry Smith   const PetscScalar *x;
152554f21887SBarry Smith   const MatScalar   *aa;
1526dfbe8321SBarry Smith   PetscErrorCode    ierr;
1527d9ca1df4SBarry Smith   const PetscInt    *aj,*ii,*ridx=NULL;
1528d9ca1df4SBarry Smith   PetscInt          m = A->rmap->n,n,i;
1529362ced78SSatish Balay   PetscScalar       sum;
1530ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
15319ea0dfa2SSatish Balay 
15323a40ed3dSBarry Smith   PetscFunctionBegin;
1533f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
1534d9ca1df4SBarry Smith   ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
15354eb6d288SHong Zhang   if (usecprow) { /* use compressed row format */
15364eb6d288SHong Zhang     if (zz != yy) {
1537580bdb30SBarry Smith       ierr = PetscArraycpy(z,y,m);CHKERRQ(ierr);
15384eb6d288SHong Zhang     }
153997952fefSHong Zhang     m    = a->compressedrow.nrows;
154097952fefSHong Zhang     ii   = a->compressedrow.i;
154197952fefSHong Zhang     ridx = a->compressedrow.rindex;
154297952fefSHong Zhang     for (i=0; i<m; i++) {
154397952fefSHong Zhang       n   = ii[i+1] - ii[i];
154497952fefSHong Zhang       aj  = a->j + ii[i];
154597952fefSHong Zhang       aa  = a->a + ii[i];
154697952fefSHong Zhang       sum = y[*ridx];
1547f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
154897952fefSHong Zhang       z[*ridx++] = sum;
154997952fefSHong Zhang     }
155097952fefSHong Zhang   } else { /* do not use compressed row format */
15513d3eaba7SBarry Smith     ii = a->i;
1552f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
15533d3eaba7SBarry Smith     aj = a->j;
15543d3eaba7SBarry Smith     aa = a->a;
1555f15663dcSBarry Smith     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1556f15663dcSBarry Smith #else
155717ab2063SBarry Smith     for (i=0; i<m; i++) {
1558f15663dcSBarry Smith       n   = ii[i+1] - ii[i];
1559f15663dcSBarry Smith       aj  = a->j + ii[i];
1560f15663dcSBarry Smith       aa  = a->a + ii[i];
156117ab2063SBarry Smith       sum = y[i];
1562f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
156317ab2063SBarry Smith       z[i] = sum;
156417ab2063SBarry Smith     }
156502ab625aSSatish Balay #endif
1566f15663dcSBarry Smith   }
1567dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1568f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
1569d9ca1df4SBarry Smith   ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr);
15703a40ed3dSBarry Smith   PetscFunctionReturn(0);
157117ab2063SBarry Smith }
157217ab2063SBarry Smith 
157317ab2063SBarry Smith /*
157417ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
157517ab2063SBarry Smith */
1576dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
157717ab2063SBarry Smith {
1578416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15796849ba73SBarry Smith   PetscErrorCode ierr;
1580d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
158117ab2063SBarry Smith 
15823a40ed3dSBarry Smith   PetscFunctionBegin;
158309f38230SBarry Smith   if (!a->diag) {
1584785e854fSJed Brown     ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr);
15853bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr);
158609f38230SBarry Smith   }
1587d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
158809f38230SBarry Smith     a->diag[i] = a->i[i+1];
1589bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1590bfeeae90SHong Zhang       if (a->j[j] == i) {
159109f38230SBarry Smith         a->diag[i] = j;
159217ab2063SBarry Smith         break;
159317ab2063SBarry Smith       }
159417ab2063SBarry Smith     }
159517ab2063SBarry Smith   }
15963a40ed3dSBarry Smith   PetscFunctionReturn(0);
159717ab2063SBarry Smith }
159817ab2063SBarry Smith 
159961ecd0c6SBarry Smith PetscErrorCode MatShift_SeqAIJ(Mat A,PetscScalar v)
160061ecd0c6SBarry Smith {
160161ecd0c6SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
160261ecd0c6SBarry Smith   const PetscInt    *diag = (const PetscInt*)a->diag;
160361ecd0c6SBarry Smith   const PetscInt    *ii = (const PetscInt*) a->i;
160461ecd0c6SBarry Smith   PetscInt          i,*mdiag = NULL;
160561ecd0c6SBarry Smith   PetscErrorCode    ierr;
160661ecd0c6SBarry Smith   PetscInt          cnt = 0; /* how many diagonals are missing */
160761ecd0c6SBarry Smith 
160861ecd0c6SBarry Smith   PetscFunctionBegin;
160961ecd0c6SBarry Smith   if (!A->preallocated || !a->nz) {
161061ecd0c6SBarry Smith     ierr = MatSeqAIJSetPreallocation(A,1,NULL);CHKERRQ(ierr);
161161ecd0c6SBarry Smith     ierr = MatShift_Basic(A,v);CHKERRQ(ierr);
161261ecd0c6SBarry Smith     PetscFunctionReturn(0);
161361ecd0c6SBarry Smith   }
161461ecd0c6SBarry Smith 
161561ecd0c6SBarry Smith   if (a->diagonaldense) {
161661ecd0c6SBarry Smith     cnt = 0;
161761ecd0c6SBarry Smith   } else {
161861ecd0c6SBarry Smith     ierr = PetscCalloc1(A->rmap->n,&mdiag);CHKERRQ(ierr);
161961ecd0c6SBarry Smith     for (i=0; i<A->rmap->n; i++) {
162061ecd0c6SBarry Smith       if (diag[i] >= ii[i+1]) {
162161ecd0c6SBarry Smith         cnt++;
162261ecd0c6SBarry Smith         mdiag[i] = 1;
162361ecd0c6SBarry Smith       }
162461ecd0c6SBarry Smith     }
162561ecd0c6SBarry Smith   }
162661ecd0c6SBarry Smith   if (!cnt) {
162761ecd0c6SBarry Smith     ierr = MatShift_Basic(A,v);CHKERRQ(ierr);
162861ecd0c6SBarry Smith   } else {
1629b6f2aa54SBarry Smith     PetscScalar *olda = a->a;  /* preserve pointers to current matrix nonzeros structure and values */
1630b6f2aa54SBarry Smith     PetscInt    *oldj = a->j, *oldi = a->i;
163161ecd0c6SBarry Smith     PetscBool   singlemalloc = a->singlemalloc,free_a = a->free_a,free_ij = a->free_ij;
163261ecd0c6SBarry Smith 
163361ecd0c6SBarry Smith     a->a = NULL;
163461ecd0c6SBarry Smith     a->j = NULL;
163561ecd0c6SBarry Smith     a->i = NULL;
163661ecd0c6SBarry Smith     /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
163761ecd0c6SBarry Smith     for (i=0; i<A->rmap->n; i++) {
163861ecd0c6SBarry Smith       a->imax[i] += mdiag[i];
1639447d62f5SStefano Zampini       a->imax[i] = PetscMin(a->imax[i],A->cmap->n);
164061ecd0c6SBarry Smith     }
164161ecd0c6SBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,0,a->imax);CHKERRQ(ierr);
164261ecd0c6SBarry Smith 
164361ecd0c6SBarry Smith     /* copy old values into new matrix data structure */
164461ecd0c6SBarry Smith     for (i=0; i<A->rmap->n; i++) {
164561ecd0c6SBarry Smith       ierr = MatSetValues(A,1,&i,a->imax[i] - mdiag[i],&oldj[oldi[i]],&olda[oldi[i]],ADD_VALUES);CHKERRQ(ierr);
1646447d62f5SStefano Zampini       if (i < A->cmap->n) {
164761ecd0c6SBarry Smith         ierr = MatSetValue(A,i,i,v,ADD_VALUES);CHKERRQ(ierr);
164861ecd0c6SBarry Smith       }
1649447d62f5SStefano Zampini     }
165061ecd0c6SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
165161ecd0c6SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
165261ecd0c6SBarry Smith     if (singlemalloc) {
165361ecd0c6SBarry Smith       ierr = PetscFree3(olda,oldj,oldi);CHKERRQ(ierr);
165461ecd0c6SBarry Smith     } else {
165561ecd0c6SBarry Smith       if (free_a)  {ierr = PetscFree(olda);CHKERRQ(ierr);}
165661ecd0c6SBarry Smith       if (free_ij) {ierr = PetscFree(oldj);CHKERRQ(ierr);}
165761ecd0c6SBarry Smith       if (free_ij) {ierr = PetscFree(oldi);CHKERRQ(ierr);}
165861ecd0c6SBarry Smith     }
165961ecd0c6SBarry Smith   }
166061ecd0c6SBarry Smith   ierr = PetscFree(mdiag);CHKERRQ(ierr);
166161ecd0c6SBarry Smith   a->diagonaldense = PETSC_TRUE;
166261ecd0c6SBarry Smith   PetscFunctionReturn(0);
166361ecd0c6SBarry Smith }
166461ecd0c6SBarry Smith 
1665be5855fcSBarry Smith /*
1666be5855fcSBarry Smith      Checks for missing diagonals
1667be5855fcSBarry Smith */
1668ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1669be5855fcSBarry Smith {
1670be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
16717734d3b5SMatthew G. Knepley   PetscInt       *diag,*ii = a->i,i;
1672994fe344SLisandro Dalcin   PetscErrorCode ierr;
1673be5855fcSBarry Smith 
1674be5855fcSBarry Smith   PetscFunctionBegin;
167509f38230SBarry Smith   *missing = PETSC_FALSE;
16767734d3b5SMatthew G. Knepley   if (A->rmap->n > 0 && !ii) {
167709f38230SBarry Smith     *missing = PETSC_TRUE;
167809f38230SBarry Smith     if (d) *d = 0;
1679994fe344SLisandro Dalcin     ierr = PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");CHKERRQ(ierr);
168009f38230SBarry Smith   } else {
168101445905SHong Zhang     PetscInt n;
168201445905SHong Zhang     n = PetscMin(A->rmap->n, A->cmap->n);
1683f1e2ffcdSBarry Smith     diag = a->diag;
168401445905SHong Zhang     for (i=0; i<n; i++) {
16857734d3b5SMatthew G. Knepley       if (diag[i] >= ii[i+1]) {
168609f38230SBarry Smith         *missing = PETSC_TRUE;
168709f38230SBarry Smith         if (d) *d = i;
1688994fe344SLisandro Dalcin         ierr = PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);CHKERRQ(ierr);
1689358d2f5dSShri Abhyankar         break;
169009f38230SBarry Smith       }
1691be5855fcSBarry Smith     }
1692be5855fcSBarry Smith   }
1693be5855fcSBarry Smith   PetscFunctionReturn(0);
1694be5855fcSBarry Smith }
1695be5855fcSBarry Smith 
16960da83c2eSBarry Smith #include <petscblaslapack.h>
16970da83c2eSBarry Smith #include <petsc/private/kernels/blockinvert.h>
16980da83c2eSBarry Smith 
16990da83c2eSBarry Smith /*
17000da83c2eSBarry Smith     Note that values is allocated externally by the PC and then passed into this routine
17010da83c2eSBarry Smith */
17020da83c2eSBarry Smith PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag)
17030da83c2eSBarry Smith {
17040da83c2eSBarry Smith   PetscErrorCode  ierr;
17050da83c2eSBarry Smith   PetscInt        n = A->rmap->n, i, ncnt = 0, *indx,j,bsizemax = 0,*v_pivots;
17060da83c2eSBarry Smith   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;
17070da83c2eSBarry Smith   const PetscReal shift = 0.0;
17080da83c2eSBarry Smith   PetscInt        ipvt[5];
17090da83c2eSBarry Smith   PetscScalar     work[25],*v_work;
17100da83c2eSBarry Smith 
17110da83c2eSBarry Smith   PetscFunctionBegin;
17120da83c2eSBarry Smith   allowzeropivot = PetscNot(A->erroriffailure);
17130da83c2eSBarry Smith   for (i=0; i<nblocks; i++) ncnt += bsizes[i];
17140da83c2eSBarry Smith   if (ncnt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Total blocksizes %D doesn't match number matrix rows %D",ncnt,n);
17150da83c2eSBarry Smith   for (i=0; i<nblocks; i++) {
17160da83c2eSBarry Smith     bsizemax = PetscMax(bsizemax,bsizes[i]);
17170da83c2eSBarry Smith   }
17180da83c2eSBarry Smith   ierr = PetscMalloc1(bsizemax,&indx);CHKERRQ(ierr);
17190da83c2eSBarry Smith   if (bsizemax > 7) {
17200da83c2eSBarry Smith     ierr = PetscMalloc2(bsizemax,&v_work,bsizemax,&v_pivots);CHKERRQ(ierr);
17210da83c2eSBarry Smith   }
17220da83c2eSBarry Smith   ncnt = 0;
17230da83c2eSBarry Smith   for (i=0; i<nblocks; i++) {
17240da83c2eSBarry Smith     for (j=0; j<bsizes[i]; j++) indx[j] = ncnt+j;
17250da83c2eSBarry Smith     ierr    = MatGetValues(A,bsizes[i],indx,bsizes[i],indx,diag);CHKERRQ(ierr);
17260da83c2eSBarry Smith     switch (bsizes[i]) {
17270da83c2eSBarry Smith     case 1:
17280da83c2eSBarry Smith       *diag = 1.0/(*diag);
17290da83c2eSBarry Smith       break;
17300da83c2eSBarry Smith     case 2:
17310da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
17320da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
17330da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
17340da83c2eSBarry Smith       break;
17350da83c2eSBarry Smith     case 3:
17360da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
17370da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
17380da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
17390da83c2eSBarry Smith       break;
17400da83c2eSBarry Smith     case 4:
17410da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
17420da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
17430da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
17440da83c2eSBarry Smith       break;
17450da83c2eSBarry Smith     case 5:
17460da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
17470da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
17480da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
17490da83c2eSBarry Smith       break;
17500da83c2eSBarry Smith     case 6:
17510da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
17520da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
17530da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
17540da83c2eSBarry Smith       break;
17550da83c2eSBarry Smith     case 7:
17560da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
17570da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
17580da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
17590da83c2eSBarry Smith       break;
17600da83c2eSBarry Smith     default:
17610da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_inverse_A(bsizes[i],diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
17620da83c2eSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
17630da83c2eSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bsizes[i]);CHKERRQ(ierr);
17640da83c2eSBarry Smith     }
17650da83c2eSBarry Smith     ncnt   += bsizes[i];
17660da83c2eSBarry Smith     diag += bsizes[i]*bsizes[i];
17670da83c2eSBarry Smith   }
17680da83c2eSBarry Smith   if (bsizemax > 7) {
17690da83c2eSBarry Smith     ierr = PetscFree2(v_work,v_pivots);CHKERRQ(ierr);
17700da83c2eSBarry Smith   }
17710da83c2eSBarry Smith   ierr = PetscFree(indx);CHKERRQ(ierr);
17720da83c2eSBarry Smith   PetscFunctionReturn(0);
17730da83c2eSBarry Smith }
17740da83c2eSBarry Smith 
1775422a814eSBarry Smith /*
1776422a814eSBarry Smith    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1777422a814eSBarry Smith */
17787087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
177971f1c65dSBarry Smith {
178071f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
178171f1c65dSBarry Smith   PetscErrorCode ierr;
1782d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
178354f21887SBarry Smith   MatScalar      *v = a->a;
178454f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
178571f1c65dSBarry Smith 
178671f1c65dSBarry Smith   PetscFunctionBegin;
178771f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
178871f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
178971f1c65dSBarry Smith   diag = a->diag;
179071f1c65dSBarry Smith   if (!a->idiag) {
1791dcca6d9dSJed Brown     ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr);
17923bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
179371f1c65dSBarry Smith     v    = a->a;
179471f1c65dSBarry Smith   }
179571f1c65dSBarry Smith   mdiag = a->mdiag;
179671f1c65dSBarry Smith   idiag = a->idiag;
179771f1c65dSBarry Smith 
1798422a814eSBarry Smith   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
179971f1c65dSBarry Smith     for (i=0; i<m; i++) {
180071f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1801899639b0SHong Zhang       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1802899639b0SHong Zhang         if (PetscRealPart(fshift)) {
1803899639b0SHong Zhang           ierr = PetscInfo1(A,"Zero diagonal on row %D\n",i);CHKERRQ(ierr);
18047b6c816cSBarry Smith           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
18057b6c816cSBarry Smith           A->factorerror_zeropivot_value = 0.0;
18067b6c816cSBarry Smith           A->factorerror_zeropivot_row   = i;
1807a6fa060aSHong Zhang         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1808899639b0SHong Zhang       }
180971f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
181071f1c65dSBarry Smith     }
181171f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
181271f1c65dSBarry Smith   } else {
181371f1c65dSBarry Smith     for (i=0; i<m; i++) {
181471f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
181571f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
181671f1c65dSBarry Smith     }
1817dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
181871f1c65dSBarry Smith   }
181971f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
182071f1c65dSBarry Smith   PetscFunctionReturn(0);
182171f1c65dSBarry Smith }
182271f1c65dSBarry Smith 
1823c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
182441f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
182517ab2063SBarry Smith {
1826416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1827e6d1f457SBarry Smith   PetscScalar       *x,d,sum,*t,scale;
18283d3eaba7SBarry Smith   const MatScalar   *v,*idiag=0,*mdiag;
182954f21887SBarry Smith   const PetscScalar *b, *bs,*xb, *ts;
1830dfbe8321SBarry Smith   PetscErrorCode    ierr;
18313d3eaba7SBarry Smith   PetscInt          n,m = A->rmap->n,i;
183297f1f81fSBarry Smith   const PetscInt    *idx,*diag;
183317ab2063SBarry Smith 
18343a40ed3dSBarry Smith   PetscFunctionBegin;
1835b965ef7fSBarry Smith   its = its*lits;
183691723122SBarry Smith 
183771f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
183871f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
183971f1c65dSBarry Smith   a->fshift = fshift;
184071f1c65dSBarry Smith   a->omega  = omega;
1841ed480e8bSBarry Smith 
184271f1c65dSBarry Smith   diag  = a->diag;
184371f1c65dSBarry Smith   t     = a->ssor_work;
1844ed480e8bSBarry Smith   idiag = a->idiag;
184571f1c65dSBarry Smith   mdiag = a->mdiag;
1846ed480e8bSBarry Smith 
18471ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
18483649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1849ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
185017ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
185117ab2063SBarry Smith     /* apply (U + D/omega) to the vector */
1852ed480e8bSBarry Smith     bs = b;
185317ab2063SBarry Smith     for (i=0; i<m; i++) {
185471f1c65dSBarry Smith       d   = fshift + mdiag[i];
1855416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1856ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1857ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
185817ab2063SBarry Smith       sum = b[i]*d/omega;
1859003131ecSBarry Smith       PetscSparseDensePlusDot(sum,bs,v,idx,n);
186017ab2063SBarry Smith       x[i] = sum;
186117ab2063SBarry Smith     }
18621ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
18633649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1864efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18653a40ed3dSBarry Smith     PetscFunctionReturn(0);
186617ab2063SBarry Smith   }
1867c783ea89SBarry Smith 
18682205254eSKarl Rupp   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
18692205254eSKarl Rupp   else if (flag & SOR_EISENSTAT) {
18704c500f23SPierre Jolivet     /* Let  A = L + U + D; where L is lower triangular,
1871887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
187217ab2063SBarry Smith 
187317ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
187417ab2063SBarry Smith 
1875887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
187617ab2063SBarry Smith     */
187717ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
187817ab2063SBarry Smith 
187917ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
188017ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1881416022c9SBarry Smith       n   = a->i[i+1] - diag[i] - 1;
1882ed480e8bSBarry Smith       idx = a->j + diag[i] + 1;
1883ed480e8bSBarry Smith       v   = a->a + diag[i] + 1;
188417ab2063SBarry Smith       sum = b[i];
1885e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1886ed480e8bSBarry Smith       x[i] = sum*idiag[i];
188717ab2063SBarry Smith     }
188817ab2063SBarry Smith 
188917ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1890416022c9SBarry Smith     v = a->a;
18912205254eSKarl Rupp     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
189217ab2063SBarry Smith 
189317ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1894ed480e8bSBarry Smith     ts   = t;
1895416022c9SBarry Smith     diag = a->diag;
189617ab2063SBarry Smith     for (i=0; i<m; i++) {
1897416022c9SBarry Smith       n   = diag[i] - a->i[i];
1898ed480e8bSBarry Smith       idx = a->j + a->i[i];
1899ed480e8bSBarry Smith       v   = a->a + a->i[i];
190017ab2063SBarry Smith       sum = t[i];
1901003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1902ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1903733d66baSBarry Smith       /*  x = x + t */
1904733d66baSBarry Smith       x[i] += t[i];
190517ab2063SBarry Smith     }
190617ab2063SBarry Smith 
1907dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
19081ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
19093649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
19103a40ed3dSBarry Smith     PetscFunctionReturn(0);
191117ab2063SBarry Smith   }
191217ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
191317ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
191417ab2063SBarry Smith       for (i=0; i<m; i++) {
1915416022c9SBarry Smith         n   = diag[i] - a->i[i];
1916ed480e8bSBarry Smith         idx = a->j + a->i[i];
1917ed480e8bSBarry Smith         v   = a->a + a->i[i];
191817ab2063SBarry Smith         sum = b[i];
1919e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
19205c99c7daSBarry Smith         t[i] = sum;
1921ed480e8bSBarry Smith         x[i] = sum*idiag[i];
192217ab2063SBarry Smith       }
19235c99c7daSBarry Smith       xb   = t;
1924efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
19253a40ed3dSBarry Smith     } else xb = b;
192617ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
192717ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1928416022c9SBarry Smith         n   = a->i[i+1] - diag[i] - 1;
1929ed480e8bSBarry Smith         idx = a->j + diag[i] + 1;
1930ed480e8bSBarry Smith         v   = a->a + diag[i] + 1;
193117ab2063SBarry Smith         sum = xb[i];
1932e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
19335c99c7daSBarry Smith         if (xb == b) {
1934ed480e8bSBarry Smith           x[i] = sum*idiag[i];
19355c99c7daSBarry Smith         } else {
1936b19a5dc2SMark Adams           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
193717ab2063SBarry Smith         }
19385c99c7daSBarry Smith       }
1939b19a5dc2SMark Adams       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
194017ab2063SBarry Smith     }
194117ab2063SBarry Smith     its--;
194217ab2063SBarry Smith   }
194317ab2063SBarry Smith   while (its--) {
194417ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
194517ab2063SBarry Smith       for (i=0; i<m; i++) {
1946b19a5dc2SMark Adams         /* lower */
1947b19a5dc2SMark Adams         n   = diag[i] - a->i[i];
1948ed480e8bSBarry Smith         idx = a->j + a->i[i];
1949ed480e8bSBarry Smith         v   = a->a + a->i[i];
195017ab2063SBarry Smith         sum = b[i];
1951e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1952b19a5dc2SMark Adams         t[i] = sum;             /* save application of the lower-triangular part */
1953b19a5dc2SMark Adams         /* upper */
1954b19a5dc2SMark Adams         n   = a->i[i+1] - diag[i] - 1;
1955b19a5dc2SMark Adams         idx = a->j + diag[i] + 1;
1956b19a5dc2SMark Adams         v   = a->a + diag[i] + 1;
1957b19a5dc2SMark Adams         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1958b19a5dc2SMark Adams         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
195917ab2063SBarry Smith       }
1960b19a5dc2SMark Adams       xb   = t;
19619f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1962b19a5dc2SMark Adams     } else xb = b;
196317ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
196417ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1965b19a5dc2SMark Adams         sum = xb[i];
1966b19a5dc2SMark Adams         if (xb == b) {
1967b19a5dc2SMark Adams           /* whole matrix (no checkpointing available) */
1968416022c9SBarry Smith           n   = a->i[i+1] - a->i[i];
1969ed480e8bSBarry Smith           idx = a->j + a->i[i];
1970ed480e8bSBarry Smith           v   = a->a + a->i[i];
1971e6d1f457SBarry Smith           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1972ed480e8bSBarry Smith           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1973b19a5dc2SMark Adams         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1974b19a5dc2SMark Adams           n   = a->i[i+1] - diag[i] - 1;
1975b19a5dc2SMark Adams           idx = a->j + diag[i] + 1;
1976b19a5dc2SMark Adams           v   = a->a + diag[i] + 1;
1977b19a5dc2SMark Adams           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1978b19a5dc2SMark Adams           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
197917ab2063SBarry Smith         }
1980b19a5dc2SMark Adams       }
1981b19a5dc2SMark Adams       if (xb == b) {
19829f863219SBarry Smith         ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1983b19a5dc2SMark Adams       } else {
1984b19a5dc2SMark Adams         ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */
1985b19a5dc2SMark Adams       }
198617ab2063SBarry Smith     }
198717ab2063SBarry Smith   }
19881ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
19893649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1990365a8a9eSBarry Smith   PetscFunctionReturn(0);
199117ab2063SBarry Smith }
199217ab2063SBarry Smith 
19932af78befSBarry Smith 
1994dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
199517ab2063SBarry Smith {
1996416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
19974e220ebcSLois Curfman McInnes 
19983a40ed3dSBarry Smith   PetscFunctionBegin;
19994e220ebcSLois Curfman McInnes   info->block_size   = 1.0;
20003966268fSBarry Smith   info->nz_allocated = a->maxnz;
20013966268fSBarry Smith   info->nz_used      = a->nz;
20023966268fSBarry Smith   info->nz_unneeded  = (a->maxnz - a->nz);
20033966268fSBarry Smith   info->assemblies   = A->num_ass;
20043966268fSBarry Smith   info->mallocs      = A->info.mallocs;
20057adad957SLisandro Dalcin   info->memory       = ((PetscObject)A)->mem;
2006d5f3da31SBarry Smith   if (A->factortype) {
20074e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
20084e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
20094e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
20104e220ebcSLois Curfman McInnes   } else {
20114e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
20124e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
20134e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
20144e220ebcSLois Curfman McInnes   }
20153a40ed3dSBarry Smith   PetscFunctionReturn(0);
201617ab2063SBarry Smith }
201717ab2063SBarry Smith 
20182b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
201917ab2063SBarry Smith {
2020416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2021c7da8527SEric Chamberland   PetscInt          i,m = A->rmap->n - 1;
20226849ba73SBarry Smith   PetscErrorCode    ierr;
202397b48c8fSBarry Smith   const PetscScalar *xx;
202497b48c8fSBarry Smith   PetscScalar       *bb;
2025c7da8527SEric Chamberland   PetscInt          d = 0;
202617ab2063SBarry Smith 
20273a40ed3dSBarry Smith   PetscFunctionBegin;
202897b48c8fSBarry Smith   if (x && b) {
202997b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
203097b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
203197b48c8fSBarry Smith     for (i=0; i<N; i++) {
203297b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2033447d62f5SStefano Zampini       if (rows[i] >= A->cmap->n) continue;
203497b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
203597b48c8fSBarry Smith     }
203697b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
203797b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
203897b48c8fSBarry Smith   }
203997b48c8fSBarry Smith 
2040a9817697SBarry Smith   if (a->keepnonzeropattern) {
2041f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
2042e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2043580bdb30SBarry Smith       ierr = PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);CHKERRQ(ierr);
2044f1e2ffcdSBarry Smith     }
2045f4df32b1SMatthew Knepley     if (diag != 0.0) {
2046c7da8527SEric Chamberland       for (i=0; i<N; i++) {
2047c7da8527SEric Chamberland         d = rows[i];
2048447d62f5SStefano Zampini         if (rows[i] >= A->cmap->n) continue;
2049c7da8527SEric 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);
2050c7da8527SEric Chamberland       }
2051f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
2052447d62f5SStefano Zampini         if (rows[i] >= A->cmap->n) continue;
2053f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
2054f1e2ffcdSBarry Smith       }
2055f1e2ffcdSBarry Smith     }
2056f1e2ffcdSBarry Smith   } else {
2057f4df32b1SMatthew Knepley     if (diag != 0.0) {
205817ab2063SBarry Smith       for (i=0; i<N; i++) {
2059e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
20607ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
2061447d62f5SStefano Zampini 	  if (rows[i] >= A->cmap->n) {
2062447d62f5SStefano Zampini             a->ilen[rows[i]] = 0;
2063447d62f5SStefano Zampini           } else {
2064416022c9SBarry Smith             a->ilen[rows[i]]    = 1;
2065f4df32b1SMatthew Knepley             a->a[a->i[rows[i]]] = diag;
2066bfeeae90SHong Zhang             a->j[a->i[rows[i]]] = rows[i];
2067447d62f5SStefano Zampini           }
2068447d62f5SStefano Zampini         } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
2069f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
207017ab2063SBarry Smith         }
207117ab2063SBarry Smith       }
20723a40ed3dSBarry Smith     } else {
207317ab2063SBarry Smith       for (i=0; i<N; i++) {
2074e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2075416022c9SBarry Smith         a->ilen[rows[i]] = 0;
207617ab2063SBarry Smith       }
207717ab2063SBarry Smith     }
2078e56f5c9eSBarry Smith     A->nonzerostate++;
2079f1e2ffcdSBarry Smith   }
2080e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2081c70f7ee4SJunchao Zhang   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2082e2cf4d64SStefano Zampini #endif
20834099cc6bSBarry Smith   ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20843a40ed3dSBarry Smith   PetscFunctionReturn(0);
208517ab2063SBarry Smith }
208617ab2063SBarry Smith 
20876e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
20886e169961SBarry Smith {
20896e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
20906e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
20916e169961SBarry Smith   PetscErrorCode    ierr;
20922b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
20936e169961SBarry Smith   const PetscScalar *xx;
20946e169961SBarry Smith   PetscScalar       *bb;
20956e169961SBarry Smith 
20966e169961SBarry Smith   PetscFunctionBegin;
20976e169961SBarry Smith   if (x && b) {
20986e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
20996e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
21002b40b63fSBarry Smith     vecs = PETSC_TRUE;
21016e169961SBarry Smith   }
21021795a4d1SJed Brown   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
21036e169961SBarry Smith   for (i=0; i<N; i++) {
21046e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2105580bdb30SBarry Smith     ierr = PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);CHKERRQ(ierr);
21062205254eSKarl Rupp 
21076e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
21086e169961SBarry Smith   }
21096e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
21106e169961SBarry Smith     if (!zeroed[i]) {
21116e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
21124cf107fdSStefano Zampini         if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
21132b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
21146e169961SBarry Smith           a->a[j] = 0.0;
21156e169961SBarry Smith         }
21166e169961SBarry Smith       }
21174cf107fdSStefano Zampini     } else if (vecs && i < A->cmap->N) bb[i] = diag*xx[i];
21186e169961SBarry Smith   }
21196e169961SBarry Smith   if (x && b) {
21206e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
21216e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
21226e169961SBarry Smith   }
21236e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
21246e169961SBarry Smith   if (diag != 0.0) {
21256e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
21261d5a398dSstefano_zampini     if (missing) {
21271d5a398dSstefano_zampini       for (i=0; i<N; i++) {
21284cf107fdSStefano Zampini         if (rows[i] >= A->cmap->N) continue;
21294cf107fdSStefano 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]);
21301d5a398dSstefano_zampini         ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
21311d5a398dSstefano_zampini       }
21321d5a398dSstefano_zampini     } else {
21336e169961SBarry Smith       for (i=0; i<N; i++) {
21346e169961SBarry Smith         a->a[a->diag[rows[i]]] = diag;
21356e169961SBarry Smith       }
21366e169961SBarry Smith     }
21371d5a398dSstefano_zampini   }
2138e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2139c70f7ee4SJunchao Zhang   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2140e2cf4d64SStefano Zampini #endif
21414099cc6bSBarry Smith   ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21426e169961SBarry Smith   PetscFunctionReturn(0);
21436e169961SBarry Smith }
21446e169961SBarry Smith 
2145a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
214617ab2063SBarry Smith {
2147416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
214897f1f81fSBarry Smith   PetscInt   *itmp;
214917ab2063SBarry Smith 
21503a40ed3dSBarry Smith   PetscFunctionBegin;
2151e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
215217ab2063SBarry Smith 
2153416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
2154bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
215517ab2063SBarry Smith   if (idx) {
2156bfeeae90SHong Zhang     itmp = a->j + a->i[row];
215726fbe8dcSKarl Rupp     if (*nz) *idx = itmp;
215817ab2063SBarry Smith     else *idx = 0;
215917ab2063SBarry Smith   }
21603a40ed3dSBarry Smith   PetscFunctionReturn(0);
216117ab2063SBarry Smith }
216217ab2063SBarry Smith 
2163bfeeae90SHong Zhang /* remove this function? */
2164a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
216517ab2063SBarry Smith {
21663a40ed3dSBarry Smith   PetscFunctionBegin;
21673a40ed3dSBarry Smith   PetscFunctionReturn(0);
216817ab2063SBarry Smith }
216917ab2063SBarry Smith 
2170dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
217117ab2063SBarry Smith {
2172416022c9SBarry Smith   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
217354f21887SBarry Smith   MatScalar      *v  = a->a;
217436db0b34SBarry Smith   PetscReal      sum = 0.0;
21756849ba73SBarry Smith   PetscErrorCode ierr;
217697f1f81fSBarry Smith   PetscInt       i,j;
217717ab2063SBarry Smith 
21783a40ed3dSBarry Smith   PetscFunctionBegin;
217917ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
2180570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FP16)
2181570b7f6dSBarry Smith     PetscBLASInt one = 1,nz = a->nz;
2182570b7f6dSBarry Smith     *nrm = BLASnrm2_(&nz,v,&one);
2183570b7f6dSBarry Smith #else
2184416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
218536db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
218617ab2063SBarry Smith     }
21878f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
2188570b7f6dSBarry Smith #endif
218951f70360SJed Brown     ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
21903a40ed3dSBarry Smith   } else if (type == NORM_1) {
219136db0b34SBarry Smith     PetscReal *tmp;
219297f1f81fSBarry Smith     PetscInt  *jj = a->j;
21931795a4d1SJed Brown     ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr);
2194064f8208SBarry Smith     *nrm = 0.0;
2195416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
2196bfeeae90SHong Zhang       tmp[*jj++] += PetscAbsScalar(*v);  v++;
219717ab2063SBarry Smith     }
2198d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
2199064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
220017ab2063SBarry Smith     }
2201606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
220251f70360SJed Brown     ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr);
22033a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
2204064f8208SBarry Smith     *nrm = 0.0;
2205d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
2206bfeeae90SHong Zhang       v   = a->a + a->i[j];
220717ab2063SBarry Smith       sum = 0.0;
2208416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2209cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
221017ab2063SBarry Smith       }
2211064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
221217ab2063SBarry Smith     }
221351f70360SJed Brown     ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr);
2214f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
22153a40ed3dSBarry Smith   PetscFunctionReturn(0);
221617ab2063SBarry Smith }
221717ab2063SBarry Smith 
22184e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
22194e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
22204e938277SHong Zhang {
22214e938277SHong Zhang   PetscErrorCode ierr;
22224e938277SHong Zhang   PetscInt       i,j,anzj;
22234e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
22244e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
22254e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
22264e938277SHong Zhang 
22274e938277SHong Zhang   PetscFunctionBegin;
22284e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
2229854ce69bSBarry Smith   ierr = PetscCalloc1(an+1,&ati);CHKERRQ(ierr);
2230785e854fSJed Brown   ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr);
2231785e854fSJed Brown   ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr);
22324e938277SHong Zhang 
22334e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
22344e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
223526fbe8dcSKarl Rupp   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
22364e938277SHong Zhang   /* Form ati for csr format of A^T. */
223726fbe8dcSKarl Rupp   for (i=0;i<an;i++) ati[i+1] += ati[i];
22384e938277SHong Zhang 
22394e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
2240580bdb30SBarry Smith   ierr = PetscArraycpy(atfill,ati,an);CHKERRQ(ierr);
22414e938277SHong Zhang 
22424e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
22434e938277SHong Zhang   for (i=0;i<am;i++) {
22444e938277SHong Zhang     anzj = ai[i+1] - ai[i];
22454e938277SHong Zhang     for (j=0;j<anzj;j++) {
22464e938277SHong Zhang       atj[atfill[*aj]] = i;
22474e938277SHong Zhang       atfill[*aj++]   += 1;
22484e938277SHong Zhang     }
22494e938277SHong Zhang   }
22504e938277SHong Zhang 
22514e938277SHong Zhang   /* Clean up temporary space and complete requests. */
22524e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
2253ce94432eSBarry Smith   ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr);
225433d57670SJed Brown   ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
2255b5bb3eecSMark Adams   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2256a2f3521dSMark F. Adams 
22574e938277SHong Zhang   b          = (Mat_SeqAIJ*)((*B)->data);
22584e938277SHong Zhang   b->free_a  = PETSC_FALSE;
22594e938277SHong Zhang   b->free_ij = PETSC_TRUE;
22604e938277SHong Zhang   b->nonew   = 0;
22614e938277SHong Zhang   PetscFunctionReturn(0);
22624e938277SHong Zhang }
22634e938277SHong Zhang 
22647087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2265cd0d46ebSvictorle {
22663d3eaba7SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
226754f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
226854f21887SBarry Smith   MatScalar      *va,*vb;
22696849ba73SBarry Smith   PetscErrorCode ierr;
227097f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
2271cd0d46ebSvictorle 
2272cd0d46ebSvictorle   PetscFunctionBegin;
2273cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
2274cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
22755485867bSBarry Smith   if (ma!=nb || na!=mb) {
22765485867bSBarry Smith     *f = PETSC_FALSE;
22775485867bSBarry Smith     PetscFunctionReturn(0);
22785485867bSBarry Smith   }
2279cd0d46ebSvictorle   aii  = aij->i; bii = bij->i;
2280cd0d46ebSvictorle   adx  = aij->j; bdx = bij->j;
2281cd0d46ebSvictorle   va   = aij->a; vb = bij->a;
2282785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2283785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
2284cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
2285cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
2286cd0d46ebSvictorle 
2287cd0d46ebSvictorle   *f = PETSC_TRUE;
2288cd0d46ebSvictorle   for (i=0; i<ma; i++) {
2289cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
229097f1f81fSBarry Smith       PetscInt    idc,idr;
22915485867bSBarry Smith       PetscScalar vc,vr;
2292cd0d46ebSvictorle       /* column/row index/value */
22935485867bSBarry Smith       idc = adx[aptr[i]];
22945485867bSBarry Smith       idr = bdx[bptr[idc]];
22955485867bSBarry Smith       vc  = va[aptr[i]];
22965485867bSBarry Smith       vr  = vb[bptr[idc]];
22975485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
22985485867bSBarry Smith         *f = PETSC_FALSE;
22995485867bSBarry Smith         goto done;
2300cd0d46ebSvictorle       } else {
23015485867bSBarry Smith         aptr[i]++;
23025485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
2303cd0d46ebSvictorle       }
2304cd0d46ebSvictorle     }
2305cd0d46ebSvictorle   }
2306cd0d46ebSvictorle done:
2307cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
23083aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
2309cd0d46ebSvictorle   PetscFunctionReturn(0);
2310cd0d46ebSvictorle }
2311cd0d46ebSvictorle 
23127087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
23131cbb95d3SBarry Smith {
23143d3eaba7SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
231554f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
231654f21887SBarry Smith   MatScalar      *va,*vb;
23171cbb95d3SBarry Smith   PetscErrorCode ierr;
23181cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
23191cbb95d3SBarry Smith 
23201cbb95d3SBarry Smith   PetscFunctionBegin;
23211cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
23221cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
23231cbb95d3SBarry Smith   if (ma!=nb || na!=mb) {
23241cbb95d3SBarry Smith     *f = PETSC_FALSE;
23251cbb95d3SBarry Smith     PetscFunctionReturn(0);
23261cbb95d3SBarry Smith   }
23271cbb95d3SBarry Smith   aii  = aij->i; bii = bij->i;
23281cbb95d3SBarry Smith   adx  = aij->j; bdx = bij->j;
23291cbb95d3SBarry Smith   va   = aij->a; vb = bij->a;
2330785e854fSJed Brown   ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr);
2331785e854fSJed Brown   ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr);
23321cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
23331cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
23341cbb95d3SBarry Smith 
23351cbb95d3SBarry Smith   *f = PETSC_TRUE;
23361cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
23371cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
23381cbb95d3SBarry Smith       PetscInt    idc,idr;
23391cbb95d3SBarry Smith       PetscScalar vc,vr;
23401cbb95d3SBarry Smith       /* column/row index/value */
23411cbb95d3SBarry Smith       idc = adx[aptr[i]];
23421cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
23431cbb95d3SBarry Smith       vc  = va[aptr[i]];
23441cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
23451cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
23461cbb95d3SBarry Smith         *f = PETSC_FALSE;
23471cbb95d3SBarry Smith         goto done;
23481cbb95d3SBarry Smith       } else {
23491cbb95d3SBarry Smith         aptr[i]++;
23501cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
23511cbb95d3SBarry Smith       }
23521cbb95d3SBarry Smith     }
23531cbb95d3SBarry Smith   }
23541cbb95d3SBarry Smith done:
23551cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
23561cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
23571cbb95d3SBarry Smith   PetscFunctionReturn(0);
23581cbb95d3SBarry Smith }
23591cbb95d3SBarry Smith 
2360ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
23619e29f15eSvictorle {
2362dfbe8321SBarry Smith   PetscErrorCode ierr;
23636e111a19SKarl Rupp 
23649e29f15eSvictorle   PetscFunctionBegin;
23655485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
23669e29f15eSvictorle   PetscFunctionReturn(0);
23679e29f15eSvictorle }
23689e29f15eSvictorle 
2369ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
23701cbb95d3SBarry Smith {
23711cbb95d3SBarry Smith   PetscErrorCode ierr;
23726e111a19SKarl Rupp 
23731cbb95d3SBarry Smith   PetscFunctionBegin;
23741cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
23751cbb95d3SBarry Smith   PetscFunctionReturn(0);
23761cbb95d3SBarry Smith }
23771cbb95d3SBarry Smith 
2378dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
237917ab2063SBarry Smith {
2380416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2381fff8e43fSBarry Smith   const PetscScalar *l,*r;
2382fff8e43fSBarry Smith   PetscScalar       x;
238354f21887SBarry Smith   MatScalar         *v;
2384dfbe8321SBarry Smith   PetscErrorCode    ierr;
2385fff8e43fSBarry Smith   PetscInt          i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz;
2386fff8e43fSBarry Smith   const PetscInt    *jj;
238717ab2063SBarry Smith 
23883a40ed3dSBarry Smith   PetscFunctionBegin;
238917ab2063SBarry Smith   if (ll) {
23903ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
23913ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2392e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2393e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
2394fff8e43fSBarry Smith     ierr = VecGetArrayRead(ll,&l);CHKERRQ(ierr);
2395416022c9SBarry Smith     v    = a->a;
239617ab2063SBarry Smith     for (i=0; i<m; i++) {
239717ab2063SBarry Smith       x = l[i];
2398416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
23992205254eSKarl Rupp       for (j=0; j<M; j++) (*v++) *= x;
240017ab2063SBarry Smith     }
2401fff8e43fSBarry Smith     ierr = VecRestoreArrayRead(ll,&l);CHKERRQ(ierr);
2402efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
240317ab2063SBarry Smith   }
240417ab2063SBarry Smith   if (rr) {
2405e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2406e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
2407fff8e43fSBarry Smith     ierr = VecGetArrayRead(rr,&r);CHKERRQ(ierr);
2408416022c9SBarry Smith     v    = a->a; jj = a->j;
24092205254eSKarl Rupp     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
2410fff8e43fSBarry Smith     ierr = VecRestoreArrayRead(rr,&r);CHKERRQ(ierr);
2411efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
241217ab2063SBarry Smith   }
2413acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr);
2414e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2415c70f7ee4SJunchao Zhang   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2416e2cf4d64SStefano Zampini #endif
24173a40ed3dSBarry Smith   PetscFunctionReturn(0);
241817ab2063SBarry Smith }
241917ab2063SBarry Smith 
24207dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
242117ab2063SBarry Smith {
2422db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
24236849ba73SBarry Smith   PetscErrorCode ierr;
2424d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
242597f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
24265d0c19d7SBarry Smith   const PetscInt *irow,*icol;
24275d0c19d7SBarry Smith   PetscInt       nrows,ncols;
242897f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
242954f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2430416022c9SBarry Smith   Mat            C;
2431cdc6f3adSToby Isaac   PetscBool      stride;
243217ab2063SBarry Smith 
24333a40ed3dSBarry Smith   PetscFunctionBegin;
243499141d43SSatish Balay 
243517ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2436b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2437b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
243817ab2063SBarry Smith 
2439251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2440ff718158SBarry Smith   if (stride) {
2441ff718158SBarry Smith     ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2442ff718158SBarry Smith   } else {
2443ff718158SBarry Smith     first = 0;
2444ff718158SBarry Smith     step  = 0;
2445ff718158SBarry Smith   }
2446fee21e36SBarry Smith   if (stride && step == 1) {
244702834360SBarry Smith     /* special case of contiguous rows */
2448dcca6d9dSJed Brown     ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr);
244902834360SBarry Smith     /* loop over new rows determining lens and starting points */
245002834360SBarry Smith     for (i=0; i<nrows; i++) {
2451bfeeae90SHong Zhang       kstart = ai[irow[i]];
2452a2744918SBarry Smith       kend   = kstart + ailen[irow[i]];
2453a91a9bebSLisandro Dalcin       starts[i] = kstart;
245402834360SBarry Smith       for (k=kstart; k<kend; k++) {
2455bfeeae90SHong Zhang         if (aj[k] >= first) {
245602834360SBarry Smith           starts[i] = k;
245702834360SBarry Smith           break;
245802834360SBarry Smith         }
245902834360SBarry Smith       }
2460a2744918SBarry Smith       sum = 0;
246102834360SBarry Smith       while (k < kend) {
2462bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2463a2744918SBarry Smith         sum++;
246402834360SBarry Smith       }
2465a2744918SBarry Smith       lens[i] = sum;
246602834360SBarry Smith     }
246702834360SBarry Smith     /* create submatrix */
2468cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
246997f1f81fSBarry Smith       PetscInt n_cols,n_rows;
247008480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2471e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2472d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
247308480c60SBarry Smith       C    = *B;
24743a40ed3dSBarry Smith     } else {
24753bef6203SJed Brown       PetscInt rbs,cbs;
2476ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2477f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
24783bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
24793bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
24803bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
24817adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2482ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
248308480c60SBarry Smith     }
2484db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2485db02288aSLois Curfman McInnes 
248602834360SBarry Smith     /* loop over rows inserting into submatrix */
2487db02288aSLois Curfman McInnes     a_new = c->a;
2488db02288aSLois Curfman McInnes     j_new = c->j;
2489db02288aSLois Curfman McInnes     i_new = c->i;
2490bfeeae90SHong Zhang 
249102834360SBarry Smith     for (i=0; i<nrows; i++) {
2492a2744918SBarry Smith       ii    = starts[i];
2493a2744918SBarry Smith       lensi = lens[i];
2494a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2495a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
249602834360SBarry Smith       }
2497580bdb30SBarry Smith       ierr       = PetscArraycpy(a_new,a->a + starts[i],lensi);CHKERRQ(ierr);
2498a2744918SBarry Smith       a_new     += lensi;
2499a2744918SBarry Smith       i_new[i+1] = i_new[i] + lensi;
2500a2744918SBarry Smith       c->ilen[i] = lensi;
250102834360SBarry Smith     }
25020e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
25033a40ed3dSBarry Smith   } else {
250402834360SBarry Smith     ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
25051795a4d1SJed Brown     ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr);
2506854ce69bSBarry Smith     ierr = PetscMalloc1(1+nrows,&lens);CHKERRQ(ierr);
25074dcab191SBarry Smith     for (i=0; i<ncols; i++) {
2508*cf9c20a2SJed Brown       if (PetscUnlikelyDebug(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);
25094dcab191SBarry Smith       smap[icol[i]] = i+1;
25104dcab191SBarry Smith     }
25114dcab191SBarry Smith 
251202834360SBarry Smith     /* determine lens of each row */
251302834360SBarry Smith     for (i=0; i<nrows; i++) {
2514bfeeae90SHong Zhang       kstart  = ai[irow[i]];
251502834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
251602834360SBarry Smith       lens[i] = 0;
251702834360SBarry Smith       for (k=kstart; k<kend; k++) {
2518bfeeae90SHong Zhang         if (smap[aj[k]]) {
251902834360SBarry Smith           lens[i]++;
252002834360SBarry Smith         }
252102834360SBarry Smith       }
252202834360SBarry Smith     }
252317ab2063SBarry Smith     /* Create and fill new matrix */
2524a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2525ace3abfcSBarry Smith       PetscBool equal;
25260f5bd95cSBarry Smith 
252799141d43SSatish Balay       c = (Mat_SeqAIJ*)((*B)->data);
2528e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2529580bdb30SBarry Smith       ierr = PetscArraycmp(c->ilen,lens,(*B)->rmap->n,&equal);CHKERRQ(ierr);
2530f23aa3ddSBarry Smith       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2531580bdb30SBarry Smith       ierr = PetscArrayzero(c->ilen,(*B)->rmap->n);CHKERRQ(ierr);
253208480c60SBarry Smith       C    = *B;
25333a40ed3dSBarry Smith     } else {
25343bef6203SJed Brown       PetscInt rbs,cbs;
2535ce94432eSBarry Smith       ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2536f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
25373bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
25383bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
25393bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
25407adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2541ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
254208480c60SBarry Smith     }
254399141d43SSatish Balay     c = (Mat_SeqAIJ*)(C->data);
254417ab2063SBarry Smith     for (i=0; i<nrows; i++) {
254599141d43SSatish Balay       row      = irow[i];
2546bfeeae90SHong Zhang       kstart   = ai[row];
254799141d43SSatish Balay       kend     = kstart + a->ilen[row];
2548bfeeae90SHong Zhang       mat_i    = c->i[i];
254999141d43SSatish Balay       mat_j    = c->j + mat_i;
255099141d43SSatish Balay       mat_a    = c->a + mat_i;
255199141d43SSatish Balay       mat_ilen = c->ilen + i;
255217ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2553bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2554ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
255599141d43SSatish Balay           *mat_a++ = a->a[k];
255699141d43SSatish Balay           (*mat_ilen)++;
255799141d43SSatish Balay 
255817ab2063SBarry Smith         }
255917ab2063SBarry Smith       }
256017ab2063SBarry Smith     }
256102834360SBarry Smith     /* Free work space */
256202834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2563606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2564606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
2565cdc6f3adSToby Isaac     /* sort */
2566cdc6f3adSToby Isaac     for (i = 0; i < nrows; i++) {
2567cdc6f3adSToby Isaac       PetscInt ilen;
2568cdc6f3adSToby Isaac 
2569cdc6f3adSToby Isaac       mat_i = c->i[i];
2570cdc6f3adSToby Isaac       mat_j = c->j + mat_i;
2571cdc6f3adSToby Isaac       mat_a = c->a + mat_i;
2572cdc6f3adSToby Isaac       ilen  = c->ilen[i];
2573390e1bf2SBarry Smith       ierr  = PetscSortIntWithScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr);
2574cdc6f3adSToby Isaac     }
257502834360SBarry Smith   }
2576305c6ccfSStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2577b470e4b4SRichard Tran Mills   ierr = MatBindToCPU(C,A->boundtocpu);CHKERRQ(ierr);
2578305c6ccfSStefano Zampini #endif
25796d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
25806d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
258117ab2063SBarry Smith 
258217ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2583416022c9SBarry Smith   *B   = C;
25843a40ed3dSBarry Smith   PetscFunctionReturn(0);
258517ab2063SBarry Smith }
258617ab2063SBarry Smith 
2587fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
258882d44351SHong Zhang {
258982d44351SHong Zhang   PetscErrorCode ierr;
259082d44351SHong Zhang   Mat            B;
259182d44351SHong Zhang 
259282d44351SHong Zhang   PetscFunctionBegin;
2593c2d650bdSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
259482d44351SHong Zhang     ierr    = MatCreate(subComm,&B);CHKERRQ(ierr);
259582d44351SHong Zhang     ierr    = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
259633d57670SJed Brown     ierr    = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr);
259782d44351SHong Zhang     ierr    = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
259882d44351SHong Zhang     ierr    = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
259982d44351SHong Zhang     *subMat = B;
2600c2d650bdSHong Zhang   } else {
2601c2d650bdSHong Zhang     ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
2602c2d650bdSHong Zhang   }
260382d44351SHong Zhang   PetscFunctionReturn(0);
260482d44351SHong Zhang }
260582d44351SHong Zhang 
26069a625307SHong Zhang PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2607a871dcd8SBarry Smith {
260863b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2609dfbe8321SBarry Smith   PetscErrorCode ierr;
261063b91edcSBarry Smith   Mat            outA;
2611ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
261263b91edcSBarry Smith 
26133a40ed3dSBarry Smith   PetscFunctionBegin;
2614e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
26151df811f5SHong Zhang 
2616b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2617b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2618a871dcd8SBarry Smith 
261963b91edcSBarry Smith   outA             = inA;
2620d5f3da31SBarry Smith   outA->factortype = MAT_FACTOR_LU;
2621f6224b95SHong Zhang   ierr = PetscFree(inA->solvertype);CHKERRQ(ierr);
2622f6224b95SHong Zhang   ierr = PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);CHKERRQ(ierr);
26232205254eSKarl Rupp 
2624c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
26256bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
26262205254eSKarl Rupp 
2627c3122656SLisandro Dalcin   a->row = row;
26282205254eSKarl Rupp 
2629c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
26306bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
26312205254eSKarl Rupp 
2632c3122656SLisandro Dalcin   a->col = col;
263363b91edcSBarry Smith 
263436db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
26356bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
26364c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
26373bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
2638f0ec6fceSSatish Balay 
263994a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2640854ce69bSBarry Smith     ierr = PetscMalloc1(inA->rmap->n+1,&a->solve_work);CHKERRQ(ierr);
26413bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
264294a9d846SBarry Smith   }
264363b91edcSBarry Smith 
2644f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2645137fb511SHong Zhang   if (row_identity && col_identity) {
2646ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2647137fb511SHong Zhang   } else {
2648719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2649137fb511SHong Zhang   }
26503a40ed3dSBarry Smith   PetscFunctionReturn(0);
2651a871dcd8SBarry Smith }
2652a871dcd8SBarry Smith 
2653f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2654f0b747eeSBarry Smith {
2655f0b747eeSBarry Smith   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2656f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2657efee365bSSatish Balay   PetscErrorCode ierr;
2658c5df96a5SBarry Smith   PetscBLASInt   one = 1,bnz;
26593a40ed3dSBarry Smith 
26603a40ed3dSBarry Smith   PetscFunctionBegin;
2661c5df96a5SBarry Smith   ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr);
26628b83055fSJed Brown   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2663efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2664acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr);
2665e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2666c70f7ee4SJunchao Zhang   if (inA->offloadmask != PETSC_OFFLOAD_UNALLOCATED) inA->offloadmask = PETSC_OFFLOAD_CPU;
2667e2cf4d64SStefano Zampini #endif
26683a40ed3dSBarry Smith   PetscFunctionReturn(0);
2669f0b747eeSBarry Smith }
2670f0b747eeSBarry Smith 
2671f68bb481SHong Zhang PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
267216b64355SHong Zhang {
267316b64355SHong Zhang   PetscErrorCode ierr;
267416b64355SHong Zhang   PetscInt       i;
267516b64355SHong Zhang 
267616b64355SHong Zhang   PetscFunctionBegin;
267716b64355SHong Zhang   if (!submatj->id) { /* delete data that are linked only to submats[id=0] */
267816b64355SHong Zhang     ierr = PetscFree4(submatj->sbuf1,submatj->ptr,submatj->tmp,submatj->ctr);CHKERRQ(ierr);
267916b64355SHong Zhang 
268016b64355SHong Zhang     for (i=0; i<submatj->nrqr; ++i) {
268116b64355SHong Zhang       ierr = PetscFree(submatj->sbuf2[i]);CHKERRQ(ierr);
268216b64355SHong Zhang     }
268316b64355SHong Zhang     ierr = PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);CHKERRQ(ierr);
268416b64355SHong Zhang 
268516b64355SHong Zhang     if (submatj->rbuf1) {
268616b64355SHong Zhang       ierr = PetscFree(submatj->rbuf1[0]);CHKERRQ(ierr);
268716b64355SHong Zhang       ierr = PetscFree(submatj->rbuf1);CHKERRQ(ierr);
268816b64355SHong Zhang     }
268916b64355SHong Zhang 
269016b64355SHong Zhang     for (i=0; i<submatj->nrqs; ++i) {
269116b64355SHong Zhang       ierr = PetscFree(submatj->rbuf3[i]);CHKERRQ(ierr);
269216b64355SHong Zhang     }
269316b64355SHong Zhang     ierr = PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);CHKERRQ(ierr);
269416b64355SHong Zhang     ierr = PetscFree(submatj->pa);CHKERRQ(ierr);
269516b64355SHong Zhang   }
269616b64355SHong Zhang 
269716b64355SHong Zhang #if defined(PETSC_USE_CTABLE)
269816b64355SHong Zhang   ierr = PetscTableDestroy((PetscTable*)&submatj->rmap);CHKERRQ(ierr);
269916b64355SHong Zhang   if (submatj->cmap_loc) {ierr = PetscFree(submatj->cmap_loc);CHKERRQ(ierr);}
270016b64355SHong Zhang   ierr = PetscFree(submatj->rmap_loc);CHKERRQ(ierr);
270116b64355SHong Zhang #else
270216b64355SHong Zhang   ierr = PetscFree(submatj->rmap);CHKERRQ(ierr);
270316b64355SHong Zhang #endif
270416b64355SHong Zhang 
270516b64355SHong Zhang   if (!submatj->allcolumns) {
270616b64355SHong Zhang #if defined(PETSC_USE_CTABLE)
270716b64355SHong Zhang     ierr = PetscTableDestroy((PetscTable*)&submatj->cmap);CHKERRQ(ierr);
270816b64355SHong Zhang #else
270916b64355SHong Zhang     ierr = PetscFree(submatj->cmap);CHKERRQ(ierr);
271016b64355SHong Zhang #endif
271116b64355SHong Zhang   }
271216b64355SHong Zhang   ierr = PetscFree(submatj->row2proc);CHKERRQ(ierr);
271316b64355SHong Zhang 
271416b64355SHong Zhang   ierr = PetscFree(submatj);CHKERRQ(ierr);
271516b64355SHong Zhang   PetscFunctionReturn(0);
271616b64355SHong Zhang }
271716b64355SHong Zhang 
27180fb991dcSHong Zhang PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
271916b64355SHong Zhang {
272016b64355SHong Zhang   PetscErrorCode ierr;
272116b64355SHong Zhang   Mat_SeqAIJ     *c = (Mat_SeqAIJ*)C->data;
27225c39f6d9SHong Zhang   Mat_SubSppt    *submatj = c->submatis1;
272316b64355SHong Zhang 
272416b64355SHong Zhang   PetscFunctionBegin;
272534136279SStefano Zampini   ierr = (*submatj->destroy)(C);CHKERRQ(ierr);
2726f68bb481SHong Zhang   ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr);
272716b64355SHong Zhang   PetscFunctionReturn(0);
272816b64355SHong Zhang }
272916b64355SHong Zhang 
27302d033e1fSHong Zhang PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[])
27312d033e1fSHong Zhang {
27322d033e1fSHong Zhang   PetscErrorCode ierr;
27332d033e1fSHong Zhang   PetscInt       i;
27340fb991dcSHong Zhang   Mat            C;
27350fb991dcSHong Zhang   Mat_SeqAIJ     *c;
27360fb991dcSHong Zhang   Mat_SubSppt    *submatj;
27372d033e1fSHong Zhang 
27382d033e1fSHong Zhang   PetscFunctionBegin;
27392d033e1fSHong Zhang   for (i=0; i<n; i++) {
27400fb991dcSHong Zhang     C       = (*mat)[i];
27410fb991dcSHong Zhang     c       = (Mat_SeqAIJ*)C->data;
27420fb991dcSHong Zhang     submatj = c->submatis1;
27432d033e1fSHong Zhang     if (submatj) {
2744682e4c99SStefano Zampini       if (--((PetscObject)C)->refct <= 0) {
274534136279SStefano Zampini         ierr = (*submatj->destroy)(C);CHKERRQ(ierr);
2746f68bb481SHong Zhang         ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr);
274734136279SStefano Zampini         ierr = PetscFree(C->defaultvectype);CHKERRQ(ierr);
27482d033e1fSHong Zhang         ierr = PetscLayoutDestroy(&C->rmap);CHKERRQ(ierr);
27492d033e1fSHong Zhang         ierr = PetscLayoutDestroy(&C->cmap);CHKERRQ(ierr);
27502d033e1fSHong Zhang         ierr = PetscHeaderDestroy(&C);CHKERRQ(ierr);
2751682e4c99SStefano Zampini       }
27522d033e1fSHong Zhang     } else {
27532d033e1fSHong Zhang       ierr = MatDestroy(&C);CHKERRQ(ierr);
27542d033e1fSHong Zhang     }
27552d033e1fSHong Zhang   }
275686e85357SHong Zhang 
275763a75b2aSHong Zhang   /* Destroy Dummy submatrices created for reuse */
275863a75b2aSHong Zhang   ierr = MatDestroySubMatrices_Dummy(n,mat);CHKERRQ(ierr);
275963a75b2aSHong Zhang 
27602d033e1fSHong Zhang   ierr = PetscFree(*mat);CHKERRQ(ierr);
27612d033e1fSHong Zhang   PetscFunctionReturn(0);
27622d033e1fSHong Zhang }
27632d033e1fSHong Zhang 
27647dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2765cddf8d76SBarry Smith {
2766dfbe8321SBarry Smith   PetscErrorCode ierr;
276797f1f81fSBarry Smith   PetscInt       i;
2768cddf8d76SBarry Smith 
27693a40ed3dSBarry Smith   PetscFunctionBegin;
2770cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2771df750dc8SHong Zhang     ierr = PetscCalloc1(n+1,B);CHKERRQ(ierr);
2772cddf8d76SBarry Smith   }
2773cddf8d76SBarry Smith 
2774cddf8d76SBarry Smith   for (i=0; i<n; i++) {
27757dae84e0SHong Zhang     ierr = MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2776cddf8d76SBarry Smith   }
27773a40ed3dSBarry Smith   PetscFunctionReturn(0);
2778cddf8d76SBarry Smith }
2779cddf8d76SBarry Smith 
278097f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
27814dcbc457SBarry Smith {
2782e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
27836849ba73SBarry Smith   PetscErrorCode ierr;
27845d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
27855d0c19d7SBarry Smith   const PetscInt *idx;
278697f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2787f1af5d2fSBarry Smith   PetscBT        table;
2788bbd702dbSSatish Balay 
27893a40ed3dSBarry Smith   PetscFunctionBegin;
2790d0f46423SBarry Smith   m  = A->rmap->n;
2791e4d965acSSatish Balay   ai = a->i;
2792bfeeae90SHong Zhang   aj = a->j;
27938a047759SSatish Balay 
2794e32f2f54SBarry Smith   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
279506763907SSatish Balay 
2796854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&nidx);CHKERRQ(ierr);
279753b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
279806763907SSatish Balay 
2799e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2800b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2801e4d965acSSatish Balay     isz  = 0;
28026831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2803e4d965acSSatish Balay 
2804e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
28054dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2806b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2807e4d965acSSatish Balay 
2808dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2809e4d965acSSatish Balay     for (j=0; j<n; ++j) {
28102205254eSKarl Rupp       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
28114dcbc457SBarry Smith     }
281206763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
28136bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2814e4d965acSSatish Balay 
281504a348a9SBarry Smith     k = 0;
281604a348a9SBarry Smith     for (j=0; j<ov; j++) { /* for each overlap */
281704a348a9SBarry Smith       n = isz;
281806763907SSatish Balay       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2819e4d965acSSatish Balay         row   = nidx[k];
2820e4d965acSSatish Balay         start = ai[row];
2821e4d965acSSatish Balay         end   = ai[row+1];
282204a348a9SBarry Smith         for (l = start; l<end; l++) {
2823efb16452SHong Zhang           val = aj[l];
28242205254eSKarl Rupp           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2825e4d965acSSatish Balay         }
2826e4d965acSSatish Balay       }
2827e4d965acSSatish Balay     }
282870b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2829e4d965acSSatish Balay   }
283094bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2831606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
28323a40ed3dSBarry Smith   PetscFunctionReturn(0);
28334dcbc457SBarry Smith }
283417ab2063SBarry Smith 
28350513a670SBarry Smith /* -------------------------------------------------------------- */
2836dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
28370513a670SBarry Smith {
28380513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
28396849ba73SBarry Smith   PetscErrorCode ierr;
28403b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
28415d0c19d7SBarry Smith   const PetscInt *row,*col;
28425d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
284356cd22aeSBarry Smith   IS             icolp,irowp;
28440298fd71SBarry Smith   PetscInt       *cwork = NULL;
28450298fd71SBarry Smith   PetscScalar    *vwork = NULL;
28460513a670SBarry Smith 
28473a40ed3dSBarry Smith   PetscFunctionBegin;
28484c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
284956cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
28504c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
285156cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
28520513a670SBarry Smith 
28530513a670SBarry Smith   /* determine lengths of permuted rows */
2854854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&lens);CHKERRQ(ierr);
28552205254eSKarl Rupp   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2856ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
2857f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
285833d57670SJed Brown   ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
28597adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2860ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2861606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
28620513a670SBarry Smith 
2863785e854fSJed Brown   ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr);
28640513a670SBarry Smith   for (i=0; i<m; i++) {
286532ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
28662205254eSKarl Rupp     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2867cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
286832ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
28690513a670SBarry Smith   }
2870606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
28712205254eSKarl Rupp 
28723c7d62e4SBarry Smith   (*B)->assembled = PETSC_FALSE;
28732205254eSKarl Rupp 
28749fe5e383SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2875b470e4b4SRichard Tran Mills   ierr = MatBindToCPU(*B,A->boundtocpu);CHKERRQ(ierr);
28769fe5e383SStefano Zampini #endif
28770513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
28780513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
287956cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
288056cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
28816bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
28826bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
28836768869dSprj-   if (rowp == colp) {
28846768869dSprj-     if (A->symmetric) {
28856768869dSprj-       ierr = MatSetOption(*B,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
28866768869dSprj-     }
28876768869dSprj-     if (A->hermitian) {
28886768869dSprj-       ierr = MatSetOption(*B,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
28896768869dSprj-     }
28906768869dSprj-   }
28913a40ed3dSBarry Smith   PetscFunctionReturn(0);
28920513a670SBarry Smith }
28930513a670SBarry Smith 
2894dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2895cb5b572fSBarry Smith {
2896dfbe8321SBarry Smith   PetscErrorCode ierr;
2897cb5b572fSBarry Smith 
2898cb5b572fSBarry Smith   PetscFunctionBegin;
289933f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
290033f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2901be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2902be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2903be6bf707SBarry Smith 
29044d805d7cSStefano Zampini     if (a->i[A->rmap->n] != b->i[B->rmap->n]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different %D != %D",a->i[A->rmap->n],b->i[B->rmap->n]);
2905580bdb30SBarry Smith     ierr = PetscArraycpy(b->a,a->a,a->i[A->rmap->n]);CHKERRQ(ierr);
2906cdc753b6SBarry Smith     ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr);
2907cb5b572fSBarry Smith   } else {
2908cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2909cb5b572fSBarry Smith   }
2910cb5b572fSBarry Smith   PetscFunctionReturn(0);
2911cb5b572fSBarry Smith }
2912cb5b572fSBarry Smith 
29134994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2914273d9f13SBarry Smith {
2915dfbe8321SBarry Smith   PetscErrorCode ierr;
2916273d9f13SBarry Smith 
2917273d9f13SBarry Smith   PetscFunctionBegin;
2918ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2919273d9f13SBarry Smith   PetscFunctionReturn(0);
2920273d9f13SBarry Smith }
2921273d9f13SBarry Smith 
2922f38c1e66SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
29236c0721eeSBarry Smith {
29246c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
29256e111a19SKarl Rupp 
29266c0721eeSBarry Smith   PetscFunctionBegin;
29276c0721eeSBarry Smith   *array = a->a;
29286c0721eeSBarry Smith   PetscFunctionReturn(0);
29296c0721eeSBarry Smith }
29306c0721eeSBarry Smith 
2931f38c1e66SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
29326c0721eeSBarry Smith {
29336c0721eeSBarry Smith   PetscFunctionBegin;
2934f38c1e66SStefano Zampini   *array = NULL;
29356c0721eeSBarry Smith   PetscFunctionReturn(0);
29366c0721eeSBarry Smith }
2937273d9f13SBarry Smith 
29388229c054SShri Abhyankar /*
29398229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
29408229c054SShri Abhyankar    have different nonzero structure.
29418229c054SShri Abhyankar */
2942b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2943ec7775f6SShri Abhyankar {
2944b264fe52SHong Zhang   PetscInt       i,j,k,nzx,nzy;
2945ec7775f6SShri Abhyankar 
2946ec7775f6SShri Abhyankar   PetscFunctionBegin;
2947ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2948ec7775f6SShri Abhyankar   for (i=0; i<m; i++) {
2949b264fe52SHong Zhang     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2950b264fe52SHong Zhang     nzx = xi[i+1] - xi[i];
2951b264fe52SHong Zhang     nzy = yi[i+1] - yi[i];
29528af7cee1SJed Brown     nnz[i] = 0;
29538af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2954b264fe52SHong Zhang       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2955b264fe52SHong Zhang       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
29568af7cee1SJed Brown       nnz[i]++;
29578af7cee1SJed Brown     }
29588af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2959ec7775f6SShri Abhyankar   }
2960ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2961ec7775f6SShri Abhyankar }
2962ec7775f6SShri Abhyankar 
2963b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2964b264fe52SHong Zhang {
2965b264fe52SHong Zhang   PetscInt       m = Y->rmap->N;
2966b264fe52SHong Zhang   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2967b264fe52SHong Zhang   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;
2968b264fe52SHong Zhang   PetscErrorCode ierr;
2969b264fe52SHong Zhang 
2970b264fe52SHong Zhang   PetscFunctionBegin;
2971b264fe52SHong Zhang   /* Set the number of nonzeros in the new matrix */
2972b264fe52SHong Zhang   ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr);
2973b264fe52SHong Zhang   PetscFunctionReturn(0);
2974b264fe52SHong Zhang }
2975b264fe52SHong Zhang 
2976f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2977ac90fabeSBarry Smith {
2978dfbe8321SBarry Smith   PetscErrorCode ierr;
2979ac90fabeSBarry Smith   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2980c5df96a5SBarry Smith   PetscBLASInt   one=1,bnz;
2981ac90fabeSBarry Smith 
2982ac90fabeSBarry Smith   PetscFunctionBegin;
2983c5df96a5SBarry Smith   ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2984ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2985f4df32b1SMatthew Knepley     PetscScalar alpha = a;
29868b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2987acf2f550SJed Brown     ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr);
2988a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2989e2cf4d64SStefano Zampini     /* the MatAXPY_Basic* subroutines calls MatAssembly, so the matrix on the GPU
2990e2cf4d64SStefano Zampini        will be updated */
2991e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2992c70f7ee4SJunchao Zhang     if (Y->offloadmask != PETSC_OFFLOAD_UNALLOCATED) {
2993c70f7ee4SJunchao Zhang       Y->offloadmask = PETSC_OFFLOAD_CPU;
2994e2cf4d64SStefano Zampini     }
2995e2cf4d64SStefano Zampini #endif
2996ab784542SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2997ab784542SHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2998ac90fabeSBarry Smith   } else {
29998229c054SShri Abhyankar     Mat      B;
30008229c054SShri Abhyankar     PetscInt *nnz;
3001785e854fSJed Brown     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
3002ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
3003bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
30044aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
300533d57670SJed Brown     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
3006176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
30078229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
3008ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
3009ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
301028be2f97SBarry Smith     ierr = MatHeaderReplace(Y,&B);CHKERRQ(ierr);
30118229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
3012ac90fabeSBarry Smith   }
3013ac90fabeSBarry Smith   PetscFunctionReturn(0);
3014ac90fabeSBarry Smith }
3015ac90fabeSBarry Smith 
30167087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
3017354c94deSBarry Smith {
3018354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
3019354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
3020354c94deSBarry Smith   PetscInt    i,nz;
3021354c94deSBarry Smith   PetscScalar *a;
3022354c94deSBarry Smith 
3023354c94deSBarry Smith   PetscFunctionBegin;
3024354c94deSBarry Smith   nz = aij->nz;
3025354c94deSBarry Smith   a  = aij->a;
30262205254eSKarl Rupp   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
3027e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
3028c70f7ee4SJunchao Zhang   if (mat->offloadmask != PETSC_OFFLOAD_UNALLOCATED) mat->offloadmask = PETSC_OFFLOAD_CPU;
3029e2cf4d64SStefano Zampini #endif
3030354c94deSBarry Smith #else
3031354c94deSBarry Smith   PetscFunctionBegin;
3032354c94deSBarry Smith #endif
3033354c94deSBarry Smith   PetscFunctionReturn(0);
3034354c94deSBarry Smith }
3035354c94deSBarry Smith 
3036985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3037e34fafa9SBarry Smith {
3038e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3039e34fafa9SBarry Smith   PetscErrorCode ierr;
3040d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3041e34fafa9SBarry Smith   PetscReal      atmp;
3042985db425SBarry Smith   PetscScalar    *x;
3043e34fafa9SBarry Smith   MatScalar      *aa;
3044e34fafa9SBarry Smith 
3045e34fafa9SBarry Smith   PetscFunctionBegin;
3046e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3047e34fafa9SBarry Smith   aa = a->a;
3048e34fafa9SBarry Smith   ai = a->i;
3049e34fafa9SBarry Smith   aj = a->j;
3050e34fafa9SBarry Smith 
3051985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3052e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3053e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
3054e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3055e34fafa9SBarry Smith   for (i=0; i<m; i++) {
3056e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
30579189402eSHong Zhang     x[i]  = 0.0;
3058e34fafa9SBarry Smith     for (j=0; j<ncols; j++) {
3059985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
3060985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3061985db425SBarry Smith       aa++; aj++;
3062985db425SBarry Smith     }
3063985db425SBarry Smith   }
3064985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3065985db425SBarry Smith   PetscFunctionReturn(0);
3066985db425SBarry Smith }
3067985db425SBarry Smith 
3068985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3069985db425SBarry Smith {
3070985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3071985db425SBarry Smith   PetscErrorCode ierr;
3072d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3073985db425SBarry Smith   PetscScalar    *x;
3074985db425SBarry Smith   MatScalar      *aa;
3075985db425SBarry Smith 
3076985db425SBarry Smith   PetscFunctionBegin;
3077e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3078985db425SBarry Smith   aa = a->a;
3079985db425SBarry Smith   ai = a->i;
3080985db425SBarry Smith   aj = a->j;
3081985db425SBarry Smith 
3082985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3083985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3084985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
3085e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3086985db425SBarry Smith   for (i=0; i<m; i++) {
3087985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
3088d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
3089985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
3090985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
3091985db425SBarry Smith       x[i] = 0.0;
3092985db425SBarry Smith       if (idx) {
3093985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
3094985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
3095985db425SBarry Smith           if (aj[j] > j) {
3096985db425SBarry Smith             idx[i] = j;
3097985db425SBarry Smith             break;
3098985db425SBarry Smith           }
3099985db425SBarry Smith         }
3100985db425SBarry Smith       }
3101985db425SBarry Smith     }
3102985db425SBarry Smith     for (j=0; j<ncols; j++) {
3103985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3104985db425SBarry Smith       aa++; aj++;
3105985db425SBarry Smith     }
3106985db425SBarry Smith   }
3107985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3108985db425SBarry Smith   PetscFunctionReturn(0);
3109985db425SBarry Smith }
3110985db425SBarry Smith 
3111c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3112c87e5d42SMatthew Knepley {
3113c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3114c87e5d42SMatthew Knepley   PetscErrorCode ierr;
3115c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3116c87e5d42SMatthew Knepley   PetscReal      atmp;
3117c87e5d42SMatthew Knepley   PetscScalar    *x;
3118c87e5d42SMatthew Knepley   MatScalar      *aa;
3119c87e5d42SMatthew Knepley 
3120c87e5d42SMatthew Knepley   PetscFunctionBegin;
3121e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3122c87e5d42SMatthew Knepley   aa = a->a;
3123c87e5d42SMatthew Knepley   ai = a->i;
3124c87e5d42SMatthew Knepley   aj = a->j;
3125c87e5d42SMatthew Knepley 
3126c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3127c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3128c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
312960e0710aSBarry 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);
3130c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
3131c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
3132289a08f5SMatthew Knepley     if (ncols) {
3133289a08f5SMatthew Knepley       /* Get first nonzero */
3134289a08f5SMatthew Knepley       for (j = 0; j < ncols; j++) {
3135289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
31362205254eSKarl Rupp         if (atmp > 1.0e-12) {
31372205254eSKarl Rupp           x[i] = atmp;
31382205254eSKarl Rupp           if (idx) idx[i] = aj[j];
31392205254eSKarl Rupp           break;
31402205254eSKarl Rupp         }
3141289a08f5SMatthew Knepley       }
314212431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
3143289a08f5SMatthew Knepley     } else {
3144289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
3145289a08f5SMatthew Knepley     }
3146c87e5d42SMatthew Knepley     for (j = 0; j < ncols; j++) {
3147c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
3148289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3149c87e5d42SMatthew Knepley       aa++; aj++;
3150c87e5d42SMatthew Knepley     }
3151c87e5d42SMatthew Knepley   }
3152c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3153c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
3154c87e5d42SMatthew Knepley }
3155c87e5d42SMatthew Knepley 
3156985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3157985db425SBarry Smith {
3158985db425SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3159985db425SBarry Smith   PetscErrorCode  ierr;
3160d9ca1df4SBarry Smith   PetscInt        i,j,m = A->rmap->n,ncols,n;
3161d9ca1df4SBarry Smith   const PetscInt  *ai,*aj;
3162985db425SBarry Smith   PetscScalar     *x;
3163d9ca1df4SBarry Smith   const MatScalar *aa;
3164985db425SBarry Smith 
3165985db425SBarry Smith   PetscFunctionBegin;
3166e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3167985db425SBarry Smith   aa = a->a;
3168985db425SBarry Smith   ai = a->i;
3169985db425SBarry Smith   aj = a->j;
3170985db425SBarry Smith 
3171985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
3172985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
3173985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
3174e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3175985db425SBarry Smith   for (i=0; i<m; i++) {
3176985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
3177d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
3178985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
3179985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3180985db425SBarry Smith       x[i] = 0.0;
3181985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
3182985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
3183985db425SBarry Smith         for (j=0; j<ncols; j++) {
3184985db425SBarry Smith           if (aj[j] > j) {
3185985db425SBarry Smith             idx[i] = j;
3186985db425SBarry Smith             break;
3187985db425SBarry Smith           }
3188985db425SBarry Smith         }
3189985db425SBarry Smith       }
3190985db425SBarry Smith     }
3191985db425SBarry Smith     for (j=0; j<ncols; j++) {
3192985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3193985db425SBarry Smith       aa++; aj++;
3194e34fafa9SBarry Smith     }
3195e34fafa9SBarry Smith   }
3196e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
3197e34fafa9SBarry Smith   PetscFunctionReturn(0);
3198e34fafa9SBarry Smith }
3199bbead8a2SBarry Smith 
3200713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3201bbead8a2SBarry Smith {
3202bbead8a2SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*) A->data;
3203bbead8a2SBarry Smith   PetscErrorCode  ierr;
320433d57670SJed Brown   PetscInt        i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3205bbead8a2SBarry Smith   MatScalar       *diag,work[25],*v_work;
32060da83c2eSBarry Smith   const PetscReal shift = 0.0;
32071a9391e3SHong Zhang   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;
3208bbead8a2SBarry Smith 
3209bbead8a2SBarry Smith   PetscFunctionBegin;
3210a455e926SHong Zhang   allowzeropivot = PetscNot(A->erroriffailure);
32114a0d0026SBarry Smith   if (a->ibdiagvalid) {
32124a0d0026SBarry Smith     if (values) *values = a->ibdiag;
32134a0d0026SBarry Smith     PetscFunctionReturn(0);
32144a0d0026SBarry Smith   }
3215bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
3216bbead8a2SBarry Smith   if (!a->ibdiag) {
3217785e854fSJed Brown     ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr);
32183bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
3219bbead8a2SBarry Smith   }
3220bbead8a2SBarry Smith   diag = a->ibdiag;
3221bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
3222bbead8a2SBarry Smith   /* factor and invert each block */
3223bbead8a2SBarry Smith   switch (bs) {
3224bbead8a2SBarry Smith   case 1:
3225bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3226bbead8a2SBarry Smith       ierr = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
3227ec1892c8SHong Zhang       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3228ec1892c8SHong Zhang         if (allowzeropivot) {
32297b6c816cSBarry Smith           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
32307b6c816cSBarry Smith           A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
32317b6c816cSBarry Smith           A->factorerror_zeropivot_row   = i;
32327b6c816cSBarry Smith           ierr = PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);CHKERRQ(ierr);
32337b6c816cSBarry 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);
3234ec1892c8SHong Zhang       }
3235bbead8a2SBarry Smith       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3236bbead8a2SBarry Smith     }
3237bbead8a2SBarry Smith     break;
3238bbead8a2SBarry Smith   case 2:
3239bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3240bbead8a2SBarry Smith       ij[0] = 2*i; ij[1] = 2*i + 1;
3241bbead8a2SBarry Smith       ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
3242a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
32437b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
324496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
3245bbead8a2SBarry Smith       diag += 4;
3246bbead8a2SBarry Smith     }
3247bbead8a2SBarry Smith     break;
3248bbead8a2SBarry Smith   case 3:
3249bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3250bbead8a2SBarry Smith       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3251bbead8a2SBarry Smith       ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
3252a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
32537b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
325496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
3255bbead8a2SBarry Smith       diag += 9;
3256bbead8a2SBarry Smith     }
3257bbead8a2SBarry Smith     break;
3258bbead8a2SBarry Smith   case 4:
3259bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3260bbead8a2SBarry Smith       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3261bbead8a2SBarry Smith       ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
3262a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
32637b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
326496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
3265bbead8a2SBarry Smith       diag += 16;
3266bbead8a2SBarry Smith     }
3267bbead8a2SBarry Smith     break;
3268bbead8a2SBarry Smith   case 5:
3269bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3270bbead8a2SBarry 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;
3271bbead8a2SBarry Smith       ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
3272a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
32737b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
327496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
3275bbead8a2SBarry Smith       diag += 25;
3276bbead8a2SBarry Smith     }
3277bbead8a2SBarry Smith     break;
3278bbead8a2SBarry Smith   case 6:
3279bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3280bbead8a2SBarry 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;
3281bbead8a2SBarry Smith       ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
3282a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
32837b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
328496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3285bbead8a2SBarry Smith       diag += 36;
3286bbead8a2SBarry Smith     }
3287bbead8a2SBarry Smith     break;
3288bbead8a2SBarry Smith   case 7:
3289bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3290bbead8a2SBarry 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;
3291bbead8a2SBarry Smith       ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
3292a455e926SHong Zhang       ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
32937b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
329496b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3295bbead8a2SBarry Smith       diag += 49;
3296bbead8a2SBarry Smith     }
3297bbead8a2SBarry Smith     break;
3298bbead8a2SBarry Smith   default:
3299dcca6d9dSJed Brown     ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr);
3300bbead8a2SBarry Smith     for (i=0; i<mbs; i++) {
3301bbead8a2SBarry Smith       for (j=0; j<bs; j++) {
3302bbead8a2SBarry Smith         IJ[j] = bs*i + j;
3303bbead8a2SBarry Smith       }
3304bbead8a2SBarry Smith       ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
33055f8bbccaSHong Zhang       ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr);
33067b6c816cSBarry Smith       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
330796b95a6bSBarry Smith       ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3308bbead8a2SBarry Smith       diag += bs2;
3309bbead8a2SBarry Smith     }
3310bbead8a2SBarry Smith     ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3311bbead8a2SBarry Smith   }
3312bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3313bbead8a2SBarry Smith   PetscFunctionReturn(0);
3314bbead8a2SBarry Smith }
3315bbead8a2SBarry Smith 
331673a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
331773a71a0fSBarry Smith {
331873a71a0fSBarry Smith   PetscErrorCode ierr;
331973a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
332073a71a0fSBarry Smith   PetscScalar    a;
332173a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
332273a71a0fSBarry Smith 
332373a71a0fSBarry Smith   PetscFunctionBegin;
332473a71a0fSBarry Smith   if (!x->assembled) {
332573a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
332673a71a0fSBarry Smith     for (i=0; i<m; i++) {
332773a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
332873a71a0fSBarry Smith         ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
332973a71a0fSBarry Smith         col  = (PetscInt)(n*PetscRealPart(a));
333073a71a0fSBarry Smith         ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
333173a71a0fSBarry Smith       }
333273a71a0fSBarry Smith     }
3333e2ce353bSJunchao Zhang   } else {
3334e2ce353bSJunchao Zhang     for (i=0; i<aij->nz; i++) {ierr = PetscRandomGetValue(rctx,aij->a+i);CHKERRQ(ierr);}
3335e2ce353bSJunchao Zhang   }
333673a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
333773a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
333873a71a0fSBarry Smith   PetscFunctionReturn(0);
333973a71a0fSBarry Smith }
334073a71a0fSBarry Smith 
3341679944adSJunchao Zhang /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3342679944adSJunchao Zhang PetscErrorCode  MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x,PetscInt low,PetscInt high,PetscRandom rctx)
3343679944adSJunchao Zhang {
3344679944adSJunchao Zhang   PetscErrorCode ierr;
3345679944adSJunchao Zhang   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3346679944adSJunchao Zhang   PetscScalar    a;
3347679944adSJunchao Zhang   PetscInt       m,n,i,j,col,nskip;
3348679944adSJunchao Zhang 
3349679944adSJunchao Zhang   PetscFunctionBegin;
3350679944adSJunchao Zhang   nskip = high - low;
3351679944adSJunchao Zhang   ierr  = MatGetSize(x,&m,&n);CHKERRQ(ierr);
3352679944adSJunchao Zhang   n    -= nskip; /* shrink number of columns where nonzeros can be set */
3353679944adSJunchao Zhang   for (i=0; i<m; i++) {
3354679944adSJunchao Zhang     for (j=0; j<aij->imax[i]; j++) {
3355679944adSJunchao Zhang       ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
3356679944adSJunchao Zhang       col  = (PetscInt)(n*PetscRealPart(a));
3357679944adSJunchao Zhang       if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3358679944adSJunchao Zhang       ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
3359679944adSJunchao Zhang     }
3360e2ce353bSJunchao Zhang   }
3361679944adSJunchao Zhang   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3362679944adSJunchao Zhang   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3363679944adSJunchao Zhang   PetscFunctionReturn(0);
3364679944adSJunchao Zhang }
3365679944adSJunchao Zhang 
3366679944adSJunchao Zhang 
3367682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
33680a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3369cb5b572fSBarry Smith                                         MatGetRow_SeqAIJ,
3370cb5b572fSBarry Smith                                         MatRestoreRow_SeqAIJ,
3371cb5b572fSBarry Smith                                         MatMult_SeqAIJ,
337297304618SKris Buschelman                                 /*  4*/ MatMultAdd_SeqAIJ,
33737c922b88SBarry Smith                                         MatMultTranspose_SeqAIJ,
33747c922b88SBarry Smith                                         MatMultTransposeAdd_SeqAIJ,
3375db4efbfdSBarry Smith                                         0,
3376db4efbfdSBarry Smith                                         0,
3377db4efbfdSBarry Smith                                         0,
3378db4efbfdSBarry Smith                                 /* 10*/ 0,
3379cb5b572fSBarry Smith                                         MatLUFactor_SeqAIJ,
3380cb5b572fSBarry Smith                                         0,
338141f059aeSBarry Smith                                         MatSOR_SeqAIJ,
338291e9d3e2SHong Zhang                                         MatTranspose_SeqAIJ,
338397304618SKris Buschelman                                 /*1 5*/ MatGetInfo_SeqAIJ,
3384cb5b572fSBarry Smith                                         MatEqual_SeqAIJ,
3385cb5b572fSBarry Smith                                         MatGetDiagonal_SeqAIJ,
3386cb5b572fSBarry Smith                                         MatDiagonalScale_SeqAIJ,
3387cb5b572fSBarry Smith                                         MatNorm_SeqAIJ,
338897304618SKris Buschelman                                 /* 20*/ 0,
3389cb5b572fSBarry Smith                                         MatAssemblyEnd_SeqAIJ,
3390cb5b572fSBarry Smith                                         MatSetOption_SeqAIJ,
3391cb5b572fSBarry Smith                                         MatZeroEntries_SeqAIJ,
3392d519adbfSMatthew Knepley                                 /* 24*/ MatZeroRows_SeqAIJ,
3393db4efbfdSBarry Smith                                         0,
3394db4efbfdSBarry Smith                                         0,
3395db4efbfdSBarry Smith                                         0,
3396db4efbfdSBarry Smith                                         0,
33974994cf47SJed Brown                                 /* 29*/ MatSetUp_SeqAIJ,
3398db4efbfdSBarry Smith                                         0,
3399db4efbfdSBarry Smith                                         0,
34008c778c55SBarry Smith                                         0,
34018c778c55SBarry Smith                                         0,
3402d519adbfSMatthew Knepley                                 /* 34*/ MatDuplicate_SeqAIJ,
3403cb5b572fSBarry Smith                                         0,
3404cb5b572fSBarry Smith                                         0,
3405cb5b572fSBarry Smith                                         MatILUFactor_SeqAIJ,
3406cb5b572fSBarry Smith                                         0,
3407d519adbfSMatthew Knepley                                 /* 39*/ MatAXPY_SeqAIJ,
34087dae84e0SHong Zhang                                         MatCreateSubMatrices_SeqAIJ,
3409cb5b572fSBarry Smith                                         MatIncreaseOverlap_SeqAIJ,
3410cb5b572fSBarry Smith                                         MatGetValues_SeqAIJ,
3411cb5b572fSBarry Smith                                         MatCopy_SeqAIJ,
3412d519adbfSMatthew Knepley                                 /* 44*/ MatGetRowMax_SeqAIJ,
3413cb5b572fSBarry Smith                                         MatScale_SeqAIJ,
34147d68702bSBarry Smith                                         MatShift_SeqAIJ,
341579299369SBarry Smith                                         MatDiagonalSet_SeqAIJ,
34166e169961SBarry Smith                                         MatZeroRowsColumns_SeqAIJ,
341773a71a0fSBarry Smith                                 /* 49*/ MatSetRandom_SeqAIJ,
34183b2fbd54SBarry Smith                                         MatGetRowIJ_SeqAIJ,
34193b2fbd54SBarry Smith                                         MatRestoreRowIJ_SeqAIJ,
34203b2fbd54SBarry Smith                                         MatGetColumnIJ_SeqAIJ,
3421a93ec695SBarry Smith                                         MatRestoreColumnIJ_SeqAIJ,
342293dfae19SHong Zhang                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3423b9617806SBarry Smith                                         0,
34240513a670SBarry Smith                                         0,
3425cda55fadSBarry Smith                                         MatPermute_SeqAIJ,
3426cda55fadSBarry Smith                                         0,
3427d519adbfSMatthew Knepley                                 /* 59*/ 0,
3428b9b97703SBarry Smith                                         MatDestroy_SeqAIJ,
3429b9b97703SBarry Smith                                         MatView_SeqAIJ,
3430357abbc8SBarry Smith                                         0,
34314222ddf1SHong Zhang                                         0,
34324222ddf1SHong Zhang                                 /* 64*/ 0,
3433321b30b9SSatish Balay                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3434ee4f033dSBarry Smith                                         0,
3435ee4f033dSBarry Smith                                         0,
3436ee4f033dSBarry Smith                                         0,
3437d519adbfSMatthew Knepley                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3438c87e5d42SMatthew Knepley                                         MatGetRowMinAbs_SeqAIJ,
3439ee4f033dSBarry Smith                                         0,
3440dcf5cc72SBarry Smith                                         0,
34412c93a97aSBarry Smith                                         0,
34422c93a97aSBarry Smith                                 /* 74*/ 0,
34433acb8795SBarry Smith                                         MatFDColoringApply_AIJ,
344497304618SKris Buschelman                                         0,
344597304618SKris Buschelman                                         0,
344697304618SKris Buschelman                                         0,
34476ce1633cSBarry Smith                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
344897304618SKris Buschelman                                         0,
344997304618SKris Buschelman                                         0,
345097304618SKris Buschelman                                         0,
3451bc011b1eSHong Zhang                                         MatLoad_SeqAIJ,
3452d519adbfSMatthew Knepley                                 /* 84*/ MatIsSymmetric_SeqAIJ,
34531cbb95d3SBarry Smith                                         MatIsHermitian_SeqAIJ,
34546284ec50SHong Zhang                                         0,
34556284ec50SHong Zhang                                         0,
3456bc011b1eSHong Zhang                                         0,
34574222ddf1SHong Zhang                                 /* 89*/ 0,
34584222ddf1SHong Zhang                                         0,
345926be0446SHong Zhang                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
34604222ddf1SHong Zhang                                         0,
34614222ddf1SHong Zhang                                         0,
34628fa4b5a6SHong Zhang                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
34634222ddf1SHong Zhang                                         0,
34644222ddf1SHong Zhang                                         0,
34656fc122caSHong Zhang                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
34662121bac1SHong Zhang                                         0,
34674222ddf1SHong Zhang                                 /* 99*/ MatProductSetFromOptions_SeqAIJ,
3468609c6c4dSKris Buschelman                                         0,
3469609c6c4dSKris Buschelman                                         0,
347087d4246cSBarry Smith                                         MatConjugate_SeqAIJ,
347187d4246cSBarry Smith                                         0,
3472d519adbfSMatthew Knepley                                 /*104*/ MatSetValuesRow_SeqAIJ,
347399cafbc1SBarry Smith                                         MatRealPart_SeqAIJ,
3474f5edf698SHong Zhang                                         MatImaginaryPart_SeqAIJ,
3475f5edf698SHong Zhang                                         0,
34762bebee5dSHong Zhang                                         0,
3477cbd44569SHong Zhang                                 /*109*/ MatMatSolve_SeqAIJ,
3478985db425SBarry Smith                                         0,
34792af78befSBarry Smith                                         MatGetRowMin_SeqAIJ,
34802af78befSBarry Smith                                         0,
3481599ef60dSHong Zhang                                         MatMissingDiagonal_SeqAIJ,
3482d519adbfSMatthew Knepley                                 /*114*/ 0,
3483599ef60dSHong Zhang                                         0,
34843c2a7987SHong Zhang                                         0,
3485fe97e370SBarry Smith                                         0,
3486fbdbba38SShri Abhyankar                                         0,
3487fbdbba38SShri Abhyankar                                 /*119*/ 0,
3488fbdbba38SShri Abhyankar                                         0,
3489fbdbba38SShri Abhyankar                                         0,
349082d44351SHong Zhang                                         0,
3491b3a44c85SBarry Smith                                         MatGetMultiProcBlock_SeqAIJ,
34920716a85fSBarry Smith                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3493bbead8a2SBarry Smith                                         MatGetColumnNorms_SeqAIJ,
349437868618SMatthew G Knepley                                         MatInvertBlockDiagonal_SeqAIJ,
34950da83c2eSBarry Smith                                         MatInvertVariableBlockDiagonal_SeqAIJ,
349637868618SMatthew G Knepley                                         0,
34975df89d91SHong Zhang                                 /*129*/ 0,
34984222ddf1SHong Zhang                                         0,
34994222ddf1SHong Zhang                                         0,
350075648e8dSHong Zhang                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3501b9af6bddSHong Zhang                                         MatTransposeColoringCreate_SeqAIJ,
3502b9af6bddSHong Zhang                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
35032b8ad9a3SHong Zhang                                         MatTransColoringApplyDenToSp_SeqAIJ,
35044222ddf1SHong Zhang                                         0,
35054222ddf1SHong Zhang                                         0,
35063964eb88SJed Brown                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
35073964eb88SJed Brown                                  /*139*/0,
3508f9426fe0SMark Adams                                         0,
35091919a2e2SJed Brown                                         0,
35103a062f41SBarry Smith                                         MatFDColoringSetUp_SeqXAIJ,
35119c8f2541SHong Zhang                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
35124222ddf1SHong Zhang                                         MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
35134222ddf1SHong Zhang                                  /*145*/MatDestroySubMatrices_SeqAIJ,
35144222ddf1SHong Zhang                                         0,
35154222ddf1SHong Zhang                                         0
35169e29f15eSvictorle };
351717ab2063SBarry Smith 
35187087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3519bef8e0ddSBarry Smith {
3520bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
352197f1f81fSBarry Smith   PetscInt   i,nz,n;
3522bef8e0ddSBarry Smith 
3523bef8e0ddSBarry Smith   PetscFunctionBegin;
3524bef8e0ddSBarry Smith   nz = aij->maxnz;
3525d0f46423SBarry Smith   n  = mat->rmap->n;
3526bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3527bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3528bef8e0ddSBarry Smith   }
3529bef8e0ddSBarry Smith   aij->nz = nz;
3530bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3531bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3532bef8e0ddSBarry Smith   }
3533bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3534bef8e0ddSBarry Smith }
3535bef8e0ddSBarry Smith 
3536a3bb6f32SFande Kong /*
3537e8b528d9SFande Kong  * When a sparse matrix has many zero columns, we should compact them out to save the space
3538a3bb6f32SFande Kong  * This happens in MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3539a3bb6f32SFande Kong  * */
3540a3bb6f32SFande Kong PetscErrorCode  MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3541a3bb6f32SFande Kong {
3542a3bb6f32SFande Kong   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3543a3bb6f32SFande Kong   PetscTable         gid1_lid1;
3544a3bb6f32SFande Kong   PetscTablePosition tpos;
3545a3bb6f32SFande Kong   PetscInt           gid,lid,i,j,ncols,ec;
3546a3bb6f32SFande Kong   PetscInt           *garray;
3547a3bb6f32SFande Kong   PetscErrorCode  ierr;
3548a3bb6f32SFande Kong 
3549a3bb6f32SFande Kong   PetscFunctionBegin;
3550a3bb6f32SFande Kong   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3551a3bb6f32SFande Kong   PetscValidPointer(mapping,2);
3552a3bb6f32SFande Kong   /* use a table */
3553a3bb6f32SFande Kong   ierr = PetscTableCreate(mat->rmap->n,mat->cmap->N+1,&gid1_lid1);CHKERRQ(ierr);
3554a3bb6f32SFande Kong   ec = 0;
3555a3bb6f32SFande Kong   for (i=0; i<mat->rmap->n; i++) {
3556a3bb6f32SFande Kong     ncols = aij->i[i+1] - aij->i[i];
3557a3bb6f32SFande Kong     for (j=0; j<ncols; j++) {
3558a3bb6f32SFande Kong       PetscInt data,gid1 = aij->j[aij->i[i] + j] + 1;
3559a3bb6f32SFande Kong       ierr = PetscTableFind(gid1_lid1,gid1,&data);CHKERRQ(ierr);
3560a3bb6f32SFande Kong       if (!data) {
3561a3bb6f32SFande Kong         /* one based table */
3562a3bb6f32SFande Kong         ierr = PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);CHKERRQ(ierr);
3563a3bb6f32SFande Kong       }
3564a3bb6f32SFande Kong     }
3565a3bb6f32SFande Kong   }
3566a3bb6f32SFande Kong   /* form array of columns we need */
3567a3bb6f32SFande Kong   ierr = PetscMalloc1(ec+1,&garray);CHKERRQ(ierr);
3568a3bb6f32SFande Kong   ierr = PetscTableGetHeadPosition(gid1_lid1,&tpos);CHKERRQ(ierr);
3569a3bb6f32SFande Kong   while (tpos) {
3570a3bb6f32SFande Kong     ierr = PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);CHKERRQ(ierr);
3571a3bb6f32SFande Kong     gid--;
3572a3bb6f32SFande Kong     lid--;
3573a3bb6f32SFande Kong     garray[lid] = gid;
3574a3bb6f32SFande Kong   }
3575a3bb6f32SFande Kong   ierr = PetscSortInt(ec,garray);CHKERRQ(ierr); /* sort, and rebuild */
3576a3bb6f32SFande Kong   ierr = PetscTableRemoveAll(gid1_lid1);CHKERRQ(ierr);
3577a3bb6f32SFande Kong   for (i=0; i<ec; i++) {
3578a3bb6f32SFande Kong     ierr = PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr);
3579a3bb6f32SFande Kong   }
3580a3bb6f32SFande Kong   /* compact out the extra columns in B */
3581a3bb6f32SFande Kong   for (i=0; i<mat->rmap->n; i++) {
3582a3bb6f32SFande Kong 	ncols = aij->i[i+1] - aij->i[i];
3583a3bb6f32SFande Kong     for (j=0; j<ncols; j++) {
3584a3bb6f32SFande Kong       PetscInt gid1 = aij->j[aij->i[i] + j] + 1;
3585a3bb6f32SFande Kong       ierr = PetscTableFind(gid1_lid1,gid1,&lid);CHKERRQ(ierr);
3586a3bb6f32SFande Kong       lid--;
3587a3bb6f32SFande Kong       aij->j[aij->i[i] + j] = lid;
3588a3bb6f32SFande Kong     }
3589a3bb6f32SFande Kong   }
3590ca5434daSLawrence Mitchell   ierr = PetscLayoutDestroy(&mat->cmap);CHKERRQ(ierr);
3591ca5434daSLawrence Mitchell   ierr = PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat),ec,ec,1,&mat->cmap);CHKERRQ(ierr);
3592a3bb6f32SFande Kong   ierr = PetscTableDestroy(&gid1_lid1);CHKERRQ(ierr);
3593a3bb6f32SFande Kong   ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,mat->cmap->bs,mat->cmap->n,garray,PETSC_OWN_POINTER,mapping);CHKERRQ(ierr);
3594a3bb6f32SFande Kong   ierr = ISLocalToGlobalMappingSetType(*mapping,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
3595a3bb6f32SFande Kong   PetscFunctionReturn(0);
3596a3bb6f32SFande Kong }
3597a3bb6f32SFande Kong 
3598bef8e0ddSBarry Smith /*@
3599bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3600bef8e0ddSBarry Smith        in the matrix.
3601bef8e0ddSBarry Smith 
3602bef8e0ddSBarry Smith   Input Parameters:
3603bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3604bef8e0ddSBarry Smith -  indices - the column indices
3605bef8e0ddSBarry Smith 
360615091d37SBarry Smith   Level: advanced
360715091d37SBarry Smith 
3608bef8e0ddSBarry Smith   Notes:
3609bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3610bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3611bef8e0ddSBarry Smith   of the MatSetValues() operation.
3612bef8e0ddSBarry Smith 
3613bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3614d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3615bef8e0ddSBarry Smith 
3616bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3617bef8e0ddSBarry Smith 
3618b9617806SBarry Smith     The indices should start with zero, not one.
3619b9617806SBarry Smith 
3620bef8e0ddSBarry Smith @*/
36217087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3622bef8e0ddSBarry Smith {
36234ac538c5SBarry Smith   PetscErrorCode ierr;
3624bef8e0ddSBarry Smith 
3625bef8e0ddSBarry Smith   PetscFunctionBegin;
36260700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
36274482741eSBarry Smith   PetscValidPointer(indices,2);
36284ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
3629bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3630bef8e0ddSBarry Smith }
3631bef8e0ddSBarry Smith 
3632be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3633be6bf707SBarry Smith 
36347087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3635be6bf707SBarry Smith {
3636be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
36376849ba73SBarry Smith   PetscErrorCode ierr;
3638d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3639be6bf707SBarry Smith 
3640be6bf707SBarry Smith   PetscFunctionBegin;
3641169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3642be6bf707SBarry Smith 
3643be6bf707SBarry Smith   /* allocate space for values if not already there */
3644be6bf707SBarry Smith   if (!aij->saved_values) {
3645854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr);
36463bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3647be6bf707SBarry Smith   }
3648be6bf707SBarry Smith 
3649be6bf707SBarry Smith   /* copy values over */
3650580bdb30SBarry Smith   ierr = PetscArraycpy(aij->saved_values,aij->a,nz);CHKERRQ(ierr);
3651be6bf707SBarry Smith   PetscFunctionReturn(0);
3652be6bf707SBarry Smith }
3653be6bf707SBarry Smith 
3654be6bf707SBarry Smith /*@
3655be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3656be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3657be6bf707SBarry Smith        nonlinear portion.
3658be6bf707SBarry Smith 
3659be6bf707SBarry Smith    Collect on Mat
3660be6bf707SBarry Smith 
3661be6bf707SBarry Smith   Input Parameters:
36620e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3663be6bf707SBarry Smith 
366415091d37SBarry Smith   Level: advanced
366515091d37SBarry Smith 
3666be6bf707SBarry Smith   Common Usage, with SNESSolve():
3667be6bf707SBarry Smith $    Create Jacobian matrix
3668be6bf707SBarry Smith $    Set linear terms into matrix
3669be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3670be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3671be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3672512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3673be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3674be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3675be6bf707SBarry Smith $    In your Jacobian routine
3676be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3677be6bf707SBarry Smith $      Set nonlinear terms in matrix
3678be6bf707SBarry Smith 
3679be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3680be6bf707SBarry Smith $    // build linear portion of Jacobian
3681512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3682be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3683be6bf707SBarry Smith $    loop over nonlinear iterations
3684be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3685be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3686be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3687be6bf707SBarry Smith $       Solve linear system with Jacobian
3688be6bf707SBarry Smith $    endloop
3689be6bf707SBarry Smith 
3690be6bf707SBarry Smith   Notes:
3691be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3692512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3693be6bf707SBarry Smith     calling this routine.
3694be6bf707SBarry Smith 
36950c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
36960c468ba9SBarry Smith     and does not allocated additional space.
36970c468ba9SBarry Smith 
3698be6bf707SBarry Smith .seealso: MatRetrieveValues()
3699be6bf707SBarry Smith 
3700be6bf707SBarry Smith @*/
37017087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3702be6bf707SBarry Smith {
37034ac538c5SBarry Smith   PetscErrorCode ierr;
3704be6bf707SBarry Smith 
3705be6bf707SBarry Smith   PetscFunctionBegin;
37060700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3707e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3708e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
37094ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3710be6bf707SBarry Smith   PetscFunctionReturn(0);
3711be6bf707SBarry Smith }
3712be6bf707SBarry Smith 
37137087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3714be6bf707SBarry Smith {
3715be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
37166849ba73SBarry Smith   PetscErrorCode ierr;
3717d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3718be6bf707SBarry Smith 
3719be6bf707SBarry Smith   PetscFunctionBegin;
3720169f6850SBarry Smith   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3721f23aa3ddSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3722be6bf707SBarry Smith   /* copy values over */
3723580bdb30SBarry Smith   ierr = PetscArraycpy(aij->a,aij->saved_values,nz);CHKERRQ(ierr);
3724be6bf707SBarry Smith   PetscFunctionReturn(0);
3725be6bf707SBarry Smith }
3726be6bf707SBarry Smith 
3727be6bf707SBarry Smith /*@
3728be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3729be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3730be6bf707SBarry Smith        nonlinear portion.
3731be6bf707SBarry Smith 
3732be6bf707SBarry Smith    Collect on Mat
3733be6bf707SBarry Smith 
3734be6bf707SBarry Smith   Input Parameters:
3735386f7cf9SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3736be6bf707SBarry Smith 
373715091d37SBarry Smith   Level: advanced
373815091d37SBarry Smith 
3739be6bf707SBarry Smith .seealso: MatStoreValues()
3740be6bf707SBarry Smith 
3741be6bf707SBarry Smith @*/
37427087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3743be6bf707SBarry Smith {
37444ac538c5SBarry Smith   PetscErrorCode ierr;
3745be6bf707SBarry Smith 
3746be6bf707SBarry Smith   PetscFunctionBegin;
37470700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3748e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3749e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
37504ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3751be6bf707SBarry Smith   PetscFunctionReturn(0);
3752be6bf707SBarry Smith }
3753be6bf707SBarry Smith 
3754f83d6046SBarry Smith 
3755be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
375617ab2063SBarry Smith /*@C
3757682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
37580d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
37596e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
376051c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
37612bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
376217ab2063SBarry Smith 
3763d083f849SBarry Smith    Collective
3764db81eaa0SLois Curfman McInnes 
376517ab2063SBarry Smith    Input Parameters:
3766db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
376717ab2063SBarry Smith .  m - number of rows
376817ab2063SBarry Smith .  n - number of columns
376917ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
377051c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
37710298fd71SBarry Smith          (possibly different for each row) or NULL
377217ab2063SBarry Smith 
377317ab2063SBarry Smith    Output Parameter:
3774416022c9SBarry Smith .  A - the matrix
377517ab2063SBarry Smith 
3776175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3777f6f02116SRichard Tran Mills    MatXXXXSetPreallocation() paradigm instead of this routine directly.
3778175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3779175b88e8SBarry Smith 
3780b259b22eSLois Curfman McInnes    Notes:
378149a6f317SBarry Smith    If nnz is given then nz is ignored
378249a6f317SBarry Smith 
378317ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
378417ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
37850002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
378644cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
378717ab2063SBarry Smith 
378817ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
37890298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
37903d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
37916da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
379217ab2063SBarry Smith 
3793682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
37944fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3795682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
37966c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
37976c7ebb05SLois Curfman McInnes 
37986c7ebb05SLois Curfman McInnes    Options Database Keys:
3799698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
38009db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
380117ab2063SBarry Smith 
3802027ccd11SLois Curfman McInnes    Level: intermediate
3803027ccd11SLois Curfman McInnes 
380469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
380536db0b34SBarry Smith 
380617ab2063SBarry Smith @*/
38077087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
380817ab2063SBarry Smith {
3809dfbe8321SBarry Smith   PetscErrorCode ierr;
38106945ee14SBarry Smith 
38113a40ed3dSBarry Smith   PetscFunctionBegin;
3812f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3813117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3814c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3815d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3816273d9f13SBarry Smith   PetscFunctionReturn(0);
3817273d9f13SBarry Smith }
3818273d9f13SBarry Smith 
3819273d9f13SBarry Smith /*@C
3820273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3821273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3822273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3823273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3824273d9f13SBarry Smith 
3825d083f849SBarry Smith    Collective
3826273d9f13SBarry Smith 
3827273d9f13SBarry Smith    Input Parameters:
38281c4f3114SJed Brown +  B - The matrix
3829273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3830273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
38310298fd71SBarry Smith          (possibly different for each row) or NULL
3832273d9f13SBarry Smith 
3833273d9f13SBarry Smith    Notes:
383449a6f317SBarry Smith      If nnz is given then nz is ignored
383549a6f317SBarry Smith 
3836273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3837273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3838273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3839273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3840273d9f13SBarry Smith 
3841273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
38420298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3843273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3844273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3845273d9f13SBarry Smith 
3846aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3847aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3848aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3849aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3850aa95bbe8SBarry Smith 
3851a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3852a96a251dSBarry Smith    entries or columns indices
3853a96a251dSBarry Smith 
3854273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3855273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3856273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3857273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3858273d9f13SBarry Smith 
3859273d9f13SBarry Smith    Options Database Keys:
3860698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
386147b2e64bSBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3862273d9f13SBarry Smith 
3863273d9f13SBarry Smith    Level: intermediate
3864273d9f13SBarry Smith 
386569b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3866273d9f13SBarry Smith 
3867273d9f13SBarry Smith @*/
38687087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3869273d9f13SBarry Smith {
38704ac538c5SBarry Smith   PetscErrorCode ierr;
3871a23d5eceSKris Buschelman 
3872a23d5eceSKris Buschelman   PetscFunctionBegin;
38736ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
38746ba663aaSJed Brown   PetscValidType(B,1);
38754ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3876a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3877a23d5eceSKris Buschelman }
3878a23d5eceSKris Buschelman 
38797087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3880a23d5eceSKris Buschelman {
3881273d9f13SBarry Smith   Mat_SeqAIJ     *b;
38822576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
38836849ba73SBarry Smith   PetscErrorCode ierr;
388497f1f81fSBarry Smith   PetscInt       i;
3885273d9f13SBarry Smith 
3886273d9f13SBarry Smith   PetscFunctionBegin;
38872576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3888a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3889c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3890c461c341SBarry Smith     nz             = 0;
3891c461c341SBarry Smith   }
389226283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
389326283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3894899cda47SBarry Smith 
3895435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
389660e0710aSBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3897*cf9c20a2SJed Brown   if (PetscUnlikelyDebug(nnz)) {
3898d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
389960e0710aSBarry 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]);
390060e0710aSBarry 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);
3901b73539f3SBarry Smith     }
3902b73539f3SBarry Smith   }
3903b73539f3SBarry Smith 
3904273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
39052205254eSKarl Rupp 
3906273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3907273d9f13SBarry Smith 
3908ab93d7beSBarry Smith   if (!skipallocation) {
39092ee49352SLisandro Dalcin     if (!b->imax) {
3910071fcb05SBarry Smith       ierr = PetscMalloc1(B->rmap->n,&b->imax);CHKERRQ(ierr);
3911071fcb05SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3912071fcb05SBarry Smith     }
3913071fcb05SBarry Smith     if (!b->ilen) {
3914071fcb05SBarry Smith       /* b->ilen will count nonzeros in each row so far. */
3915071fcb05SBarry Smith       ierr = PetscCalloc1(B->rmap->n,&b->ilen);CHKERRQ(ierr);
3916071fcb05SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3917071fcb05SBarry Smith     } else {
3918071fcb05SBarry Smith       ierr = PetscMemzero(b->ilen,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
39192ee49352SLisandro Dalcin     }
3920846b4da1SFande Kong     if (!b->ipre) {
3921846b4da1SFande Kong       ierr = PetscMalloc1(B->rmap->n,&b->ipre);CHKERRQ(ierr);
3922846b4da1SFande Kong       ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
3923846b4da1SFande Kong     }
3924273d9f13SBarry Smith     if (!nnz) {
3925435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3926c62bd62aSJed Brown       else if (nz < 0) nz = 1;
39275d2a9ed1SStefano Zampini       nz = PetscMin(nz,B->cmap->n);
3928d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3929d0f46423SBarry Smith       nz = nz*B->rmap->n;
3930273d9f13SBarry Smith     } else {
3931c73702f5SBarry Smith       PetscInt64 nz64 = 0;
3932c73702f5SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz64 += nnz[i];}
3933c73702f5SBarry Smith       ierr = PetscIntCast(nz64,&nz);CHKERRQ(ierr);
3934273d9f13SBarry Smith     }
3935ab93d7beSBarry Smith 
3936273d9f13SBarry Smith     /* allocate the matrix space */
393753dd7562SDmitry Karpeev     /* FIXME: should B's old memory be unlogged? */
39382ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3939396832f4SHong Zhang     if (B->structure_only) {
39405848002fSHong Zhang       ierr = PetscMalloc1(nz,&b->j);CHKERRQ(ierr);
39415848002fSHong Zhang       ierr = PetscMalloc1(B->rmap->n+1,&b->i);CHKERRQ(ierr);
3942396832f4SHong Zhang       ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));CHKERRQ(ierr);
3943396832f4SHong Zhang     } else {
3944dcca6d9dSJed Brown       ierr = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr);
39453bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3946396832f4SHong Zhang     }
3947bfeeae90SHong Zhang     b->i[0] = 0;
3948d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
39495da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
39505da197adSKris Buschelman     }
3951396832f4SHong Zhang     if (B->structure_only) {
3952396832f4SHong Zhang       b->singlemalloc = PETSC_FALSE;
3953396832f4SHong Zhang       b->free_a       = PETSC_FALSE;
3954396832f4SHong Zhang     } else {
3955273d9f13SBarry Smith       b->singlemalloc = PETSC_TRUE;
3956e6b907acSBarry Smith       b->free_a       = PETSC_TRUE;
3957396832f4SHong Zhang     }
3958e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3959c461c341SBarry Smith   } else {
3960e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
3961e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
3962c461c341SBarry Smith   }
3963273d9f13SBarry Smith 
3964846b4da1SFande Kong   if (b->ipre && nnz != b->ipre  && b->imax) {
3965846b4da1SFande Kong     /* reserve user-requested sparsity */
3966580bdb30SBarry Smith     ierr = PetscArraycpy(b->ipre,b->imax,B->rmap->n);CHKERRQ(ierr);
3967846b4da1SFande Kong   }
3968846b4da1SFande Kong 
3969846b4da1SFande Kong 
3970273d9f13SBarry Smith   b->nz               = 0;
3971273d9f13SBarry Smith   b->maxnz            = nz;
3972273d9f13SBarry Smith   B->info.nz_unneeded = (double)b->maxnz;
39732205254eSKarl Rupp   if (realalloc) {
39742205254eSKarl Rupp     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
39752205254eSKarl Rupp   }
3976cb7b82ddSBarry Smith   B->was_assembled = PETSC_FALSE;
3977cb7b82ddSBarry Smith   B->assembled     = PETSC_FALSE;
3978273d9f13SBarry Smith   PetscFunctionReturn(0);
3979273d9f13SBarry Smith }
3980273d9f13SBarry Smith 
3981846b4da1SFande Kong 
3982846b4da1SFande Kong PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
3983846b4da1SFande Kong {
3984846b4da1SFande Kong   Mat_SeqAIJ     *a;
3985a5bbaf83SFande Kong   PetscInt       i;
3986846b4da1SFande Kong   PetscErrorCode ierr;
3987846b4da1SFande Kong 
3988846b4da1SFande Kong   PetscFunctionBegin;
3989846b4da1SFande Kong   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
399014d0e64fSAlex Lindsay 
399114d0e64fSAlex Lindsay   /* Check local size. If zero, then return */
399214d0e64fSAlex Lindsay   if (!A->rmap->n) PetscFunctionReturn(0);
399314d0e64fSAlex Lindsay 
3994846b4da1SFande Kong   a = (Mat_SeqAIJ*)A->data;
39952c814fdeSFande Kong   /* if no saved info, we error out */
3996fb4dc15dSAlex Lindsay   if (!a->ipre) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"No saved preallocation info \n");
39972c814fdeSFande Kong 
3998fb4dc15dSAlex Lindsay   if (!a->i || !a->j || !a->a || !a->imax || !a->ilen) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Memory info is incomplete, and can not reset preallocation \n");
39992c814fdeSFande Kong 
4000580bdb30SBarry Smith   ierr = PetscArraycpy(a->imax,a->ipre,A->rmap->n);CHKERRQ(ierr);
4001580bdb30SBarry Smith   ierr = PetscArrayzero(a->ilen,A->rmap->n);CHKERRQ(ierr);
4002846b4da1SFande Kong   a->i[0] = 0;
4003846b4da1SFande Kong   for (i=1; i<A->rmap->n+1; i++) {
4004846b4da1SFande Kong     a->i[i] = a->i[i-1] + a->imax[i-1];
4005846b4da1SFande Kong   }
4006846b4da1SFande Kong   A->preallocated     = PETSC_TRUE;
4007846b4da1SFande Kong   a->nz               = 0;
4008846b4da1SFande Kong   a->maxnz            = a->i[A->rmap->n];
4009846b4da1SFande Kong   A->info.nz_unneeded = (double)a->maxnz;
4010846b4da1SFande Kong   A->was_assembled    = PETSC_FALSE;
4011846b4da1SFande Kong   A->assembled        = PETSC_FALSE;
4012846b4da1SFande Kong   PetscFunctionReturn(0);
4013846b4da1SFande Kong }
4014846b4da1SFande Kong 
401558d36128SBarry Smith /*@
4016a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
4017a1661176SMatthew Knepley 
4018a1661176SMatthew Knepley    Input Parameters:
4019a1661176SMatthew Knepley +  B - the matrix
4020a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
4021a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
4022a1661176SMatthew Knepley -  v - optional values in the matrix
4023a1661176SMatthew Knepley 
4024a1661176SMatthew Knepley    Level: developer
4025a1661176SMatthew Knepley 
402658d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
402758d36128SBarry Smith 
4028c1c1d628SHong Zhang .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ
4029a1661176SMatthew Knepley @*/
4030a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
4031a1661176SMatthew Knepley {
4032a1661176SMatthew Knepley   PetscErrorCode ierr;
4033a1661176SMatthew Knepley 
4034a1661176SMatthew Knepley   PetscFunctionBegin;
40350700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
40366ba663aaSJed Brown   PetscValidType(B,1);
40374ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
4038a1661176SMatthew Knepley   PetscFunctionReturn(0);
4039a1661176SMatthew Knepley }
4040a1661176SMatthew Knepley 
40417087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
4042a1661176SMatthew Knepley {
4043a1661176SMatthew Knepley   PetscInt       i;
4044a1661176SMatthew Knepley   PetscInt       m,n;
4045a1661176SMatthew Knepley   PetscInt       nz;
4046a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
4047a1661176SMatthew Knepley   PetscErrorCode ierr;
4048a1661176SMatthew Knepley 
4049a1661176SMatthew Knepley   PetscFunctionBegin;
405065e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
4051779a8d59SSatish Balay 
4052779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
4053779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
4054779a8d59SSatish Balay 
4055779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
4056854ce69bSBarry Smith   ierr = PetscMalloc1(m+1, &nnz);CHKERRQ(ierr);
4057a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
4058b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
4059a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
406065e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
4061a1661176SMatthew Knepley     nnz[i] = nz;
4062a1661176SMatthew Knepley   }
4063a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
4064a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
4065a1661176SMatthew Knepley 
4066a1661176SMatthew Knepley   for (i = 0; i < m; i++) {
4067071fcb05SBarry Smith     ierr = MatSetValues_SeqAIJ(B, 1, &i, Ii[i+1] - Ii[i], J+Ii[i], v ? v + Ii[i] : NULL, INSERT_VALUES);CHKERRQ(ierr);
4068a1661176SMatthew Knepley   }
4069a1661176SMatthew Knepley 
4070a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4071a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4072a1661176SMatthew Knepley 
40737827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
4074a1661176SMatthew Knepley   PetscFunctionReturn(0);
4075a1661176SMatthew Knepley }
4076a1661176SMatthew Knepley 
4077c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
4078af0996ceSBarry Smith #include <petsc/private/kernels/petscaxpy.h>
4079170fe5c8SBarry Smith 
4080170fe5c8SBarry Smith /*
4081170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
4082170fe5c8SBarry Smith 
4083170fe5c8SBarry Smith                n                       p                          p
4084170fe5c8SBarry Smith         (              )       (              )         (                  )
4085170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
4086170fe5c8SBarry Smith         (              )       (              )         (                  )
4087170fe5c8SBarry Smith 
4088170fe5c8SBarry Smith */
4089170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
4090170fe5c8SBarry Smith {
4091170fe5c8SBarry Smith   PetscErrorCode    ierr;
4092170fe5c8SBarry Smith   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
4093170fe5c8SBarry Smith   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
4094170fe5c8SBarry Smith   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
40951de00fd4SBarry Smith   PetscInt          i,n,m,q,p;
4096170fe5c8SBarry Smith   const PetscInt    *ii,*idx;
4097170fe5c8SBarry Smith   const PetscScalar *b,*a,*a_q;
4098170fe5c8SBarry Smith   PetscScalar       *c,*c_q;
4099170fe5c8SBarry Smith 
4100170fe5c8SBarry Smith   PetscFunctionBegin;
4101d0f46423SBarry Smith   m    = A->rmap->n;
4102d0f46423SBarry Smith   n    = A->cmap->n;
4103d0f46423SBarry Smith   p    = B->cmap->n;
4104170fe5c8SBarry Smith   a    = sub_a->v;
4105170fe5c8SBarry Smith   b    = sub_b->a;
4106170fe5c8SBarry Smith   c    = sub_c->v;
4107580bdb30SBarry Smith   ierr = PetscArrayzero(c,m*p);CHKERRQ(ierr);
4108170fe5c8SBarry Smith 
4109170fe5c8SBarry Smith   ii  = sub_b->i;
4110170fe5c8SBarry Smith   idx = sub_b->j;
4111170fe5c8SBarry Smith   for (i=0; i<n; i++) {
4112170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
4113170fe5c8SBarry Smith     while (q-->0) {
4114170fe5c8SBarry Smith       c_q = c + m*(*idx);
4115170fe5c8SBarry Smith       a_q = a + m*i;
4116854c7f52SBarry Smith       PetscKernelAXPY(c_q,*b,a_q,m);
4117170fe5c8SBarry Smith       idx++;
4118170fe5c8SBarry Smith       b++;
4119170fe5c8SBarry Smith     }
4120170fe5c8SBarry Smith   }
4121170fe5c8SBarry Smith   PetscFunctionReturn(0);
4122170fe5c8SBarry Smith }
4123170fe5c8SBarry Smith 
41244222ddf1SHong Zhang PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat C)
4125170fe5c8SBarry Smith {
4126170fe5c8SBarry Smith   PetscErrorCode ierr;
4127d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
4128170fe5c8SBarry Smith 
4129170fe5c8SBarry Smith   PetscFunctionBegin;
413060e0710aSBarry 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);
41314222ddf1SHong Zhang   ierr = MatSetSizes(C,m,n,m,n);CHKERRQ(ierr);
41324222ddf1SHong Zhang   ierr = MatSetBlockSizesFromMats(C,A,B);CHKERRQ(ierr);
41334222ddf1SHong Zhang   ierr = MatSetType(C,MATSEQDENSE);CHKERRQ(ierr);
41344222ddf1SHong Zhang   ierr = MatSeqDenseSetPreallocation(C,NULL);CHKERRQ(ierr);
4135d73949e8SHong Zhang 
41364222ddf1SHong Zhang   C->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
4137170fe5c8SBarry Smith   PetscFunctionReturn(0);
4138170fe5c8SBarry Smith }
4139170fe5c8SBarry Smith 
4140170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
41410bad9183SKris Buschelman /*MC
4142fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
41430bad9183SKris Buschelman    based on compressed sparse row format.
41440bad9183SKris Buschelman 
41450bad9183SKris Buschelman    Options Database Keys:
41460bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
41470bad9183SKris Buschelman 
41480bad9183SKris Buschelman    Level: beginner
41490bad9183SKris Buschelman 
41500cd7f59aSBarry Smith    Notes:
41510cd7f59aSBarry Smith     MatSetValues() may be called for this matrix type with a NULL argument for the numerical values,
41520cd7f59aSBarry Smith     in this case the values associated with the rows and columns one passes in are set to zero
41530cd7f59aSBarry Smith     in the matrix
41540cd7f59aSBarry Smith 
41550cd7f59aSBarry Smith     MatSetOptions(,MAT_STRUCTURE_ONLY,PETSC_TRUE) may be called for this matrix type. In this no
41560cd7f59aSBarry Smith     space is allocated for the nonzero entries and any entries passed with MatSetValues() are ignored
41570cd7f59aSBarry Smith 
41580cd7f59aSBarry Smith   Developer Notes:
41590cd7f59aSBarry Smith     It would be nice if all matrix formats supported passing NULL in for the numerical values
41600cd7f59aSBarry Smith 
4161f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
41620bad9183SKris Buschelman M*/
41630bad9183SKris Buschelman 
4164ccd284c7SBarry Smith /*MC
4165ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
4166ccd284c7SBarry Smith 
4167ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
4168ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
41690cd7f59aSBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation() is supported
4170ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
4171ccd284c7SBarry Smith   the above preallocation routines for simplicity.
4172ccd284c7SBarry Smith 
4173ccd284c7SBarry Smith    Options Database Keys:
4174ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
4175ccd284c7SBarry Smith 
417695452b02SPatrick Sanan   Developer Notes:
4177ca9cdca7SRichard Tran Mills     Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
4178ccd284c7SBarry Smith    enough exist.
4179ccd284c7SBarry Smith 
4180ccd284c7SBarry Smith   Level: beginner
4181ccd284c7SBarry Smith 
4182ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
4183ccd284c7SBarry Smith M*/
4184ccd284c7SBarry Smith 
4185ccd284c7SBarry Smith /*MC
4186ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
4187ccd284c7SBarry Smith 
4188ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
4189ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
4190ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
4191ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
4192ccd284c7SBarry Smith   the above preallocation routines for simplicity.
4193ccd284c7SBarry Smith 
4194ccd284c7SBarry Smith    Options Database Keys:
4195ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
4196ccd284c7SBarry Smith 
4197ccd284c7SBarry Smith   Level: beginner
4198ccd284c7SBarry Smith 
4199ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4200ccd284c7SBarry Smith M*/
4201ccd284c7SBarry Smith 
42027906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
42037906f579SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
42047906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
42057906f579SHong Zhang #endif
42067906f579SHong Zhang #if defined(PETSC_HAVE_HYPRE)
42077906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*);
42087906f579SHong Zhang #endif
42097906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);
42107906f579SHong Zhang 
4211d4002b98SHong Zhang PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*);
4212c9225affSStefano Zampini PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
42134222ddf1SHong Zhang PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);
42147906f579SHong Zhang 
42158c778c55SBarry Smith /*@C
42168f1ea47aSStefano Zampini    MatSeqAIJGetArray - gives read/write access to the array where the data for a MATSEQAIJ matrix is stored
42178c778c55SBarry Smith 
42188c778c55SBarry Smith    Not Collective
42198c778c55SBarry Smith 
42208c778c55SBarry Smith    Input Parameter:
4221579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
42228c778c55SBarry Smith 
42238c778c55SBarry Smith    Output Parameter:
42248c778c55SBarry Smith .   array - pointer to the data
42258c778c55SBarry Smith 
42268c778c55SBarry Smith    Level: intermediate
42278c778c55SBarry Smith 
4228774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
42298c778c55SBarry Smith @*/
42308c778c55SBarry Smith PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
42318c778c55SBarry Smith {
42328c778c55SBarry Smith   PetscErrorCode ierr;
42338c778c55SBarry Smith 
42348c778c55SBarry Smith   PetscFunctionBegin;
42358c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
42368c778c55SBarry Smith   PetscFunctionReturn(0);
42378c778c55SBarry Smith }
42388c778c55SBarry Smith 
423921e72a00SBarry Smith /*@C
42408f1ea47aSStefano Zampini    MatSeqAIJGetArrayRead - gives read-only access to the array where the data for a MATSEQAIJ matrix is stored
42418f1ea47aSStefano Zampini 
42428f1ea47aSStefano Zampini    Not Collective
42438f1ea47aSStefano Zampini 
42448f1ea47aSStefano Zampini    Input Parameter:
42458f1ea47aSStefano Zampini .  mat - a MATSEQAIJ matrix
42468f1ea47aSStefano Zampini 
42478f1ea47aSStefano Zampini    Output Parameter:
42488f1ea47aSStefano Zampini .   array - pointer to the data
42498f1ea47aSStefano Zampini 
42508f1ea47aSStefano Zampini    Level: intermediate
42518f1ea47aSStefano Zampini 
42528f1ea47aSStefano Zampini .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayRead()
42538f1ea47aSStefano Zampini @*/
42548f1ea47aSStefano Zampini PetscErrorCode  MatSeqAIJGetArrayRead(Mat A,const PetscScalar **array)
42558f1ea47aSStefano Zampini {
42568f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4257c70f7ee4SJunchao Zhang   PetscOffloadMask oval;
42588f1ea47aSStefano Zampini #endif
42598f1ea47aSStefano Zampini   PetscErrorCode ierr;
42608f1ea47aSStefano Zampini 
42618f1ea47aSStefano Zampini   PetscFunctionBegin;
42628f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4263c70f7ee4SJunchao Zhang   oval = A->offloadmask;
42648f1ea47aSStefano Zampini #endif
42658f1ea47aSStefano Zampini   ierr = MatSeqAIJGetArray(A,(PetscScalar**)array);CHKERRQ(ierr);
42668f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4267c70f7ee4SJunchao Zhang   if (oval == PETSC_OFFLOAD_GPU || oval == PETSC_OFFLOAD_BOTH) A->offloadmask = PETSC_OFFLOAD_BOTH;
42688f1ea47aSStefano Zampini #endif
42698f1ea47aSStefano Zampini   PetscFunctionReturn(0);
42708f1ea47aSStefano Zampini }
42718f1ea47aSStefano Zampini 
42728f1ea47aSStefano Zampini /*@C
42738f1ea47aSStefano Zampini    MatSeqAIJRestoreArrayRead - restore the read-only access array obtained from MatSeqAIJGetArrayRead
42748f1ea47aSStefano Zampini 
42758f1ea47aSStefano Zampini    Not Collective
42768f1ea47aSStefano Zampini 
42778f1ea47aSStefano Zampini    Input Parameter:
42788f1ea47aSStefano Zampini .  mat - a MATSEQAIJ matrix
42798f1ea47aSStefano Zampini 
42808f1ea47aSStefano Zampini    Output Parameter:
42818f1ea47aSStefano Zampini .   array - pointer to the data
42828f1ea47aSStefano Zampini 
42838f1ea47aSStefano Zampini    Level: intermediate
42848f1ea47aSStefano Zampini 
42858f1ea47aSStefano Zampini .seealso: MatSeqAIJGetArray(), MatSeqAIJGetArrayRead()
42868f1ea47aSStefano Zampini @*/
42878f1ea47aSStefano Zampini PetscErrorCode  MatSeqAIJRestoreArrayRead(Mat A,const PetscScalar **array)
42888f1ea47aSStefano Zampini {
42898f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4290c70f7ee4SJunchao Zhang   PetscOffloadMask oval;
42918f1ea47aSStefano Zampini #endif
42928f1ea47aSStefano Zampini   PetscErrorCode ierr;
42938f1ea47aSStefano Zampini 
42948f1ea47aSStefano Zampini   PetscFunctionBegin;
42958f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4296c70f7ee4SJunchao Zhang   oval = A->offloadmask;
42978f1ea47aSStefano Zampini #endif
42988f1ea47aSStefano Zampini   ierr = MatSeqAIJRestoreArray(A,(PetscScalar**)array);CHKERRQ(ierr);
42998f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4300c70f7ee4SJunchao Zhang   A->offloadmask = oval;
43018f1ea47aSStefano Zampini #endif
43028f1ea47aSStefano Zampini   PetscFunctionReturn(0);
43038f1ea47aSStefano Zampini }
43048f1ea47aSStefano Zampini 
43058f1ea47aSStefano Zampini /*@C
430621e72a00SBarry Smith    MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
430721e72a00SBarry Smith 
430821e72a00SBarry Smith    Not Collective
430921e72a00SBarry Smith 
431021e72a00SBarry Smith    Input Parameter:
4311579dbff0SBarry Smith .  mat - a MATSEQAIJ matrix
431221e72a00SBarry Smith 
431321e72a00SBarry Smith    Output Parameter:
431421e72a00SBarry Smith .   nz - the maximum number of nonzeros in any row
431521e72a00SBarry Smith 
431621e72a00SBarry Smith    Level: intermediate
431721e72a00SBarry Smith 
431821e72a00SBarry Smith .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
431921e72a00SBarry Smith @*/
432021e72a00SBarry Smith PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
432121e72a00SBarry Smith {
432221e72a00SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
432321e72a00SBarry Smith 
432421e72a00SBarry Smith   PetscFunctionBegin;
432521e72a00SBarry Smith   *nz = aij->rmax;
432621e72a00SBarry Smith   PetscFunctionReturn(0);
432721e72a00SBarry Smith }
432821e72a00SBarry Smith 
43298c778c55SBarry Smith /*@C
4330579dbff0SBarry Smith    MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray()
43318c778c55SBarry Smith 
43328c778c55SBarry Smith    Not Collective
43338c778c55SBarry Smith 
43348c778c55SBarry Smith    Input Parameters:
4335a2b725a8SWilliam Gropp +  mat - a MATSEQAIJ matrix
4336a2b725a8SWilliam Gropp -  array - pointer to the data
43378c778c55SBarry Smith 
43388c778c55SBarry Smith    Level: intermediate
43398c778c55SBarry Smith 
4340774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
43418c778c55SBarry Smith @*/
43428c778c55SBarry Smith PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
43438c778c55SBarry Smith {
43448c778c55SBarry Smith   PetscErrorCode ierr;
43458c778c55SBarry Smith 
43468c778c55SBarry Smith   PetscFunctionBegin;
43478c778c55SBarry Smith   ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr);
43488c778c55SBarry Smith   PetscFunctionReturn(0);
43498c778c55SBarry Smith }
43508c778c55SBarry Smith 
435134b5b067SBarry Smith #if defined(PETSC_HAVE_CUDA)
435202fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat);
435302fe1965SBarry Smith #endif
435402fe1965SBarry Smith 
43558cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4356273d9f13SBarry Smith {
4357273d9f13SBarry Smith   Mat_SeqAIJ     *b;
4358dfbe8321SBarry Smith   PetscErrorCode ierr;
435938baddfdSBarry Smith   PetscMPIInt    size;
4360273d9f13SBarry Smith 
4361273d9f13SBarry Smith   PetscFunctionBegin;
4362ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
4363e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
4364273d9f13SBarry Smith 
4365b00a9115SJed Brown   ierr = PetscNewLog(B,&b);CHKERRQ(ierr);
43662205254eSKarl Rupp 
4367b0a32e0cSBarry Smith   B->data = (void*)b;
43682205254eSKarl Rupp 
4369549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
4370071fcb05SBarry Smith   if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
43712205254eSKarl Rupp 
4372416022c9SBarry Smith   b->row                = 0;
4373416022c9SBarry Smith   b->col                = 0;
437482bf6240SBarry Smith   b->icol               = 0;
4375b810aeb4SBarry Smith   b->reallocs           = 0;
437636db0b34SBarry Smith   b->ignorezeroentries  = PETSC_FALSE;
4377f1e2ffcdSBarry Smith   b->roworiented        = PETSC_TRUE;
4378416022c9SBarry Smith   b->nonew              = 0;
4379416022c9SBarry Smith   b->diag               = 0;
4380416022c9SBarry Smith   b->solve_work         = 0;
43812a1b7f2aSHong Zhang   B->spptr              = 0;
4382be6bf707SBarry Smith   b->saved_values       = 0;
4383d7f994e1SBarry Smith   b->idiag              = 0;
438471f1c65dSBarry Smith   b->mdiag              = 0;
438571f1c65dSBarry Smith   b->ssor_work          = 0;
438671f1c65dSBarry Smith   b->omega              = 1.0;
438771f1c65dSBarry Smith   b->fshift             = 0.0;
438871f1c65dSBarry Smith   b->idiagvalid         = PETSC_FALSE;
4389bbead8a2SBarry Smith   b->ibdiagvalid        = PETSC_FALSE;
4390a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
439117ab2063SBarry Smith 
439235d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
4393bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr);
4394bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr);
43958c778c55SBarry Smith 
4396b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4397bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
4398bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
4399b3866ffcSBarry Smith #endif
440017f1a0eaSHong Zhang 
4401bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
4402bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
4403bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
4404bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
4405bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
4406bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
44074dfdc2d9SRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijsell_C",MatConvert_SeqAIJ_SeqAIJSELL);CHKERRQ(ierr);
44089779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE)
44094a2a386eSRichard Tran Mills   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr);
4410191b95cbSRichard Tran Mills #endif
441134b5b067SBarry Smith #if defined(PETSC_HAVE_CUDA)
441202fe1965SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcusparse_C",MatConvert_SeqAIJ_SeqAIJCUSPARSE);CHKERRQ(ierr);
44134222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaijcusparse_seqaij_C",MatProductSetFromOptions_SeqAIJ);CHKERRQ(ierr);
441402fe1965SBarry Smith #endif
4415bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
4416af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
4417af8000cdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);CHKERRQ(ierr);
4418af8000cdSHong Zhang #endif
441963c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE)
442063c07aadSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);CHKERRQ(ierr);
44214222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_transpose_seqaij_seqaij_C",MatProductSetFromOptions_Transpose_AIJ_AIJ);CHKERRQ(ierr);
442263c07aadSStefano Zampini #endif
4423b49cda9fSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);CHKERRQ(ierr);
4424d4002b98SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);CHKERRQ(ierr);
4425c9225affSStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_is_C",MatConvert_XAIJ_IS);CHKERRQ(ierr);
4426bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4427bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
4428bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
4429846b4da1SFande Kong   ierr = PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);CHKERRQ(ierr);
4430bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
4431bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
4432bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
4433bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
44344222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_is_seqaij_C",MatProductSetFromOptions_IS_XAIJ);CHKERRQ(ierr);
44354222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqdense_seqaij_C",MatProductSetFromOptions_SeqDense_SeqAIJ);CHKERRQ(ierr);
44364222ddf1SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaij_seqaij_C",MatProductSetFromOptions_SeqAIJ);CHKERRQ(ierr);
44374108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
443817667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
44394099cc6bSBarry Smith   ierr = MatSeqAIJSetTypeFromOptions(B);CHKERRQ(ierr);  /* this allows changing the matrix subtype to say MATSEQAIJPERM */
44403a40ed3dSBarry Smith   PetscFunctionReturn(0);
444117ab2063SBarry Smith }
444217ab2063SBarry Smith 
4443b24902e0SBarry Smith /*
4444b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4445b24902e0SBarry Smith */
4446ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
444717ab2063SBarry Smith {
4448416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
44496849ba73SBarry Smith   PetscErrorCode ierr;
4450071fcb05SBarry Smith   PetscInt       m = A->rmap->n,i;
445117ab2063SBarry Smith 
44523a40ed3dSBarry Smith   PetscFunctionBegin;
4453273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
4454273d9f13SBarry Smith 
4455d5f3da31SBarry Smith   C->factortype = A->factortype;
4456416022c9SBarry Smith   c->row        = 0;
4457416022c9SBarry Smith   c->col        = 0;
445882bf6240SBarry Smith   c->icol       = 0;
44596ad4291fSHong Zhang   c->reallocs   = 0;
446017ab2063SBarry Smith 
44616ad4291fSHong Zhang   C->assembled = PETSC_TRUE;
446217ab2063SBarry Smith 
4463aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
4464aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
4465eec197d1SBarry Smith 
4466071fcb05SBarry Smith   ierr = PetscMalloc1(m,&c->imax);CHKERRQ(ierr);
4467071fcb05SBarry Smith   ierr = PetscMemcpy(c->imax,a->imax,m*sizeof(PetscInt));CHKERRQ(ierr);
4468071fcb05SBarry Smith   ierr = PetscMalloc1(m,&c->ilen);CHKERRQ(ierr);
4469071fcb05SBarry Smith   ierr = PetscMemcpy(c->ilen,a->ilen,m*sizeof(PetscInt));CHKERRQ(ierr);
44703bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
447117ab2063SBarry Smith 
447217ab2063SBarry Smith   /* allocate the matrix space */
4473f77e22a1SHong Zhang   if (mallocmatspace) {
4474dcca6d9dSJed Brown     ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr);
44753bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
44762205254eSKarl Rupp 
4477f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
44782205254eSKarl Rupp 
4479580bdb30SBarry Smith     ierr = PetscArraycpy(c->i,a->i,m+1);CHKERRQ(ierr);
448017ab2063SBarry Smith     if (m > 0) {
4481580bdb30SBarry Smith       ierr = PetscArraycpy(c->j,a->j,a->i[m]);CHKERRQ(ierr);
4482be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4483580bdb30SBarry Smith         ierr = PetscArraycpy(c->a,a->a,a->i[m]);CHKERRQ(ierr);
4484be6bf707SBarry Smith       } else {
4485580bdb30SBarry Smith         ierr = PetscArrayzero(c->a,a->i[m]);CHKERRQ(ierr);
448617ab2063SBarry Smith       }
448708480c60SBarry Smith     }
4488f77e22a1SHong Zhang   }
448917ab2063SBarry Smith 
44906ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4491416022c9SBarry Smith   c->roworiented       = a->roworiented;
4492416022c9SBarry Smith   c->nonew             = a->nonew;
4493416022c9SBarry Smith   if (a->diag) {
4494854ce69bSBarry Smith     ierr = PetscMalloc1(m+1,&c->diag);CHKERRQ(ierr);
4495071fcb05SBarry Smith     ierr = PetscMemcpy(c->diag,a->diag,m*sizeof(PetscInt));CHKERRQ(ierr);
44963bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
4497071fcb05SBarry Smith   } else c->diag = NULL;
44982205254eSKarl Rupp 
44996ad4291fSHong Zhang   c->solve_work         = 0;
45006ad4291fSHong Zhang   c->saved_values       = 0;
45016ad4291fSHong Zhang   c->idiag              = 0;
450271f1c65dSBarry Smith   c->ssor_work          = 0;
4503a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
4504e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
4505e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
45066ad4291fSHong Zhang 
4507893ad86cSHong Zhang   c->rmax         = a->rmax;
4508416022c9SBarry Smith   c->nz           = a->nz;
45098ed568f8SMatthew G Knepley   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4510273d9f13SBarry Smith   C->preallocated = PETSC_TRUE;
4511754ec7b1SSatish Balay 
45126ad4291fSHong Zhang   c->compressedrow.use   = a->compressedrow.use;
45136ad4291fSHong Zhang   c->compressedrow.nrows = a->compressedrow.nrows;
4514cd6b891eSBarry Smith   if (a->compressedrow.use) {
45156ad4291fSHong Zhang     i    = a->compressedrow.nrows;
4516dcca6d9dSJed Brown     ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr);
4517580bdb30SBarry Smith     ierr = PetscArraycpy(c->compressedrow.i,a->compressedrow.i,i+1);CHKERRQ(ierr);
4518580bdb30SBarry Smith     ierr = PetscArraycpy(c->compressedrow.rindex,a->compressedrow.rindex,i);CHKERRQ(ierr);
451927ea64f8SHong Zhang   } else {
452027ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
45210298fd71SBarry Smith     c->compressedrow.i      = NULL;
45220298fd71SBarry Smith     c->compressedrow.rindex = NULL;
45236ad4291fSHong Zhang   }
4524ea632784SBarry Smith   c->nonzerorowcnt = a->nonzerorowcnt;
4525e56f5c9eSBarry Smith   C->nonzerostate  = A->nonzerostate;
45264846f1f5SKris Buschelman 
45272205254eSKarl Rupp   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
4528140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
45293a40ed3dSBarry Smith   PetscFunctionReturn(0);
453017ab2063SBarry Smith }
453117ab2063SBarry Smith 
4532b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4533b24902e0SBarry Smith {
4534b24902e0SBarry Smith   PetscErrorCode ierr;
4535b24902e0SBarry Smith 
4536b24902e0SBarry Smith   PetscFunctionBegin;
4537ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
45384b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4539cfd3f464SBarry Smith   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
454033d57670SJed Brown     ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr);
4541cfd3f464SBarry Smith   }
4542a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4543f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4544b24902e0SBarry Smith   PetscFunctionReturn(0);
4545b24902e0SBarry Smith }
4546b24902e0SBarry Smith 
4547112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4548fbdbba38SShri Abhyankar {
454952f91c60SVaclav Hapla   PetscBool      isbinary, ishdf5;
455052f91c60SVaclav Hapla   PetscErrorCode ierr;
455152f91c60SVaclav Hapla 
455252f91c60SVaclav Hapla   PetscFunctionBegin;
455352f91c60SVaclav Hapla   PetscValidHeaderSpecific(newMat,MAT_CLASSID,1);
455452f91c60SVaclav Hapla   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4555c27b3999SVaclav Hapla   /* force binary viewer to load .info file if it has not yet done so */
4556c27b3999SVaclav Hapla   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
455752f91c60SVaclav Hapla   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
455852f91c60SVaclav Hapla   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
455952f91c60SVaclav Hapla   if (isbinary) {
456052f91c60SVaclav Hapla     ierr = MatLoad_SeqAIJ_Binary(newMat,viewer);CHKERRQ(ierr);
456152f91c60SVaclav Hapla   } else if (ishdf5) {
456252f91c60SVaclav Hapla #if defined(PETSC_HAVE_HDF5)
456352f91c60SVaclav Hapla     ierr = MatLoad_AIJ_HDF5(newMat,viewer);CHKERRQ(ierr);
456452f91c60SVaclav Hapla #else
456552f91c60SVaclav Hapla     SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
456652f91c60SVaclav Hapla #endif
456752f91c60SVaclav Hapla   } else {
456852f91c60SVaclav 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);
456952f91c60SVaclav Hapla   }
457052f91c60SVaclav Hapla   PetscFunctionReturn(0);
457152f91c60SVaclav Hapla }
457252f91c60SVaclav Hapla 
45733ea6fe3dSLisandro Dalcin PetscErrorCode MatLoad_SeqAIJ_Binary(Mat mat, PetscViewer viewer)
457452f91c60SVaclav Hapla {
45753ea6fe3dSLisandro Dalcin   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)mat->data;
4576fbdbba38SShri Abhyankar   PetscErrorCode ierr;
45773ea6fe3dSLisandro Dalcin   PetscInt       header[4],*rowlens,M,N,nz,sum,rows,cols,i;
4578fbdbba38SShri Abhyankar 
4579fbdbba38SShri Abhyankar   PetscFunctionBegin;
45803ea6fe3dSLisandro Dalcin   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
4581bbead8a2SBarry Smith 
45823ea6fe3dSLisandro Dalcin   /* read in matrix header */
45833ea6fe3dSLisandro Dalcin   ierr = PetscViewerBinaryRead(viewer,header,4,NULL,PETSC_INT);CHKERRQ(ierr);
45843ea6fe3dSLisandro Dalcin   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Not a matrix object in file");
4585fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
45863ea6fe3dSLisandro Dalcin   if (M < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix row size (%D) in file is negative",M);
45873ea6fe3dSLisandro Dalcin   if (N < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix column size (%D) in file is negative",N);
4588bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk, cannot load as SeqAIJ");
4589fbdbba38SShri Abhyankar 
45903ea6fe3dSLisandro Dalcin   /* set block sizes from the viewer's .info file */
45913ea6fe3dSLisandro Dalcin   ierr = MatLoad_Binary_BlockSizes(mat,viewer);CHKERRQ(ierr);
45923ea6fe3dSLisandro Dalcin   /* set local and global sizes if not set already */
45933ea6fe3dSLisandro Dalcin   if (mat->rmap->n < 0) mat->rmap->n = M;
45943ea6fe3dSLisandro Dalcin   if (mat->cmap->n < 0) mat->cmap->n = N;
45953ea6fe3dSLisandro Dalcin   if (mat->rmap->N < 0) mat->rmap->N = M;
45963ea6fe3dSLisandro Dalcin   if (mat->cmap->N < 0) mat->cmap->N = N;
45973ea6fe3dSLisandro Dalcin   ierr = PetscLayoutSetUp(mat->rmap);CHKERRQ(ierr);
45983ea6fe3dSLisandro Dalcin   ierr = PetscLayoutSetUp(mat->cmap);CHKERRQ(ierr);
45993ea6fe3dSLisandro Dalcin 
46003ea6fe3dSLisandro Dalcin   /* check if the matrix sizes are correct */
46013ea6fe3dSLisandro Dalcin   ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr);
46023ea6fe3dSLisandro Dalcin   if (M != rows || N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different sizes (%D, %D) than the input matrix (%D, %D)",M,N,rows,cols);
46033ea6fe3dSLisandro Dalcin 
4604fbdbba38SShri Abhyankar   /* read in row lengths */
46053ea6fe3dSLisandro Dalcin   ierr = PetscMalloc1(M,&rowlens);CHKERRQ(ierr);
46063ea6fe3dSLisandro Dalcin   ierr = PetscViewerBinaryRead(viewer,rowlens,M,NULL,PETSC_INT);CHKERRQ(ierr);
46073ea6fe3dSLisandro Dalcin   /* check if sum(rowlens) is same as nz */
46083ea6fe3dSLisandro Dalcin   sum = 0; for (i=0; i<M; i++) sum += rowlens[i];
46093ea6fe3dSLisandro Dalcin   if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Inconsistent matrix data in file: nonzeros = %D, sum-row-lengths = %D\n",nz,sum);
46103ea6fe3dSLisandro Dalcin   /* preallocate and check sizes */
46113ea6fe3dSLisandro Dalcin   ierr = MatSeqAIJSetPreallocation_SeqAIJ(mat,0,rowlens);CHKERRQ(ierr);
46123ea6fe3dSLisandro Dalcin   ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr);
461360e0710aSBarry 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);
46143ea6fe3dSLisandro Dalcin   /* store row lengths */
46153ea6fe3dSLisandro Dalcin   ierr = PetscArraycpy(a->ilen,rowlens,M);CHKERRQ(ierr);
46163ea6fe3dSLisandro Dalcin   ierr = PetscFree(rowlens);CHKERRQ(ierr);
4617fbdbba38SShri Abhyankar 
46183ea6fe3dSLisandro Dalcin   /* fill in "i" row pointers */
46193ea6fe3dSLisandro Dalcin   a->i[0] = 0; for (i=0; i<M; i++) a->i[i+1] = a->i[i] + a->ilen[i];
46203ea6fe3dSLisandro Dalcin   /* read in "j" column indices */
46213ea6fe3dSLisandro Dalcin   ierr = PetscViewerBinaryRead(viewer,a->j,nz,NULL,PETSC_INT);CHKERRQ(ierr);
46223ea6fe3dSLisandro Dalcin   /* read in "a" nonzero values */
46233ea6fe3dSLisandro Dalcin   ierr = PetscViewerBinaryRead(viewer,a->a,nz,NULL,PETSC_SCALAR);CHKERRQ(ierr);
4624fbdbba38SShri Abhyankar 
46253ea6fe3dSLisandro Dalcin   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
46263ea6fe3dSLisandro Dalcin   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4627fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4628fbdbba38SShri Abhyankar }
4629fbdbba38SShri Abhyankar 
4630ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
46317264ac53SSatish Balay {
46327264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4633dfbe8321SBarry Smith   PetscErrorCode ierr;
4634eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4635eeffb40dSHong Zhang   PetscInt k;
4636eeffb40dSHong Zhang #endif
46377264ac53SSatish Balay 
46383a40ed3dSBarry Smith   PetscFunctionBegin;
4639bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4640d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4641ca44d042SBarry Smith     *flg = PETSC_FALSE;
4642ca44d042SBarry Smith     PetscFunctionReturn(0);
4643bcd2baecSBarry Smith   }
46447264ac53SSatish Balay 
46457264ac53SSatish Balay   /* if the a->i are the same */
4646580bdb30SBarry Smith   ierr = PetscArraycmp(a->i,b->i,A->rmap->n+1,flg);CHKERRQ(ierr);
4647abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
46487264ac53SSatish Balay 
46497264ac53SSatish Balay   /* if a->j are the same */
4650580bdb30SBarry Smith   ierr = PetscArraycmp(a->j,b->j,a->nz,flg);CHKERRQ(ierr);
4651abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4652bcd2baecSBarry Smith 
4653bcd2baecSBarry Smith   /* if a->a are the same */
4654eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4655eeffb40dSHong Zhang   for (k=0; k<a->nz; k++) {
4656eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4657eeffb40dSHong Zhang       *flg = PETSC_FALSE;
46583a40ed3dSBarry Smith       PetscFunctionReturn(0);
4659eeffb40dSHong Zhang     }
4660eeffb40dSHong Zhang   }
4661eeffb40dSHong Zhang #else
4662580bdb30SBarry Smith   ierr = PetscArraycmp(a->a,b->a,a->nz,flg);CHKERRQ(ierr);
4663eeffb40dSHong Zhang #endif
4664eeffb40dSHong Zhang   PetscFunctionReturn(0);
46657264ac53SSatish Balay }
466636db0b34SBarry Smith 
466705869f15SSatish Balay /*@
466836db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
466936db0b34SBarry Smith               provided by the user.
467036db0b34SBarry Smith 
4671d083f849SBarry Smith       Collective
467236db0b34SBarry Smith 
467336db0b34SBarry Smith    Input Parameters:
467436db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
467536db0b34SBarry Smith .   m - number of rows
467636db0b34SBarry Smith .   n - number of columns
4677483a2f95SBarry Smith .   i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
467836db0b34SBarry Smith .   j - column indices
467936db0b34SBarry Smith -   a - matrix values
468036db0b34SBarry Smith 
468136db0b34SBarry Smith    Output Parameter:
468236db0b34SBarry Smith .   mat - the matrix
468336db0b34SBarry Smith 
468436db0b34SBarry Smith    Level: intermediate
468536db0b34SBarry Smith 
468636db0b34SBarry Smith    Notes:
46870551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4688292fb18eSBarry Smith     once the matrix is destroyed and not before
468936db0b34SBarry Smith 
469036db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
469136db0b34SBarry Smith 
4692bfeeae90SHong Zhang        The i and j indices are 0 based
469336db0b34SBarry Smith 
4694a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4695a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
46968eef79e4SBarry Smith     as shown
4697a4552177SSatish Balay 
46988eef79e4SBarry Smith $        1 0 0
46998eef79e4SBarry Smith $        2 0 3
47008eef79e4SBarry Smith $        4 5 6
47018eef79e4SBarry Smith $
47028eef79e4SBarry Smith $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
47038eef79e4SBarry Smith $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
47048eef79e4SBarry Smith $        v =  {1,2,3,4,5,6}  [size = 6]
4705a4552177SSatish Balay 
47069985e31cSBarry Smith 
470769b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
470836db0b34SBarry Smith 
470936db0b34SBarry Smith @*/
4710c3c607ccSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat)
471136db0b34SBarry Smith {
4712dfbe8321SBarry Smith   PetscErrorCode ierr;
4713cbcfb4deSHong Zhang   PetscInt       ii;
471436db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4715cbcfb4deSHong Zhang   PetscInt jj;
471636db0b34SBarry Smith 
471736db0b34SBarry Smith   PetscFunctionBegin;
471841096f02SStefano Zampini   if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4719f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4720f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4721a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4722ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4723ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4724ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4725071fcb05SBarry Smith   ierr = PetscMalloc1(m,&aij->imax);CHKERRQ(ierr);
4726071fcb05SBarry Smith   ierr = PetscMalloc1(m,&aij->ilen);CHKERRQ(ierr);
4727ab93d7beSBarry Smith 
472836db0b34SBarry Smith   aij->i            = i;
472936db0b34SBarry Smith   aij->j            = j;
473036db0b34SBarry Smith   aij->a            = a;
473136db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
473236db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4733e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4734e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
473536db0b34SBarry Smith 
473636db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
473736db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
473876bd3646SJed Brown     if (PetscDefined(USE_DEBUG)) {
473960e0710aSBarry 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]);
47409985e31cSBarry Smith       for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4741a061629eSStefano 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);
4742a061629eSStefano 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);
47439985e31cSBarry Smith       }
474436db0b34SBarry Smith     }
474576bd3646SJed Brown   }
474676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
474736db0b34SBarry Smith     for (ii=0; ii<aij->i[m]; ii++) {
474860e0710aSBarry Smith       if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
474960e0710aSBarry 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]);
475036db0b34SBarry Smith     }
475176bd3646SJed Brown   }
475236db0b34SBarry Smith 
4753b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4754b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
475536db0b34SBarry Smith   PetscFunctionReturn(0);
475636db0b34SBarry Smith }
475780ef6e79SMatthew G Knepley /*@C
4758d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
47598a0b0e6bSVictor Minden               provided by the user.
47608a0b0e6bSVictor Minden 
4761d083f849SBarry Smith       Collective
47628a0b0e6bSVictor Minden 
47638a0b0e6bSVictor Minden    Input Parameters:
47648a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
47658a0b0e6bSVictor Minden .   m   - number of rows
47668a0b0e6bSVictor Minden .   n   - number of columns
47678a0b0e6bSVictor Minden .   i   - row indices
47688a0b0e6bSVictor Minden .   j   - column indices
47691230e6d1SVictor Minden .   a   - matrix values
47701230e6d1SVictor Minden .   nz  - number of nonzeros
47711230e6d1SVictor Minden -   idx - 0 or 1 based
47728a0b0e6bSVictor Minden 
47738a0b0e6bSVictor Minden    Output Parameter:
47748a0b0e6bSVictor Minden .   mat - the matrix
47758a0b0e6bSVictor Minden 
47768a0b0e6bSVictor Minden    Level: intermediate
47778a0b0e6bSVictor Minden 
47788a0b0e6bSVictor Minden    Notes:
47798a0b0e6bSVictor Minden        The i and j indices are 0 based
47808a0b0e6bSVictor Minden 
47818a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
47828a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
47838a0b0e6bSVictor Minden     as shown:
47848a0b0e6bSVictor Minden 
47858a0b0e6bSVictor Minden         1 0 0
47868a0b0e6bSVictor Minden         2 0 3
47878a0b0e6bSVictor Minden         4 5 6
47888a0b0e6bSVictor Minden 
47898a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
47908a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
47918a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
47928a0b0e6bSVictor Minden 
47938a0b0e6bSVictor Minden 
479469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
47958a0b0e6bSVictor Minden 
47968a0b0e6bSVictor Minden @*/
4797c3c607ccSBarry Smith PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat,PetscInt nz,PetscBool idx)
47988a0b0e6bSVictor Minden {
47998a0b0e6bSVictor Minden   PetscErrorCode ierr;
4800d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
48018a0b0e6bSVictor Minden 
48028a0b0e6bSVictor Minden 
48038a0b0e6bSVictor Minden   PetscFunctionBegin;
48041795a4d1SJed Brown   ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr);
48051230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
4806c8d679ebSHong Zhang     nnz[i[ii] - !!idx] += 1;
48071230e6d1SVictor Minden   }
48088a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
48098a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
48108a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
48111230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
48121230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++) {
48131230e6d1SVictor Minden     if (idx) {
48141230e6d1SVictor Minden       row = i[ii] - 1;
48151230e6d1SVictor Minden       col = j[ii] - 1;
48161230e6d1SVictor Minden     } else {
48171230e6d1SVictor Minden       row = i[ii];
48181230e6d1SVictor Minden       col = j[ii];
48198a0b0e6bSVictor Minden     }
48201230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
48218a0b0e6bSVictor Minden   }
48228a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
48238a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4824d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
48258a0b0e6bSVictor Minden   PetscFunctionReturn(0);
48268a0b0e6bSVictor Minden }
482736db0b34SBarry Smith 
4828acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4829acf2f550SJed Brown {
4830acf2f550SJed Brown   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
4831acf2f550SJed Brown   PetscErrorCode ierr;
4832acf2f550SJed Brown 
4833acf2f550SJed Brown   PetscFunctionBegin;
4834acf2f550SJed Brown   a->idiagvalid  = PETSC_FALSE;
4835acf2f550SJed Brown   a->ibdiagvalid = PETSC_FALSE;
48362205254eSKarl Rupp 
4837acf2f550SJed Brown   ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr);
4838acf2f550SJed Brown   PetscFunctionReturn(0);
4839acf2f550SJed Brown }
4840acf2f550SJed Brown 
48419c8f2541SHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
48429c8f2541SHong Zhang {
48439c8f2541SHong Zhang   PetscErrorCode ierr;
48448761c3d6SHong Zhang   PetscMPIInt    size;
48459c8f2541SHong Zhang 
48469c8f2541SHong Zhang   PetscFunctionBegin;
48478761c3d6SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
48487bbdc51dSHong Zhang   if (size == 1) {
48497bbdc51dSHong Zhang     if (scall == MAT_INITIAL_MATRIX) {
48507bbdc51dSHong Zhang       ierr = MatDuplicate(inmat,MAT_COPY_VALUES,outmat);CHKERRQ(ierr);
48517bbdc51dSHong Zhang     } else {
48528761c3d6SHong Zhang       ierr = MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
48537bbdc51dSHong Zhang     }
48548761c3d6SHong Zhang   } else {
48559c8f2541SHong Zhang     ierr = MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr);
48568761c3d6SHong Zhang   }
48579c8f2541SHong Zhang   PetscFunctionReturn(0);
48589c8f2541SHong Zhang }
48599c8f2541SHong Zhang 
486081824310SBarry Smith /*
486153dd7562SDmitry Karpeev  Permute A into C's *local* index space using rowemb,colemb.
486253dd7562SDmitry Karpeev  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
486353dd7562SDmitry Karpeev  of [0,m), colemb is in [0,n).
486453dd7562SDmitry Karpeev  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
486553dd7562SDmitry Karpeev  */
486653dd7562SDmitry Karpeev PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
486753dd7562SDmitry Karpeev {
486853dd7562SDmitry Karpeev   /* If making this function public, change the error returned in this function away from _PLIB. */
486953dd7562SDmitry Karpeev   PetscErrorCode ierr;
487053dd7562SDmitry Karpeev   Mat_SeqAIJ     *Baij;
487153dd7562SDmitry Karpeev   PetscBool      seqaij;
487253dd7562SDmitry Karpeev   PetscInt       m,n,*nz,i,j,count;
487353dd7562SDmitry Karpeev   PetscScalar    v;
487453dd7562SDmitry Karpeev   const PetscInt *rowindices,*colindices;
487553dd7562SDmitry Karpeev 
487653dd7562SDmitry Karpeev   PetscFunctionBegin;
487753dd7562SDmitry Karpeev   if (!B) PetscFunctionReturn(0);
487853dd7562SDmitry Karpeev   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
48794099cc6bSBarry Smith   ierr = PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);CHKERRQ(ierr);
488053dd7562SDmitry Karpeev   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
488153dd7562SDmitry Karpeev   if (rowemb) {
488253dd7562SDmitry Karpeev     ierr = ISGetLocalSize(rowemb,&m);CHKERRQ(ierr);
488353dd7562SDmitry 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);
488453dd7562SDmitry Karpeev   } else {
48856c4ed002SBarry Smith     if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
488653dd7562SDmitry Karpeev   }
488753dd7562SDmitry Karpeev   if (colemb) {
488853dd7562SDmitry Karpeev     ierr = ISGetLocalSize(colemb,&n);CHKERRQ(ierr);
488953dd7562SDmitry 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);
489053dd7562SDmitry Karpeev   } else {
489153dd7562SDmitry Karpeev     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
489253dd7562SDmitry Karpeev   }
489353dd7562SDmitry Karpeev 
489453dd7562SDmitry Karpeev   Baij = (Mat_SeqAIJ*)(B->data);
489553dd7562SDmitry Karpeev   if (pattern == DIFFERENT_NONZERO_PATTERN) {
489653dd7562SDmitry Karpeev     ierr = PetscMalloc1(B->rmap->n,&nz);CHKERRQ(ierr);
489753dd7562SDmitry Karpeev     for (i=0; i<B->rmap->n; i++) {
489853dd7562SDmitry Karpeev       nz[i] = Baij->i[i+1] - Baij->i[i];
489953dd7562SDmitry Karpeev     }
490053dd7562SDmitry Karpeev     ierr = MatSeqAIJSetPreallocation(C,0,nz);CHKERRQ(ierr);
490153dd7562SDmitry Karpeev     ierr = PetscFree(nz);CHKERRQ(ierr);
490253dd7562SDmitry Karpeev   }
490353dd7562SDmitry Karpeev   if (pattern == SUBSET_NONZERO_PATTERN) {
490453dd7562SDmitry Karpeev     ierr = MatZeroEntries(C);CHKERRQ(ierr);
490553dd7562SDmitry Karpeev   }
490653dd7562SDmitry Karpeev   count = 0;
490753dd7562SDmitry Karpeev   rowindices = NULL;
490853dd7562SDmitry Karpeev   colindices = NULL;
490953dd7562SDmitry Karpeev   if (rowemb) {
491053dd7562SDmitry Karpeev     ierr = ISGetIndices(rowemb,&rowindices);CHKERRQ(ierr);
491153dd7562SDmitry Karpeev   }
491253dd7562SDmitry Karpeev   if (colemb) {
491353dd7562SDmitry Karpeev     ierr = ISGetIndices(colemb,&colindices);CHKERRQ(ierr);
491453dd7562SDmitry Karpeev   }
491553dd7562SDmitry Karpeev   for (i=0; i<B->rmap->n; i++) {
491653dd7562SDmitry Karpeev     PetscInt row;
491753dd7562SDmitry Karpeev     row = i;
491853dd7562SDmitry Karpeev     if (rowindices) row = rowindices[i];
491953dd7562SDmitry Karpeev     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
492053dd7562SDmitry Karpeev       PetscInt col;
492153dd7562SDmitry Karpeev       col  = Baij->j[count];
492253dd7562SDmitry Karpeev       if (colindices) col = colindices[col];
492353dd7562SDmitry Karpeev       v    = Baij->a[count];
492453dd7562SDmitry Karpeev       ierr = MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);CHKERRQ(ierr);
492553dd7562SDmitry Karpeev       ++count;
492653dd7562SDmitry Karpeev     }
492753dd7562SDmitry Karpeev   }
492853dd7562SDmitry Karpeev   /* FIXME: set C's nonzerostate correctly. */
492953dd7562SDmitry Karpeev   /* Assembly for C is necessary. */
493053dd7562SDmitry Karpeev   C->preallocated = PETSC_TRUE;
493153dd7562SDmitry Karpeev   C->assembled     = PETSC_TRUE;
493253dd7562SDmitry Karpeev   C->was_assembled = PETSC_FALSE;
493353dd7562SDmitry Karpeev   PetscFunctionReturn(0);
493453dd7562SDmitry Karpeev }
493553dd7562SDmitry Karpeev 
49364099cc6bSBarry Smith PetscFunctionList MatSeqAIJList = NULL;
49374099cc6bSBarry Smith 
49384099cc6bSBarry Smith /*@C
49394099cc6bSBarry Smith    MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype
49404099cc6bSBarry Smith 
49414099cc6bSBarry Smith    Collective on Mat
49424099cc6bSBarry Smith 
49434099cc6bSBarry Smith    Input Parameters:
49444099cc6bSBarry Smith +  mat      - the matrix object
49454099cc6bSBarry Smith -  matype   - matrix type
49464099cc6bSBarry Smith 
49474099cc6bSBarry Smith    Options Database Key:
49484099cc6bSBarry Smith .  -mat_seqai_type  <method> - for example seqaijcrl
49494099cc6bSBarry Smith 
49504099cc6bSBarry Smith 
49514099cc6bSBarry Smith   Level: intermediate
49524099cc6bSBarry Smith 
49534099cc6bSBarry Smith .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
49544099cc6bSBarry Smith @*/
49554099cc6bSBarry Smith PetscErrorCode  MatSeqAIJSetType(Mat mat, MatType matype)
49564099cc6bSBarry Smith {
4957fd9d3c67SJed Brown   PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*);
49584099cc6bSBarry Smith   PetscBool      sametype;
49594099cc6bSBarry Smith 
49604099cc6bSBarry Smith   PetscFunctionBegin;
49614099cc6bSBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
49624099cc6bSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
49634099cc6bSBarry Smith   if (sametype) PetscFunctionReturn(0);
49644099cc6bSBarry Smith 
49654099cc6bSBarry Smith   ierr =  PetscFunctionListFind(MatSeqAIJList,matype,&r);CHKERRQ(ierr);
49664099cc6bSBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
49674099cc6bSBarry Smith   ierr = (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);CHKERRQ(ierr);
49684099cc6bSBarry Smith   PetscFunctionReturn(0);
49694099cc6bSBarry Smith }
49704099cc6bSBarry Smith 
49714099cc6bSBarry Smith 
49724099cc6bSBarry Smith /*@C
49734099cc6bSBarry Smith   MatSeqAIJRegister -  - Adds a new sub-matrix type for sequential AIJ matrices
49744099cc6bSBarry Smith 
49754099cc6bSBarry Smith    Not Collective
49764099cc6bSBarry Smith 
49774099cc6bSBarry Smith    Input Parameters:
49784099cc6bSBarry Smith +  name - name of a new user-defined matrix type, for example MATSEQAIJCRL
49794099cc6bSBarry Smith -  function - routine to convert to subtype
49804099cc6bSBarry Smith 
49814099cc6bSBarry Smith    Notes:
49824099cc6bSBarry Smith    MatSeqAIJRegister() may be called multiple times to add several user-defined solvers.
49834099cc6bSBarry Smith 
49844099cc6bSBarry Smith 
49854099cc6bSBarry Smith    Then, your matrix can be chosen with the procedural interface at runtime via the option
49864099cc6bSBarry Smith $     -mat_seqaij_type my_mat
49874099cc6bSBarry Smith 
49884099cc6bSBarry Smith    Level: advanced
49894099cc6bSBarry Smith 
49904099cc6bSBarry Smith .seealso: MatSeqAIJRegisterAll()
49914099cc6bSBarry Smith 
49924099cc6bSBarry Smith 
49934099cc6bSBarry Smith   Level: advanced
49944099cc6bSBarry Smith @*/
4995388d47a6SSatish Balay PetscErrorCode  MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *))
49964099cc6bSBarry Smith {
49974099cc6bSBarry Smith   PetscErrorCode ierr;
49984099cc6bSBarry Smith 
49994099cc6bSBarry Smith   PetscFunctionBegin;
50009cc31a68SJed Brown   ierr = MatInitializePackage();CHKERRQ(ierr);
50014099cc6bSBarry Smith   ierr = PetscFunctionListAdd(&MatSeqAIJList,sname,function);CHKERRQ(ierr);
50024099cc6bSBarry Smith   PetscFunctionReturn(0);
50034099cc6bSBarry Smith }
50044099cc6bSBarry Smith 
50054099cc6bSBarry Smith PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;
50064099cc6bSBarry Smith 
50074099cc6bSBarry Smith /*@C
50084099cc6bSBarry Smith   MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ
50094099cc6bSBarry Smith 
50104099cc6bSBarry Smith   Not Collective
50114099cc6bSBarry Smith 
50124099cc6bSBarry Smith   Level: advanced
50134099cc6bSBarry Smith 
50144099cc6bSBarry Smith   Developers Note: CUSP and CUSPARSE do not yet support the  MatConvert_SeqAIJ..() paradigm and thus cannot be registered here
50154099cc6bSBarry Smith 
50164099cc6bSBarry Smith .seealso:  MatRegisterAll(), MatSeqAIJRegister()
50174099cc6bSBarry Smith @*/
50184099cc6bSBarry Smith PetscErrorCode  MatSeqAIJRegisterAll(void)
50194099cc6bSBarry Smith {
50204099cc6bSBarry Smith   PetscErrorCode ierr;
50214099cc6bSBarry Smith 
50224099cc6bSBarry Smith   PetscFunctionBegin;
50234099cc6bSBarry Smith   if (MatSeqAIJRegisterAllCalled) PetscFunctionReturn(0);
50244099cc6bSBarry Smith   MatSeqAIJRegisterAllCalled = PETSC_TRUE;
50254099cc6bSBarry Smith 
50264099cc6bSBarry Smith   ierr = MatSeqAIJRegister(MATSEQAIJCRL,      MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
50274099cc6bSBarry Smith   ierr = MatSeqAIJRegister(MATSEQAIJPERM,     MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
50284dfdc2d9SRichard Tran Mills   ierr = MatSeqAIJRegister(MATSEQAIJSELL,     MatConvert_SeqAIJ_SeqAIJSELL);CHKERRQ(ierr);
50299779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE)
50306b62b571SRichard Tran Mills   ierr = MatSeqAIJRegister(MATSEQAIJMKL,      MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr);
5031485f9817SRichard Tran Mills #endif
50324099cc6bSBarry Smith #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
50334099cc6bSBarry Smith   ierr = MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);CHKERRQ(ierr);
50344099cc6bSBarry Smith #endif
50354099cc6bSBarry Smith   PetscFunctionReturn(0);
50364099cc6bSBarry Smith }
503753dd7562SDmitry Karpeev 
503853dd7562SDmitry Karpeev /*
503981824310SBarry Smith     Special version for direct calls from Fortran
504081824310SBarry Smith */
5041af0996ceSBarry Smith #include <petsc/private/fortranimpl.h>
504281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
504381824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
504481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
504581824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
504681824310SBarry Smith #endif
504781824310SBarry Smith 
504881824310SBarry Smith /* Change these macros so can be used in void function */
504981824310SBarry Smith #undef CHKERRQ
5050ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
505181824310SBarry Smith #undef SETERRQ2
5052e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
50534994cf47SJed Brown #undef SETERRQ3
50544994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
505581824310SBarry Smith 
505619caf8f3SSatish Balay PETSC_EXTERN void matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
505781824310SBarry Smith {
505881824310SBarry Smith   Mat            A  = *AA;
505981824310SBarry Smith   PetscInt       m  = *mm, n = *nn;
506081824310SBarry Smith   InsertMode     is = *isis;
506181824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
506281824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
506381824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
506481824310SBarry Smith   PetscErrorCode ierr;
506581824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
506654f21887SBarry Smith   MatScalar      *ap,value,*aa;
5067ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
5068ace3abfcSBarry Smith   PetscBool      roworiented       = a->roworiented;
506981824310SBarry Smith 
507081824310SBarry Smith   PetscFunctionBegin;
50714994cf47SJed Brown   MatCheckPreallocated(A,1);
507281824310SBarry Smith   imax  = a->imax;
507381824310SBarry Smith   ai    = a->i;
507481824310SBarry Smith   ailen = a->ilen;
507581824310SBarry Smith   aj    = a->j;
507681824310SBarry Smith   aa    = a->a;
507781824310SBarry Smith 
507881824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
507981824310SBarry Smith     row = im[k];
508081824310SBarry Smith     if (row < 0) continue;
5081*cf9c20a2SJed Brown     if (PetscUnlikelyDebug(row >= A->rmap->n)) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
508281824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
508381824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
508481824310SBarry Smith     low  = 0;
508581824310SBarry Smith     high = nrow;
508681824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
508781824310SBarry Smith       if (in[l] < 0) continue;
5088*cf9c20a2SJed Brown       if (PetscUnlikelyDebug(in[l] >= A->cmap->n)) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
508981824310SBarry Smith       col = in[l];
50902205254eSKarl Rupp       if (roworiented) value = v[l + k*n];
50912205254eSKarl Rupp       else value = v[k + l*m];
50922205254eSKarl Rupp 
509381824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
509481824310SBarry Smith 
50952205254eSKarl Rupp       if (col <= lastcol) low = 0;
50962205254eSKarl Rupp       else high = nrow;
509781824310SBarry Smith       lastcol = col;
509881824310SBarry Smith       while (high-low > 5) {
509981824310SBarry Smith         t = (low+high)/2;
510081824310SBarry Smith         if (rp[t] > col) high = t;
510181824310SBarry Smith         else             low  = t;
510281824310SBarry Smith       }
510381824310SBarry Smith       for (i=low; i<high; i++) {
510481824310SBarry Smith         if (rp[i] > col) break;
510581824310SBarry Smith         if (rp[i] == col) {
510681824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
510781824310SBarry Smith           else                  ap[i] = value;
510881824310SBarry Smith           goto noinsert;
510981824310SBarry Smith         }
511081824310SBarry Smith       }
511181824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
511281824310SBarry Smith       if (nonew == 1) goto noinsert;
5113ce94432eSBarry Smith       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
5114fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
511581824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
511681824310SBarry Smith       /* shift up all the later entries in this row */
511781824310SBarry Smith       for (ii=N; ii>=i; ii--) {
511881824310SBarry Smith         rp[ii+1] = rp[ii];
511981824310SBarry Smith         ap[ii+1] = ap[ii];
512081824310SBarry Smith       }
512181824310SBarry Smith       rp[i] = col;
512281824310SBarry Smith       ap[i] = value;
5123e56f5c9eSBarry Smith       A->nonzerostate++;
512481824310SBarry Smith noinsert:;
512581824310SBarry Smith       low = i + 1;
512681824310SBarry Smith     }
512781824310SBarry Smith     ailen[row] = nrow;
512881824310SBarry Smith   }
512981824310SBarry Smith   PetscFunctionReturnVoid();
513081824310SBarry Smith }
5131