xref: /petsc/src/mat/impls/aij/seq/aij.c (revision a4005a5d5581e6a64413ce028b362c185ff1c1db)
1992c00aaSSatish Balay #define PETSCMAT_DLL
2b3cc6726SBarry Smith 
3d5d45c9bSBarry Smith /*
43369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
5d5d45c9bSBarry Smith   matrix storage format.
6d5d45c9bSBarry Smith */
73369ce9aSBarry Smith 
87c4f633dSBarry Smith 
97c4f633dSBarry Smith #include "../src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
100a835dfdSSatish Balay #include "petscbt.h"
1117ab2063SBarry Smith 
124a2ae208SSatish Balay #undef __FUNCT__
1379299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1479299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1579299369SBarry Smith {
1679299369SBarry Smith   PetscErrorCode ierr;
1779299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
18d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
1954f21887SBarry Smith   MatScalar      *aa = aij->a;
2054f21887SBarry Smith   PetscScalar    *v;
2109f38230SBarry Smith   PetscTruth     missing;
2279299369SBarry Smith 
2379299369SBarry Smith   PetscFunctionBegin;
2409f38230SBarry Smith   if (Y->assembled) {
2509f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
2609f38230SBarry Smith     if (!missing) {
2779299369SBarry Smith       diag = aij->diag;
2879299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
2979299369SBarry Smith       if (is == INSERT_VALUES) {
3079299369SBarry Smith 	for (i=0; i<m; i++) {
3179299369SBarry Smith 	  aa[diag[i]] = v[i];
3279299369SBarry Smith 	}
3379299369SBarry Smith       } else {
3479299369SBarry Smith 	for (i=0; i<m; i++) {
3579299369SBarry Smith 	  aa[diag[i]] += v[i];
3679299369SBarry Smith 	}
3779299369SBarry Smith       }
3879299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
3979299369SBarry Smith       PetscFunctionReturn(0);
4079299369SBarry Smith     }
4109f38230SBarry Smith   }
4209f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
4309f38230SBarry Smith   PetscFunctionReturn(0);
4409f38230SBarry Smith }
4579299369SBarry Smith 
4679299369SBarry Smith #undef __FUNCT__
474a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
488f7157efSSatish Balay PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
4917ab2063SBarry Smith {
50416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
51dfbe8321SBarry Smith   PetscErrorCode ierr;
5297f1f81fSBarry Smith   PetscInt       i,ishift;
5317ab2063SBarry Smith 
543a40ed3dSBarry Smith   PetscFunctionBegin;
55d0f46423SBarry Smith   *m     = A->rmap->n;
563a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
57bfeeae90SHong Zhang   ishift = 0;
5853e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
59d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
60bfeeae90SHong Zhang   } else if (oshift == 1) {
61d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
623b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
63d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
64d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
65ecc77c7aSBarry Smith     if (ja) {
6697f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
673b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
68ecc77c7aSBarry Smith     }
696945ee14SBarry Smith   } else {
70ecc77c7aSBarry Smith     *ia = a->i;
71ecc77c7aSBarry Smith     if (ja) *ja = a->j;
72a2ce50c7SBarry Smith   }
733a40ed3dSBarry Smith   PetscFunctionReturn(0);
74a2744918SBarry Smith }
75a2744918SBarry Smith 
764a2ae208SSatish Balay #undef __FUNCT__
774a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
788f7157efSSatish Balay PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
796945ee14SBarry Smith {
80dfbe8321SBarry Smith   PetscErrorCode ierr;
816945ee14SBarry Smith 
823a40ed3dSBarry Smith   PetscFunctionBegin;
833a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
84bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
85606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
86ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
87bcd2baecSBarry Smith   }
883a40ed3dSBarry Smith   PetscFunctionReturn(0);
8917ab2063SBarry Smith }
9017ab2063SBarry Smith 
914a2ae208SSatish Balay #undef __FUNCT__
924a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
938f7157efSSatish Balay PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
943b2fbd54SBarry Smith {
953b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
96dfbe8321SBarry Smith   PetscErrorCode ierr;
97d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
9897f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
993b2fbd54SBarry Smith 
1003a40ed3dSBarry Smith   PetscFunctionBegin;
101899cda47SBarry Smith   *nn = n;
1023a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1033b2fbd54SBarry Smith   if (symmetric) {
104d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1053b2fbd54SBarry Smith   } else {
10697f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
10797f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
10897f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
10997f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1103b2fbd54SBarry Smith     jj = a->j;
1113b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
112bfeeae90SHong Zhang       collengths[jj[i]]++;
1133b2fbd54SBarry Smith     }
1143b2fbd54SBarry Smith     cia[0] = oshift;
1153b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1163b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1173b2fbd54SBarry Smith     }
11897f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1193b2fbd54SBarry Smith     jj   = a->j;
120a93ec695SBarry Smith     for (row=0; row<m; row++) {
121a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
122a93ec695SBarry Smith       for (i=0; i<mr; i++) {
123bfeeae90SHong Zhang         col = *jj++;
1243b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1253b2fbd54SBarry Smith       }
1263b2fbd54SBarry Smith     }
127606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1283b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1293b2fbd54SBarry Smith   }
1303a40ed3dSBarry Smith   PetscFunctionReturn(0);
1313b2fbd54SBarry Smith }
1323b2fbd54SBarry Smith 
1334a2ae208SSatish Balay #undef __FUNCT__
1344a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
1358f7157efSSatish Balay PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1363b2fbd54SBarry Smith {
137dfbe8321SBarry Smith   PetscErrorCode ierr;
138606d414cSSatish Balay 
1393a40ed3dSBarry Smith   PetscFunctionBegin;
1403a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1413b2fbd54SBarry Smith 
142606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
143606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1443b2fbd54SBarry Smith 
1453a40ed3dSBarry Smith   PetscFunctionReturn(0);
1463b2fbd54SBarry Smith }
1473b2fbd54SBarry Smith 
14887d4246cSBarry Smith #undef __FUNCT__
14987d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
15087d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
15187d4246cSBarry Smith {
15287d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15387d4246cSBarry Smith   PetscInt       *ai = a->i;
15487d4246cSBarry Smith   PetscErrorCode ierr;
15587d4246cSBarry Smith 
15687d4246cSBarry Smith   PetscFunctionBegin;
15787d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
15887d4246cSBarry Smith   PetscFunctionReturn(0);
15987d4246cSBarry Smith }
16087d4246cSBarry Smith 
161227d817aSBarry Smith #define CHUNKSIZE   15
16217ab2063SBarry Smith 
1634a2ae208SSatish Balay #undef __FUNCT__
1644a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
16597f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
16617ab2063SBarry Smith {
167416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
168e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
16997f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1706849ba73SBarry Smith   PetscErrorCode ierr;
171e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
17254f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
173edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
174273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
17517ab2063SBarry Smith 
1763a40ed3dSBarry Smith   PetscFunctionBegin;
17717ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
178416022c9SBarry Smith     row  = im[k];
1795ef9f2a5SBarry Smith     if (row < 0) continue;
1802515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
181d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
1823b2fbd54SBarry Smith #endif
183bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
18417ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
185416022c9SBarry Smith     low  = 0;
186c71e6ed7SBarry Smith     high = nrow;
18717ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1885ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1892515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
190d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
1913b2fbd54SBarry Smith #endif
192bfeeae90SHong Zhang       col = in[l];
19316371a99SBarry Smith       if (v) {
1944b0e389bSBarry Smith 	if (roworiented) {
1955ef9f2a5SBarry Smith 	  value = v[l + k*n];
196bef8e0ddSBarry Smith 	} else {
1974b0e389bSBarry Smith 	  value = v[k + l*m];
1984b0e389bSBarry Smith 	}
19916371a99SBarry Smith       } else {
20016371a99SBarry Smith         value = 0;
20116371a99SBarry Smith       }
202abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
20336db0b34SBarry Smith 
2047cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
205e2ee6c50SBarry Smith       lastcol = col;
206416022c9SBarry Smith       while (high-low > 5) {
207416022c9SBarry Smith         t = (low+high)/2;
208416022c9SBarry Smith         if (rp[t] > col) high = t;
209416022c9SBarry Smith         else             low  = t;
21017ab2063SBarry Smith       }
211416022c9SBarry Smith       for (i=low; i<high; i++) {
21217ab2063SBarry Smith         if (rp[i] > col) break;
21317ab2063SBarry Smith         if (rp[i] == col) {
214416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
21517ab2063SBarry Smith           else                  ap[i] = value;
216e44c0bd4SBarry Smith           low = i + 1;
21717ab2063SBarry Smith           goto noinsert;
21817ab2063SBarry Smith         }
21917ab2063SBarry Smith       }
220abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
221c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
222cc72174dSBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
223d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
224c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
225416022c9SBarry Smith       /* shift up all the later entries in this row */
226416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
22717ab2063SBarry Smith         rp[ii+1] = rp[ii];
22817ab2063SBarry Smith         ap[ii+1] = ap[ii];
22917ab2063SBarry Smith       }
23017ab2063SBarry Smith       rp[i] = col;
23117ab2063SBarry Smith       ap[i] = value;
232416022c9SBarry Smith       low   = i + 1;
233e44c0bd4SBarry Smith       noinsert:;
23417ab2063SBarry Smith     }
23517ab2063SBarry Smith     ailen[row] = nrow;
23617ab2063SBarry Smith   }
23788e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
2383a40ed3dSBarry Smith   PetscFunctionReturn(0);
23917ab2063SBarry Smith }
24017ab2063SBarry Smith 
24181824310SBarry Smith 
2424a2ae208SSatish Balay #undef __FUNCT__
2434a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
244a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
2457eb43aa7SLois Curfman McInnes {
2467eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
24797f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
24897f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
24954f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
2507eb43aa7SLois Curfman McInnes 
2513a40ed3dSBarry Smith   PetscFunctionBegin;
2527eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2537eb43aa7SLois Curfman McInnes     row  = im[k];
25497e567efSBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
255d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
256bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2577eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2587eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
25997e567efSBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
260d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
261bfeeae90SHong Zhang       col = in[l] ;
2627eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2637eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2647eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2657eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2667eb43aa7SLois Curfman McInnes         else             low  = t;
2677eb43aa7SLois Curfman McInnes       }
2687eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2697eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2707eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
271b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2727eb43aa7SLois Curfman McInnes           goto finished;
2737eb43aa7SLois Curfman McInnes         }
2747eb43aa7SLois Curfman McInnes       }
27597e567efSBarry Smith       *v++ = 0.0;
2767eb43aa7SLois Curfman McInnes       finished:;
2777eb43aa7SLois Curfman McInnes     }
2787eb43aa7SLois Curfman McInnes   }
2793a40ed3dSBarry Smith   PetscFunctionReturn(0);
2807eb43aa7SLois Curfman McInnes }
2817eb43aa7SLois Curfman McInnes 
28217ab2063SBarry Smith 
2834a2ae208SSatish Balay #undef __FUNCT__
2844a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
285dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
28617ab2063SBarry Smith {
287416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2886849ba73SBarry Smith   PetscErrorCode ierr;
2896f69ff64SBarry Smith   PetscInt       i,*col_lens;
2906f69ff64SBarry Smith   int            fd;
29117ab2063SBarry Smith 
2923a40ed3dSBarry Smith   PetscFunctionBegin;
293b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
294d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
295552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
296d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
297d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
298416022c9SBarry Smith   col_lens[3] = a->nz;
299416022c9SBarry Smith 
300416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
301d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
302416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
30317ab2063SBarry Smith   }
304d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
305606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
306416022c9SBarry Smith 
307416022c9SBarry Smith   /* store column indices (zero start index) */
3086f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
309416022c9SBarry Smith 
310416022c9SBarry Smith   /* store nonzero values */
3116f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
3123a40ed3dSBarry Smith   PetscFunctionReturn(0);
31317ab2063SBarry Smith }
314416022c9SBarry Smith 
315dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
316cd155464SBarry Smith 
3174a2ae208SSatish Balay #undef __FUNCT__
3184a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
319dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
320416022c9SBarry Smith {
321416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
322dfbe8321SBarry Smith   PetscErrorCode    ierr;
323d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
324e060cb09SBarry Smith   const char        *name;
325f3ef73ceSBarry Smith   PetscViewerFormat format;
32617ab2063SBarry Smith 
3273a40ed3dSBarry Smith   PetscFunctionBegin;
328435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
329b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
33071c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
33197f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
332d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
333d00d2cf4SBarry Smith       nofinalvalue = 1;
334d00d2cf4SBarry Smith     }
335b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
336d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
33777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
33877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
339b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
34017ab2063SBarry Smith 
34117ab2063SBarry Smith     for (i=0; i<m; i++) {
342416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
343aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34477431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e + %18.16ei \n",i+1,a->j[j]+!shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
34517ab2063SBarry Smith #else
34677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
34717ab2063SBarry Smith #endif
34817ab2063SBarry Smith       }
34917ab2063SBarry Smith     }
350d00d2cf4SBarry Smith     if (nofinalvalue) {
351d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
352d00d2cf4SBarry Smith     }
353fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
354b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
35568369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
356cd155464SBarry Smith      PetscFunctionReturn(0);
357fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
358b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
35944cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
36077431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
36144cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
362aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36336db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
364a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36536db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
366a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36736db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
368a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3696831982aSBarry Smith         }
37044cd7ae7SLois Curfman McInnes #else
371a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
37244cd7ae7SLois Curfman McInnes #endif
37344cd7ae7SLois Curfman McInnes       }
374b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
37544cd7ae7SLois Curfman McInnes     }
376b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
377fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
37897f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
379b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
38097f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
381496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
382496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
383496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
384496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
385aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
38636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
387496be53dSLois Curfman McInnes #else
388496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
389496be53dSLois Curfman McInnes #endif
390496be53dSLois Curfman McInnes         }
391496be53dSLois Curfman McInnes       }
392496be53dSLois Curfman McInnes     }
3932e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
39477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3952e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
39677431f27SBarry Smith       if (i+4<m) {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);}
39777431f27SBarry Smith       else if (i+3<m) {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);}
39877431f27SBarry Smith       else if (i+2<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);}
39977431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
40077431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
40177431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
402496be53dSLois Curfman McInnes     }
403b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
404606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
405496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
406496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
40777431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
408496be53dSLois Curfman McInnes       }
409b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
410496be53dSLois Curfman McInnes     }
411b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
412496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
413496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
414496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
415aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
417b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4186831982aSBarry Smith           }
419496be53dSLois Curfman McInnes #else
420b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
421496be53dSLois Curfman McInnes #endif
422496be53dSLois Curfman McInnes         }
423496be53dSLois Curfman McInnes       }
424b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
425496be53dSLois Curfman McInnes     }
426b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
427fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
42897f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
42987828ca2SBarry Smith     PetscScalar value;
43002594712SBarry Smith 
431b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
43202594712SBarry Smith     for (i=0; i<m; i++) {
43302594712SBarry Smith       jcnt = 0;
434d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
435e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
43602594712SBarry Smith           value = a->a[cnt++];
437e24b481bSBarry Smith           jcnt++;
43802594712SBarry Smith         } else {
43902594712SBarry Smith           value = 0.0;
44002594712SBarry Smith         }
441aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
442b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
44302594712SBarry Smith #else
444b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
44502594712SBarry Smith #endif
44602594712SBarry Smith       }
447b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
44802594712SBarry Smith     }
449b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4503c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
4513c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
4523c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4533c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
4543c215bfdSMatthew Knepley #else
4553c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
4563c215bfdSMatthew Knepley #endif
457d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
4583c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
4593c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
4603c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4613c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
4623c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4633c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
4643c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G -%G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4653c215bfdSMatthew Knepley         } else {
4663c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4673c215bfdSMatthew Knepley         }
4683c215bfdSMatthew Knepley #else
4693c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
4703c215bfdSMatthew Knepley #endif
4713c215bfdSMatthew Knepley       }
4723c215bfdSMatthew Knepley     }
4733c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4743a40ed3dSBarry Smith   } else {
475b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
47617ab2063SBarry Smith     for (i=0; i<m; i++) {
47777431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
478416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
479aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
48036db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
481a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48236db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
483a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4843a40ed3dSBarry Smith         } else {
485a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
48617ab2063SBarry Smith         }
48717ab2063SBarry Smith #else
488a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
48917ab2063SBarry Smith #endif
49017ab2063SBarry Smith       }
491b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
49217ab2063SBarry Smith     }
493b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
49417ab2063SBarry Smith   }
495b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4963a40ed3dSBarry Smith   PetscFunctionReturn(0);
497416022c9SBarry Smith }
498416022c9SBarry Smith 
4994a2ae208SSatish Balay #undef __FUNCT__
5004a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
501dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
502416022c9SBarry Smith {
503480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
504416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
505dfbe8321SBarry Smith   PetscErrorCode    ierr;
506d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
50736db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
508b0a32e0cSBarry Smith   PetscViewer       viewer;
509f3ef73ceSBarry Smith   PetscViewerFormat format;
510cddf8d76SBarry Smith 
5113a40ed3dSBarry Smith   PetscFunctionBegin;
512480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
513b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
51419bcc07fSBarry Smith 
515b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
516416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
5170513a670SBarry Smith 
518fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
5190513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
520b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
521416022c9SBarry Smith     for (i=0; i<m; i++) {
522cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
523bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
524bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
525aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52636db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
527cddf8d76SBarry Smith #else
528cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
529cddf8d76SBarry Smith #endif
530b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
531cddf8d76SBarry Smith       }
532cddf8d76SBarry Smith     }
533b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
534cddf8d76SBarry Smith     for (i=0; i<m; i++) {
535cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
536bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
537bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
538cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
539b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
540cddf8d76SBarry Smith       }
541cddf8d76SBarry Smith     }
542b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
543cddf8d76SBarry Smith     for (i=0; i<m; i++) {
544cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
545bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
546bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
547aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
54836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
549cddf8d76SBarry Smith #else
550cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
551cddf8d76SBarry Smith #endif
552b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
553416022c9SBarry Smith       }
554416022c9SBarry Smith     }
5550513a670SBarry Smith   } else {
5560513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5570513a670SBarry Smith     /* first determine max of all nonzero values */
55897f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
559b0a32e0cSBarry Smith     PetscDraw   popup;
56036db0b34SBarry Smith     PetscReal scale;
5610513a670SBarry Smith 
5620513a670SBarry Smith     for (i=0; i<nz; i++) {
5630513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5640513a670SBarry Smith     }
565b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
566b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
567b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5680513a670SBarry Smith     count = 0;
5690513a670SBarry Smith     for (i=0; i<m; i++) {
5700513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
571bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
572bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
57397f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
574b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5750513a670SBarry Smith         count++;
5760513a670SBarry Smith       }
5770513a670SBarry Smith     }
5780513a670SBarry Smith   }
579480ef9eaSBarry Smith   PetscFunctionReturn(0);
580480ef9eaSBarry Smith }
581cddf8d76SBarry Smith 
5824a2ae208SSatish Balay #undef __FUNCT__
5834a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
584dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
585480ef9eaSBarry Smith {
586dfbe8321SBarry Smith   PetscErrorCode ierr;
587b0a32e0cSBarry Smith   PetscDraw      draw;
58836db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
589480ef9eaSBarry Smith   PetscTruth     isnull;
590480ef9eaSBarry Smith 
591480ef9eaSBarry Smith   PetscFunctionBegin;
592b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
593b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
594480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
595480ef9eaSBarry Smith 
596480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
597d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
598480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
599b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
600b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
601480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
6023a40ed3dSBarry Smith   PetscFunctionReturn(0);
603416022c9SBarry Smith }
604416022c9SBarry Smith 
6054a2ae208SSatish Balay #undef __FUNCT__
6064a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
607dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
608416022c9SBarry Smith {
609dfbe8321SBarry Smith   PetscErrorCode ierr;
6106805f65bSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
611416022c9SBarry Smith 
6123a40ed3dSBarry Smith   PetscFunctionBegin;
61332077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
614fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
615fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
616c45a1595SBarry Smith   if (iascii) {
6173a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
6180f5bd95cSBarry Smith   } else if (isbinary) {
6193a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
6200f5bd95cSBarry Smith   } else if (isdraw) {
6213a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6225cd90555SBarry Smith   } else {
623958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
62417ab2063SBarry Smith   }
6254846f1f5SKris Buschelman   ierr = MatView_Inode(A,viewer);CHKERRQ(ierr);
6263a40ed3dSBarry Smith   PetscFunctionReturn(0);
62717ab2063SBarry Smith }
62819bcc07fSBarry Smith 
6294a2ae208SSatish Balay #undef __FUNCT__
6304a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
631dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
63217ab2063SBarry Smith {
633416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6346849ba73SBarry Smith   PetscErrorCode ierr;
63597f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
636d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
63754f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6383447b6efSHong Zhang   PetscReal      ratio=0.6;
63917ab2063SBarry Smith 
6403a40ed3dSBarry Smith   PetscFunctionBegin;
6413a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
64217ab2063SBarry Smith 
64343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
64417ab2063SBarry Smith   for (i=1; i<m; i++) {
645416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
64617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
64794a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
64817ab2063SBarry Smith     if (fshift) {
649bfeeae90SHong Zhang       ip = aj + ai[i] ;
650bfeeae90SHong Zhang       ap = aa + ai[i] ;
65117ab2063SBarry Smith       N  = ailen[i];
65217ab2063SBarry Smith       for (j=0; j<N; j++) {
65317ab2063SBarry Smith         ip[j-fshift] = ip[j];
65417ab2063SBarry Smith         ap[j-fshift] = ap[j];
65517ab2063SBarry Smith       }
65617ab2063SBarry Smith     }
65717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
65817ab2063SBarry Smith   }
65917ab2063SBarry Smith   if (m) {
66017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
66117ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
66217ab2063SBarry Smith   }
66317ab2063SBarry Smith   /* reset ilen and imax for each row */
66417ab2063SBarry Smith   for (i=0; i<m; i++) {
66517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
66617ab2063SBarry Smith   }
667bfeeae90SHong Zhang   a->nz = ai[m];
66828b2fa4aSMatthew Knepley   if (fshift && a->nounused == -1) {
66928b2fa4aSMatthew Knepley     SETERRQ3(PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
67028b2fa4aSMatthew Knepley   }
67117ab2063SBarry Smith 
67209f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
673d0f46423SBarry 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);
674ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
675ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
676a0e1a203SBarry Smith 
677dd5f02e7SSatish Balay   a->reallocs          = 0;
6784e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
67936db0b34SBarry Smith   a->rmax              = rmax;
6804e220ebcSLois Curfman McInnes 
681cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
682317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
68388e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
68471c2f376SKris Buschelman 
6854846f1f5SKris Buschelman   ierr = MatAssemblyEnd_Inode(A,mode);CHKERRQ(ierr);
68671f1c65dSBarry Smith 
68771f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
6883a40ed3dSBarry Smith   PetscFunctionReturn(0);
68917ab2063SBarry Smith }
69017ab2063SBarry Smith 
6914a2ae208SSatish Balay #undef __FUNCT__
69299cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
69399cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
69499cafbc1SBarry Smith {
69599cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
69699cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
69754f21887SBarry Smith   MatScalar      *aa = a->a;
69899cafbc1SBarry Smith 
69999cafbc1SBarry Smith   PetscFunctionBegin;
70099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
70199cafbc1SBarry Smith   PetscFunctionReturn(0);
70299cafbc1SBarry Smith }
70399cafbc1SBarry Smith 
70499cafbc1SBarry Smith #undef __FUNCT__
70599cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
70699cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
70799cafbc1SBarry Smith {
70899cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
70999cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
71054f21887SBarry Smith   MatScalar      *aa = a->a;
71199cafbc1SBarry Smith 
71299cafbc1SBarry Smith   PetscFunctionBegin;
71399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
71499cafbc1SBarry Smith   PetscFunctionReturn(0);
71599cafbc1SBarry Smith }
71699cafbc1SBarry Smith 
71799cafbc1SBarry Smith #undef __FUNCT__
7184a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
719dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
72017ab2063SBarry Smith {
721416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
722dfbe8321SBarry Smith   PetscErrorCode ierr;
7233a40ed3dSBarry Smith 
7243a40ed3dSBarry Smith   PetscFunctionBegin;
725d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
7263a40ed3dSBarry Smith   PetscFunctionReturn(0);
72717ab2063SBarry Smith }
728416022c9SBarry Smith 
7294a2ae208SSatish Balay #undef __FUNCT__
7304a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
731dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
73217ab2063SBarry Smith {
733416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
734dfbe8321SBarry Smith   PetscErrorCode ierr;
735d5d45c9bSBarry Smith 
7363a40ed3dSBarry Smith   PetscFunctionBegin;
737aa482453SBarry Smith #if defined(PETSC_USE_LOG)
738d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
73917ab2063SBarry Smith #endif
740e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
741c38d4ed2SBarry Smith   if (a->row) {
742c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
743c38d4ed2SBarry Smith   }
744c38d4ed2SBarry Smith   if (a->col) {
745c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
746c38d4ed2SBarry Smith   }
74705b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
74805b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
74971f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
75005b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
75182bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
75205b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
753cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
75405b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
755407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
756d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
757a30b2313SHong Zhang 
7584846f1f5SKris Buschelman   ierr = MatDestroy_Inode(A);CHKERRQ(ierr);
7594846f1f5SKris Buschelman 
760606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
761901853e0SKris Buschelman 
762dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
763901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
764901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
765901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
766901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
767901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
768ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
769901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
770901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
771a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
772901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
7733a40ed3dSBarry Smith   PetscFunctionReturn(0);
77417ab2063SBarry Smith }
77517ab2063SBarry Smith 
7764a2ae208SSatish Balay #undef __FUNCT__
7774a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7784e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg)
77917ab2063SBarry Smith {
780416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7814846f1f5SKris Buschelman   PetscErrorCode ierr;
7823a40ed3dSBarry Smith 
7833a40ed3dSBarry Smith   PetscFunctionBegin;
784a65d3064SKris Buschelman   switch (op) {
785a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
7864e0d8c25SBarry Smith       a->roworiented       = flg;
787a65d3064SKris Buschelman       break;
788a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
789a9817697SBarry Smith       a->keepnonzeropattern    = flg;
790a65d3064SKris Buschelman       break;
791512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
792512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
793a65d3064SKris Buschelman       break;
794a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
7954e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
796a65d3064SKris Buschelman       break;
797a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
7984e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
799a65d3064SKris Buschelman       break;
80028b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
80128b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
80228b2fa4aSMatthew Knepley       break;
803a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
8044e0d8c25SBarry Smith       a->ignorezeroentries = flg;
8050df259c2SBarry Smith       break;
806d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
8074e0d8c25SBarry Smith       a->compressedrow.use = flg;
808d487561eSHong Zhang       break;
8094e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
810a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
811a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
812290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
813a65d3064SKris Buschelman       break;
814a65d3064SKris Buschelman     default:
81571c2f376SKris Buschelman       break;
816a65d3064SKris Buschelman   }
8174e0d8c25SBarry Smith   ierr = MatSetOption_Inode(A,op,flg);CHKERRQ(ierr);
8183a40ed3dSBarry Smith   PetscFunctionReturn(0);
81917ab2063SBarry Smith }
82017ab2063SBarry Smith 
8214a2ae208SSatish Balay #undef __FUNCT__
8224a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
823dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
82417ab2063SBarry Smith {
825416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8266849ba73SBarry Smith   PetscErrorCode ierr;
82797f1f81fSBarry Smith   PetscInt       i,j,n;
82887828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
82917ab2063SBarry Smith 
8303a40ed3dSBarry Smith   PetscFunctionBegin;
8312dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
8321ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
83336db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
834d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
835d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
836bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
837bfeeae90SHong Zhang       if (a->j[j] == i) {
838416022c9SBarry Smith         x[i] = a->a[j];
83917ab2063SBarry Smith         break;
84017ab2063SBarry Smith       }
84117ab2063SBarry Smith     }
84217ab2063SBarry Smith   }
8431ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8443a40ed3dSBarry Smith   PetscFunctionReturn(0);
84517ab2063SBarry Smith }
84617ab2063SBarry Smith 
8474a2ae208SSatish Balay #undef __FUNCT__
8484a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
849dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
85017ab2063SBarry Smith {
851416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
8525c897100SBarry Smith   PetscScalar       *x,*y;
853dfbe8321SBarry Smith   PetscErrorCode    ierr;
854d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
8555c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
856a77337e4SBarry Smith   MatScalar         *v;
857a77337e4SBarry Smith   PetscScalar       alpha;
8587b2bb3b9SHong Zhang   PetscInt          n,i,*idx,*ii,*ridx=PETSC_NULL;
8593447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
8604eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
8615c897100SBarry Smith #endif
86217ab2063SBarry Smith 
8633a40ed3dSBarry Smith   PetscFunctionBegin;
8642e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
8651ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8661ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8675c897100SBarry Smith 
8685c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
869bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8705c897100SBarry Smith #else
8713447b6efSHong Zhang   if (usecprow){
8723447b6efSHong Zhang     m    = cprow.nrows;
8733447b6efSHong Zhang     ii   = cprow.i;
8747b2bb3b9SHong Zhang     ridx = cprow.rindex;
8753447b6efSHong Zhang   } else {
8763447b6efSHong Zhang     ii = a->i;
8773447b6efSHong Zhang   }
87817ab2063SBarry Smith   for (i=0; i<m; i++) {
8793447b6efSHong Zhang     idx   = a->j + ii[i] ;
8803447b6efSHong Zhang     v     = a->a + ii[i] ;
8813447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8823447b6efSHong Zhang     if (usecprow){
8837b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8843447b6efSHong Zhang     } else {
88517ab2063SBarry Smith       alpha = x[i];
8863447b6efSHong Zhang     }
88717ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
88817ab2063SBarry Smith   }
8895c897100SBarry Smith #endif
890dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
8911ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8921ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8933a40ed3dSBarry Smith   PetscFunctionReturn(0);
89417ab2063SBarry Smith }
89517ab2063SBarry Smith 
8964a2ae208SSatish Balay #undef __FUNCT__
8975c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
898dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8995c897100SBarry Smith {
900dfbe8321SBarry Smith   PetscErrorCode ierr;
9015c897100SBarry Smith 
9025c897100SBarry Smith   PetscFunctionBegin;
903170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
9045c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9055c897100SBarry Smith   PetscFunctionReturn(0);
9065c897100SBarry Smith }
9075c897100SBarry Smith 
908*a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9095c897100SBarry Smith #undef __FUNCT__
9104a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
911dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
91217ab2063SBarry Smith {
913416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
914d9fead3dSBarry Smith   PetscScalar       *y;
91554f21887SBarry Smith   const PetscScalar *x;
91654f21887SBarry Smith   const MatScalar   *aa;
917dfbe8321SBarry Smith   PetscErrorCode    ierr;
918003131ecSBarry Smith   PetscInt          m=A->rmap->n;
919003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
9208aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
921362ced78SSatish Balay   PetscScalar       sum;
92297952fefSHong Zhang   PetscTruth        usecprow=a->compressedrow.use;
92317ab2063SBarry Smith 
924b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
92597952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
926fee21e36SBarry Smith #endif
927fee21e36SBarry Smith 
9283a40ed3dSBarry Smith   PetscFunctionBegin;
929d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9301ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
93197952fefSHong Zhang   aj  = a->j;
93297952fefSHong Zhang   aa  = a->a;
933416022c9SBarry Smith   ii  = a->i;
9344eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
93597952fefSHong Zhang     m    = a->compressedrow.nrows;
93697952fefSHong Zhang     ii   = a->compressedrow.i;
93797952fefSHong Zhang     ridx = a->compressedrow.rindex;
93897952fefSHong Zhang     for (i=0; i<m; i++){
93997952fefSHong Zhang       n   = ii[i+1] - ii[i];
94097952fefSHong Zhang       aj  = a->j + ii[i];
94197952fefSHong Zhang       aa  = a->a + ii[i];
94297952fefSHong Zhang       sum = 0.0;
943a46b3154SVictor Eijkhout       nonzerorow += (n>0);
944003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
945003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
94697952fefSHong Zhang       y[*ridx++] = sum;
94797952fefSHong Zhang     }
94897952fefSHong Zhang   } else { /* do not use compressed row format */
949b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
950b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
951b05257ddSBarry Smith #else
95217ab2063SBarry Smith     for (i=0; i<m; i++) {
953003131ecSBarry Smith       n   = ii[i+1] - ii[i];
954003131ecSBarry Smith       aj  = a->j + ii[i];
955003131ecSBarry Smith       aa  = a->a + ii[i];
95617ab2063SBarry Smith       sum  = 0.0;
957a46b3154SVictor Eijkhout       nonzerorow += (n>0);
958003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
95917ab2063SBarry Smith       y[i] = sum;
96017ab2063SBarry Smith     }
9618d195f9aSBarry Smith #endif
962b05257ddSBarry Smith   }
963dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
964d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9651ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9663a40ed3dSBarry Smith   PetscFunctionReturn(0);
96717ab2063SBarry Smith }
96817ab2063SBarry Smith 
969*a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h"
9704a2ae208SSatish Balay #undef __FUNCT__
9714a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
972dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
97317ab2063SBarry Smith {
974416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
97554f21887SBarry Smith   PetscScalar     *x,*y,*z;
97654f21887SBarry Smith   const MatScalar *aa;
977dfbe8321SBarry Smith   PetscErrorCode  ierr;
978d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
979aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
98097952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
981362ced78SSatish Balay   PetscScalar     sum;
98297952fefSHong Zhang   PetscTruth      usecprow=a->compressedrow.use;
983e36a17ebSSatish Balay #endif
9849ea0dfa2SSatish Balay 
9853a40ed3dSBarry Smith   PetscFunctionBegin;
9861ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9871ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9882e8a6d31SBarry Smith   if (zz != yy) {
9891ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9902e8a6d31SBarry Smith   } else {
9912e8a6d31SBarry Smith     z = y;
9922e8a6d31SBarry Smith   }
993bfeeae90SHong Zhang 
99497952fefSHong Zhang   aj  = a->j;
99597952fefSHong Zhang   aa  = a->a;
996cddf8d76SBarry Smith   ii  = a->i;
997aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
99897952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
99902ab625aSSatish Balay #else
10004eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
10014eb6d288SHong Zhang     if (zz != yy){
10024eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
10034eb6d288SHong Zhang     }
100497952fefSHong Zhang     m    = a->compressedrow.nrows;
100597952fefSHong Zhang     ii   = a->compressedrow.i;
100697952fefSHong Zhang     ridx = a->compressedrow.rindex;
100797952fefSHong Zhang     for (i=0; i<m; i++){
100897952fefSHong Zhang       n  = ii[i+1] - ii[i];
100997952fefSHong Zhang       aj  = a->j + ii[i];
101097952fefSHong Zhang       aa  = a->a + ii[i];
101197952fefSHong Zhang       sum = y[*ridx];
101297952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
101397952fefSHong Zhang       z[*ridx++] = sum;
101497952fefSHong Zhang     }
101597952fefSHong Zhang   } else { /* do not use compressed row format */
101617ab2063SBarry Smith     for (i=0; i<m; i++) {
10179ea0dfa2SSatish Balay       jrow = ii[i];
10189ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
101917ab2063SBarry Smith       sum  = y[i];
10209ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
102197952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10229ea0dfa2SSatish Balay       }
102317ab2063SBarry Smith       z[i] = sum;
102417ab2063SBarry Smith     }
102597952fefSHong Zhang   }
102602ab625aSSatish Balay #endif
1027dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10281ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10291ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10302e8a6d31SBarry Smith   if (zz != yy) {
10311ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
10322e8a6d31SBarry Smith   }
10333a40ed3dSBarry Smith   PetscFunctionReturn(0);
103417ab2063SBarry Smith }
103517ab2063SBarry Smith 
103617ab2063SBarry Smith /*
103717ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
103817ab2063SBarry Smith */
10394a2ae208SSatish Balay #undef __FUNCT__
10404a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1041dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
104217ab2063SBarry Smith {
1043416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10446849ba73SBarry Smith   PetscErrorCode ierr;
1045d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
104617ab2063SBarry Smith 
10473a40ed3dSBarry Smith   PetscFunctionBegin;
104809f38230SBarry Smith   if (!a->diag) {
104909f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
10509518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
105109f38230SBarry Smith   }
1052d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
105309f38230SBarry Smith     a->diag[i] = a->i[i+1];
1054bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1055bfeeae90SHong Zhang       if (a->j[j] == i) {
105609f38230SBarry Smith         a->diag[i] = j;
105717ab2063SBarry Smith         break;
105817ab2063SBarry Smith       }
105917ab2063SBarry Smith     }
106017ab2063SBarry Smith   }
10613a40ed3dSBarry Smith   PetscFunctionReturn(0);
106217ab2063SBarry Smith }
106317ab2063SBarry Smith 
1064be5855fcSBarry Smith /*
1065be5855fcSBarry Smith      Checks for missing diagonals
1066be5855fcSBarry Smith */
10674a2ae208SSatish Balay #undef __FUNCT__
10684a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
106909f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1070be5855fcSBarry Smith {
1071be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
107297f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1073be5855fcSBarry Smith 
1074be5855fcSBarry Smith   PetscFunctionBegin;
107509f38230SBarry Smith   *missing = PETSC_FALSE;
1076d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
107709f38230SBarry Smith     *missing  = PETSC_TRUE;
107809f38230SBarry Smith     if (d) *d = 0;
107909f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
108009f38230SBarry Smith   } else {
1081f1e2ffcdSBarry Smith     diag = a->diag;
1082d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1083bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
108409f38230SBarry Smith 	*missing = PETSC_TRUE;
108509f38230SBarry Smith 	if (d) *d = i;
108609f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
108709f38230SBarry Smith       }
1088be5855fcSBarry Smith     }
1089be5855fcSBarry Smith   }
1090be5855fcSBarry Smith   PetscFunctionReturn(0);
1091be5855fcSBarry Smith }
1092be5855fcSBarry Smith 
109371f1c65dSBarry Smith EXTERN_C_BEGIN
109471f1c65dSBarry Smith #undef __FUNCT__
109571f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
109671f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
109771f1c65dSBarry Smith {
109871f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
109971f1c65dSBarry Smith   PetscErrorCode ierr;
1100d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
110154f21887SBarry Smith   MatScalar      *v = a->a;
110254f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
110371f1c65dSBarry Smith 
110471f1c65dSBarry Smith   PetscFunctionBegin;
110571f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
110671f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
110771f1c65dSBarry Smith   diag = a->diag;
110871f1c65dSBarry Smith   if (!a->idiag) {
110971f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
111071f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
111171f1c65dSBarry Smith     v        = a->a;
111271f1c65dSBarry Smith   }
111371f1c65dSBarry Smith   mdiag = a->mdiag;
111471f1c65dSBarry Smith   idiag = a->idiag;
111571f1c65dSBarry Smith 
1116028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
111771f1c65dSBarry Smith     for (i=0; i<m; i++) {
111871f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
111971f1c65dSBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
112071f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
112171f1c65dSBarry Smith     }
112271f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
112371f1c65dSBarry Smith   } else {
112471f1c65dSBarry Smith     for (i=0; i<m; i++) {
112571f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
112671f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
112771f1c65dSBarry Smith     }
1128dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
112971f1c65dSBarry Smith   }
113071f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
113171f1c65dSBarry Smith   PetscFunctionReturn(0);
113271f1c65dSBarry Smith }
11335a9745a3SMatthew Knepley EXTERN_C_END
113471f1c65dSBarry Smith 
1135*a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/frelax.h"
11364a2ae208SSatish Balay #undef __FUNCT__
11374a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
113897f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
113917ab2063SBarry Smith {
1140416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1141e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1142e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
114354f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1144dfbe8321SBarry Smith   PetscErrorCode     ierr;
1145d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
114697f1f81fSBarry Smith   const PetscInt     *idx,*diag;
114717ab2063SBarry Smith 
11483a40ed3dSBarry Smith   PetscFunctionBegin;
1149b965ef7fSBarry Smith   its = its*lits;
115091723122SBarry Smith 
115171f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
115271f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
115371f1c65dSBarry Smith   a->fshift = fshift;
115471f1c65dSBarry Smith   a->omega  = omega;
1155ed480e8bSBarry Smith 
115671f1c65dSBarry Smith   diag = a->diag;
115771f1c65dSBarry Smith   t     = a->ssor_work;
1158ed480e8bSBarry Smith   idiag = a->idiag;
115971f1c65dSBarry Smith   mdiag = a->mdiag;
1160ed480e8bSBarry Smith 
11611ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1162fb2e594dSBarry Smith   if (xx != bb) {
11631ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1164fb2e594dSBarry Smith   } else {
1165fb2e594dSBarry Smith     b = x;
1166fb2e594dSBarry Smith   }
116771f1c65dSBarry Smith   CHKMEMQ;
1168ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
116917ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
117017ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1171ed480e8bSBarry Smith     bs = b;
117217ab2063SBarry Smith     for (i=0; i<m; i++) {
117371f1c65dSBarry Smith         d    = fshift + mdiag[i];
1174416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1175ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1176ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
117717ab2063SBarry Smith         sum  = b[i]*d/omega;
1178003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
117917ab2063SBarry Smith         x[i] = sum;
118017ab2063SBarry Smith     }
11811ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11821ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1183efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11843a40ed3dSBarry Smith     PetscFunctionReturn(0);
118517ab2063SBarry Smith   }
1186c783ea89SBarry Smith 
118748af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
118829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11893a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
119017ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
119117ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
119217ab2063SBarry Smith 
119317ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
119417ab2063SBarry Smith 
119517ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
119617ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
119717ab2063SBarry Smith     is the relaxation factor.
119817ab2063SBarry Smith     */
119917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
120017ab2063SBarry Smith 
120117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
120217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1203416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1204ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1205ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
120617ab2063SBarry Smith       sum  = b[i];
1207e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1208ed480e8bSBarry Smith       x[i] = sum*idiag[i];
120917ab2063SBarry Smith     }
121017ab2063SBarry Smith 
121117ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1212416022c9SBarry Smith     v = a->a;
1213ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
121417ab2063SBarry Smith 
121517ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1216ed480e8bSBarry Smith     ts = t;
1217416022c9SBarry Smith     diag = a->diag;
121817ab2063SBarry Smith     for (i=0; i<m; i++) {
1219416022c9SBarry Smith       n    = diag[i] - a->i[i];
1220ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1221ed480e8bSBarry Smith       v    = a->a + a->i[i];
122217ab2063SBarry Smith       sum  = t[i];
1223003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1224ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1225733d66baSBarry Smith       /*  x = x + t */
1226733d66baSBarry Smith       x[i] += t[i];
122717ab2063SBarry Smith     }
122817ab2063SBarry Smith 
1229dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
12301ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12311ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12323a40ed3dSBarry Smith     PetscFunctionReturn(0);
123317ab2063SBarry Smith   }
123417ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
123517ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
123677d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
123797f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
123877d8c4bbSBarry Smith #else
123917ab2063SBarry Smith       for (i=0; i<m; i++) {
1240416022c9SBarry Smith         n    = diag[i] - a->i[i];
1241ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1242ed480e8bSBarry Smith         v    = a->a + a->i[i];
124317ab2063SBarry Smith         sum  = b[i];
1244e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1245ed480e8bSBarry Smith         x[i] = sum*idiag[i];
124617ab2063SBarry Smith       }
124777d8c4bbSBarry Smith #endif
124817ab2063SBarry Smith       xb = x;
1249efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12503a40ed3dSBarry Smith     } else xb = b;
125117ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
125217ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
125317ab2063SBarry Smith       for (i=0; i<m; i++) {
1254ed480e8bSBarry Smith         x[i] *= mdiag[i];
125517ab2063SBarry Smith       }
1256efee365bSSatish Balay       ierr = PetscLogFlops(m);CHKERRQ(ierr);
125717ab2063SBarry Smith     }
125817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
125977d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
126097f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
126177d8c4bbSBarry Smith #else
126217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1263416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1264ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1265ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
126617ab2063SBarry Smith         sum  = xb[i];
1267e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1268ed480e8bSBarry Smith         x[i] = sum*idiag[i];
126917ab2063SBarry Smith       }
127077d8c4bbSBarry Smith #endif
1271efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
127217ab2063SBarry Smith     }
127317ab2063SBarry Smith     its--;
127417ab2063SBarry Smith   }
127517ab2063SBarry Smith   while (its--) {
127617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
127777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
127897f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
127977d8c4bbSBarry Smith #else
128017ab2063SBarry Smith       for (i=0; i<m; i++) {
1281416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1282ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1283ed480e8bSBarry Smith         v    = a->a + a->i[i];
128417ab2063SBarry Smith         sum  = b[i];
1285e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1286ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
128717ab2063SBarry Smith       }
128877d8c4bbSBarry Smith #endif
1289efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
129017ab2063SBarry Smith     }
129117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
129277d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
129397f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
129477d8c4bbSBarry Smith #else
129517ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1296416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1297ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1298ed480e8bSBarry Smith         v    = a->a + a->i[i];
129917ab2063SBarry Smith         sum  = b[i];
1300e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1301ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
130217ab2063SBarry Smith       }
130377d8c4bbSBarry Smith #endif
1304efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
130517ab2063SBarry Smith     }
130617ab2063SBarry Smith   }
13071ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
13081ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
130971f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
131017ab2063SBarry Smith }
131117ab2063SBarry Smith 
13122af78befSBarry Smith 
13134a2ae208SSatish Balay #undef __FUNCT__
13144a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1315dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
131617ab2063SBarry Smith {
1317416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13184e220ebcSLois Curfman McInnes 
13193a40ed3dSBarry Smith   PetscFunctionBegin;
13204e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13214e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13224e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13234e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13244e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13254e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
13267adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
13274e220ebcSLois Curfman McInnes   if (A->factor) {
13284e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13294e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13304e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13314e220ebcSLois Curfman McInnes   } else {
13324e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13334e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13344e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13354e220ebcSLois Curfman McInnes   }
13363a40ed3dSBarry Smith   PetscFunctionReturn(0);
133717ab2063SBarry Smith }
133817ab2063SBarry Smith 
13394a2ae208SSatish Balay #undef __FUNCT__
13404a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1341f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
134217ab2063SBarry Smith {
1343416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1344d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n - 1,d;
13456849ba73SBarry Smith   PetscErrorCode ierr;
134609f38230SBarry Smith   PetscTruth     missing;
134717ab2063SBarry Smith 
13483a40ed3dSBarry Smith   PetscFunctionBegin;
1349a9817697SBarry Smith   if (a->keepnonzeropattern) {
1350f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
135177431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1352bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1353f1e2ffcdSBarry Smith     }
1354f4df32b1SMatthew Knepley     if (diag != 0.0) {
135509f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
135609f38230SBarry Smith       if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1357f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1358f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1359f1e2ffcdSBarry Smith       }
1360f1e2ffcdSBarry Smith     }
136188e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1362f1e2ffcdSBarry Smith   } else {
1363f4df32b1SMatthew Knepley     if (diag != 0.0) {
136417ab2063SBarry Smith       for (i=0; i<N; i++) {
136577431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13667ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1367416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1368f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1369bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13707ae801bdSBarry Smith         } else { /* in case row was completely empty */
1371f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
137217ab2063SBarry Smith         }
137317ab2063SBarry Smith       }
13743a40ed3dSBarry Smith     } else {
137517ab2063SBarry Smith       for (i=0; i<N; i++) {
137677431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1377416022c9SBarry Smith         a->ilen[rows[i]] = 0;
137817ab2063SBarry Smith       }
137917ab2063SBarry Smith     }
138088e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1381f1e2ffcdSBarry Smith   }
138243a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13833a40ed3dSBarry Smith   PetscFunctionReturn(0);
138417ab2063SBarry Smith }
138517ab2063SBarry Smith 
13864a2ae208SSatish Balay #undef __FUNCT__
13874a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1388a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
138917ab2063SBarry Smith {
1390416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
139197f1f81fSBarry Smith   PetscInt   *itmp;
139217ab2063SBarry Smith 
13933a40ed3dSBarry Smith   PetscFunctionBegin;
1394d0f46423SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
139517ab2063SBarry Smith 
1396416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1397bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
139817ab2063SBarry Smith   if (idx) {
1399bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1400bfeeae90SHong Zhang     if (*nz) {
14014e093b46SBarry Smith       *idx = itmp;
140217ab2063SBarry Smith     }
140317ab2063SBarry Smith     else *idx = 0;
140417ab2063SBarry Smith   }
14053a40ed3dSBarry Smith   PetscFunctionReturn(0);
140617ab2063SBarry Smith }
140717ab2063SBarry Smith 
1408bfeeae90SHong Zhang /* remove this function? */
14094a2ae208SSatish Balay #undef __FUNCT__
14104a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1411a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
141217ab2063SBarry Smith {
14133a40ed3dSBarry Smith   PetscFunctionBegin;
14143a40ed3dSBarry Smith   PetscFunctionReturn(0);
141517ab2063SBarry Smith }
141617ab2063SBarry Smith 
14174a2ae208SSatish Balay #undef __FUNCT__
14184a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1419dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
142017ab2063SBarry Smith {
1421416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
142254f21887SBarry Smith   MatScalar      *v = a->a;
142336db0b34SBarry Smith   PetscReal      sum = 0.0;
14246849ba73SBarry Smith   PetscErrorCode ierr;
142597f1f81fSBarry Smith   PetscInt       i,j;
142617ab2063SBarry Smith 
14273a40ed3dSBarry Smith   PetscFunctionBegin;
142817ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1429416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1430aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
143136db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
143217ab2063SBarry Smith #else
143317ab2063SBarry Smith       sum += (*v)*(*v); v++;
143417ab2063SBarry Smith #endif
143517ab2063SBarry Smith     }
1436064f8208SBarry Smith     *nrm = sqrt(sum);
14373a40ed3dSBarry Smith   } else if (type == NORM_1) {
143836db0b34SBarry Smith     PetscReal *tmp;
143997f1f81fSBarry Smith     PetscInt    *jj = a->j;
1440d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1441d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1442064f8208SBarry Smith     *nrm = 0.0;
1443416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1444bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
144517ab2063SBarry Smith     }
1446d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1447064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
144817ab2063SBarry Smith     }
1449606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14503a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1451064f8208SBarry Smith     *nrm = 0.0;
1452d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1453bfeeae90SHong Zhang       v = a->a + a->i[j];
145417ab2063SBarry Smith       sum = 0.0;
1455416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1456cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
145717ab2063SBarry Smith       }
1458064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
145917ab2063SBarry Smith     }
14603a40ed3dSBarry Smith   } else {
146129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
146217ab2063SBarry Smith   }
14633a40ed3dSBarry Smith   PetscFunctionReturn(0);
146417ab2063SBarry Smith }
146517ab2063SBarry Smith 
14664a2ae208SSatish Balay #undef __FUNCT__
14674a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1468fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
146917ab2063SBarry Smith {
1470416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1471416022c9SBarry Smith   Mat            C;
14726849ba73SBarry Smith   PetscErrorCode ierr;
1473d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
147454f21887SBarry Smith   MatScalar      *array = a->a;
147517ab2063SBarry Smith 
14763a40ed3dSBarry Smith   PetscFunctionBegin;
1477d0f46423SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1478fc4dec0aSBarry Smith 
1479fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1480d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1481d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1482bfeeae90SHong Zhang 
1483bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
14847adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1485d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
14867adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1487ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1488606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1489a541d17aSBarry Smith   } else {
1490a541d17aSBarry Smith     C = *B;
1491a541d17aSBarry Smith   }
1492a541d17aSBarry Smith 
149317ab2063SBarry Smith   for (i=0; i<m; i++) {
149417ab2063SBarry Smith     len    = ai[i+1]-ai[i];
149587d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1496b9b97703SBarry Smith     array += len;
1497b9b97703SBarry Smith     aj    += len;
149817ab2063SBarry Smith   }
14996d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15006d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
150117ab2063SBarry Smith 
1502815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1503416022c9SBarry Smith     *B = C;
150417ab2063SBarry Smith   } else {
1505273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
150617ab2063SBarry Smith   }
15073a40ed3dSBarry Smith   PetscFunctionReturn(0);
150817ab2063SBarry Smith }
150917ab2063SBarry Smith 
1510cd0d46ebSvictorle EXTERN_C_BEGIN
1511cd0d46ebSvictorle #undef __FUNCT__
15125fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1513be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1514cd0d46ebSvictorle {
1515cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
151654f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
151754f21887SBarry Smith   MatScalar      *va,*vb;
15186849ba73SBarry Smith   PetscErrorCode ierr;
151997f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1520cd0d46ebSvictorle 
1521cd0d46ebSvictorle   PetscFunctionBegin;
1522cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1523cd0d46ebSvictorle 
1524cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1525cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15265485867bSBarry Smith   if (ma!=nb || na!=mb){
15275485867bSBarry Smith     *f = PETSC_FALSE;
15285485867bSBarry Smith     PetscFunctionReturn(0);
15295485867bSBarry Smith   }
1530cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1531cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1532cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
153397f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
153497f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1535cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1536cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1537cd0d46ebSvictorle 
1538cd0d46ebSvictorle   *f = PETSC_TRUE;
1539cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1540cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
154197f1f81fSBarry Smith       PetscInt         idc,idr;
15425485867bSBarry Smith       PetscScalar vc,vr;
1543cd0d46ebSvictorle       /* column/row index/value */
15445485867bSBarry Smith       idc = adx[aptr[i]];
15455485867bSBarry Smith       idr = bdx[bptr[idc]];
15465485867bSBarry Smith       vc  = va[aptr[i]];
15475485867bSBarry Smith       vr  = vb[bptr[idc]];
15485485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15495485867bSBarry Smith 	*f = PETSC_FALSE;
15505485867bSBarry Smith         goto done;
1551cd0d46ebSvictorle       } else {
15525485867bSBarry Smith 	aptr[i]++;
15535485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1554cd0d46ebSvictorle       }
1555cd0d46ebSvictorle     }
1556cd0d46ebSvictorle   }
1557cd0d46ebSvictorle  done:
1558cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
15593aeef889SHong Zhang   if (B) {
15603aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
15613aeef889SHong Zhang   }
1562cd0d46ebSvictorle   PetscFunctionReturn(0);
1563cd0d46ebSvictorle }
1564cd0d46ebSvictorle EXTERN_C_END
1565cd0d46ebSvictorle 
15661cbb95d3SBarry Smith EXTERN_C_BEGIN
15671cbb95d3SBarry Smith #undef __FUNCT__
15681cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
15691cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
15701cbb95d3SBarry Smith {
15711cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
157254f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
157354f21887SBarry Smith   MatScalar      *va,*vb;
15741cbb95d3SBarry Smith   PetscErrorCode ierr;
15751cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
15761cbb95d3SBarry Smith 
15771cbb95d3SBarry Smith   PetscFunctionBegin;
15781cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
15791cbb95d3SBarry Smith 
15801cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
15811cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15821cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
15831cbb95d3SBarry Smith     *f = PETSC_FALSE;
15841cbb95d3SBarry Smith     PetscFunctionReturn(0);
15851cbb95d3SBarry Smith   }
15861cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
15871cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
15881cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
15891cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
15901cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
15911cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
15921cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
15931cbb95d3SBarry Smith 
15941cbb95d3SBarry Smith   *f = PETSC_TRUE;
15951cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
15961cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
15971cbb95d3SBarry Smith       PetscInt         idc,idr;
15981cbb95d3SBarry Smith       PetscScalar vc,vr;
15991cbb95d3SBarry Smith       /* column/row index/value */
16001cbb95d3SBarry Smith       idc = adx[aptr[i]];
16011cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
16021cbb95d3SBarry Smith       vc  = va[aptr[i]];
16031cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
16041cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
16051cbb95d3SBarry Smith 	*f = PETSC_FALSE;
16061cbb95d3SBarry Smith         goto done;
16071cbb95d3SBarry Smith       } else {
16081cbb95d3SBarry Smith 	aptr[i]++;
16091cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
16101cbb95d3SBarry Smith       }
16111cbb95d3SBarry Smith     }
16121cbb95d3SBarry Smith   }
16131cbb95d3SBarry Smith  done:
16141cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
16151cbb95d3SBarry Smith   if (B) {
16161cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
16171cbb95d3SBarry Smith   }
16181cbb95d3SBarry Smith   PetscFunctionReturn(0);
16191cbb95d3SBarry Smith }
16201cbb95d3SBarry Smith EXTERN_C_END
16211cbb95d3SBarry Smith 
16229e29f15eSvictorle #undef __FUNCT__
16239e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1624dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16259e29f15eSvictorle {
1626dfbe8321SBarry Smith   PetscErrorCode ierr;
16279e29f15eSvictorle   PetscFunctionBegin;
16285485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16299e29f15eSvictorle   PetscFunctionReturn(0);
16309e29f15eSvictorle }
16319e29f15eSvictorle 
16324a2ae208SSatish Balay #undef __FUNCT__
16331cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
16341cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16351cbb95d3SBarry Smith {
16361cbb95d3SBarry Smith   PetscErrorCode ierr;
16371cbb95d3SBarry Smith   PetscFunctionBegin;
16381cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16391cbb95d3SBarry Smith   PetscFunctionReturn(0);
16401cbb95d3SBarry Smith }
16411cbb95d3SBarry Smith 
16421cbb95d3SBarry Smith #undef __FUNCT__
16434a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1644dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
164517ab2063SBarry Smith {
1646416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
164754f21887SBarry Smith   PetscScalar    *l,*r,x;
164854f21887SBarry Smith   MatScalar      *v;
1649dfbe8321SBarry Smith   PetscErrorCode ierr;
1650d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
165117ab2063SBarry Smith 
16523a40ed3dSBarry Smith   PetscFunctionBegin;
165317ab2063SBarry Smith   if (ll) {
16543ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
16553ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1656e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1657d0f46423SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
16581ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1659416022c9SBarry Smith     v = a->a;
166017ab2063SBarry Smith     for (i=0; i<m; i++) {
166117ab2063SBarry Smith       x = l[i];
1662416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
166317ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
166417ab2063SBarry Smith     }
16651ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1666efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
166717ab2063SBarry Smith   }
166817ab2063SBarry Smith   if (rr) {
1669e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1670d0f46423SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
16711ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1672416022c9SBarry Smith     v = a->a; jj = a->j;
167317ab2063SBarry Smith     for (i=0; i<nz; i++) {
1674bfeeae90SHong Zhang       (*v++) *= r[*jj++];
167517ab2063SBarry Smith     }
16761ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1677efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
167817ab2063SBarry Smith   }
16793a40ed3dSBarry Smith   PetscFunctionReturn(0);
168017ab2063SBarry Smith }
168117ab2063SBarry Smith 
16824a2ae208SSatish Balay #undef __FUNCT__
16834a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
168497f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
168517ab2063SBarry Smith {
1686db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
16876849ba73SBarry Smith   PetscErrorCode ierr;
1688d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
168997f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
16905d0c19d7SBarry Smith   const PetscInt *irow,*icol;
16915d0c19d7SBarry Smith   PetscInt       nrows,ncols;
169297f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
169354f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1694416022c9SBarry Smith   Mat            C;
169514ca34e6SBarry Smith   PetscTruth     stride,sorted;
169617ab2063SBarry Smith 
16973a40ed3dSBarry Smith   PetscFunctionBegin;
169814ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
169914ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
170014ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
170114ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
170299141d43SSatish Balay 
170317ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1704b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1705b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
170617ab2063SBarry Smith 
1707fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1708fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1709fee21e36SBarry Smith   if (stride && step == 1) {
171002834360SBarry Smith     /* special case of contiguous rows */
171197f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
171231ebf83bSSatish Balay     starts = lens + nrows;
171302834360SBarry Smith     /* loop over new rows determining lens and starting points */
171402834360SBarry Smith     for (i=0; i<nrows; i++) {
1715bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1716a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
171702834360SBarry Smith       for (k=kstart; k<kend; k++) {
1718bfeeae90SHong Zhang         if (aj[k] >= first) {
171902834360SBarry Smith           starts[i] = k;
172002834360SBarry Smith           break;
172102834360SBarry Smith 	}
172202834360SBarry Smith       }
1723a2744918SBarry Smith       sum = 0;
172402834360SBarry Smith       while (k < kend) {
1725bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1726a2744918SBarry Smith         sum++;
172702834360SBarry Smith       }
1728a2744918SBarry Smith       lens[i] = sum;
172902834360SBarry Smith     }
173002834360SBarry Smith     /* create submatrix */
1731cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
173297f1f81fSBarry Smith       PetscInt n_cols,n_rows;
173308480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
173429bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1735d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
173608480c60SBarry Smith       C = *B;
17373a40ed3dSBarry Smith     } else {
17387adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1739f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17407adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1741ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
174208480c60SBarry Smith     }
1743db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1744db02288aSLois Curfman McInnes 
174502834360SBarry Smith     /* loop over rows inserting into submatrix */
1746db02288aSLois Curfman McInnes     a_new    = c->a;
1747db02288aSLois Curfman McInnes     j_new    = c->j;
1748db02288aSLois Curfman McInnes     i_new    = c->i;
1749bfeeae90SHong Zhang 
175002834360SBarry Smith     for (i=0; i<nrows; i++) {
1751a2744918SBarry Smith       ii    = starts[i];
1752a2744918SBarry Smith       lensi = lens[i];
1753a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1754a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
175502834360SBarry Smith       }
175687828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1757a2744918SBarry Smith       a_new      += lensi;
1758a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1759a2744918SBarry Smith       c->ilen[i]  = lensi;
176002834360SBarry Smith     }
1761606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
17623a40ed3dSBarry Smith   } else {
176302834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
176497f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1765bfeeae90SHong Zhang 
176697f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
176797f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
176817ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
176902834360SBarry Smith     /* determine lens of each row */
177002834360SBarry Smith     for (i=0; i<nrows; i++) {
1771bfeeae90SHong Zhang       kstart  = ai[irow[i]];
177202834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
177302834360SBarry Smith       lens[i] = 0;
177402834360SBarry Smith       for (k=kstart; k<kend; k++) {
1775bfeeae90SHong Zhang         if (smap[aj[k]]) {
177602834360SBarry Smith           lens[i]++;
177702834360SBarry Smith         }
177802834360SBarry Smith       }
177902834360SBarry Smith     }
178017ab2063SBarry Smith     /* Create and fill new matrix */
1781a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
17820f5bd95cSBarry Smith       PetscTruth equal;
17830f5bd95cSBarry Smith 
178499141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1785d0f46423SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1786d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
17870f5bd95cSBarry Smith       if (!equal) {
178829bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
178999141d43SSatish Balay       }
1790d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
179108480c60SBarry Smith       C = *B;
17923a40ed3dSBarry Smith     } else {
17937adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1794f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17957adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1796ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
179708480c60SBarry Smith     }
179899141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
179917ab2063SBarry Smith     for (i=0; i<nrows; i++) {
180099141d43SSatish Balay       row    = irow[i];
1801bfeeae90SHong Zhang       kstart = ai[row];
180299141d43SSatish Balay       kend   = kstart + a->ilen[row];
1803bfeeae90SHong Zhang       mat_i  = c->i[i];
180499141d43SSatish Balay       mat_j  = c->j + mat_i;
180599141d43SSatish Balay       mat_a  = c->a + mat_i;
180699141d43SSatish Balay       mat_ilen = c->ilen + i;
180717ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1808bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1809ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
181099141d43SSatish Balay           *mat_a++ = a->a[k];
181199141d43SSatish Balay           (*mat_ilen)++;
181299141d43SSatish Balay 
181317ab2063SBarry Smith         }
181417ab2063SBarry Smith       }
181517ab2063SBarry Smith     }
181602834360SBarry Smith     /* Free work space */
181702834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1818606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1819606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
182002834360SBarry Smith   }
18216d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18226d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
182317ab2063SBarry Smith 
182417ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1825416022c9SBarry Smith   *B = C;
18263a40ed3dSBarry Smith   PetscFunctionReturn(0);
182717ab2063SBarry Smith }
182817ab2063SBarry Smith 
1829a871dcd8SBarry Smith /*
1830a871dcd8SBarry Smith */
18314a2ae208SSatish Balay #undef __FUNCT__
18324a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
18330481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
1834a871dcd8SBarry Smith {
183563b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1836dfbe8321SBarry Smith   PetscErrorCode ierr;
183763b91edcSBarry Smith   Mat            outA;
1838b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
183963b91edcSBarry Smith 
18403a40ed3dSBarry Smith   PetscFunctionBegin;
1841d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1842b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1843b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1844a871dcd8SBarry Smith 
184563b91edcSBarry Smith   outA          = inA;
18465c9eb25fSBarry Smith   inA->factor   = MAT_FACTOR_LU;
1847c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1848c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);}
1849c3122656SLisandro Dalcin   a->row = row;
1850c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1851c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);}
1852c3122656SLisandro Dalcin   a->col = col;
185363b91edcSBarry Smith 
185436db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1855b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
18564c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
185752e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1858f0ec6fceSSatish Balay 
185994a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1860d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1861d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
186294a9d846SBarry Smith   }
186363b91edcSBarry Smith 
1864f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1865137fb511SHong Zhang   if (row_identity && col_identity) {
1866719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ(outA,inA,info);CHKERRQ(ierr);
1867137fb511SHong Zhang   } else {
1868719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
1869137fb511SHong Zhang   }
18703a40ed3dSBarry Smith   PetscFunctionReturn(0);
1871a871dcd8SBarry Smith }
1872a871dcd8SBarry Smith 
1873d9eff348SSatish Balay #include "petscblaslapack.h"
18744a2ae208SSatish Balay #undef __FUNCT__
18754a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1876f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1877f0b747eeSBarry Smith {
1878f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1879f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1880efee365bSSatish Balay   PetscErrorCode ierr;
18810805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
18823a40ed3dSBarry Smith 
18833a40ed3dSBarry Smith   PetscFunctionBegin;
1884f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1885efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18863a40ed3dSBarry Smith   PetscFunctionReturn(0);
1887f0b747eeSBarry Smith }
1888f0b747eeSBarry Smith 
18894a2ae208SSatish Balay #undef __FUNCT__
18904a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
189197f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1892cddf8d76SBarry Smith {
1893dfbe8321SBarry Smith   PetscErrorCode ierr;
189497f1f81fSBarry Smith   PetscInt       i;
1895cddf8d76SBarry Smith 
18963a40ed3dSBarry Smith   PetscFunctionBegin;
1897cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1898b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1899cddf8d76SBarry Smith   }
1900cddf8d76SBarry Smith 
1901cddf8d76SBarry Smith   for (i=0; i<n; i++) {
19026a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1903cddf8d76SBarry Smith   }
19043a40ed3dSBarry Smith   PetscFunctionReturn(0);
1905cddf8d76SBarry Smith }
1906cddf8d76SBarry Smith 
19074a2ae208SSatish Balay #undef __FUNCT__
19084a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
190997f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
19104dcbc457SBarry Smith {
1911e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19126849ba73SBarry Smith   PetscErrorCode ierr;
19135d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
19145d0c19d7SBarry Smith   const PetscInt *idx;
191597f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1916f1af5d2fSBarry Smith   PetscBT        table;
1917bbd702dbSSatish Balay 
19183a40ed3dSBarry Smith   PetscFunctionBegin;
1919d0f46423SBarry Smith   m     = A->rmap->n;
1920e4d965acSSatish Balay   ai    = a->i;
1921bfeeae90SHong Zhang   aj    = a->j;
19228a047759SSatish Balay 
1923a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
192406763907SSatish Balay 
192597f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19266831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
192706763907SSatish Balay 
1928e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1929b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1930e4d965acSSatish Balay     isz  = 0;
19316831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1932e4d965acSSatish Balay 
1933e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
19344dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1935b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1936e4d965acSSatish Balay 
1937dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1938e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1939f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
19404dcbc457SBarry Smith     }
194106763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
194206763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1943e4d965acSSatish Balay 
194404a348a9SBarry Smith     k = 0;
194504a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
194604a348a9SBarry Smith       n = isz;
194706763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1948e4d965acSSatish Balay         row   = nidx[k];
1949e4d965acSSatish Balay         start = ai[row];
1950e4d965acSSatish Balay         end   = ai[row+1];
195104a348a9SBarry Smith         for (l = start; l<end ; l++){
1952efb16452SHong Zhang           val = aj[l] ;
1953f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1954e4d965acSSatish Balay         }
1955e4d965acSSatish Balay       }
1956e4d965acSSatish Balay     }
1957029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1958e4d965acSSatish Balay   }
19596831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1960606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
19613a40ed3dSBarry Smith   PetscFunctionReturn(0);
19624dcbc457SBarry Smith }
196317ab2063SBarry Smith 
19640513a670SBarry Smith /* -------------------------------------------------------------- */
19654a2ae208SSatish Balay #undef __FUNCT__
19664a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1967dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
19680513a670SBarry Smith {
19690513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19706849ba73SBarry Smith   PetscErrorCode ierr;
19715d0c19d7SBarry Smith   PetscInt       i,nz,m = A->rmap->n,n = A->cmap->n;
19725d0c19d7SBarry Smith   const PetscInt *row,*col;
19735d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
197456cd22aeSBarry Smith   IS             icolp,irowp;
197597f1f81fSBarry Smith   PetscInt       *cwork;
197632ec9ce4SBarry Smith   PetscScalar    *vwork;
19770513a670SBarry Smith 
19783a40ed3dSBarry Smith   PetscFunctionBegin;
19794c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
198056cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
19814c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
198256cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
19830513a670SBarry Smith 
19840513a670SBarry Smith   /* determine lengths of permuted rows */
198597f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
19860513a670SBarry Smith   for (i=0; i<m; i++) {
19870513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
19880513a670SBarry Smith   }
19897adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
1990f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
19917adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
1992ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
1993606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
19940513a670SBarry Smith 
199597f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
19960513a670SBarry Smith   for (i=0; i<m; i++) {
199732ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
19980513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1999cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
200032ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20010513a670SBarry Smith   }
2002606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
20033c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
20040513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20050513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
200656cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
200756cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
200856cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
200956cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
20103a40ed3dSBarry Smith   PetscFunctionReturn(0);
20110513a670SBarry Smith }
20120513a670SBarry Smith 
20134a2ae208SSatish Balay #undef __FUNCT__
20144a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2015dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2016cb5b572fSBarry Smith {
2017dfbe8321SBarry Smith   PetscErrorCode ierr;
2018cb5b572fSBarry Smith 
2019cb5b572fSBarry Smith   PetscFunctionBegin;
202033f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
202133f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2022be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2023be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2024be6bf707SBarry Smith 
2025d0f46423SBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) {
2026634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2027cb5b572fSBarry Smith     }
2028d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2029cb5b572fSBarry Smith   } else {
2030cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2031cb5b572fSBarry Smith   }
2032cb5b572fSBarry Smith   PetscFunctionReturn(0);
2033cb5b572fSBarry Smith }
2034cb5b572fSBarry Smith 
20354a2ae208SSatish Balay #undef __FUNCT__
20364a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2037dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2038273d9f13SBarry Smith {
2039dfbe8321SBarry Smith   PetscErrorCode ierr;
2040273d9f13SBarry Smith 
2041273d9f13SBarry Smith   PetscFunctionBegin;
2042ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2043273d9f13SBarry Smith   PetscFunctionReturn(0);
2044273d9f13SBarry Smith }
2045273d9f13SBarry Smith 
20464a2ae208SSatish Balay #undef __FUNCT__
20474a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2048a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
20496c0721eeSBarry Smith {
20506c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
20516c0721eeSBarry Smith   PetscFunctionBegin;
20526c0721eeSBarry Smith   *array = a->a;
20536c0721eeSBarry Smith   PetscFunctionReturn(0);
20546c0721eeSBarry Smith }
20556c0721eeSBarry Smith 
20564a2ae208SSatish Balay #undef __FUNCT__
20574a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2058dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
20596c0721eeSBarry Smith {
20606c0721eeSBarry Smith   PetscFunctionBegin;
20616c0721eeSBarry Smith   PetscFunctionReturn(0);
20626c0721eeSBarry Smith }
2063273d9f13SBarry Smith 
2064ee4f033dSBarry Smith #undef __FUNCT__
2065ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2066dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2067ee4f033dSBarry Smith {
20686849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
20696849ba73SBarry Smith   PetscErrorCode ierr;
207097f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2071efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
207287828ca2SBarry Smith   PetscScalar    *vscale_array;
2073ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2074ee4f033dSBarry Smith   Vec            w1,w2,w3;
2075ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
207690d69ab7SBarry Smith   PetscTruth     flg = PETSC_FALSE;
2077ee4f033dSBarry Smith 
2078ee4f033dSBarry Smith   PetscFunctionBegin;
2079ee4f033dSBarry Smith   if (!coloring->w1) {
2080ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
208152e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2082ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
208352e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2084ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
208552e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2086ee4f033dSBarry Smith   }
2087ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2088ee4f033dSBarry Smith 
2089ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
209090d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2091ee4f033dSBarry Smith   if (flg) {
2092ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2093ee4f033dSBarry Smith   } else {
20940b9b6f31SBarry Smith     PetscTruth assembled;
20950b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
20960b9b6f31SBarry Smith     if (assembled) {
2097ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2098ee4f033dSBarry Smith     }
20990b9b6f31SBarry Smith   }
2100ee4f033dSBarry Smith 
2101ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2102ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2103ee4f033dSBarry Smith 
2104ee4f033dSBarry Smith   /*
2105ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2106ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2107ee4f033dSBarry Smith   */
2108ee4f033dSBarry Smith   if (coloring->F) {
2109ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2110ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2111ee4f033dSBarry Smith     if (m1 != m2) {
2112ee4f033dSBarry Smith       coloring->F = 0;
2113ee4f033dSBarry Smith     }
2114ee4f033dSBarry Smith   }
2115ee4f033dSBarry Smith 
2116ee4f033dSBarry Smith   if (coloring->F) {
2117ee4f033dSBarry Smith     w1          = coloring->F;
2118ee4f033dSBarry Smith     coloring->F = 0;
2119ee4f033dSBarry Smith   } else {
212066f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2121ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
212266f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2123ee4f033dSBarry Smith   }
2124ee4f033dSBarry Smith 
2125ee4f033dSBarry Smith   /*
2126ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2127ee4f033dSBarry Smith   */
21281ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
21291ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2130ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2131ee4f033dSBarry Smith     /*
2132ee4f033dSBarry Smith        Loop over each column associated with color adding the
2133ee4f033dSBarry Smith        perturbation to the vector w3.
2134ee4f033dSBarry Smith     */
2135ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2136ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2137ee4f033dSBarry Smith       dx  = xx[col];
2138ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2139ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2140ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2141ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2142ee4f033dSBarry Smith #else
2143ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2144ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2145ee4f033dSBarry Smith #endif
2146ee4f033dSBarry Smith       dx                *= epsilon;
2147ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2148ee4f033dSBarry Smith     }
2149ee4f033dSBarry Smith   }
21501ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2151ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2152ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2153ee4f033dSBarry Smith 
2154ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2155ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2156ee4f033dSBarry Smith 
2157ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2158ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2159ee4f033dSBarry Smith 
21601ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2161ee4f033dSBarry Smith   /*
2162ee4f033dSBarry Smith       Loop over each color
2163ee4f033dSBarry Smith   */
2164ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
216549b058dcSBarry Smith     coloring->currentcolor = k;
2166ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
21671ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2168ee4f033dSBarry Smith     /*
2169ee4f033dSBarry Smith        Loop over each column associated with color adding the
2170ee4f033dSBarry Smith        perturbation to the vector w3.
2171ee4f033dSBarry Smith     */
2172ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2173ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2174ee4f033dSBarry Smith       dx  = xx[col];
21755b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2176ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2177ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2178ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2179ee4f033dSBarry Smith #else
2180ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2181ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2182ee4f033dSBarry Smith #endif
2183ee4f033dSBarry Smith       dx            *= epsilon;
2184634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2185ee4f033dSBarry Smith       w3_array[col] += dx;
2186ee4f033dSBarry Smith     }
21871ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2188ee4f033dSBarry Smith 
2189ee4f033dSBarry Smith     /*
2190ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2191ee4f033dSBarry Smith     */
2192ee4f033dSBarry Smith 
219366f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2194ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
219566f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2196efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2197ee4f033dSBarry Smith 
2198ee4f033dSBarry Smith     /*
2199ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2200ee4f033dSBarry Smith     */
22011ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2202ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2203ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2204ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2205ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2206ee4f033dSBarry Smith       srow   = row + start;
2207ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2208ee4f033dSBarry Smith     }
22091ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2210ee4f033dSBarry Smith   }
221149b058dcSBarry Smith   coloring->currentcolor = k;
22121ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
22131ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2214ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2215ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2216ee4f033dSBarry Smith   PetscFunctionReturn(0);
2217ee4f033dSBarry Smith }
2218ee4f033dSBarry Smith 
2219ac90fabeSBarry Smith #include "petscblaslapack.h"
2220ac90fabeSBarry Smith #undef __FUNCT__
2221ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2222f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2223ac90fabeSBarry Smith {
2224dfbe8321SBarry Smith   PetscErrorCode ierr;
222597f1f81fSBarry Smith   PetscInt       i;
2226ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
22270805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2228ac90fabeSBarry Smith 
2229ac90fabeSBarry Smith   PetscFunctionBegin;
2230ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2231f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2232f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2233c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2234a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2235a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2236a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2237a30b2313SHong Zhang     }
2238a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2239d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2240a30b2313SHong Zhang       y->XtoY = X;
2241407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2242c537a176SHong Zhang     }
2243f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
22441e2582c4SBarry Smith     ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %d/%d = %G\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz);CHKERRQ(ierr);
2245ac90fabeSBarry Smith   } else {
2246f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2247ac90fabeSBarry Smith   }
2248ac90fabeSBarry Smith   PetscFunctionReturn(0);
2249ac90fabeSBarry Smith }
2250ac90fabeSBarry Smith 
2251521d7252SBarry Smith #undef __FUNCT__
2252521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2253521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2254521d7252SBarry Smith {
2255521d7252SBarry Smith   PetscFunctionBegin;
2256521d7252SBarry Smith   PetscFunctionReturn(0);
2257521d7252SBarry Smith }
2258521d7252SBarry Smith 
2259354c94deSBarry Smith #undef __FUNCT__
2260354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2261354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2262354c94deSBarry Smith {
2263354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2264354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2265354c94deSBarry Smith   PetscInt    i,nz;
2266354c94deSBarry Smith   PetscScalar *a;
2267354c94deSBarry Smith 
2268354c94deSBarry Smith   PetscFunctionBegin;
2269354c94deSBarry Smith   nz = aij->nz;
2270354c94deSBarry Smith   a  = aij->a;
2271354c94deSBarry Smith   for (i=0; i<nz; i++) {
2272354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2273354c94deSBarry Smith   }
2274354c94deSBarry Smith #else
2275354c94deSBarry Smith   PetscFunctionBegin;
2276354c94deSBarry Smith #endif
2277354c94deSBarry Smith   PetscFunctionReturn(0);
2278354c94deSBarry Smith }
2279354c94deSBarry Smith 
2280e34fafa9SBarry Smith #undef __FUNCT__
2281985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2282985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2283e34fafa9SBarry Smith {
2284e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2285e34fafa9SBarry Smith   PetscErrorCode ierr;
2286d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2287e34fafa9SBarry Smith   PetscReal      atmp;
2288985db425SBarry Smith   PetscScalar    *x;
2289e34fafa9SBarry Smith   MatScalar      *aa;
2290e34fafa9SBarry Smith 
2291e34fafa9SBarry Smith   PetscFunctionBegin;
2292e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2293e34fafa9SBarry Smith   aa   = a->a;
2294e34fafa9SBarry Smith   ai   = a->i;
2295e34fafa9SBarry Smith   aj   = a->j;
2296e34fafa9SBarry Smith 
2297985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2298e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2299e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2300d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2301e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2302e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
23039189402eSHong Zhang     x[i] = 0.0;
2304e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2305985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2306985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2307985db425SBarry Smith       aa++; aj++;
2308985db425SBarry Smith     }
2309985db425SBarry Smith   }
2310985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2311985db425SBarry Smith   PetscFunctionReturn(0);
2312985db425SBarry Smith }
2313985db425SBarry Smith 
2314985db425SBarry Smith #undef __FUNCT__
2315985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2316985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2317985db425SBarry Smith {
2318985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2319985db425SBarry Smith   PetscErrorCode ierr;
2320d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2321985db425SBarry Smith   PetscScalar    *x;
2322985db425SBarry Smith   MatScalar      *aa;
2323985db425SBarry Smith 
2324985db425SBarry Smith   PetscFunctionBegin;
2325985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2326985db425SBarry Smith   aa   = a->a;
2327985db425SBarry Smith   ai   = a->i;
2328985db425SBarry Smith   aj   = a->j;
2329985db425SBarry Smith 
2330985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2331985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2332985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2333d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2334985db425SBarry Smith   for (i=0; i<m; i++) {
2335985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2336d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2337985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2338985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2339985db425SBarry Smith       x[i] = 0.0;
2340985db425SBarry Smith       if (idx) {
2341985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2342985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2343985db425SBarry Smith           if (aj[j] > j) {
2344985db425SBarry Smith             idx[i] = j;
2345985db425SBarry Smith             break;
2346985db425SBarry Smith           }
2347985db425SBarry Smith         }
2348985db425SBarry Smith       }
2349985db425SBarry Smith     }
2350985db425SBarry Smith     for (j=0; j<ncols; j++){
2351985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2352985db425SBarry Smith       aa++; aj++;
2353985db425SBarry Smith     }
2354985db425SBarry Smith   }
2355985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2356985db425SBarry Smith   PetscFunctionReturn(0);
2357985db425SBarry Smith }
2358985db425SBarry Smith 
2359985db425SBarry Smith #undef __FUNCT__
2360c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2361c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2362c87e5d42SMatthew Knepley {
2363c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2364c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2365c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2366c87e5d42SMatthew Knepley   PetscReal      atmp;
2367c87e5d42SMatthew Knepley   PetscScalar    *x;
2368c87e5d42SMatthew Knepley   MatScalar      *aa;
2369c87e5d42SMatthew Knepley 
2370c87e5d42SMatthew Knepley   PetscFunctionBegin;
2371c87e5d42SMatthew Knepley   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2372c87e5d42SMatthew Knepley   aa   = a->a;
2373c87e5d42SMatthew Knepley   ai   = a->i;
2374c87e5d42SMatthew Knepley   aj   = a->j;
2375c87e5d42SMatthew Knepley 
2376c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2377c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2378c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2379c87e5d42SMatthew Knepley   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2380c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2381c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2382289a08f5SMatthew Knepley     if (ncols) {
2383289a08f5SMatthew Knepley       /* Get first nonzero */
2384289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2385289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2386289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2387289a08f5SMatthew Knepley       }
2388289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2389289a08f5SMatthew Knepley     } else {
2390289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2391289a08f5SMatthew Knepley     }
2392c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2393c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2394289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2395c87e5d42SMatthew Knepley       aa++; aj++;
2396c87e5d42SMatthew Knepley     }
2397c87e5d42SMatthew Knepley   }
2398c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2399c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2400c87e5d42SMatthew Knepley }
2401c87e5d42SMatthew Knepley 
2402c87e5d42SMatthew Knepley #undef __FUNCT__
2403985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2404985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2405985db425SBarry Smith {
2406985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2407985db425SBarry Smith   PetscErrorCode ierr;
2408d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2409985db425SBarry Smith   PetscScalar    *x;
2410985db425SBarry Smith   MatScalar      *aa;
2411985db425SBarry Smith 
2412985db425SBarry Smith   PetscFunctionBegin;
2413985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2414985db425SBarry Smith   aa   = a->a;
2415985db425SBarry Smith   ai   = a->i;
2416985db425SBarry Smith   aj   = a->j;
2417985db425SBarry Smith 
2418985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2419985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2420985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2421d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2422985db425SBarry Smith   for (i=0; i<m; i++) {
2423985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2424d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2425985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2426985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2427985db425SBarry Smith       x[i] = 0.0;
2428985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2429985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2430985db425SBarry Smith         for (j=0;j<ncols;j++) {
2431985db425SBarry Smith           if (aj[j] > j) {
2432985db425SBarry Smith             idx[i] = j;
2433985db425SBarry Smith             break;
2434985db425SBarry Smith           }
2435985db425SBarry Smith         }
2436985db425SBarry Smith       }
2437985db425SBarry Smith     }
2438985db425SBarry Smith     for (j=0; j<ncols; j++){
2439985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2440985db425SBarry Smith       aa++; aj++;
2441e34fafa9SBarry Smith     }
2442e34fafa9SBarry Smith   }
2443e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2444e34fafa9SBarry Smith   PetscFunctionReturn(0);
2445e34fafa9SBarry Smith }
2446e34fafa9SBarry Smith 
2447682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
24480a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2449cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2450cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2451cb5b572fSBarry Smith        MatMult_SeqAIJ,
245297304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
24537c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
24547c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2455db4efbfdSBarry Smith        0,
2456db4efbfdSBarry Smith        0,
2457db4efbfdSBarry Smith        0,
2458db4efbfdSBarry Smith /*10*/ 0,
2459cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2460cb5b572fSBarry Smith        0,
246117ab2063SBarry Smith        MatRelax_SeqAIJ,
246217ab2063SBarry Smith        MatTranspose_SeqAIJ,
246397304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2464cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2465cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2466cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2467cb5b572fSBarry Smith        MatNorm_SeqAIJ,
246897304618SKris Buschelman /*20*/ 0,
2469cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2470cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2471cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2472d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2473db4efbfdSBarry Smith        0,
2474db4efbfdSBarry Smith        0,
2475db4efbfdSBarry Smith        0,
2476db4efbfdSBarry Smith        0,
2477d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2478db4efbfdSBarry Smith        0,
2479db4efbfdSBarry Smith        0,
24806c0721eeSBarry Smith        MatGetArray_SeqAIJ,
24816c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2482d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2483cb5b572fSBarry Smith        0,
2484cb5b572fSBarry Smith        0,
2485cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2486cb5b572fSBarry Smith        0,
2487d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2488cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2489cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2490cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2491cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2492d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2493cb5b572fSBarry Smith        MatScale_SeqAIJ,
2494cb5b572fSBarry Smith        0,
249579299369SBarry Smith        MatDiagonalSet_SeqAIJ,
24966945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2497d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
24983b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
24993b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
25003b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2501a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2502d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2503b9617806SBarry Smith        0,
25040513a670SBarry Smith        0,
2505cda55fadSBarry Smith        MatPermute_SeqAIJ,
2506cda55fadSBarry Smith        0,
2507d519adbfSMatthew Knepley /*59*/ 0,
2508b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2509b9b97703SBarry Smith        MatView_SeqAIJ,
2510357abbc8SBarry Smith        0,
2511ee4f033dSBarry Smith        0,
2512d519adbfSMatthew Knepley /*64*/ 0,
2513ee4f033dSBarry Smith        0,
2514ee4f033dSBarry Smith        0,
2515ee4f033dSBarry Smith        0,
2516ee4f033dSBarry Smith        0,
2517d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2518c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2519ee4f033dSBarry Smith        0,
2520ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2521dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2522ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2523dcf5cc72SBarry Smith #else
2524dcf5cc72SBarry Smith        0,
2525dcf5cc72SBarry Smith #endif
2526d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
252797304618SKris Buschelman        0,
252897304618SKris Buschelman        0,
252997304618SKris Buschelman        0,
253097304618SKris Buschelman        0,
2531d519adbfSMatthew Knepley /*79*/ 0,
253297304618SKris Buschelman        0,
253397304618SKris Buschelman        0,
253497304618SKris Buschelman        0,
2535bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2536d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
25371cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
25386284ec50SHong Zhang        0,
25396284ec50SHong Zhang        0,
2540bc011b1eSHong Zhang        0,
2541d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
254226be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
254326be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2544d439da42SKris Buschelman        MatPtAP_Basic,
25457ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2546d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2547bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2548bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2549bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
25507ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2551d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2552609c6c4dSKris Buschelman        0,
2553609c6c4dSKris Buschelman        0,
255487d4246cSBarry Smith        MatConjugate_SeqAIJ,
255587d4246cSBarry Smith        0,
2556d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
255799cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2558f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2559f5edf698SHong Zhang        0,
25602bebee5dSHong Zhang        0,
2561d519adbfSMatthew Knepley /*109*/0,
2562985db425SBarry Smith        0,
25632af78befSBarry Smith        MatGetRowMin_SeqAIJ,
25642af78befSBarry Smith        0,
2565599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2566d519adbfSMatthew Knepley /*114*/0,
2567599ef60dSHong Zhang        0,
25683c2a7987SHong Zhang        0,
25693c2a7987SHong Zhang        MatILUDTFactorSymbolic_SeqAIJ,
25703c2a7987SHong Zhang        MatILUDTFactorNumeric_SeqAIJ
25719e29f15eSvictorle };
257217ab2063SBarry Smith 
2573fb2e594dSBarry Smith EXTERN_C_BEGIN
25744a2ae208SSatish Balay #undef __FUNCT__
25754a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2576be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2577bef8e0ddSBarry Smith {
2578bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
257997f1f81fSBarry Smith   PetscInt   i,nz,n;
2580bef8e0ddSBarry Smith 
2581bef8e0ddSBarry Smith   PetscFunctionBegin;
2582bef8e0ddSBarry Smith 
2583bef8e0ddSBarry Smith   nz = aij->maxnz;
2584d0f46423SBarry Smith   n  = mat->rmap->n;
2585bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2586bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2587bef8e0ddSBarry Smith   }
2588bef8e0ddSBarry Smith   aij->nz = nz;
2589bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2590bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2591bef8e0ddSBarry Smith   }
2592bef8e0ddSBarry Smith 
2593bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2594bef8e0ddSBarry Smith }
2595fb2e594dSBarry Smith EXTERN_C_END
2596bef8e0ddSBarry Smith 
25974a2ae208SSatish Balay #undef __FUNCT__
25984a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2599bef8e0ddSBarry Smith /*@
2600bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2601bef8e0ddSBarry Smith        in the matrix.
2602bef8e0ddSBarry Smith 
2603bef8e0ddSBarry Smith   Input Parameters:
2604bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2605bef8e0ddSBarry Smith -  indices - the column indices
2606bef8e0ddSBarry Smith 
260715091d37SBarry Smith   Level: advanced
260815091d37SBarry Smith 
2609bef8e0ddSBarry Smith   Notes:
2610bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2611bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2612bef8e0ddSBarry Smith   of the MatSetValues() operation.
2613bef8e0ddSBarry Smith 
2614bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2615d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2616bef8e0ddSBarry Smith 
2617bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2618bef8e0ddSBarry Smith 
2619b9617806SBarry Smith     The indices should start with zero, not one.
2620b9617806SBarry Smith 
2621bef8e0ddSBarry Smith @*/
2622be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2623bef8e0ddSBarry Smith {
262497f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2625bef8e0ddSBarry Smith 
2626bef8e0ddSBarry Smith   PetscFunctionBegin;
26274482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
26284482741eSBarry Smith   PetscValidPointer(indices,2);
2629c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2630bef8e0ddSBarry Smith   if (f) {
2631bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2632bef8e0ddSBarry Smith   } else {
2633634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2634bef8e0ddSBarry Smith   }
2635bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2636bef8e0ddSBarry Smith }
2637bef8e0ddSBarry Smith 
2638be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2639be6bf707SBarry Smith 
2640fb2e594dSBarry Smith EXTERN_C_BEGIN
26414a2ae208SSatish Balay #undef __FUNCT__
26424a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2643be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2644be6bf707SBarry Smith {
2645be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
26466849ba73SBarry Smith   PetscErrorCode ierr;
2647d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2648be6bf707SBarry Smith 
2649be6bf707SBarry Smith   PetscFunctionBegin;
2650be6bf707SBarry Smith   if (aij->nonew != 1) {
2651512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2652be6bf707SBarry Smith   }
2653be6bf707SBarry Smith 
2654be6bf707SBarry Smith   /* allocate space for values if not already there */
2655be6bf707SBarry Smith   if (!aij->saved_values) {
265687828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
26579518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2658be6bf707SBarry Smith   }
2659be6bf707SBarry Smith 
2660be6bf707SBarry Smith   /* copy values over */
266187828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2662be6bf707SBarry Smith   PetscFunctionReturn(0);
2663be6bf707SBarry Smith }
2664fb2e594dSBarry Smith EXTERN_C_END
2665be6bf707SBarry Smith 
26664a2ae208SSatish Balay #undef __FUNCT__
2667b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2668be6bf707SBarry Smith /*@
2669be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2670be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2671be6bf707SBarry Smith        nonlinear portion.
2672be6bf707SBarry Smith 
2673be6bf707SBarry Smith    Collect on Mat
2674be6bf707SBarry Smith 
2675be6bf707SBarry Smith   Input Parameters:
26760e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2677be6bf707SBarry Smith 
267815091d37SBarry Smith   Level: advanced
267915091d37SBarry Smith 
2680be6bf707SBarry Smith   Common Usage, with SNESSolve():
2681be6bf707SBarry Smith $    Create Jacobian matrix
2682be6bf707SBarry Smith $    Set linear terms into matrix
2683be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2684be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2685be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2686512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2687be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2688be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2689be6bf707SBarry Smith $    In your Jacobian routine
2690be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2691be6bf707SBarry Smith $      Set nonlinear terms in matrix
2692be6bf707SBarry Smith 
2693be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2694be6bf707SBarry Smith $    // build linear portion of Jacobian
2695512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2696be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2697be6bf707SBarry Smith $    loop over nonlinear iterations
2698be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2699be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2700be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2701be6bf707SBarry Smith $       Solve linear system with Jacobian
2702be6bf707SBarry Smith $    endloop
2703be6bf707SBarry Smith 
2704be6bf707SBarry Smith   Notes:
2705be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2706512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2707be6bf707SBarry Smith     calling this routine.
2708be6bf707SBarry Smith 
27090c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
27100c468ba9SBarry Smith     and does not allocated additional space.
27110c468ba9SBarry Smith 
2712be6bf707SBarry Smith .seealso: MatRetrieveValues()
2713be6bf707SBarry Smith 
2714be6bf707SBarry Smith @*/
2715be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2716be6bf707SBarry Smith {
2717dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2718be6bf707SBarry Smith 
2719be6bf707SBarry Smith   PetscFunctionBegin;
27204482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
272129bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
272229bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2723be6bf707SBarry Smith 
2724c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2725be6bf707SBarry Smith   if (f) {
2726be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2727be6bf707SBarry Smith   } else {
2728634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2729be6bf707SBarry Smith   }
2730be6bf707SBarry Smith   PetscFunctionReturn(0);
2731be6bf707SBarry Smith }
2732be6bf707SBarry Smith 
2733fb2e594dSBarry Smith EXTERN_C_BEGIN
27344a2ae208SSatish Balay #undef __FUNCT__
27354a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2736be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2737be6bf707SBarry Smith {
2738be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
27396849ba73SBarry Smith   PetscErrorCode ierr;
2740d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2741be6bf707SBarry Smith 
2742be6bf707SBarry Smith   PetscFunctionBegin;
2743be6bf707SBarry Smith   if (aij->nonew != 1) {
2744512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2745be6bf707SBarry Smith   }
2746be6bf707SBarry Smith   if (!aij->saved_values) {
2747634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2748be6bf707SBarry Smith   }
2749be6bf707SBarry Smith   /* copy values over */
275087828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2751be6bf707SBarry Smith   PetscFunctionReturn(0);
2752be6bf707SBarry Smith }
2753fb2e594dSBarry Smith EXTERN_C_END
2754be6bf707SBarry Smith 
27554a2ae208SSatish Balay #undef __FUNCT__
27564a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2757be6bf707SBarry Smith /*@
2758be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2759be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2760be6bf707SBarry Smith        nonlinear portion.
2761be6bf707SBarry Smith 
2762be6bf707SBarry Smith    Collect on Mat
2763be6bf707SBarry Smith 
2764be6bf707SBarry Smith   Input Parameters:
2765be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2766be6bf707SBarry Smith 
276715091d37SBarry Smith   Level: advanced
276815091d37SBarry Smith 
2769be6bf707SBarry Smith .seealso: MatStoreValues()
2770be6bf707SBarry Smith 
2771be6bf707SBarry Smith @*/
2772be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2773be6bf707SBarry Smith {
2774dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2775be6bf707SBarry Smith 
2776be6bf707SBarry Smith   PetscFunctionBegin;
27774482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
277829bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
277929bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2780be6bf707SBarry Smith 
2781c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2782be6bf707SBarry Smith   if (f) {
2783be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2784be6bf707SBarry Smith   } else {
2785634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2786be6bf707SBarry Smith   }
2787be6bf707SBarry Smith   PetscFunctionReturn(0);
2788be6bf707SBarry Smith }
2789be6bf707SBarry Smith 
2790f83d6046SBarry Smith 
2791be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
27924a2ae208SSatish Balay #undef __FUNCT__
27934a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
279417ab2063SBarry Smith /*@C
2795682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
27960d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
27976e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
279851c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
27992bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
280017ab2063SBarry Smith 
2801db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2802db81eaa0SLois Curfman McInnes 
280317ab2063SBarry Smith    Input Parameters:
2804db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
280517ab2063SBarry Smith .  m - number of rows
280617ab2063SBarry Smith .  n - number of columns
280717ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
280851c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
28092bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
281017ab2063SBarry Smith 
281117ab2063SBarry Smith    Output Parameter:
2812416022c9SBarry Smith .  A - the matrix
281317ab2063SBarry Smith 
2814175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2815ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2816175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2817175b88e8SBarry Smith 
2818b259b22eSLois Curfman McInnes    Notes:
281949a6f317SBarry Smith    If nnz is given then nz is ignored
282049a6f317SBarry Smith 
282117ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
282217ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
28230002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
282444cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
282517ab2063SBarry Smith 
282617ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2827a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
28283d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
28296da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
283017ab2063SBarry Smith 
2831682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
28324fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2833682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
28346c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
28356c7ebb05SLois Curfman McInnes 
28366c7ebb05SLois Curfman McInnes    Options Database Keys:
2837698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2838698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2839db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2840db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2841db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
284217ab2063SBarry Smith 
2843027ccd11SLois Curfman McInnes    Level: intermediate
2844027ccd11SLois Curfman McInnes 
284536db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
284636db0b34SBarry Smith 
284717ab2063SBarry Smith @*/
2848be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
284917ab2063SBarry Smith {
2850dfbe8321SBarry Smith   PetscErrorCode ierr;
28516945ee14SBarry Smith 
28523a40ed3dSBarry Smith   PetscFunctionBegin;
2853f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2854117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2855c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2856ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2857273d9f13SBarry Smith   PetscFunctionReturn(0);
2858273d9f13SBarry Smith }
2859273d9f13SBarry Smith 
28604a2ae208SSatish Balay #undef __FUNCT__
28614a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2862273d9f13SBarry Smith /*@C
2863273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2864273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2865273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2866273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2867273d9f13SBarry Smith 
2868273d9f13SBarry Smith    Collective on MPI_Comm
2869273d9f13SBarry Smith 
2870273d9f13SBarry Smith    Input Parameters:
2871117016b1SBarry Smith +  B - The matrix-free
2872273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2873273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2874273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2875273d9f13SBarry Smith 
2876273d9f13SBarry Smith    Notes:
287749a6f317SBarry Smith      If nnz is given then nz is ignored
287849a6f317SBarry Smith 
2879273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2880273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2881273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2882273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2883273d9f13SBarry Smith 
2884273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2885273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2886273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2887273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2888273d9f13SBarry Smith 
2889aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2890aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2891aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2892aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2893aa95bbe8SBarry Smith 
2894a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2895a96a251dSBarry Smith    entries or columns indices
2896a96a251dSBarry Smith 
2897273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2898273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2899273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2900273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2901273d9f13SBarry Smith 
2902273d9f13SBarry Smith    Options Database Keys:
2903698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2904698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2905273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2906273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2907273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2908273d9f13SBarry Smith 
2909273d9f13SBarry Smith    Level: intermediate
2910273d9f13SBarry Smith 
2911aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
2912273d9f13SBarry Smith 
2913273d9f13SBarry Smith @*/
2914be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2915273d9f13SBarry Smith {
291697f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2917a23d5eceSKris Buschelman 
2918a23d5eceSKris Buschelman   PetscFunctionBegin;
2919a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2920a23d5eceSKris Buschelman   if (f) {
2921a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2922a23d5eceSKris Buschelman   }
2923a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2924a23d5eceSKris Buschelman }
2925a23d5eceSKris Buschelman 
2926a23d5eceSKris Buschelman EXTERN_C_BEGIN
2927a23d5eceSKris Buschelman #undef __FUNCT__
2928a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2929be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2930a23d5eceSKris Buschelman {
2931273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2932a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
29336849ba73SBarry Smith   PetscErrorCode ierr;
293497f1f81fSBarry Smith   PetscInt       i;
2935273d9f13SBarry Smith 
2936273d9f13SBarry Smith   PetscFunctionBegin;
2937d5d45c9bSBarry Smith 
2938a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2939c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2940c461c341SBarry Smith     nz             = 0;
2941c461c341SBarry Smith   }
2942c461c341SBarry Smith 
29437408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(B->rmap,1);CHKERRQ(ierr);
29447408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(B->cmap,1);CHKERRQ(ierr);
2945d0f46423SBarry Smith   ierr = PetscMapSetUp(B->rmap);CHKERRQ(ierr);
2946d0f46423SBarry Smith   ierr = PetscMapSetUp(B->cmap);CHKERRQ(ierr);
2947899cda47SBarry Smith 
2948435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2949435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2950b73539f3SBarry Smith   if (nnz) {
2951d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
295229bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2953d0f46423SBarry Smith       if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap->n);
2954b73539f3SBarry Smith     }
2955b73539f3SBarry Smith   }
2956b73539f3SBarry Smith 
2957273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2958273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2959273d9f13SBarry Smith 
2960ab93d7beSBarry Smith   if (!skipallocation) {
29612ee49352SLisandro Dalcin     if (!b->imax) {
2962d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
2963d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
29642ee49352SLisandro Dalcin     }
2965273d9f13SBarry Smith     if (!nnz) {
2966435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2967273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2968d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
2969d0f46423SBarry Smith       nz = nz*B->rmap->n;
2970273d9f13SBarry Smith     } else {
2971273d9f13SBarry Smith       nz = 0;
2972d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2973273d9f13SBarry Smith     }
2974ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2975d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
2976ab93d7beSBarry Smith 
2977273d9f13SBarry Smith     /* allocate the matrix space */
29782ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
2979d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
2980d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
2981bfeeae90SHong Zhang     b->i[0] = 0;
2982d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
29835da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
29845da197adSKris Buschelman     }
2985273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2986e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
2987e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
2988c461c341SBarry Smith   } else {
2989e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
2990e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
2991c461c341SBarry Smith   }
2992273d9f13SBarry Smith 
2993273d9f13SBarry Smith   b->nz                = 0;
2994273d9f13SBarry Smith   b->maxnz             = nz;
2995273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2996273d9f13SBarry Smith   PetscFunctionReturn(0);
2997273d9f13SBarry Smith }
2998a23d5eceSKris Buschelman EXTERN_C_END
2999273d9f13SBarry Smith 
3000a1661176SMatthew Knepley #undef  __FUNCT__
3001a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
300258d36128SBarry Smith /*@
3003a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3004a1661176SMatthew Knepley 
3005a1661176SMatthew Knepley    Input Parameters:
3006a1661176SMatthew Knepley +  B - the matrix
3007a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3008a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3009a1661176SMatthew Knepley -  v - optional values in the matrix
3010a1661176SMatthew Knepley 
3011d58b2322SMatthew Knepley    Contributed by: Lisandro Dalchin
3012d58b2322SMatthew Knepley 
3013a1661176SMatthew Knepley    Level: developer
3014a1661176SMatthew Knepley 
301558d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
301658d36128SBarry Smith 
3017a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3018a1661176SMatthew Knepley 
3019a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3020a1661176SMatthew Knepley @*/
3021a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3022a1661176SMatthew Knepley {
3023a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
3024a1661176SMatthew Knepley   PetscErrorCode ierr;
3025a1661176SMatthew Knepley 
3026a1661176SMatthew Knepley   PetscFunctionBegin;
3027a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
3028a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3029a1661176SMatthew Knepley   if (f) {
3030a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
3031a1661176SMatthew Knepley   }
3032a1661176SMatthew Knepley   PetscFunctionReturn(0);
3033a1661176SMatthew Knepley }
3034a1661176SMatthew Knepley 
3035a1661176SMatthew Knepley EXTERN_C_BEGIN
3036a1661176SMatthew Knepley #undef  __FUNCT__
3037a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
3038b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3039a1661176SMatthew Knepley {
3040a1661176SMatthew Knepley   PetscInt       i;
3041a1661176SMatthew Knepley   PetscInt       m,n;
3042a1661176SMatthew Knepley   PetscInt       nz;
3043a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3044a1661176SMatthew Knepley   PetscScalar    *values;
3045a1661176SMatthew Knepley   PetscErrorCode ierr;
3046a1661176SMatthew Knepley 
3047a1661176SMatthew Knepley   PetscFunctionBegin;
3048a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3049a1661176SMatthew Knepley 
3050b7940d39SSatish Balay   if (Ii[0]) {
3051b7940d39SSatish Balay     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3052a1661176SMatthew Knepley   }
3053a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3054a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3055b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3056a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
3057a1661176SMatthew Knepley     if (nz < 0) {
3058a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3059a1661176SMatthew Knepley     }
3060a1661176SMatthew Knepley     nnz[i] = nz;
3061a1661176SMatthew Knepley   }
3062a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3063a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3064a1661176SMatthew Knepley 
3065a1661176SMatthew Knepley   if (v) {
3066a1661176SMatthew Knepley     values = (PetscScalar*) v;
3067a1661176SMatthew Knepley   } else {
3068a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3069a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3070a1661176SMatthew Knepley   }
3071a1661176SMatthew Knepley 
3072a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3073b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3074b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3075a1661176SMatthew Knepley   }
3076a1661176SMatthew Knepley 
3077a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3078a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3079a1661176SMatthew Knepley 
3080a1661176SMatthew Knepley   if (!v) {
3081a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3082a1661176SMatthew Knepley   }
3083a1661176SMatthew Knepley   PetscFunctionReturn(0);
3084a1661176SMatthew Knepley }
3085a1661176SMatthew Knepley EXTERN_C_END
3086a1661176SMatthew Knepley 
30877c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h"
3088be7314b0SBarry Smith #include "private/petscaxpy.h"
3089170fe5c8SBarry Smith 
3090170fe5c8SBarry Smith #undef __FUNCT__
3091170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3092170fe5c8SBarry Smith /*
3093170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3094170fe5c8SBarry Smith 
3095170fe5c8SBarry Smith                n                       p                          p
3096170fe5c8SBarry Smith         (              )       (              )         (                  )
3097170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3098170fe5c8SBarry Smith         (              )       (              )         (                  )
3099170fe5c8SBarry Smith 
3100170fe5c8SBarry Smith */
3101170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3102170fe5c8SBarry Smith {
3103170fe5c8SBarry Smith   PetscErrorCode     ierr;
3104170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3105170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3106170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
31071de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3108170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3109170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3110170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3111170fe5c8SBarry Smith 
3112170fe5c8SBarry Smith   PetscFunctionBegin;
3113d0f46423SBarry Smith   m = A->rmap->n;
3114d0f46423SBarry Smith   n = A->cmap->n;
3115d0f46423SBarry Smith   p = B->cmap->n;
3116170fe5c8SBarry Smith   a = sub_a->v;
3117170fe5c8SBarry Smith   b = sub_b->a;
3118170fe5c8SBarry Smith   c = sub_c->v;
3119170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3120170fe5c8SBarry Smith 
3121170fe5c8SBarry Smith   ii  = sub_b->i;
3122170fe5c8SBarry Smith   idx = sub_b->j;
3123170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3124170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3125170fe5c8SBarry Smith     while (q-->0) {
3126170fe5c8SBarry Smith       c_q = c + m*(*idx);
3127170fe5c8SBarry Smith       a_q = a + m*i;
3128be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3129170fe5c8SBarry Smith       idx++;
3130170fe5c8SBarry Smith       b++;
3131170fe5c8SBarry Smith     }
3132170fe5c8SBarry Smith   }
3133170fe5c8SBarry Smith   PetscFunctionReturn(0);
3134170fe5c8SBarry Smith }
3135170fe5c8SBarry Smith 
3136170fe5c8SBarry Smith #undef __FUNCT__
3137170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3138170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3139170fe5c8SBarry Smith {
3140170fe5c8SBarry Smith   PetscErrorCode ierr;
3141d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3142170fe5c8SBarry Smith   Mat            Cmat;
3143170fe5c8SBarry Smith 
3144170fe5c8SBarry Smith   PetscFunctionBegin;
3145d0f46423SBarry Smith   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
314639804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3147170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3148170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3149170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3150170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3151170fe5c8SBarry Smith   *C = Cmat;
3152170fe5c8SBarry Smith   PetscFunctionReturn(0);
3153170fe5c8SBarry Smith }
3154170fe5c8SBarry Smith 
3155170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3156170fe5c8SBarry Smith #undef __FUNCT__
3157170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3158170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3159170fe5c8SBarry Smith {
3160170fe5c8SBarry Smith   PetscErrorCode ierr;
3161170fe5c8SBarry Smith 
3162170fe5c8SBarry Smith   PetscFunctionBegin;
3163170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3164170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3165170fe5c8SBarry Smith   }
3166170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3167170fe5c8SBarry Smith   PetscFunctionReturn(0);
3168170fe5c8SBarry Smith }
3169170fe5c8SBarry Smith 
3170170fe5c8SBarry Smith 
31710bad9183SKris Buschelman /*MC
3172fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
31730bad9183SKris Buschelman    based on compressed sparse row format.
31740bad9183SKris Buschelman 
31750bad9183SKris Buschelman    Options Database Keys:
31760bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
31770bad9183SKris Buschelman 
31780bad9183SKris Buschelman   Level: beginner
31790bad9183SKris Buschelman 
3180f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
31810bad9183SKris Buschelman M*/
31820bad9183SKris Buschelman 
3183a6175056SHong Zhang EXTERN_C_BEGIN
3184b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3185b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3186b5e56a35SBarry Smith #endif
3187611a798aSSatish Balay #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE)
3188af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3189af1023dbSSatish Balay #endif
319017667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*);
3191b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3192db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscTruth *);
3193611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
31945c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_mumps(Mat,MatFactorType,Mat*);
3195611f576cSBarry Smith #endif
3196611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
31975c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3198611f576cSBarry Smith #endif
3199f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3200f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3201f3c0ef26SHong Zhang #endif
3202611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
32035c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3204611f576cSBarry Smith #endif
3205eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3206eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3207eb3b5408SSatish Balay #endif
3208719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3209719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3210719d5645SBarry Smith #endif
321117667f90SBarry Smith EXTERN_C_END
321217667f90SBarry Smith 
3213b24902e0SBarry Smith 
321417667f90SBarry Smith EXTERN_C_BEGIN
32154a2ae208SSatish Balay #undef __FUNCT__
32164a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3217be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3218273d9f13SBarry Smith {
3219273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3220dfbe8321SBarry Smith   PetscErrorCode ierr;
322138baddfdSBarry Smith   PetscMPIInt    size;
3222273d9f13SBarry Smith 
3223273d9f13SBarry Smith   PetscFunctionBegin;
32247adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3225273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3226273d9f13SBarry Smith 
322738f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3228b0a32e0cSBarry Smith   B->data             = (void*)b;
3229549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
323090f02eecSBarry Smith   B->mapping          = 0;
3231416022c9SBarry Smith   b->row              = 0;
3232416022c9SBarry Smith   b->col              = 0;
323382bf6240SBarry Smith   b->icol             = 0;
3234b810aeb4SBarry Smith   b->reallocs         = 0;
323536db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3236f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3237416022c9SBarry Smith   b->nonew             = 0;
3238416022c9SBarry Smith   b->diag              = 0;
3239416022c9SBarry Smith   b->solve_work        = 0;
32402a1b7f2aSHong Zhang   B->spptr             = 0;
3241be6bf707SBarry Smith   b->saved_values      = 0;
3242d7f994e1SBarry Smith   b->idiag             = 0;
324371f1c65dSBarry Smith   b->mdiag             = 0;
324471f1c65dSBarry Smith   b->ssor_work         = 0;
324571f1c65dSBarry Smith   b->omega             = 1.0;
324671f1c65dSBarry Smith   b->fshift            = 0.0;
324771f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3248a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3249a30b2313SHong Zhang   b->xtoy              = 0;
3250a30b2313SHong Zhang   b->XtoY              = 0;
325173e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3252d0f46423SBarry Smith   b->compressedrow.nrows   = B->rmap->n;
3253d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3254d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3255d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
325688e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
325717ab2063SBarry Smith 
325835d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3259b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3260b5e56a35SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_pastix_C",
3261b5e56a35SBarry Smith 					   "MatGetFactor_seqaij_pastix",
3262b5e56a35SBarry Smith 					   MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3263b5e56a35SBarry Smith #endif
3264611a798aSSatish Balay #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE)
3265719d5645SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_essl_C",
3266719d5645SBarry Smith                                      "MatGetFactor_seqaij_essl",
3267719d5645SBarry Smith                                      MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3268719d5645SBarry Smith #endif
3269611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
32705c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_superlu_C",
32715c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
32725c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3273611f576cSBarry Smith #endif
3274f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3275f3c0ef26SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_superlu_dist_C",
3276f3c0ef26SHong Zhang                                      "MatGetFactor_seqaij_superlu_dist",
3277f3c0ef26SHong Zhang                                      MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3278f3c0ef26SHong Zhang #endif
3279611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
32805c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_spooles_C",
32815c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
32825c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3283611f576cSBarry Smith #endif
3284611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
32855c9eb25fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_mumps_C",
32865c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_mumps",
32875c9eb25fSBarry Smith                                      MatGetFactor_seqaij_mumps);CHKERRQ(ierr);
3288611f576cSBarry Smith #endif
3289eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3290eb3b5408SSatish Balay     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_umfpack_C",
3291eb3b5408SSatish Balay                                      "MatGetFactor_seqaij_umfpack",
3292eb3b5408SSatish Balay                                      MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3293eb3b5408SSatish Balay #endif
3294719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3295719d5645SBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_lusol_C",
3296719d5645SBarry Smith                                      "MatGetFactor_seqaij_lusol",
3297719d5645SBarry Smith                                      MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3298719d5645SBarry Smith #endif
3299b24902e0SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_seqaij_petsc_C",
3300b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3301b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3302db4efbfdSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_seqaij_petsc_C",
3303db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqaij_petsc",
3304db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3305f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3306bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3307bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3308f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3309be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3310bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3311f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3312be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3313bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3314a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3315a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3316a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
331785fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
331885fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
331985fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3320ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
3321ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
3322ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
332317667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
332417667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
332517667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
33265fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
33275fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
33285fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
33291cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
33301cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
33311cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3332a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3333a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3334a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3335a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3336a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3337a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
333805b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
333905b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
334005b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3341170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3342170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3343170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3344170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3345170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3346170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3347170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3348170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3349170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
33504846f1f5SKris Buschelman   ierr = MatCreate_Inode(B);CHKERRQ(ierr);
335117667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
33523a40ed3dSBarry Smith   PetscFunctionReturn(0);
335317ab2063SBarry Smith }
3354273d9f13SBarry Smith EXTERN_C_END
335517ab2063SBarry Smith 
33564a2ae208SSatish Balay #undef __FUNCT__
3357b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3358b24902e0SBarry Smith /*
3359b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3360b24902e0SBarry Smith */
3361719d5645SBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues)
336217ab2063SBarry Smith {
3363416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
33646849ba73SBarry Smith   PetscErrorCode ierr;
3365d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
336617ab2063SBarry Smith 
33673a40ed3dSBarry Smith   PetscFunctionBegin;
33681d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
33691d5dac46SHong Zhang 
3370273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3371273d9f13SBarry Smith 
3372416022c9SBarry Smith   C->factor           = A->factor;
33736ad4291fSHong Zhang 
3374416022c9SBarry Smith   c->row            = 0;
3375416022c9SBarry Smith   c->col            = 0;
337682bf6240SBarry Smith   c->icol           = 0;
33776ad4291fSHong Zhang   c->reallocs       = 0;
337817ab2063SBarry Smith 
33796ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
338017ab2063SBarry Smith 
33817408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(C->rmap,1);CHKERRQ(ierr);
33827408324eSLisandro Dalcin   ierr = PetscMapSetBlockSize(C->cmap,1);CHKERRQ(ierr);
3383d0f46423SBarry Smith   ierr = PetscMapSetUp(C->rmap);CHKERRQ(ierr);
3384d0f46423SBarry Smith   ierr = PetscMapSetUp(C->cmap);CHKERRQ(ierr);
3385eec197d1SBarry Smith 
338633b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
33879518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
338817ab2063SBarry Smith   for (i=0; i<m; i++) {
3389416022c9SBarry Smith     c->imax[i] = a->imax[i];
3390416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
339117ab2063SBarry Smith   }
339217ab2063SBarry Smith 
339317ab2063SBarry Smith   /* allocate the matrix space */
3394a96a251dSBarry Smith   ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
33959518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3396f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
339797f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
339817ab2063SBarry Smith   if (m > 0) {
339997f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3400be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
3401bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3402be6bf707SBarry Smith     } else {
3403bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
340417ab2063SBarry Smith     }
340508480c60SBarry Smith   }
340617ab2063SBarry Smith 
34076ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3408416022c9SBarry Smith   c->roworiented       = a->roworiented;
3409416022c9SBarry Smith   c->nonew             = a->nonew;
3410416022c9SBarry Smith   if (a->diag) {
341197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
341252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
341317ab2063SBarry Smith     for (i=0; i<m; i++) {
3414416022c9SBarry Smith       c->diag[i] = a->diag[i];
341517ab2063SBarry Smith     }
34163a40ed3dSBarry Smith   } else c->diag           = 0;
34176ad4291fSHong Zhang   c->solve_work            = 0;
34186ad4291fSHong Zhang   c->saved_values          = 0;
34196ad4291fSHong Zhang   c->idiag                 = 0;
342071f1c65dSBarry Smith   c->ssor_work             = 0;
3421a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3422e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3423e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
34246ad4291fSHong Zhang   c->xtoy                  = 0;
34256ad4291fSHong Zhang   c->XtoY                  = 0;
34266ad4291fSHong Zhang 
3427416022c9SBarry Smith   c->nz                 = a->nz;
3428416022c9SBarry Smith   c->maxnz              = a->maxnz;
3429273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3430754ec7b1SSatish Balay 
34316ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
34326ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
34336ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
34346ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
34356ad4291fSHong Zhang     i = a->compressedrow.nrows;
34366ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
34376ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
34386ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
34396ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
344027ea64f8SHong Zhang   } else {
344127ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
344227ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
344327ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
34446ad4291fSHong Zhang   }
344588e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
34464846f1f5SKris Buschelman   ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr);
34474846f1f5SKris Buschelman 
34487adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
34493a40ed3dSBarry Smith   PetscFunctionReturn(0);
345017ab2063SBarry Smith }
345117ab2063SBarry Smith 
34524a2ae208SSatish Balay #undef __FUNCT__
3453b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3454b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3455b24902e0SBarry Smith {
3456b24902e0SBarry Smith   PetscErrorCode ierr;
3457d0f46423SBarry Smith   PetscInt       n = A->rmap->n;
3458b24902e0SBarry Smith 
3459b24902e0SBarry Smith   PetscFunctionBegin;
3460b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
3461b24902e0SBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
3462b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3463719d5645SBarry Smith   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues);CHKERRQ(ierr);
3464b24902e0SBarry Smith   PetscFunctionReturn(0);
3465b24902e0SBarry Smith }
3466b24902e0SBarry Smith 
3467b24902e0SBarry Smith #undef __FUNCT__
34684a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3469a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A)
347017ab2063SBarry Smith {
3471416022c9SBarry Smith   Mat_SeqAIJ     *a;
3472416022c9SBarry Smith   Mat            B;
34736849ba73SBarry Smith   PetscErrorCode ierr;
34743c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
347538baddfdSBarry Smith   int            fd;
347638baddfdSBarry Smith   PetscMPIInt    size;
3477bcd2baecSBarry Smith   MPI_Comm       comm;
347817ab2063SBarry Smith 
34793a40ed3dSBarry Smith   PetscFunctionBegin;
3480e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3481d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
348229bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3483b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
34840752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3485552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
348617ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
348717ab2063SBarry Smith 
3488d64ed03dSBarry Smith   if (nz < 0) {
348929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3490d64ed03dSBarry Smith   }
3491d64ed03dSBarry Smith 
349217ab2063SBarry Smith   /* read in row lengths */
349397f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
34940752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
349517ab2063SBarry Smith 
34963c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
34973c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
34983c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
34993c601197SSatish Balay 
350017ab2063SBarry Smith   /* create our matrix */
3501f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3502f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3503b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3504ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3505416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
350617ab2063SBarry Smith 
35070752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
350817ab2063SBarry Smith 
350917ab2063SBarry Smith   /* read in nonzero values */
35100752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
351117ab2063SBarry Smith 
351217ab2063SBarry Smith   /* set matrix "i" values */
3513efb16452SHong Zhang   a->i[0] = 0;
351417ab2063SBarry Smith   for (i=1; i<= M; i++) {
3515416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3516416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
351717ab2063SBarry Smith   }
3518606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
351917ab2063SBarry Smith 
35206d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35216d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3522b3a1e11cSKris Buschelman   *A = B;
35233a40ed3dSBarry Smith   PetscFunctionReturn(0);
352417ab2063SBarry Smith }
352517ab2063SBarry Smith 
35264a2ae208SSatish Balay #undef __FUNCT__
3527b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3528dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
35297264ac53SSatish Balay {
35307264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3531dfbe8321SBarry Smith   PetscErrorCode ierr;
35327264ac53SSatish Balay 
35333a40ed3dSBarry Smith   PetscFunctionBegin;
3534bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3535d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3536ca44d042SBarry Smith     *flg = PETSC_FALSE;
3537ca44d042SBarry Smith     PetscFunctionReturn(0);
3538bcd2baecSBarry Smith   }
35397264ac53SSatish Balay 
35407264ac53SSatish Balay   /* if the a->i are the same */
3541d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3542abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
35437264ac53SSatish Balay 
35447264ac53SSatish Balay   /* if a->j are the same */
354597f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3546abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3547bcd2baecSBarry Smith 
3548bcd2baecSBarry Smith   /* if a->a are the same */
354987828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
35500f5bd95cSBarry Smith 
35513a40ed3dSBarry Smith   PetscFunctionReturn(0);
35527264ac53SSatish Balay 
35537264ac53SSatish Balay }
355436db0b34SBarry Smith 
35554a2ae208SSatish Balay #undef __FUNCT__
35564a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
355705869f15SSatish Balay /*@
355836db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
355936db0b34SBarry Smith               provided by the user.
356036db0b34SBarry Smith 
3561c75a6043SHong Zhang       Collective on MPI_Comm
356236db0b34SBarry Smith 
356336db0b34SBarry Smith    Input Parameters:
356436db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
356536db0b34SBarry Smith .   m - number of rows
356636db0b34SBarry Smith .   n - number of columns
356736db0b34SBarry Smith .   i - row indices
356836db0b34SBarry Smith .   j - column indices
356936db0b34SBarry Smith -   a - matrix values
357036db0b34SBarry Smith 
357136db0b34SBarry Smith    Output Parameter:
357236db0b34SBarry Smith .   mat - the matrix
357336db0b34SBarry Smith 
357436db0b34SBarry Smith    Level: intermediate
357536db0b34SBarry Smith 
357636db0b34SBarry Smith    Notes:
35770551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
357836db0b34SBarry Smith     once the matrix is destroyed
357936db0b34SBarry Smith 
358036db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
358136db0b34SBarry Smith 
3582bfeeae90SHong Zhang        The i and j indices are 0 based
358336db0b34SBarry Smith 
3584a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3585a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3586a4552177SSatish Balay     as shown:
3587a4552177SSatish Balay 
3588a4552177SSatish Balay         1 0 0
3589a4552177SSatish Balay         2 0 3
3590a4552177SSatish Balay         4 5 6
3591a4552177SSatish Balay 
3592a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
35939985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3594a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3595a4552177SSatish Balay 
35969985e31cSBarry Smith 
35972fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
359836db0b34SBarry Smith 
359936db0b34SBarry Smith @*/
3600a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
360136db0b34SBarry Smith {
3602dfbe8321SBarry Smith   PetscErrorCode ierr;
3603cbcfb4deSHong Zhang   PetscInt       ii;
360436db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3605cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3606cbcfb4deSHong Zhang   PetscInt       jj;
3607cbcfb4deSHong Zhang #endif
360836db0b34SBarry Smith 
360936db0b34SBarry Smith   PetscFunctionBegin;
3610a96a251dSBarry Smith   if (i[0]) {
3611634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
361236db0b34SBarry Smith   }
3613f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3614f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3615ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3616ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3617ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3618ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3619ab93d7beSBarry Smith 
362036db0b34SBarry Smith   aij->i = i;
362136db0b34SBarry Smith   aij->j = j;
362236db0b34SBarry Smith   aij->a = a;
362336db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
362436db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3625e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3626e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
362736db0b34SBarry Smith 
362836db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
362936db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
36302515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
363179a5c55eSBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
36329985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
36339985e31cSBarry Smith       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
36349985e31cSBarry Smith       if (j[jj] == j[jj]-1) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
36359985e31cSBarry Smith     }
363636db0b34SBarry Smith #endif
363736db0b34SBarry Smith   }
36382515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
363936db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
364079a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
364179a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
364236db0b34SBarry Smith   }
364336db0b34SBarry Smith #endif
364436db0b34SBarry Smith 
3645b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3646b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
364736db0b34SBarry Smith   PetscFunctionReturn(0);
364836db0b34SBarry Smith }
364936db0b34SBarry Smith 
3650cc8ba8e1SBarry Smith #undef __FUNCT__
3651ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3652dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3653cc8ba8e1SBarry Smith {
3654dfbe8321SBarry Smith   PetscErrorCode ierr;
3655cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
365636db0b34SBarry Smith 
3657cc8ba8e1SBarry Smith   PetscFunctionBegin;
36588ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3659cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3660cc8ba8e1SBarry Smith     a->coloring = coloring;
366112c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
366297f1f81fSBarry Smith     PetscInt             i,*larray;
366312c595b3SBarry Smith     ISColoring      ocoloring;
366408b6dcc0SBarry Smith     ISColoringValue *colors;
366512c595b3SBarry Smith 
366612c595b3SBarry Smith     /* set coloring for diagonal portion */
3667d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3668d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
366912c595b3SBarry Smith       larray[i] = i;
367012c595b3SBarry Smith     }
3671d0f46423SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
3672d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3673d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
367412c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
367512c595b3SBarry Smith     }
367612c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3677d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
367812c595b3SBarry Smith     a->coloring = ocoloring;
367912c595b3SBarry Smith   }
3680cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3681cc8ba8e1SBarry Smith }
3682cc8ba8e1SBarry Smith 
3683dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3684ee4f033dSBarry Smith EXTERN_C_BEGIN
368529c1e371SBarry Smith #include "adic/ad_utils.h"
3686ee4f033dSBarry Smith EXTERN_C_END
3687cc8ba8e1SBarry Smith 
3688cc8ba8e1SBarry Smith #undef __FUNCT__
3689ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3690dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3691cc8ba8e1SBarry Smith {
3692cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3693d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
36944440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
369508b6dcc0SBarry Smith   ISColoringValue *color;
3696cc8ba8e1SBarry Smith 
3697cc8ba8e1SBarry Smith   PetscFunctionBegin;
3698e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
36994440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3700cc8ba8e1SBarry Smith   color = a->coloring->colors;
3701cc8ba8e1SBarry Smith   /* loop over rows */
3702cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3703cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3704cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3705cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3706cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3707cc8ba8e1SBarry Smith     }
37084440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3709ee4f033dSBarry Smith   }
3710ee4f033dSBarry Smith   PetscFunctionReturn(0);
3711ee4f033dSBarry Smith }
3712ee4f033dSBarry Smith #endif
3713ee4f033dSBarry Smith 
3714ee4f033dSBarry Smith #undef __FUNCT__
3715ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
371697f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3717ee4f033dSBarry Smith {
3718ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3719d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
372054f21887SBarry Smith   MatScalar       *v = a->a;
372154f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
372208b6dcc0SBarry Smith   ISColoringValue *color;
3723ee4f033dSBarry Smith 
3724ee4f033dSBarry Smith   PetscFunctionBegin;
3725e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3726ee4f033dSBarry Smith   color = a->coloring->colors;
3727ee4f033dSBarry Smith   /* loop over rows */
3728ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3729ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3730ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3731ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3732ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3733ee4f033dSBarry Smith     }
3734ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3735cc8ba8e1SBarry Smith   }
3736cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3737cc8ba8e1SBarry Smith }
373836db0b34SBarry Smith 
373981824310SBarry Smith /*
374081824310SBarry Smith     Special version for direct calls from Fortran
374181824310SBarry Smith */
374281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
374381824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
374481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
374581824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
374681824310SBarry Smith #endif
374781824310SBarry Smith 
374881824310SBarry Smith /* Change these macros so can be used in void function */
374981824310SBarry Smith #undef CHKERRQ
37507adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
375181824310SBarry Smith #undef SETERRQ2
37527adad957SLisandro Dalcin #define SETERRQ2(ierr,b,c,d) CHKERRABORT(((PetscObject)A)->comm,ierr)
375381824310SBarry Smith 
375481824310SBarry Smith EXTERN_C_BEGIN
375581824310SBarry Smith #undef __FUNCT__
375681824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
37571f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
375881824310SBarry Smith {
375981824310SBarry Smith   Mat            A = *AA;
376081824310SBarry Smith   PetscInt       m = *mm, n = *nn;
376181824310SBarry Smith   InsertMode     is = *isis;
376281824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
376381824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
376481824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
376581824310SBarry Smith   PetscErrorCode ierr;
376681824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
376754f21887SBarry Smith   MatScalar      *ap,value,*aa;
3768edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
376981824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
377081824310SBarry Smith 
377181824310SBarry Smith   PetscFunctionBegin;
3772d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
377381824310SBarry Smith   imax = a->imax;
377481824310SBarry Smith   ai = a->i;
377581824310SBarry Smith   ailen = a->ilen;
377681824310SBarry Smith   aj = a->j;
377781824310SBarry Smith   aa = a->a;
377881824310SBarry Smith 
377981824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
378081824310SBarry Smith     row  = im[k];
378181824310SBarry Smith     if (row < 0) continue;
378281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3783d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
378481824310SBarry Smith #endif
378581824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
378681824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
378781824310SBarry Smith     low  = 0;
378881824310SBarry Smith     high = nrow;
378981824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
379081824310SBarry Smith       if (in[l] < 0) continue;
379181824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3792d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
379381824310SBarry Smith #endif
379481824310SBarry Smith       col = in[l];
379581824310SBarry Smith       if (roworiented) {
379681824310SBarry Smith         value = v[l + k*n];
379781824310SBarry Smith       } else {
379881824310SBarry Smith         value = v[k + l*m];
379981824310SBarry Smith       }
380081824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
380181824310SBarry Smith 
380281824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
380381824310SBarry Smith       lastcol = col;
380481824310SBarry Smith       while (high-low > 5) {
380581824310SBarry Smith         t = (low+high)/2;
380681824310SBarry Smith         if (rp[t] > col) high = t;
380781824310SBarry Smith         else             low  = t;
380881824310SBarry Smith       }
380981824310SBarry Smith       for (i=low; i<high; i++) {
381081824310SBarry Smith         if (rp[i] > col) break;
381181824310SBarry Smith         if (rp[i] == col) {
381281824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
381381824310SBarry Smith           else                  ap[i] = value;
381481824310SBarry Smith           goto noinsert;
381581824310SBarry Smith         }
381681824310SBarry Smith       }
381781824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
381881824310SBarry Smith       if (nonew == 1) goto noinsert;
38197adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3820d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
382181824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
382281824310SBarry Smith       /* shift up all the later entries in this row */
382381824310SBarry Smith       for (ii=N; ii>=i; ii--) {
382481824310SBarry Smith         rp[ii+1] = rp[ii];
382581824310SBarry Smith         ap[ii+1] = ap[ii];
382681824310SBarry Smith       }
382781824310SBarry Smith       rp[i] = col;
382881824310SBarry Smith       ap[i] = value;
382981824310SBarry Smith       noinsert:;
383081824310SBarry Smith       low = i + 1;
383181824310SBarry Smith     }
383281824310SBarry Smith     ailen[row] = nrow;
383381824310SBarry Smith   }
383481824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
383581824310SBarry Smith   PetscFunctionReturnVoid();
383681824310SBarry Smith }
383781824310SBarry Smith EXTERN_C_END
3838