xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 25922d2b4d5ad70bed352f4782abcd1a61dd4c80)
173f4d377SMatthew Knepley /*$Id: aij.c,v 1.385 2001/09/07 20:09:22 bsmith Exp $*/
2d5d45c9bSBarry Smith /*
33369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
4d5d45c9bSBarry Smith   matrix storage format.
5d5d45c9bSBarry Smith */
63369ce9aSBarry Smith 
79e070d67SMatthew Knepley #include "src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
8f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
9f5eb4b81SSatish Balay #include "src/inline/spops.h"
108d195f9aSBarry Smith #include "src/inline/dot.h"
110a835dfdSSatish Balay #include "petscbt.h"
1217ab2063SBarry Smith 
13a2ce50c7SBarry Smith 
14ca44d042SBarry Smith EXTERN int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**);
1517ab2063SBarry Smith 
164a2ae208SSatish Balay #undef __FUNCT__
174a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
184e7234bfSBarry Smith int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int *ia[],int *ja[],PetscTruth *done)
1917ab2063SBarry Smith {
20416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
216945ee14SBarry Smith   int        ierr,i,ishift;
2217ab2063SBarry Smith 
233a40ed3dSBarry Smith   PetscFunctionBegin;
2431625ec5SSatish Balay   *m     = A->m;
253a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
26bfeeae90SHong Zhang   ishift = 0;
2753e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
28273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
29bfeeae90SHong Zhang   } else if (oshift == 1) {
30435da068SBarry Smith     int nz = a->i[A->m];
313b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
321bc30dd5SBarry Smith     ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr);
33b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr);
343b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
35273d9f13SBarry Smith     for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1;
366945ee14SBarry Smith   } else {
376945ee14SBarry Smith     *ia = a->i; *ja = a->j;
38a2ce50c7SBarry Smith   }
393a40ed3dSBarry Smith   PetscFunctionReturn(0);
40a2744918SBarry Smith }
41a2744918SBarry Smith 
424a2ae208SSatish Balay #undef __FUNCT__
434a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
444e7234bfSBarry Smith int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int *ia[],int *ja[],PetscTruth *done)
456945ee14SBarry Smith {
46bfeeae90SHong Zhang   int ierr;
476945ee14SBarry Smith 
483a40ed3dSBarry Smith   PetscFunctionBegin;
493a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
50bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
51606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
52606d414cSSatish Balay     ierr = PetscFree(*ja);CHKERRQ(ierr);
53bcd2baecSBarry Smith   }
543a40ed3dSBarry Smith   PetscFunctionReturn(0);
5517ab2063SBarry Smith }
5617ab2063SBarry Smith 
574a2ae208SSatish Balay #undef __FUNCT__
584a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
594e7234bfSBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int *ia[],int *ja[],PetscTruth *done)
603b2fbd54SBarry Smith {
613b2fbd54SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
62bfeeae90SHong Zhang   int        ierr,i,*collengths,*cia,*cja,n = A->n,m = A->m;
63bfeeae90SHong Zhang   int        nz = a->i[m],row,*jj,mr,col;
643b2fbd54SBarry Smith 
653a40ed3dSBarry Smith   PetscFunctionBegin;
663b2fbd54SBarry Smith   *nn     = A->n;
673a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
683b2fbd54SBarry Smith   if (symmetric) {
69bfeeae90SHong Zhang     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
703b2fbd54SBarry Smith   } else {
71b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&collengths);CHKERRQ(ierr);
72549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
73b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&cia);CHKERRQ(ierr);
74b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&cja);CHKERRQ(ierr);
753b2fbd54SBarry Smith     jj = a->j;
763b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
77bfeeae90SHong Zhang       collengths[jj[i]]++;
783b2fbd54SBarry Smith     }
793b2fbd54SBarry Smith     cia[0] = oshift;
803b2fbd54SBarry Smith     for (i=0; i<n; i++) {
813b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
823b2fbd54SBarry Smith     }
83549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
843b2fbd54SBarry Smith     jj   = a->j;
85a93ec695SBarry Smith     for (row=0; row<m; row++) {
86a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
87a93ec695SBarry Smith       for (i=0; i<mr; i++) {
88bfeeae90SHong Zhang         col = *jj++;
893b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
903b2fbd54SBarry Smith       }
913b2fbd54SBarry Smith     }
92606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
933b2fbd54SBarry Smith     *ia = cia; *ja = cja;
943b2fbd54SBarry Smith   }
953a40ed3dSBarry Smith   PetscFunctionReturn(0);
963b2fbd54SBarry Smith }
973b2fbd54SBarry Smith 
984a2ae208SSatish Balay #undef __FUNCT__
994a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
1004e7234bfSBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int *ia[],int *ja[],PetscTruth *done)
1013b2fbd54SBarry Smith {
102606d414cSSatish Balay   int ierr;
103606d414cSSatish Balay 
1043a40ed3dSBarry Smith   PetscFunctionBegin;
1053a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1063b2fbd54SBarry Smith 
107606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
108606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1093b2fbd54SBarry Smith 
1103a40ed3dSBarry Smith   PetscFunctionReturn(0);
1113b2fbd54SBarry Smith }
1123b2fbd54SBarry Smith 
113227d817aSBarry Smith #define CHUNKSIZE   15
11417ab2063SBarry Smith 
1154a2ae208SSatish Balay #undef __FUNCT__
1164a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
117f15d580aSBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode is)
11817ab2063SBarry Smith {
119416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
120416022c9SBarry Smith   int         *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,sorted = a->sorted;
121273d9f13SBarry Smith   int         *imax = a->imax,*ai = a->i,*ailen = a->ilen;
122bfeeae90SHong Zhang   int         *aj = a->j,nonew = a->nonew,ierr;
12387828ca2SBarry Smith   PetscScalar *ap,value,*aa = a->a;
12436db0b34SBarry Smith   PetscTruth  ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE);
125273d9f13SBarry Smith   PetscTruth  roworiented = a->roworiented;
12617ab2063SBarry Smith 
1273a40ed3dSBarry Smith   PetscFunctionBegin;
12817ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
129416022c9SBarry Smith     row  = im[k];
1305ef9f2a5SBarry Smith     if (row < 0) continue;
131aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
132590ac198SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",row,A->m-1);
1333b2fbd54SBarry Smith #endif
134bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
13517ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
136416022c9SBarry Smith     low = 0;
13717ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1385ef9f2a5SBarry Smith       if (in[l] < 0) continue;
139aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
140590ac198SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[l],A->n-1);
1413b2fbd54SBarry Smith #endif
142bfeeae90SHong Zhang       col = in[l];
1434b0e389bSBarry Smith       if (roworiented) {
1445ef9f2a5SBarry Smith         value = v[l + k*n];
145bef8e0ddSBarry Smith       } else {
1464b0e389bSBarry Smith         value = v[k + l*m];
1474b0e389bSBarry Smith       }
14836db0b34SBarry Smith       if (value == 0.0 && ignorezeroentries) continue;
14936db0b34SBarry Smith 
150416022c9SBarry Smith       if (!sorted) low = 0; high = nrow;
151416022c9SBarry Smith       while (high-low > 5) {
152416022c9SBarry Smith         t = (low+high)/2;
153416022c9SBarry Smith         if (rp[t] > col) high = t;
154416022c9SBarry Smith         else             low  = t;
15517ab2063SBarry Smith       }
156416022c9SBarry Smith       for (i=low; i<high; i++) {
15717ab2063SBarry Smith         if (rp[i] > col) break;
15817ab2063SBarry Smith         if (rp[i] == col) {
159416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
16017ab2063SBarry Smith           else                  ap[i] = value;
16117ab2063SBarry Smith           goto noinsert;
16217ab2063SBarry Smith         }
16317ab2063SBarry Smith       }
164c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
16529bbc08cSBarry Smith       else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix",row,col);
16617ab2063SBarry Smith       if (nrow >= rmax) {
16717ab2063SBarry Smith         /* there is no extra room in row, therefore enlarge */
168a7ed9263SMatthew Knepley         int         new_nz = ai[A->m] + CHUNKSIZE,*new_i,*new_j;
169a7ed9263SMatthew Knepley         size_t      len;
17087828ca2SBarry Smith         PetscScalar *new_a;
17117ab2063SBarry Smith 
17229bbc08cSBarry Smith         if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix requiring new malloc()",row,col);
17396854ed6SLois Curfman McInnes 
17417ab2063SBarry Smith         /* malloc new storage space */
175a7ed9263SMatthew Knepley         len     = ((size_t) new_nz)*(sizeof(int)+sizeof(PetscScalar))+(A->m+1)*sizeof(int);
176b0a32e0cSBarry Smith 	ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr);
17717ab2063SBarry Smith         new_j   = (int*)(new_a + new_nz);
17817ab2063SBarry Smith         new_i   = new_j + new_nz;
17917ab2063SBarry Smith 
18017ab2063SBarry Smith         /* copy over old data into new slots */
18117ab2063SBarry Smith         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];}
182273d9f13SBarry Smith         for (ii=row+1; ii<A->m+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;}
183bfeeae90SHong Zhang         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow)*sizeof(int));CHKERRQ(ierr);
184bfeeae90SHong Zhang         len  = (((size_t) new_nz) - CHUNKSIZE - ai[row] - nrow );
185bfeeae90SHong Zhang         ierr = PetscMemcpy(new_j+ai[row]+nrow+CHUNKSIZE,aj+ai[row]+nrow,len*sizeof(int));CHKERRQ(ierr);
186bfeeae90SHong Zhang         ierr = PetscMemcpy(new_a,aa,(((size_t) ai[row])+nrow)*sizeof(PetscScalar));CHKERRQ(ierr);
187bfeeae90SHong Zhang         ierr = PetscMemcpy(new_a+ai[row]+nrow+CHUNKSIZE,aa+ai[row]+nrow,len*sizeof(PetscScalar));CHKERRQ(ierr);
18817ab2063SBarry Smith         /* free up old matrix storage */
189606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);
190606d414cSSatish Balay         if (!a->singlemalloc) {
191606d414cSSatish Balay           ierr = PetscFree(a->i);CHKERRQ(ierr);
192606d414cSSatish Balay           ierr = PetscFree(a->j);CHKERRQ(ierr);
193606d414cSSatish Balay         }
194416022c9SBarry Smith         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;
195f1e2ffcdSBarry Smith         a->singlemalloc = PETSC_TRUE;
19617ab2063SBarry Smith 
197bfeeae90SHong Zhang         rp   = aj + ai[row]; ap = aa + ai[row] ;
198416022c9SBarry Smith         rmax = imax[row] = imax[row] + CHUNKSIZE;
19987828ca2SBarry Smith         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar)));
200416022c9SBarry Smith         a->maxnz += CHUNKSIZE;
201b810aeb4SBarry Smith         a->reallocs++;
20217ab2063SBarry Smith       }
203416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
204416022c9SBarry Smith       /* shift up all the later entries in this row */
205416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
20617ab2063SBarry Smith         rp[ii+1] = rp[ii];
20717ab2063SBarry Smith         ap[ii+1] = ap[ii];
20817ab2063SBarry Smith       }
20917ab2063SBarry Smith       rp[i] = col;
21017ab2063SBarry Smith       ap[i] = value;
21117ab2063SBarry Smith       noinsert:;
212416022c9SBarry Smith       low = i + 1;
21317ab2063SBarry Smith     }
21417ab2063SBarry Smith     ailen[row] = nrow;
21517ab2063SBarry Smith   }
2163a40ed3dSBarry Smith   PetscFunctionReturn(0);
21717ab2063SBarry Smith }
21817ab2063SBarry Smith 
2194a2ae208SSatish Balay #undef __FUNCT__
2204a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
221f15d580aSBarry Smith int MatGetValues_SeqAIJ(Mat A,int m,const int im[],int n,const int in[],PetscScalar v[])
2227eb43aa7SLois Curfman McInnes {
2237eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
224b49de8d1SLois Curfman McInnes   int          *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
225bfeeae90SHong Zhang   int          *ai = a->i,*ailen = a->ilen;
22687828ca2SBarry Smith   PetscScalar  *ap,*aa = a->a,zero = 0.0;
2277eb43aa7SLois Curfman McInnes 
2283a40ed3dSBarry Smith   PetscFunctionBegin;
2297eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2307eb43aa7SLois Curfman McInnes     row  = im[k];
23129bbc08cSBarry Smith     if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",row);
232590ac198SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",row,A->m-1);
233bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2347eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2357eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
23629bbc08cSBarry Smith       if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",in[l]);
237590ac198SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[l],A->n-1);
238bfeeae90SHong Zhang       col = in[l] ;
2397eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2407eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2417eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2427eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2437eb43aa7SLois Curfman McInnes         else             low  = t;
2447eb43aa7SLois Curfman McInnes       }
2457eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2467eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2477eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
248b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2497eb43aa7SLois Curfman McInnes           goto finished;
2507eb43aa7SLois Curfman McInnes         }
2517eb43aa7SLois Curfman McInnes       }
252b49de8d1SLois Curfman McInnes       *v++ = zero;
2537eb43aa7SLois Curfman McInnes       finished:;
2547eb43aa7SLois Curfman McInnes     }
2557eb43aa7SLois Curfman McInnes   }
2563a40ed3dSBarry Smith   PetscFunctionReturn(0);
2577eb43aa7SLois Curfman McInnes }
2587eb43aa7SLois Curfman McInnes 
25917ab2063SBarry Smith 
2604a2ae208SSatish Balay #undef __FUNCT__
2614a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
262b0a32e0cSBarry Smith int MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
26317ab2063SBarry Smith {
264416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
265416022c9SBarry Smith   int        i,fd,*col_lens,ierr;
26617ab2063SBarry Smith 
2673a40ed3dSBarry Smith   PetscFunctionBegin;
268b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
269b0a32e0cSBarry Smith   ierr = PetscMalloc((4+A->m)*sizeof(int),&col_lens);CHKERRQ(ierr);
270552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
271273d9f13SBarry Smith   col_lens[1] = A->m;
272273d9f13SBarry Smith   col_lens[2] = A->n;
273416022c9SBarry Smith   col_lens[3] = a->nz;
274416022c9SBarry Smith 
275416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
276273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
277416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
27817ab2063SBarry Smith   }
279273d9f13SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->m,PETSC_INT,1);CHKERRQ(ierr);
280606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
281416022c9SBarry Smith 
282416022c9SBarry Smith   /* store column indices (zero start index) */
2830752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,0);CHKERRQ(ierr);
284416022c9SBarry Smith 
285416022c9SBarry Smith   /* store nonzero values */
2860752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,0);CHKERRQ(ierr);
2873a40ed3dSBarry Smith   PetscFunctionReturn(0);
28817ab2063SBarry Smith }
289416022c9SBarry Smith 
2904ffef66eSHong Zhang extern int MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
291cd155464SBarry Smith 
2924a2ae208SSatish Balay #undef __FUNCT__
2934a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
294b0a32e0cSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
295416022c9SBarry Smith {
296416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
297bfeeae90SHong Zhang   int               ierr,i,j,m = A->m,shift=0;
298fb9695e5SSatish Balay   char              *name;
299f3ef73ceSBarry Smith   PetscViewerFormat format;
30017ab2063SBarry Smith 
3013a40ed3dSBarry Smith   PetscFunctionBegin;
302435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
303b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
304456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL || format == PETSC_VIEWER_ASCII_INFO) {
3056831982aSBarry Smith     if (a->inode.size) {
306b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"using I-node routines: found %d nodes, limit used is %d\n",a->inode.node_count,a->inode.limit);CHKERRQ(ierr);
3076831982aSBarry Smith     } else {
308b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"not using I-node routines\n");CHKERRQ(ierr);
3096831982aSBarry Smith     }
310fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
311d00d2cf4SBarry Smith     int nofinalvalue = 0;
312273d9f13SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) {
313d00d2cf4SBarry Smith       nofinalvalue = 1;
314d00d2cf4SBarry Smith     }
315b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
316b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %d %d \n",m,A->n);CHKERRQ(ierr);
317b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %d \n",a->nz);CHKERRQ(ierr);
318b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
319b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
32017ab2063SBarry Smith 
32117ab2063SBarry Smith     for (i=0; i<m; i++) {
322416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
323aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
324b0a32e0cSBarry 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);
32517ab2063SBarry Smith #else
326b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
32717ab2063SBarry Smith #endif
32817ab2063SBarry Smith       }
32917ab2063SBarry Smith     }
330d00d2cf4SBarry Smith     if (nofinalvalue) {
331b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",m,A->n,0.0);CHKERRQ(ierr);
332d00d2cf4SBarry Smith     }
333fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
334b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
335cd155464SBarry Smith   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
3364ffef66eSHong Zhang #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_COMPLEX)
3374ffef66eSHong Zhang      ierr = MatSeqAIJFactorInfo_Matlab(A,viewer);CHKERRQ(ierr);
3384ffef66eSHong Zhang #endif
339cd155464SBarry Smith      PetscFunctionReturn(0);
340fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
341b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
34244cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
343b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
34444cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
345aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34636db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
34762b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
34836db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
34962b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
35036db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
35162b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3526831982aSBarry Smith         }
35344cd7ae7SLois Curfman McInnes #else
35462b941d6SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
35544cd7ae7SLois Curfman McInnes #endif
35644cd7ae7SLois Curfman McInnes       }
357b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
35844cd7ae7SLois Curfman McInnes     }
359b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
360fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
361496be53dSLois Curfman McInnes     int nzd=0,fshift=1,*sptr;
362b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
363b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&sptr);CHKERRQ(ierr);
364496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
365496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
366496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
367496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
368aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
370496be53dSLois Curfman McInnes #else
371496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
372496be53dSLois Curfman McInnes #endif
373496be53dSLois Curfman McInnes         }
374496be53dSLois Curfman McInnes       }
375496be53dSLois Curfman McInnes     }
3762e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
377b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %d %d\n\n",m,nzd);CHKERRQ(ierr);
3782e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
379b0a32e0cSBarry 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);}
380b0a32e0cSBarry 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);}
381b0a32e0cSBarry 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);}
382b0a32e0cSBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
383b0a32e0cSBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %d %d\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
384b0a32e0cSBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %d\n",sptr[i]);CHKERRQ(ierr);}
385496be53dSLois Curfman McInnes     }
386b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
387606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
388496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
389496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
390b0a32e0cSBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %d ",a->j[j]+fshift);CHKERRQ(ierr);}
391496be53dSLois Curfman McInnes       }
392b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
393496be53dSLois Curfman McInnes     }
394b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
395496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
396496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
397496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
398aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
39936db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
400b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4016831982aSBarry Smith           }
402496be53dSLois Curfman McInnes #else
403b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
404496be53dSLois Curfman McInnes #endif
405496be53dSLois Curfman McInnes         }
406496be53dSLois Curfman McInnes       }
407b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
408496be53dSLois Curfman McInnes     }
409b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
410fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
41102594712SBarry Smith     int         cnt = 0,jcnt;
41287828ca2SBarry Smith     PetscScalar value;
41302594712SBarry Smith 
414b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
41502594712SBarry Smith     for (i=0; i<m; i++) {
41602594712SBarry Smith       jcnt = 0;
417273d9f13SBarry Smith       for (j=0; j<A->n; j++) {
418e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
41902594712SBarry Smith           value = a->a[cnt++];
420e24b481bSBarry Smith           jcnt++;
42102594712SBarry Smith         } else {
42202594712SBarry Smith           value = 0.0;
42302594712SBarry Smith         }
424aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
425b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
42602594712SBarry Smith #else
427b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
42802594712SBarry Smith #endif
42902594712SBarry Smith       }
430b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
43102594712SBarry Smith     }
432b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4333a40ed3dSBarry Smith   } else {
434b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
43517ab2063SBarry Smith     for (i=0; i<m; i++) {
436b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
437416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
438aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
43936db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
44062b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
44136db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
44262b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4433a40ed3dSBarry Smith         } else {
44462b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
44517ab2063SBarry Smith         }
44617ab2063SBarry Smith #else
44762b941d6SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
44817ab2063SBarry Smith #endif
44917ab2063SBarry Smith       }
450b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45117ab2063SBarry Smith     }
452b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
45317ab2063SBarry Smith   }
454b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4553a40ed3dSBarry Smith   PetscFunctionReturn(0);
456416022c9SBarry Smith }
457416022c9SBarry Smith 
4584a2ae208SSatish Balay #undef __FUNCT__
4594a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
460b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
461416022c9SBarry Smith {
462480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
463416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
464bfeeae90SHong Zhang   int               ierr,i,j,m = A->m,color;
46536db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
466b0a32e0cSBarry Smith   PetscViewer       viewer;
467f3ef73ceSBarry Smith   PetscViewerFormat format;
468cddf8d76SBarry Smith 
4693a40ed3dSBarry Smith   PetscFunctionBegin;
470480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
471b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
47219bcc07fSBarry Smith 
473b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
474416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4750513a670SBarry Smith 
476fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
4770513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
478b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
479416022c9SBarry Smith     for (i=0; i<m; i++) {
480cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
481bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
482bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
483aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
48436db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
485cddf8d76SBarry Smith #else
486cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
487cddf8d76SBarry Smith #endif
488b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
489cddf8d76SBarry Smith       }
490cddf8d76SBarry Smith     }
491b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
492cddf8d76SBarry Smith     for (i=0; i<m; i++) {
493cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
494bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
495bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
496cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
497b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
498cddf8d76SBarry Smith       }
499cddf8d76SBarry Smith     }
500b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
501cddf8d76SBarry Smith     for (i=0; i<m; i++) {
502cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
503bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
504bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
505aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
50636db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
507cddf8d76SBarry Smith #else
508cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
509cddf8d76SBarry Smith #endif
510b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
511416022c9SBarry Smith       }
512416022c9SBarry Smith     }
5130513a670SBarry Smith   } else {
5140513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5150513a670SBarry Smith     /* first determine max of all nonzero values */
5160513a670SBarry Smith     int    nz = a->nz,count;
517b0a32e0cSBarry Smith     PetscDraw   popup;
51836db0b34SBarry Smith     PetscReal scale;
5190513a670SBarry Smith 
5200513a670SBarry Smith     for (i=0; i<nz; i++) {
5210513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5220513a670SBarry Smith     }
523b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
524b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
525b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5260513a670SBarry Smith     count = 0;
5270513a670SBarry Smith     for (i=0; i<m; i++) {
5280513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
529bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
530bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
531b0a32e0cSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count]));
532b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5330513a670SBarry Smith         count++;
5340513a670SBarry Smith       }
5350513a670SBarry Smith     }
5360513a670SBarry Smith   }
537480ef9eaSBarry Smith   PetscFunctionReturn(0);
538480ef9eaSBarry Smith }
539cddf8d76SBarry Smith 
5404a2ae208SSatish Balay #undef __FUNCT__
5414a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
542b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
543480ef9eaSBarry Smith {
544480ef9eaSBarry Smith   int        ierr;
545b0a32e0cSBarry Smith   PetscDraw  draw;
54636db0b34SBarry Smith   PetscReal  xr,yr,xl,yl,h,w;
547480ef9eaSBarry Smith   PetscTruth isnull;
548480ef9eaSBarry Smith 
549480ef9eaSBarry Smith   PetscFunctionBegin;
550b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
551b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
552480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
553480ef9eaSBarry Smith 
554480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
555273d9f13SBarry Smith   xr  = A->n; yr = A->m; h = yr/10.0; w = xr/10.0;
556480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
557b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
558b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
559480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5603a40ed3dSBarry Smith   PetscFunctionReturn(0);
561416022c9SBarry Smith }
562416022c9SBarry Smith 
5634a2ae208SSatish Balay #undef __FUNCT__
5644a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
565b0a32e0cSBarry Smith int MatView_SeqAIJ(Mat A,PetscViewer viewer)
566416022c9SBarry Smith {
567416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
568bcd2baecSBarry Smith   int         ierr;
5696831982aSBarry Smith   PetscTruth  issocket,isascii,isbinary,isdraw;
570416022c9SBarry Smith 
5713a40ed3dSBarry Smith   PetscFunctionBegin;
572b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
573b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
574fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
575fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
5760f5bd95cSBarry Smith   if (issocket) {
577b0a32e0cSBarry Smith     ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr);
5780f5bd95cSBarry Smith   } else if (isascii) {
5793a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5800f5bd95cSBarry Smith   } else if (isbinary) {
5813a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5820f5bd95cSBarry Smith   } else if (isdraw) {
5833a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
5845cd90555SBarry Smith   } else {
58529bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
58617ab2063SBarry Smith   }
5873a40ed3dSBarry Smith   PetscFunctionReturn(0);
58817ab2063SBarry Smith }
58919bcc07fSBarry Smith 
5903a7fca6bSBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat,PetscTruth);
5914a2ae208SSatish Balay #undef __FUNCT__
5924a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
5938f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
59417ab2063SBarry Smith {
595416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
59641c01911SSatish Balay   int          fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax,ierr;
597bfeeae90SHong Zhang   int          m = A->m,*ip,N,*ailen = a->ilen,rmax = 0;
59887828ca2SBarry Smith   PetscScalar  *aa = a->a,*ap;
59917ab2063SBarry Smith 
6003a40ed3dSBarry Smith   PetscFunctionBegin;
6013a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
60217ab2063SBarry Smith 
60343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
60417ab2063SBarry Smith   for (i=1; i<m; i++) {
605416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
60617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
60794a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
60817ab2063SBarry Smith     if (fshift) {
609bfeeae90SHong Zhang       ip = aj + ai[i] ;
610bfeeae90SHong Zhang       ap = aa + ai[i] ;
61117ab2063SBarry Smith       N  = ailen[i];
61217ab2063SBarry Smith       for (j=0; j<N; j++) {
61317ab2063SBarry Smith         ip[j-fshift] = ip[j];
61417ab2063SBarry Smith         ap[j-fshift] = ap[j];
61517ab2063SBarry Smith       }
61617ab2063SBarry Smith     }
61717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
61817ab2063SBarry Smith   }
61917ab2063SBarry Smith   if (m) {
62017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
62117ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
62217ab2063SBarry Smith   }
62317ab2063SBarry Smith   /* reset ilen and imax for each row */
62417ab2063SBarry Smith   for (i=0; i<m; i++) {
62517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
62617ab2063SBarry Smith   }
627bfeeae90SHong Zhang   a->nz = ai[m];
62817ab2063SBarry Smith 
62917ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
630416022c9SBarry Smith   if (fshift && a->diag) {
631606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
632b0a32e0cSBarry Smith     PetscLogObjectMemory(A,-(m+1)*sizeof(int));
633416022c9SBarry Smith     a->diag = 0;
63417ab2063SBarry Smith   }
635b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded,%d used\n",m,A->n,fshift,a->nz);
636b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %d\n",a->reallocs);
637b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax);
638dd5f02e7SSatish Balay   a->reallocs          = 0;
6394e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
64036db0b34SBarry Smith   a->rmax              = rmax;
6414e220ebcSLois Curfman McInnes 
64276dd722bSSatish Balay   /* check out for identical nodes. If found, use inode functions */
6433a7fca6bSBarry Smith   ierr = Mat_AIJ_CheckInode(A,(PetscTruth)(!fshift));CHKERRQ(ierr);
644cfef3cccSHong Zhang 
6453a40ed3dSBarry Smith   PetscFunctionReturn(0);
64617ab2063SBarry Smith }
64717ab2063SBarry Smith 
6484a2ae208SSatish Balay #undef __FUNCT__
6494a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
6508f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A)
65117ab2063SBarry Smith {
652416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
653549d3d68SSatish Balay   int        ierr;
6543a40ed3dSBarry Smith 
6553a40ed3dSBarry Smith   PetscFunctionBegin;
656bfeeae90SHong Zhang   ierr = PetscMemzero(a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
6573a40ed3dSBarry Smith   PetscFunctionReturn(0);
65817ab2063SBarry Smith }
659416022c9SBarry Smith 
6604a2ae208SSatish Balay #undef __FUNCT__
6614a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
662e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A)
66317ab2063SBarry Smith {
664416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
66582bf6240SBarry Smith   int        ierr;
666d5d45c9bSBarry Smith 
6673a40ed3dSBarry Smith   PetscFunctionBegin;
668aa482453SBarry Smith #if defined(PETSC_USE_LOG)
669b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",A->m,A->n,a->nz);
67017ab2063SBarry Smith #endif
67136db0b34SBarry Smith   if (a->freedata) {
672606d414cSSatish Balay     ierr = PetscFree(a->a);CHKERRQ(ierr);
673606d414cSSatish Balay     if (!a->singlemalloc) {
674606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
675606d414cSSatish Balay       ierr = PetscFree(a->j);CHKERRQ(ierr);
676606d414cSSatish Balay     }
67736db0b34SBarry Smith   }
678c38d4ed2SBarry Smith   if (a->row) {
679c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
680c38d4ed2SBarry Smith   }
681c38d4ed2SBarry Smith   if (a->col) {
682c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
683c38d4ed2SBarry Smith   }
684606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
685606d414cSSatish Balay   if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);}
686606d414cSSatish Balay   if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);}
687273d9f13SBarry Smith   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
688606d414cSSatish Balay   if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
689606d414cSSatish Balay   if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
69082bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
691606d414cSSatish Balay   if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);}
692cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
693a30b2313SHong Zhang   if (a->xtoy) {ierr = PetscFree(a->xtoy);CHKERRQ(ierr);}
694a30b2313SHong Zhang 
695606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
6963a40ed3dSBarry Smith   PetscFunctionReturn(0);
69717ab2063SBarry Smith }
69817ab2063SBarry Smith 
6994a2ae208SSatish Balay #undef __FUNCT__
7004a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
7018f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
70217ab2063SBarry Smith {
7033a40ed3dSBarry Smith   PetscFunctionBegin;
7043a40ed3dSBarry Smith   PetscFunctionReturn(0);
70517ab2063SBarry Smith }
70617ab2063SBarry Smith 
7074a2ae208SSatish Balay #undef __FUNCT__
7084a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7098f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
71017ab2063SBarry Smith {
711416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7123a40ed3dSBarry Smith 
7133a40ed3dSBarry Smith   PetscFunctionBegin;
714a65d3064SKris Buschelman   switch (op) {
715a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
716a65d3064SKris Buschelman       a->roworiented       = PETSC_TRUE;
717a65d3064SKris Buschelman       break;
718a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
719a65d3064SKris Buschelman       a->keepzeroedrows    = PETSC_TRUE;
720a65d3064SKris Buschelman       break;
721a65d3064SKris Buschelman     case MAT_COLUMN_ORIENTED:
722a65d3064SKris Buschelman       a->roworiented       = PETSC_FALSE;
723a65d3064SKris Buschelman       break;
724a65d3064SKris Buschelman     case MAT_COLUMNS_SORTED:
725a65d3064SKris Buschelman       a->sorted            = PETSC_TRUE;
726a65d3064SKris Buschelman       break;
727a65d3064SKris Buschelman     case MAT_COLUMNS_UNSORTED:
728a65d3064SKris Buschelman       a->sorted            = PETSC_FALSE;
729a65d3064SKris Buschelman       break;
730a65d3064SKris Buschelman     case MAT_NO_NEW_NONZERO_LOCATIONS:
731a65d3064SKris Buschelman       a->nonew             = 1;
732a65d3064SKris Buschelman       break;
733a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
734a65d3064SKris Buschelman       a->nonew             = -1;
735a65d3064SKris Buschelman       break;
736a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
737a65d3064SKris Buschelman       a->nonew             = -2;
738a65d3064SKris Buschelman       break;
739a65d3064SKris Buschelman     case MAT_YES_NEW_NONZERO_LOCATIONS:
740a65d3064SKris Buschelman       a->nonew             = 0;
741a65d3064SKris Buschelman       break;
742a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
743a65d3064SKris Buschelman       a->ignorezeroentries = PETSC_TRUE;
744a65d3064SKris Buschelman       break;
745a65d3064SKris Buschelman     case MAT_USE_INODES:
746a65d3064SKris Buschelman       a->inode.use         = PETSC_TRUE;
747a65d3064SKris Buschelman       break;
748a65d3064SKris Buschelman     case MAT_DO_NOT_USE_INODES:
749a65d3064SKris Buschelman       a->inode.use         = PETSC_FALSE;
750a65d3064SKris Buschelman       break;
751a65d3064SKris Buschelman     case MAT_ROWS_SORTED:
752a65d3064SKris Buschelman     case MAT_ROWS_UNSORTED:
753a65d3064SKris Buschelman     case MAT_YES_NEW_DIAGONALS:
754a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
755a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
756b0a32e0cSBarry Smith       PetscLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
757a65d3064SKris Buschelman       break;
758a65d3064SKris Buschelman     case MAT_NO_NEW_DIAGONALS:
75929bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
760a65d3064SKris Buschelman     case MAT_INODE_LIMIT_1:
761a65d3064SKris Buschelman       a->inode.limit  = 1;
762a65d3064SKris Buschelman       break;
763a65d3064SKris Buschelman     case MAT_INODE_LIMIT_2:
764a65d3064SKris Buschelman       a->inode.limit  = 2;
765a65d3064SKris Buschelman       break;
766a65d3064SKris Buschelman     case MAT_INODE_LIMIT_3:
767a65d3064SKris Buschelman       a->inode.limit  = 3;
768a65d3064SKris Buschelman       break;
769a65d3064SKris Buschelman     case MAT_INODE_LIMIT_4:
770a65d3064SKris Buschelman       a->inode.limit  = 4;
771a65d3064SKris Buschelman       break;
772a65d3064SKris Buschelman     case MAT_INODE_LIMIT_5:
773a65d3064SKris Buschelman       a->inode.limit  = 5;
774a65d3064SKris Buschelman       break;
775a65d3064SKris Buschelman     default:
776a65d3064SKris Buschelman       SETERRQ(PETSC_ERR_SUP,"unknown option");
777a65d3064SKris Buschelman   }
7783a40ed3dSBarry Smith   PetscFunctionReturn(0);
77917ab2063SBarry Smith }
78017ab2063SBarry Smith 
7814a2ae208SSatish Balay #undef __FUNCT__
7824a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
7838f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
78417ab2063SBarry Smith {
785416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
786bfeeae90SHong Zhang   int          i,j,n,ierr;
78787828ca2SBarry Smith   PetscScalar  *x,zero = 0.0;
78817ab2063SBarry Smith 
7893a40ed3dSBarry Smith   PetscFunctionBegin;
7903a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
791ed480e8bSBarry Smith   ierr = VecGetArrayFast(v,&x);CHKERRQ(ierr);
79236db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
793273d9f13SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
794273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
795bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
796bfeeae90SHong Zhang       if (a->j[j] == i) {
797416022c9SBarry Smith         x[i] = a->a[j];
79817ab2063SBarry Smith         break;
79917ab2063SBarry Smith       }
80017ab2063SBarry Smith     }
80117ab2063SBarry Smith   }
802ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(v,&x);CHKERRQ(ierr);
8033a40ed3dSBarry Smith   PetscFunctionReturn(0);
80417ab2063SBarry Smith }
80517ab2063SBarry Smith 
806d5d45c9bSBarry Smith 
8074a2ae208SSatish Balay #undef __FUNCT__
8084a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
8097c922b88SBarry Smith int MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
81017ab2063SBarry Smith {
811416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
8125c897100SBarry Smith   PetscScalar  *x,*y;
813bfeeae90SHong Zhang   int          ierr,m = A->m;
8145c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
8155c897100SBarry Smith   PetscScalar  *v,alpha;
8165c897100SBarry Smith   int          n,i,*idx;
8175c897100SBarry Smith #endif
81817ab2063SBarry Smith 
8193a40ed3dSBarry Smith   PetscFunctionBegin;
8202e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
821ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
822ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
8235c897100SBarry Smith 
8245c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
825bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8265c897100SBarry Smith #else
82717ab2063SBarry Smith   for (i=0; i<m; i++) {
828bfeeae90SHong Zhang     idx   = a->j + a->i[i] ;
829bfeeae90SHong Zhang     v     = a->a + a->i[i] ;
830416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
83117ab2063SBarry Smith     alpha = x[i];
83217ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
83317ab2063SBarry Smith   }
8345c897100SBarry Smith #endif
835b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
836ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
837ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
8383a40ed3dSBarry Smith   PetscFunctionReturn(0);
83917ab2063SBarry Smith }
84017ab2063SBarry Smith 
8414a2ae208SSatish Balay #undef __FUNCT__
8425c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
8435c897100SBarry Smith int MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8445c897100SBarry Smith {
8458d5b0100SBarry Smith   PetscScalar  zero = 0.0;
8465c897100SBarry Smith   int          ierr;
8475c897100SBarry Smith 
8485c897100SBarry Smith   PetscFunctionBegin;
8495c897100SBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
8505c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
8515c897100SBarry Smith   PetscFunctionReturn(0);
8525c897100SBarry Smith }
8535c897100SBarry Smith 
8545c897100SBarry Smith 
8555c897100SBarry Smith #undef __FUNCT__
8564a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
85744cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
85817ab2063SBarry Smith {
859416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
860362ced78SSatish Balay   PetscScalar  *x,*y,*v;
861bfeeae90SHong Zhang   int          ierr,m = A->m,*idx,*ii;
862aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
863e36a17ebSSatish Balay   int          n,i,jrow,j;
864362ced78SSatish Balay   PetscScalar  sum;
865e36a17ebSSatish Balay #endif
86617ab2063SBarry Smith 
867b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
868fee21e36SBarry Smith #pragma disjoint(*x,*y,*v)
869fee21e36SBarry Smith #endif
870fee21e36SBarry Smith 
8713a40ed3dSBarry Smith   PetscFunctionBegin;
872ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
873ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
874416022c9SBarry Smith   idx  = a->j;
875d64ed03dSBarry Smith   v    = a->a;
876416022c9SBarry Smith   ii   = a->i;
877aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
878bfeeae90SHong Zhang   fortranmultaij_(&m,x,ii,idx,v,y);
8798d195f9aSBarry Smith #else
88017ab2063SBarry Smith   for (i=0; i<m; i++) {
8819ea0dfa2SSatish Balay     jrow = ii[i];
8829ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
88317ab2063SBarry Smith     sum  = 0.0;
8849ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
8859ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8869ea0dfa2SSatish Balay      }
88717ab2063SBarry Smith     y[i] = sum;
88817ab2063SBarry Smith   }
8898d195f9aSBarry Smith #endif
890b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz - m);
891ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
892ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
8933a40ed3dSBarry Smith   PetscFunctionReturn(0);
89417ab2063SBarry Smith }
89517ab2063SBarry Smith 
8964a2ae208SSatish Balay #undef __FUNCT__
8974a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
89844cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
89917ab2063SBarry Smith {
900416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
901362ced78SSatish Balay   PetscScalar  *x,*y,*z,*v;
902bfeeae90SHong Zhang   int          ierr,m = A->m,*idx,*ii;
903aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
904e36a17ebSSatish Balay   int          n,i,jrow,j;
905362ced78SSatish Balay PetscScalar    sum;
906e36a17ebSSatish Balay #endif
9079ea0dfa2SSatish Balay 
9083a40ed3dSBarry Smith   PetscFunctionBegin;
909ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
910ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
9112e8a6d31SBarry Smith   if (zz != yy) {
912ed480e8bSBarry Smith     ierr = VecGetArrayFast(zz,&z);CHKERRQ(ierr);
9132e8a6d31SBarry Smith   } else {
9142e8a6d31SBarry Smith     z = y;
9152e8a6d31SBarry Smith   }
916bfeeae90SHong Zhang 
917cddf8d76SBarry Smith   idx  = a->j;
918d64ed03dSBarry Smith   v    = a->a;
919cddf8d76SBarry Smith   ii   = a->i;
920aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
921bfeeae90SHong Zhang   fortranmultaddaij_(&m,x,ii,idx,v,y,z);
92202ab625aSSatish Balay #else
92317ab2063SBarry Smith   for (i=0; i<m; i++) {
9249ea0dfa2SSatish Balay     jrow = ii[i];
9259ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
92617ab2063SBarry Smith     sum  = y[i];
9279ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
9289ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9299ea0dfa2SSatish Balay      }
93017ab2063SBarry Smith     z[i] = sum;
93117ab2063SBarry Smith   }
93202ab625aSSatish Balay #endif
933b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
934ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
935ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
9362e8a6d31SBarry Smith   if (zz != yy) {
937ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(zz,&z);CHKERRQ(ierr);
9382e8a6d31SBarry Smith   }
9393a40ed3dSBarry Smith   PetscFunctionReturn(0);
94017ab2063SBarry Smith }
94117ab2063SBarry Smith 
94217ab2063SBarry Smith /*
94317ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
94417ab2063SBarry Smith */
9454a2ae208SSatish Balay #undef __FUNCT__
9464a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
947f1e2ffcdSBarry Smith int MatMarkDiagonal_SeqAIJ(Mat A)
94817ab2063SBarry Smith {
949416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
950bfeeae90SHong Zhang   int        i,j,*diag,m = A->m,ierr;
95117ab2063SBarry Smith 
9523a40ed3dSBarry Smith   PetscFunctionBegin;
953f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
954f1e2ffcdSBarry Smith 
955b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&diag);CHKERRQ(ierr);
956b0a32e0cSBarry Smith   PetscLogObjectMemory(A,(m+1)*sizeof(int));
957273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
95835b0346bSBarry Smith     diag[i] = a->i[i+1];
959bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
960bfeeae90SHong Zhang       if (a->j[j] == i) {
961bfeeae90SHong Zhang         diag[i] = j;
96217ab2063SBarry Smith         break;
96317ab2063SBarry Smith       }
96417ab2063SBarry Smith     }
96517ab2063SBarry Smith   }
966416022c9SBarry Smith   a->diag = diag;
9673a40ed3dSBarry Smith   PetscFunctionReturn(0);
96817ab2063SBarry Smith }
96917ab2063SBarry Smith 
970be5855fcSBarry Smith /*
971be5855fcSBarry Smith      Checks for missing diagonals
972be5855fcSBarry Smith */
9734a2ae208SSatish Balay #undef __FUNCT__
9744a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
975f1e2ffcdSBarry Smith int MatMissingDiagonal_SeqAIJ(Mat A)
976be5855fcSBarry Smith {
977be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
978bfeeae90SHong Zhang   int        *diag,*jj = a->j,i,ierr;
979be5855fcSBarry Smith 
980be5855fcSBarry Smith   PetscFunctionBegin;
981f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
982f1e2ffcdSBarry Smith   diag = a->diag;
983273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
984bfeeae90SHong Zhang     if (jj[diag[i]] != i) {
98529bbc08cSBarry Smith       SETERRQ1(1,"Matrix is missing diagonal number %d",i);
986be5855fcSBarry Smith     }
987be5855fcSBarry Smith   }
988be5855fcSBarry Smith   PetscFunctionReturn(0);
989be5855fcSBarry Smith }
990be5855fcSBarry Smith 
9914a2ae208SSatish Balay #undef __FUNCT__
9924a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
993c14dc6b6SHong Zhang int MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
99417ab2063SBarry Smith {
995416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
996beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
997beeb8507SBarry Smith   const PetscScalar  *v = a->a, *b, *bs,*xb, *ts;
998beeb8507SBarry Smith   int                ierr,n = A->n,m = A->m,i;
999beeb8507SBarry Smith   const int          *idx,*diag;
100017ab2063SBarry Smith 
10013a40ed3dSBarry Smith   PetscFunctionBegin;
1002b965ef7fSBarry Smith   its = its*lits;
100391723122SBarry Smith   if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
100491723122SBarry Smith 
1005ed480e8bSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1006ed480e8bSBarry Smith   diag = a->diag;
1007ed480e8bSBarry Smith   if (!a->idiag) {
1008ed480e8bSBarry Smith     ierr     = PetscMalloc(3*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
1009ed480e8bSBarry Smith     a->ssor  = a->idiag + m;
1010ed480e8bSBarry Smith     mdiag    = a->ssor + m;
1011ed480e8bSBarry Smith 
1012ed480e8bSBarry Smith     v        = a->a;
1013ed480e8bSBarry Smith 
1014ed480e8bSBarry Smith     /* this is wrong when fshift omega changes each iteration */
1015ed480e8bSBarry Smith     if (omega == 1.0 && fshift == 0.0) {
1016ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1017ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1018ed480e8bSBarry Smith         a->idiag[i] = 1.0/v[diag[i]];
1019ed480e8bSBarry Smith       }
1020beeb8507SBarry Smith       PetscLogFlops(m);
1021ed480e8bSBarry Smith     } else {
1022ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1023ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1024beeb8507SBarry Smith         a->idiag[i] = omega/(fshift + v[diag[i]]);
1025ed480e8bSBarry Smith       }
1026beeb8507SBarry Smith       PetscLogFlops(2*m);
1027beeb8507SBarry Smith     }
1028ed480e8bSBarry Smith   }
1029ed480e8bSBarry Smith   t     = a->ssor;
1030ed480e8bSBarry Smith   idiag = a->idiag;
1031ed480e8bSBarry Smith   mdiag = a->idiag + 2*m;
1032ed480e8bSBarry Smith 
1033ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
1034fb2e594dSBarry Smith   if (xx != bb) {
1035beeb8507SBarry Smith     ierr = VecGetArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1036fb2e594dSBarry Smith   } else {
1037fb2e594dSBarry Smith     b = x;
1038fb2e594dSBarry Smith   }
1039fb2e594dSBarry Smith 
1040ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1041ed480e8bSBarry Smith   xs   = x;
104217ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
104317ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1044ed480e8bSBarry Smith     bs = b;
104517ab2063SBarry Smith     for (i=0; i<m; i++) {
1046ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1047416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1048ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1049ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
105017ab2063SBarry Smith         sum  = b[i]*d/omega;
105117ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
105217ab2063SBarry Smith         x[i] = sum;
105317ab2063SBarry Smith     }
1054ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1055beeb8507SBarry Smith     if (bb != xx) {ierr = VecRestoreArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1056ed480e8bSBarry Smith     PetscLogFlops(a->nz);
10573a40ed3dSBarry Smith     PetscFunctionReturn(0);
105817ab2063SBarry Smith   }
1059c783ea89SBarry Smith 
1060ed480e8bSBarry Smith 
1061fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1062fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1063fc3d8934SBarry Smith 
1064fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1065fc3d8934SBarry Smith 
1066fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1067fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
106848af12d7SBarry Smith     is the relaxation factor.
1069fc3d8934SBarry Smith     */
1070fc3d8934SBarry Smith 
107148af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
107229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
10733a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
107417ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
107517ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
107617ab2063SBarry Smith 
107717ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
107817ab2063SBarry Smith 
107917ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
108017ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
108117ab2063SBarry Smith     is the relaxation factor.
108217ab2063SBarry Smith     */
108317ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
108417ab2063SBarry Smith 
108517ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
108617ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1087416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1088ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1089ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
109017ab2063SBarry Smith       sum  = b[i];
109117ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1092ed480e8bSBarry Smith       x[i] = sum*idiag[i];
109317ab2063SBarry Smith     }
109417ab2063SBarry Smith 
109517ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1096416022c9SBarry Smith     v = a->a;
1097ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
109817ab2063SBarry Smith 
109917ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1100ed480e8bSBarry Smith     ts = t;
1101416022c9SBarry Smith     diag = a->diag;
110217ab2063SBarry Smith     for (i=0; i<m; i++) {
1103416022c9SBarry Smith       n    = diag[i] - a->i[i];
1104ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1105ed480e8bSBarry Smith       v    = a->a + a->i[i];
110617ab2063SBarry Smith       sum  = t[i];
110717ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1108ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1109733d66baSBarry Smith       /*  x = x + t */
1110733d66baSBarry Smith       x[i] += t[i];
111117ab2063SBarry Smith     }
111217ab2063SBarry Smith 
1113b0a32e0cSBarry Smith     PetscLogFlops(6*m-1 + 2*a->nz);
1114ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1115beeb8507SBarry Smith     if (bb != xx) {ierr = VecRestoreArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
11163a40ed3dSBarry Smith     PetscFunctionReturn(0);
111717ab2063SBarry Smith   }
111817ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
111917ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
112077d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
11210c559628SSatish Balay       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(int *)diag,idiag,a->a,(void*)b);
112277d8c4bbSBarry Smith #else
112317ab2063SBarry Smith       for (i=0; i<m; i++) {
1124416022c9SBarry Smith         n    = diag[i] - a->i[i];
1125ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1126ed480e8bSBarry Smith         v    = a->a + a->i[i];
112717ab2063SBarry Smith         sum  = b[i];
112817ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1129ed480e8bSBarry Smith         x[i] = sum*idiag[i];
113017ab2063SBarry Smith       }
113177d8c4bbSBarry Smith #endif
113217ab2063SBarry Smith       xb = x;
1133ed480e8bSBarry Smith       PetscLogFlops(a->nz);
11343a40ed3dSBarry Smith     } else xb = b;
113517ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
113617ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
113717ab2063SBarry Smith       for (i=0; i<m; i++) {
1138ed480e8bSBarry Smith         x[i] *= mdiag[i];
113917ab2063SBarry Smith       }
1140b0a32e0cSBarry Smith       PetscLogFlops(m);
114117ab2063SBarry Smith     }
114217ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
114377d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
11440c559628SSatish Balay       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(int*)diag,idiag,a->a,(void*)xb);
114577d8c4bbSBarry Smith #else
114617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1147416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1148ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1149ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
115017ab2063SBarry Smith         sum  = xb[i];
115117ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1152ed480e8bSBarry Smith         x[i] = sum*idiag[i];
115317ab2063SBarry Smith       }
115477d8c4bbSBarry Smith #endif
1155ed480e8bSBarry Smith       PetscLogFlops(a->nz);
115617ab2063SBarry Smith     }
115717ab2063SBarry Smith     its--;
115817ab2063SBarry Smith   }
115917ab2063SBarry Smith   while (its--) {
116017ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
116177d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
11620c559628SSatish Balay       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(int*)diag,a->a,(void*)b);
116377d8c4bbSBarry Smith #else
116417ab2063SBarry Smith       for (i=0; i<m; i++) {
1165ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1166416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1167ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1168ed480e8bSBarry Smith         v    = a->a + a->i[i];
116917ab2063SBarry Smith         sum  = b[i];
117017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1171ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
117217ab2063SBarry Smith       }
117377d8c4bbSBarry Smith #endif
1174ed480e8bSBarry Smith       PetscLogFlops(a->nz);
117517ab2063SBarry Smith     }
117617ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
117777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
11780c559628SSatish Balay       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(int*)diag,a->a,(void*)b);
117977d8c4bbSBarry Smith #else
118017ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1181ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1182416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1183ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1184ed480e8bSBarry Smith         v    = a->a + a->i[i];
118517ab2063SBarry Smith         sum  = b[i];
118617ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1187ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
118817ab2063SBarry Smith       }
118977d8c4bbSBarry Smith #endif
1190ed480e8bSBarry Smith       PetscLogFlops(a->nz);
119117ab2063SBarry Smith     }
119217ab2063SBarry Smith   }
1193ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1194beeb8507SBarry Smith   if (bb != xx) {ierr = VecRestoreArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
11953a40ed3dSBarry Smith   PetscFunctionReturn(0);
119617ab2063SBarry Smith }
119717ab2063SBarry Smith 
11984a2ae208SSatish Balay #undef __FUNCT__
11994a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
12008f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
120117ab2063SBarry Smith {
1202416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12034e220ebcSLois Curfman McInnes 
12043a40ed3dSBarry Smith   PetscFunctionBegin;
1205273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1206273d9f13SBarry Smith   info->columns_global = (double)A->n;
1207273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1208273d9f13SBarry Smith   info->columns_local  = (double)A->n;
12094e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12104e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12114e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12124e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12134e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12144e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12154e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12164e220ebcSLois Curfman McInnes   if (A->factor) {
12174e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12184e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12194e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12204e220ebcSLois Curfman McInnes   } else {
12214e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12224e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12234e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12244e220ebcSLois Curfman McInnes   }
12253a40ed3dSBarry Smith   PetscFunctionReturn(0);
122617ab2063SBarry Smith }
122717ab2063SBarry Smith 
1228b380c88cSHong Zhang EXTERN int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,MatFactorInfo*,Mat*);
1229ca44d042SBarry Smith EXTERN int MatLUFactorNumeric_SeqAIJ(Mat,Mat*);
1230b380c88cSHong Zhang EXTERN int MatLUFactor_SeqAIJ(Mat,IS,IS,MatFactorInfo*);
1231ca44d042SBarry Smith EXTERN int MatSolve_SeqAIJ(Mat,Vec,Vec);
1232ca44d042SBarry Smith EXTERN int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
1233ca44d042SBarry Smith EXTERN int MatSolveTranspose_SeqAIJ(Mat,Vec,Vec);
1234ca44d042SBarry Smith EXTERN int MatSolveTransposeAdd_SeqAIJ(Mat,Vec,Vec,Vec);
123517ab2063SBarry Smith 
12364a2ae208SSatish Balay #undef __FUNCT__
12374a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1238268466fbSBarry Smith int MatZeroRows_SeqAIJ(Mat A,IS is,const PetscScalar *diag)
123917ab2063SBarry Smith {
1240416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1241bfeeae90SHong Zhang   int         i,ierr,N,*rows,m = A->m - 1;
124217ab2063SBarry Smith 
12433a40ed3dSBarry Smith   PetscFunctionBegin;
1244b9b97703SBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
124517ab2063SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
1246f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1247f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
124829bbc08cSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
1249bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1250f1e2ffcdSBarry Smith     }
1251f1e2ffcdSBarry Smith     if (diag) {
1252f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1253f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1254f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1255f1e2ffcdSBarry Smith         a->a[a->diag[rows[i]]] = *diag;
1256f1e2ffcdSBarry Smith       }
1257f1e2ffcdSBarry Smith     }
1258f1e2ffcdSBarry Smith   } else {
125917ab2063SBarry Smith     if (diag) {
126017ab2063SBarry Smith       for (i=0; i<N; i++) {
126129bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
12627ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1263416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1264bfeeae90SHong Zhang           a->a[a->i[rows[i]]] = *diag;
1265bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
12667ae801bdSBarry Smith         } else { /* in case row was completely empty */
1267d64ed03dSBarry Smith           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
126817ab2063SBarry Smith         }
126917ab2063SBarry Smith       }
12703a40ed3dSBarry Smith     } else {
127117ab2063SBarry Smith       for (i=0; i<N; i++) {
127229bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
1273416022c9SBarry Smith         a->ilen[rows[i]] = 0;
127417ab2063SBarry Smith       }
127517ab2063SBarry Smith     }
1276f1e2ffcdSBarry Smith   }
12777ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
127843a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12793a40ed3dSBarry Smith   PetscFunctionReturn(0);
128017ab2063SBarry Smith }
128117ab2063SBarry Smith 
12824a2ae208SSatish Balay #undef __FUNCT__
12834a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
128487828ca2SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v)
128517ab2063SBarry Smith {
1286416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1287b3fb0a6cSMatthew Knepley   int        *itmp;
128817ab2063SBarry Smith 
12893a40ed3dSBarry Smith   PetscFunctionBegin;
1290273d9f13SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %d out of range",row);
129117ab2063SBarry Smith 
1292416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1293bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
129417ab2063SBarry Smith   if (idx) {
1295bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1296bfeeae90SHong Zhang     if (*nz) {
12974e093b46SBarry Smith       *idx = itmp;
129817ab2063SBarry Smith     }
129917ab2063SBarry Smith     else *idx = 0;
130017ab2063SBarry Smith   }
13013a40ed3dSBarry Smith   PetscFunctionReturn(0);
130217ab2063SBarry Smith }
130317ab2063SBarry Smith 
1304bfeeae90SHong Zhang /* remove this function? */
13054a2ae208SSatish Balay #undef __FUNCT__
13064a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
130787828ca2SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v)
130817ab2063SBarry Smith {
1309bfeeae90SHong Zhang   /* Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1310bfeeae90SHong Zhang      int ierr; */
13113a40ed3dSBarry Smith 
13123a40ed3dSBarry Smith   PetscFunctionBegin;
1313bfeeae90SHong Zhang   /* if (idx) {if (*idx && a->indexshift) {ierr = PetscFree(*idx);CHKERRQ(ierr);}} */
13143a40ed3dSBarry Smith   PetscFunctionReturn(0);
131517ab2063SBarry Smith }
131617ab2063SBarry Smith 
13174a2ae208SSatish Balay #undef __FUNCT__
13184a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1319064f8208SBarry Smith int MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
132017ab2063SBarry Smith {
1321416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
132287828ca2SBarry Smith   PetscScalar  *v = a->a;
132336db0b34SBarry Smith   PetscReal    sum = 0.0;
1324bfeeae90SHong Zhang   int          i,j,ierr;
132517ab2063SBarry Smith 
13263a40ed3dSBarry Smith   PetscFunctionBegin;
132717ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1328416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1329aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
133036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
133117ab2063SBarry Smith #else
133217ab2063SBarry Smith       sum += (*v)*(*v); v++;
133317ab2063SBarry Smith #endif
133417ab2063SBarry Smith     }
1335064f8208SBarry Smith     *nrm = sqrt(sum);
13363a40ed3dSBarry Smith   } else if (type == NORM_1) {
133736db0b34SBarry Smith     PetscReal *tmp;
1338416022c9SBarry Smith     int    *jj = a->j;
1339b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1340273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
1341064f8208SBarry Smith     *nrm = 0.0;
1342416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1343bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
134417ab2063SBarry Smith     }
1345273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
1346064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
134717ab2063SBarry Smith     }
1348606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13493a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1350064f8208SBarry Smith     *nrm = 0.0;
1351273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1352bfeeae90SHong Zhang       v = a->a + a->i[j];
135317ab2063SBarry Smith       sum = 0.0;
1354416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1355cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
135617ab2063SBarry Smith       }
1357064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
135817ab2063SBarry Smith     }
13593a40ed3dSBarry Smith   } else {
136029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
136117ab2063SBarry Smith   }
13623a40ed3dSBarry Smith   PetscFunctionReturn(0);
136317ab2063SBarry Smith }
136417ab2063SBarry Smith 
13654a2ae208SSatish Balay #undef __FUNCT__
13664a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
13678f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
136817ab2063SBarry Smith {
1369416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
1370416022c9SBarry Smith   Mat          C;
1371273d9f13SBarry Smith   int          i,ierr,*aj = a->j,*ai = a->i,m = A->m,len,*col;
137287828ca2SBarry Smith   PetscScalar  *array = a->a;
137317ab2063SBarry Smith 
13743a40ed3dSBarry Smith   PetscFunctionBegin;
1375273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1376b0a32e0cSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(int),&col);CHKERRQ(ierr);
1377273d9f13SBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(int));CHKERRQ(ierr);
1378bfeeae90SHong Zhang 
1379bfeeae90SHong Zhang   for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
1380273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,A->n,m,0,col,&C);CHKERRQ(ierr);
1381606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
138217ab2063SBarry Smith   for (i=0; i<m; i++) {
138317ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1384416022c9SBarry Smith     ierr   = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1385b9b97703SBarry Smith     array += len;
1386b9b97703SBarry Smith     aj    += len;
138717ab2063SBarry Smith   }
138817ab2063SBarry Smith 
13896d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13906d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
139117ab2063SBarry Smith 
1392f1e2ffcdSBarry Smith   if (B) {
1393416022c9SBarry Smith     *B = C;
139417ab2063SBarry Smith   } else {
1395273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
139617ab2063SBarry Smith   }
13973a40ed3dSBarry Smith   PetscFunctionReturn(0);
139817ab2063SBarry Smith }
139917ab2063SBarry Smith 
1400cd0d46ebSvictorle EXTERN_C_BEGIN
1401cd0d46ebSvictorle #undef __FUNCT__
1402cd0d46ebSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1403cd0d46ebSvictorle int MatIsSymmetric_SeqAIJ(Mat A,Mat B,PetscTruth *f)
1404cd0d46ebSvictorle {
1405cd0d46ebSvictorle   Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
1406cd0d46ebSvictorle   int *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb;
1407cd0d46ebSvictorle   int ma,na,mb,nb, i,ierr;
1408cd0d46ebSvictorle 
1409cd0d46ebSvictorle   PetscFunctionBegin;
1410cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1411cd0d46ebSvictorle 
1412cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na); CHKERRQ(ierr);
1413cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb); CHKERRQ(ierr);
1414cd0d46ebSvictorle   if (ma!=nb || na!=mb)
1415cd0d46ebSvictorle     SETERRQ(1,"Incompatible A/B sizes for symmetry test");
1416cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1417cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1418cd0d46ebSvictorle   va = aij->a; vb = bij->a;
1419cd0d46ebSvictorle   ierr = PetscMalloc(ma*sizeof(int),&aptr); CHKERRQ(ierr);
1420cd0d46ebSvictorle   ierr = PetscMalloc(mb*sizeof(int),&bptr); CHKERRQ(ierr);
1421cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1422cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1423cd0d46ebSvictorle 
1424cd0d46ebSvictorle   *f = PETSC_TRUE;
1425cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1426cd0d46ebSvictorle     /*printf("row %d spans %d--%d; we start @ %d\n",
1427cd0d46ebSvictorle       i,idx[ii[i]],idx[ii[i+1]-1],idx[aptr[i]]);*/
1428cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
1429cd0d46ebSvictorle       int idc,idr; PetscScalar vc,vr;
1430cd0d46ebSvictorle       /* column/row index/value */
1431cd0d46ebSvictorle       idc = adx[aptr[i]]; idr = bdx[bptr[idc]];
1432cd0d46ebSvictorle       vc = va[aptr[i]]; vr = vb[bptr[idc]];
1433cd0d46ebSvictorle       /*printf("comparing %d: (%d,%d)=%e to (%d,%d)=%e\n",
1434cd0d46ebSvictorle 	aptr[i],i,idc,vc,idc,idr,vr);*/
1435cd0d46ebSvictorle       if (i!=idr || vc!=vr) {
1436cd0d46ebSvictorle 	*f = PETSC_FALSE; goto done;
1437cd0d46ebSvictorle       } else {
14383aeef889SHong Zhang 	aptr[i]++; if (B || i!=idc) bptr[idc]++;
1439cd0d46ebSvictorle       }
1440cd0d46ebSvictorle     }
1441cd0d46ebSvictorle   }
1442cd0d46ebSvictorle  done:
1443cd0d46ebSvictorle   ierr = PetscFree(aptr); CHKERRQ(ierr);
14443aeef889SHong Zhang   if (B) {
14453aeef889SHong Zhang     ierr = PetscFree(bptr); CHKERRQ(ierr);
14463aeef889SHong Zhang   }
1447cd0d46ebSvictorle 
1448cd0d46ebSvictorle   PetscFunctionReturn(0);
1449cd0d46ebSvictorle }
1450cd0d46ebSvictorle EXTERN_C_END
1451cd0d46ebSvictorle 
14524a2ae208SSatish Balay #undef __FUNCT__
14534a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
14548f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
145517ab2063SBarry Smith {
1456416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
145787828ca2SBarry Smith   PetscScalar  *l,*r,x,*v;
1458bfeeae90SHong Zhang   int          ierr,i,j,m = A->m,n = A->n,M,nz = a->nz,*jj;
145917ab2063SBarry Smith 
14603a40ed3dSBarry Smith   PetscFunctionBegin;
146117ab2063SBarry Smith   if (ll) {
14623ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14633ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1464e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1465273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
1466ed480e8bSBarry Smith     ierr = VecGetArrayFast(ll,&l);CHKERRQ(ierr);
1467416022c9SBarry Smith     v = a->a;
146817ab2063SBarry Smith     for (i=0; i<m; i++) {
146917ab2063SBarry Smith       x = l[i];
1470416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
147117ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
147217ab2063SBarry Smith     }
1473ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(ll,&l);CHKERRQ(ierr);
1474b0a32e0cSBarry Smith     PetscLogFlops(nz);
147517ab2063SBarry Smith   }
147617ab2063SBarry Smith   if (rr) {
1477e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1478273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
1479ed480e8bSBarry Smith     ierr = VecGetArrayFast(rr,&r);CHKERRQ(ierr);
1480416022c9SBarry Smith     v = a->a; jj = a->j;
148117ab2063SBarry Smith     for (i=0; i<nz; i++) {
1482bfeeae90SHong Zhang       (*v++) *= r[*jj++];
148317ab2063SBarry Smith     }
1484ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(rr,&r);CHKERRQ(ierr);
1485b0a32e0cSBarry Smith     PetscLogFlops(nz);
148617ab2063SBarry Smith   }
14873a40ed3dSBarry Smith   PetscFunctionReturn(0);
148817ab2063SBarry Smith }
148917ab2063SBarry Smith 
14904a2ae208SSatish Balay #undef __FUNCT__
14914a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
14927b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B)
149317ab2063SBarry Smith {
1494db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data,*c;
1495273d9f13SBarry Smith   int          *smap,i,k,kstart,kend,ierr,oldcols = A->n,*lens;
149636db0b34SBarry Smith   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
1497bfeeae90SHong Zhang   int          *irow,*icol,nrows,ncols;
149899141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
149987828ca2SBarry Smith   PetscScalar  *a_new,*mat_a;
1500416022c9SBarry Smith   Mat          C;
1501fee21e36SBarry Smith   PetscTruth   stride;
150217ab2063SBarry Smith 
15033a40ed3dSBarry Smith   PetscFunctionBegin;
1504d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
150529bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1506d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
150729bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
150899141d43SSatish Balay 
150917ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1510b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1511b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
151217ab2063SBarry Smith 
1513fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1514fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1515fee21e36SBarry Smith   if (stride && step == 1) {
151602834360SBarry Smith     /* special case of contiguous rows */
151731ebf83bSSatish Balay     ierr   = PetscMalloc((2*nrows+1)*sizeof(int),&lens);CHKERRQ(ierr);
151831ebf83bSSatish Balay     starts = lens + nrows;
151902834360SBarry Smith     /* loop over new rows determining lens and starting points */
152002834360SBarry Smith     for (i=0; i<nrows; i++) {
1521bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1522a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
152302834360SBarry Smith       for (k=kstart; k<kend; k++) {
1524bfeeae90SHong Zhang         if (aj[k] >= first) {
152502834360SBarry Smith           starts[i] = k;
152602834360SBarry Smith           break;
152702834360SBarry Smith 	}
152802834360SBarry Smith       }
1529a2744918SBarry Smith       sum = 0;
153002834360SBarry Smith       while (k < kend) {
1531bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1532a2744918SBarry Smith         sum++;
153302834360SBarry Smith       }
1534a2744918SBarry Smith       lens[i] = sum;
153502834360SBarry Smith     }
153602834360SBarry Smith     /* create submatrix */
1537cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
153808480c60SBarry Smith       int n_cols,n_rows;
153908480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
154029bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1541d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
154208480c60SBarry Smith       C = *B;
15433a40ed3dSBarry Smith     } else {
154402834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
154508480c60SBarry Smith     }
1546db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1547db02288aSLois Curfman McInnes 
154802834360SBarry Smith     /* loop over rows inserting into submatrix */
1549db02288aSLois Curfman McInnes     a_new    = c->a;
1550db02288aSLois Curfman McInnes     j_new    = c->j;
1551db02288aSLois Curfman McInnes     i_new    = c->i;
1552bfeeae90SHong Zhang 
155302834360SBarry Smith     for (i=0; i<nrows; i++) {
1554a2744918SBarry Smith       ii    = starts[i];
1555a2744918SBarry Smith       lensi = lens[i];
1556a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1557a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
155802834360SBarry Smith       }
155987828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1560a2744918SBarry Smith       a_new      += lensi;
1561a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1562a2744918SBarry Smith       c->ilen[i]  = lensi;
156302834360SBarry Smith     }
1564606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15653a40ed3dSBarry Smith   } else {
156602834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
1567b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(int),&smap);CHKERRQ(ierr);
1568bfeeae90SHong Zhang 
1569b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(int),&lens);CHKERRQ(ierr);
1570549d3d68SSatish Balay     ierr  = PetscMemzero(smap,oldcols*sizeof(int));CHKERRQ(ierr);
157117ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
157202834360SBarry Smith     /* determine lens of each row */
157302834360SBarry Smith     for (i=0; i<nrows; i++) {
1574bfeeae90SHong Zhang       kstart  = ai[irow[i]];
157502834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
157602834360SBarry Smith       lens[i] = 0;
157702834360SBarry Smith       for (k=kstart; k<kend; k++) {
1578bfeeae90SHong Zhang         if (smap[aj[k]]) {
157902834360SBarry Smith           lens[i]++;
158002834360SBarry Smith         }
158102834360SBarry Smith       }
158202834360SBarry Smith     }
158317ab2063SBarry Smith     /* Create and fill new matrix */
1584a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
15850f5bd95cSBarry Smith       PetscTruth equal;
15860f5bd95cSBarry Smith 
158799141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1588273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1589273d9f13SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(int),&equal);CHKERRQ(ierr);
15900f5bd95cSBarry Smith       if (!equal) {
159129bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
159299141d43SSatish Balay       }
1593273d9f13SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(int));CHKERRQ(ierr);
159408480c60SBarry Smith       C = *B;
15953a40ed3dSBarry Smith     } else {
159602834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
159708480c60SBarry Smith     }
159899141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
159917ab2063SBarry Smith     for (i=0; i<nrows; i++) {
160099141d43SSatish Balay       row    = irow[i];
1601bfeeae90SHong Zhang       kstart = ai[row];
160299141d43SSatish Balay       kend   = kstart + a->ilen[row];
1603bfeeae90SHong Zhang       mat_i  = c->i[i];
160499141d43SSatish Balay       mat_j  = c->j + mat_i;
160599141d43SSatish Balay       mat_a  = c->a + mat_i;
160699141d43SSatish Balay       mat_ilen = c->ilen + i;
160717ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1608bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1609ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
161099141d43SSatish Balay           *mat_a++ = a->a[k];
161199141d43SSatish Balay           (*mat_ilen)++;
161299141d43SSatish Balay 
161317ab2063SBarry Smith         }
161417ab2063SBarry Smith       }
161517ab2063SBarry Smith     }
161602834360SBarry Smith     /* Free work space */
161702834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1618606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1619606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
162002834360SBarry Smith   }
16216d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16226d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
162317ab2063SBarry Smith 
162417ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1625416022c9SBarry Smith   *B = C;
16263a40ed3dSBarry Smith   PetscFunctionReturn(0);
162717ab2063SBarry Smith }
162817ab2063SBarry Smith 
1629a871dcd8SBarry Smith /*
1630a871dcd8SBarry Smith */
16314a2ae208SSatish Balay #undef __FUNCT__
16324a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1633b380c88cSHong Zhang int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1634a871dcd8SBarry Smith {
163563b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
163608480c60SBarry Smith   int        ierr;
163763b91edcSBarry Smith   Mat        outA;
1638b8a78c4aSBarry Smith   PetscTruth row_identity,col_identity;
163963b91edcSBarry Smith 
16403a40ed3dSBarry Smith   PetscFunctionBegin;
1641d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1642b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1643b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1644b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
164529bbc08cSBarry Smith     SETERRQ(1,"Row and column permutations must be identity for in-place ILU");
1646b8a78c4aSBarry Smith   }
1647a871dcd8SBarry Smith 
164863b91edcSBarry Smith   outA          = inA;
164963b91edcSBarry Smith   inA->factor   = FACTOR_LU;
165063b91edcSBarry Smith   a->row        = row;
165163b91edcSBarry Smith   a->col        = col;
1652c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1653c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
165463b91edcSBarry Smith 
165536db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1656b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
16574c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
1658b0a32e0cSBarry Smith   PetscLogObjectParent(inA,a->icol);
1659f0ec6fceSSatish Balay 
166094a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
166187828ca2SBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
166294a9d846SBarry Smith   }
166363b91edcSBarry Smith 
166408480c60SBarry Smith   if (!a->diag) {
1665f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
166663b91edcSBarry Smith   }
166763b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA);CHKERRQ(ierr);
16683a40ed3dSBarry Smith   PetscFunctionReturn(0);
1669a871dcd8SBarry Smith }
1670a871dcd8SBarry Smith 
1671d9eff348SSatish Balay #include "petscblaslapack.h"
16724a2ae208SSatish Balay #undef __FUNCT__
16734a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1674268466fbSBarry Smith int MatScale_SeqAIJ(const PetscScalar *alpha,Mat inA)
1675f0b747eeSBarry Smith {
1676f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
1677f0b747eeSBarry Smith   int        one = 1;
16783a40ed3dSBarry Smith 
16793a40ed3dSBarry Smith   PetscFunctionBegin;
1680268466fbSBarry Smith   BLscal_(&a->nz,(PetscScalar*)alpha,a->a,&one);
1681b0a32e0cSBarry Smith   PetscLogFlops(a->nz);
16823a40ed3dSBarry Smith   PetscFunctionReturn(0);
1683f0b747eeSBarry Smith }
1684f0b747eeSBarry Smith 
16854a2ae208SSatish Balay #undef __FUNCT__
16864a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
1687268466fbSBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1688cddf8d76SBarry Smith {
1689cddf8d76SBarry Smith   int ierr,i;
1690cddf8d76SBarry Smith 
16913a40ed3dSBarry Smith   PetscFunctionBegin;
1692cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1693b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1694cddf8d76SBarry Smith   }
1695cddf8d76SBarry Smith 
1696cddf8d76SBarry Smith   for (i=0; i<n; i++) {
16976a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1698cddf8d76SBarry Smith   }
16993a40ed3dSBarry Smith   PetscFunctionReturn(0);
1700cddf8d76SBarry Smith }
1701cddf8d76SBarry Smith 
17024a2ae208SSatish Balay #undef __FUNCT__
17034a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_SeqAIJ"
17048f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A,int *bs)
17055a838052SSatish Balay {
1706f830108cSBarry Smith   PetscFunctionBegin;
17075a838052SSatish Balay   *bs = 1;
17083a40ed3dSBarry Smith   PetscFunctionReturn(0);
17095a838052SSatish Balay }
17105a838052SSatish Balay 
17114a2ae208SSatish Balay #undef __FUNCT__
17124a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
1713268466fbSBarry Smith int MatIncreaseOverlap_SeqAIJ(Mat A,int is_max,IS is[],int ov)
17144dcbc457SBarry Smith {
1715e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1716efb16452SHong Zhang   int        row,i,j,k,l,m,n,*idx,ierr,*nidx,isz,val;
17178a047759SSatish Balay   int        start,end,*ai,*aj;
1718f1af5d2fSBarry Smith   PetscBT    table;
1719bbd702dbSSatish Balay 
17203a40ed3dSBarry Smith   PetscFunctionBegin;
1721273d9f13SBarry Smith   m     = A->m;
1722e4d965acSSatish Balay   ai    = a->i;
1723bfeeae90SHong Zhang   aj    = a->j;
17248a047759SSatish Balay 
172529bbc08cSBarry Smith   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal overlap value used");
172606763907SSatish Balay 
1727b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&nidx);CHKERRQ(ierr);
17286831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
172906763907SSatish Balay 
1730e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1731b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1732e4d965acSSatish Balay     isz  = 0;
17336831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1734e4d965acSSatish Balay 
1735e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17364dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1737b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1738e4d965acSSatish Balay 
1739dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1740e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1741f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
17424dcbc457SBarry Smith     }
174306763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
174406763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1745e4d965acSSatish Balay 
174604a348a9SBarry Smith     k = 0;
174704a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
174804a348a9SBarry Smith       n = isz;
174906763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1750e4d965acSSatish Balay         row   = nidx[k];
1751e4d965acSSatish Balay         start = ai[row];
1752e4d965acSSatish Balay         end   = ai[row+1];
175304a348a9SBarry Smith         for (l = start; l<end ; l++){
1754efb16452SHong Zhang           val = aj[l] ;
1755f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1756e4d965acSSatish Balay         }
1757e4d965acSSatish Balay       }
1758e4d965acSSatish Balay     }
1759029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1760e4d965acSSatish Balay   }
17616831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1762606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17633a40ed3dSBarry Smith   PetscFunctionReturn(0);
17644dcbc457SBarry Smith }
176517ab2063SBarry Smith 
17660513a670SBarry Smith /* -------------------------------------------------------------- */
17674a2ae208SSatish Balay #undef __FUNCT__
17684a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
17690513a670SBarry Smith int MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
17700513a670SBarry Smith {
17710513a670SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
177287828ca2SBarry Smith   PetscScalar  *vwork;
1773273d9f13SBarry Smith   int          i,ierr,nz,m = A->m,n = A->n,*cwork;
17740513a670SBarry Smith   int          *row,*col,*cnew,j,*lens;
177556cd22aeSBarry Smith   IS           icolp,irowp;
17760513a670SBarry Smith 
17773a40ed3dSBarry Smith   PetscFunctionBegin;
17784c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
177956cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
17804c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
178156cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
17820513a670SBarry Smith 
17830513a670SBarry Smith   /* determine lengths of permuted rows */
1784b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&lens);CHKERRQ(ierr);
17850513a670SBarry Smith   for (i=0; i<m; i++) {
17860513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
17870513a670SBarry Smith   }
17880513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
1789606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
17900513a670SBarry Smith 
1791b0a32e0cSBarry Smith   ierr = PetscMalloc(n*sizeof(int),&cnew);CHKERRQ(ierr);
17920513a670SBarry Smith   for (i=0; i<m; i++) {
17930513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
17940513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
17950513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
17960513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
17970513a670SBarry Smith   }
1798606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
17990513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18000513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
180156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
180256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
180356cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
180456cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18053a40ed3dSBarry Smith   PetscFunctionReturn(0);
18060513a670SBarry Smith }
18070513a670SBarry Smith 
18084a2ae208SSatish Balay #undef __FUNCT__
18094a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1810682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1811682d7d0cSBarry Smith {
1812c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1813682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1814d132466eSBarry Smith   int               ierr;
1815682d7d0cSBarry Smith 
18163a40ed3dSBarry Smith   PetscFunctionBegin;
1817c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1818d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1819d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1820d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
1821d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr);
1822d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr);
182387828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1824ca44d042SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_matlab: Use Matlab engine sparse LU factorization and solve.\n");CHKERRQ(ierr);
1825ca44d042SBarry Smith #endif
18263a40ed3dSBarry Smith   PetscFunctionReturn(0);
1827682d7d0cSBarry Smith }
1828ca44d042SBarry Smith EXTERN int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg);
1829ca44d042SBarry Smith EXTERN int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
1830b380c88cSHong Zhang EXTERN int MatILUDTFactor_SeqAIJ(Mat,MatFactorInfo*,IS,IS,Mat*);
18314a2ae208SSatish Balay #undef __FUNCT__
18324a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1833cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1834cb5b572fSBarry Smith {
1835be6bf707SBarry Smith   int        ierr;
1836cb5b572fSBarry Smith 
1837cb5b572fSBarry Smith   PetscFunctionBegin;
183833f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
183933f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1840be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1841be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1842be6bf707SBarry Smith 
1843bfeeae90SHong Zhang     if (a->i[A->m] != b->i[B->m]) {
184429bbc08cSBarry Smith       SETERRQ(1,"Number of nonzeros in two matrices are different");
1845cb5b572fSBarry Smith     }
1846bfeeae90SHong Zhang     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
1847cb5b572fSBarry Smith   } else {
1848cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1849cb5b572fSBarry Smith   }
1850cb5b572fSBarry Smith   PetscFunctionReturn(0);
1851cb5b572fSBarry Smith }
1852cb5b572fSBarry Smith 
18534a2ae208SSatish Balay #undef __FUNCT__
18544a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1855273d9f13SBarry Smith int MatSetUpPreallocation_SeqAIJ(Mat A)
1856273d9f13SBarry Smith {
1857273d9f13SBarry Smith   int        ierr;
1858273d9f13SBarry Smith 
1859273d9f13SBarry Smith   PetscFunctionBegin;
1860273d9f13SBarry Smith   ierr =  MatSeqAIJSetPreallocation(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1861273d9f13SBarry Smith   PetscFunctionReturn(0);
1862273d9f13SBarry Smith }
1863273d9f13SBarry Smith 
18644a2ae208SSatish Balay #undef __FUNCT__
18654a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
18664e7234bfSBarry Smith int MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
18676c0721eeSBarry Smith {
18686c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
18696c0721eeSBarry Smith   PetscFunctionBegin;
18706c0721eeSBarry Smith   *array = a->a;
18716c0721eeSBarry Smith   PetscFunctionReturn(0);
18726c0721eeSBarry Smith }
18736c0721eeSBarry Smith 
18744a2ae208SSatish Balay #undef __FUNCT__
18754a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
18764e7234bfSBarry Smith int MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
18776c0721eeSBarry Smith {
18786c0721eeSBarry Smith   PetscFunctionBegin;
18796c0721eeSBarry Smith   PetscFunctionReturn(0);
18806c0721eeSBarry Smith }
1881273d9f13SBarry Smith 
1882ee4f033dSBarry Smith #undef __FUNCT__
1883ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1884ee4f033dSBarry Smith int MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1885ee4f033dSBarry Smith {
1886ee4f033dSBarry Smith   int           (*f)(void *,Vec,Vec,void*) = (int (*)(void *,Vec,Vec,void *))coloring->f;
1887ee4f033dSBarry Smith   int           k,ierr,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
188887828ca2SBarry Smith   PetscScalar   dx,mone = -1.0,*y,*xx,*w3_array;
188987828ca2SBarry Smith   PetscScalar   *vscale_array;
1890ee4f033dSBarry Smith   PetscReal     epsilon = coloring->error_rel,umin = coloring->umin;
1891ee4f033dSBarry Smith   Vec           w1,w2,w3;
1892ee4f033dSBarry Smith   void          *fctx = coloring->fctx;
1893ee4f033dSBarry Smith   PetscTruth    flg;
1894ee4f033dSBarry Smith 
1895ee4f033dSBarry Smith   PetscFunctionBegin;
1896ee4f033dSBarry Smith   if (!coloring->w1) {
1897ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
1898ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w1);
1899ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
1900ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w2);
1901ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
1902ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w3);
1903ee4f033dSBarry Smith   }
1904ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1905ee4f033dSBarry Smith 
1906ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1907e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1908ee4f033dSBarry Smith   if (flg) {
1909ee4f033dSBarry Smith     PetscLogInfo(coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n");
1910ee4f033dSBarry Smith   } else {
1911ee4f033dSBarry Smith     ierr = MatZeroEntries(J);CHKERRQ(ierr);
1912ee4f033dSBarry Smith   }
1913ee4f033dSBarry Smith 
1914ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1915ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1916ee4f033dSBarry Smith 
1917ee4f033dSBarry Smith   /*
1918ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1919ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1920ee4f033dSBarry Smith   */
1921ee4f033dSBarry Smith   if (coloring->F) {
1922ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1923ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1924ee4f033dSBarry Smith     if (m1 != m2) {
1925ee4f033dSBarry Smith       coloring->F = 0;
1926ee4f033dSBarry Smith     }
1927ee4f033dSBarry Smith   }
1928ee4f033dSBarry Smith 
1929ee4f033dSBarry Smith   if (coloring->F) {
1930ee4f033dSBarry Smith     w1          = coloring->F;
1931ee4f033dSBarry Smith     coloring->F = 0;
1932ee4f033dSBarry Smith   } else {
193366f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1934ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
193566f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1936ee4f033dSBarry Smith   }
1937ee4f033dSBarry Smith 
1938ee4f033dSBarry Smith   /*
1939ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
1940ee4f033dSBarry Smith   */
1941ed480e8bSBarry Smith   ierr = VecGetArrayFast(x1,&xx);CHKERRQ(ierr);xx = xx - start;
1942ed480e8bSBarry Smith   ierr = VecGetArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
1943ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
1944ee4f033dSBarry Smith     /*
1945ee4f033dSBarry Smith        Loop over each column associated with color adding the
1946ee4f033dSBarry Smith        perturbation to the vector w3.
1947ee4f033dSBarry Smith     */
1948ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1949ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1950ee4f033dSBarry Smith       dx  = xx[col];
1951ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1952ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1953ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1954ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1955ee4f033dSBarry Smith #else
1956ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1957ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1958ee4f033dSBarry Smith #endif
1959ee4f033dSBarry Smith       dx                *= epsilon;
1960ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
1961ee4f033dSBarry Smith     }
1962ee4f033dSBarry Smith   }
1963ed480e8bSBarry Smith   vscale_array = vscale_array + start;ierr = VecRestoreArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1964ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1965ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1966ee4f033dSBarry Smith 
1967ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
1968ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
1969ee4f033dSBarry Smith 
1970ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
1971ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
1972ee4f033dSBarry Smith 
1973ed480e8bSBarry Smith   ierr = VecGetArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1974ee4f033dSBarry Smith   /*
1975ee4f033dSBarry Smith       Loop over each color
1976ee4f033dSBarry Smith   */
1977ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
197849b058dcSBarry Smith     coloring->currentcolor = k;
1979ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
1980ed480e8bSBarry Smith     ierr = VecGetArrayFast(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
1981ee4f033dSBarry Smith     /*
1982ee4f033dSBarry Smith        Loop over each column associated with color adding the
1983ee4f033dSBarry Smith        perturbation to the vector w3.
1984ee4f033dSBarry Smith     */
1985ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1986ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1987ee4f033dSBarry Smith       dx  = xx[col];
1988ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1989ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1990ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1991ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1992ee4f033dSBarry Smith #else
1993ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1994ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1995ee4f033dSBarry Smith #endif
1996ee4f033dSBarry Smith       dx            *= epsilon;
1997ee4f033dSBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(1,"Computed 0 differencing parameter");
1998ee4f033dSBarry Smith       w3_array[col] += dx;
1999ee4f033dSBarry Smith     }
2000ed480e8bSBarry Smith     w3_array = w3_array + start; ierr = VecRestoreArrayFast(w3,&w3_array);CHKERRQ(ierr);
2001ee4f033dSBarry Smith 
2002ee4f033dSBarry Smith     /*
2003ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2004ee4f033dSBarry Smith     */
2005ee4f033dSBarry Smith 
200666f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2007ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
200866f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2009ee4f033dSBarry Smith     ierr = VecAXPY(&mone,w1,w2);CHKERRQ(ierr);
2010ee4f033dSBarry Smith 
2011ee4f033dSBarry Smith     /*
2012ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2013ee4f033dSBarry Smith     */
2014ed480e8bSBarry Smith     ierr = VecGetArrayFast(w2,&y);CHKERRQ(ierr);
2015ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2016ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2017ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2018ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2019ee4f033dSBarry Smith       srow   = row + start;
2020ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2021ee4f033dSBarry Smith     }
2022ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(w2,&y);CHKERRQ(ierr);
2023ee4f033dSBarry Smith   }
202449b058dcSBarry Smith   coloring->currentcolor = k;
2025ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2026ed480e8bSBarry Smith   xx = xx + start; ierr  = VecRestoreArrayFast(x1,&xx);CHKERRQ(ierr);
2027ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2028ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2029ee4f033dSBarry Smith   PetscFunctionReturn(0);
2030ee4f033dSBarry Smith }
2031ee4f033dSBarry Smith 
2032ac90fabeSBarry Smith #include "petscblaslapack.h"
2033ac90fabeSBarry Smith #undef __FUNCT__
2034ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2035268466fbSBarry Smith int MatAXPY_SeqAIJ(const PetscScalar *a,Mat X,Mat Y,MatStructure str)
2036ac90fabeSBarry Smith {
2037c537a176SHong Zhang   int        ierr,one=1,i;
2038ac90fabeSBarry Smith   Mat_SeqAIJ *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
2039ac90fabeSBarry Smith 
2040ac90fabeSBarry Smith   PetscFunctionBegin;
2041ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2042268466fbSBarry Smith     BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one);
2043c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2044a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2045a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2046a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2047a30b2313SHong Zhang     }
2048a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
204924f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(X->m,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2050a30b2313SHong Zhang       y->XtoY = X;
2051c537a176SHong Zhang     }
2052a30b2313SHong Zhang     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += (*a)*(x->a[i]);
2053e2dd4fc4Svictorle     PetscLogInfo(0,"MatAXPY_SeqAIJ: ratio of nnz(X)/nnz(Y): %d/%d = %g\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz);
2054ac90fabeSBarry Smith   } else {
2055ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
2056ac90fabeSBarry Smith   }
2057ac90fabeSBarry Smith   PetscFunctionReturn(0);
2058ac90fabeSBarry Smith }
2059ac90fabeSBarry Smith 
2060682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
20610a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2062cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2063cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2064cb5b572fSBarry Smith        MatMult_SeqAIJ,
2065cb5b572fSBarry Smith        MatMultAdd_SeqAIJ,
20667c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
20677c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2068cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2069cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
20707c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
20717c922b88SBarry Smith        MatSolveTransposeAdd_SeqAIJ,
2072cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2073cb5b572fSBarry Smith        0,
207417ab2063SBarry Smith        MatRelax_SeqAIJ,
207517ab2063SBarry Smith        MatTranspose_SeqAIJ,
2076cb5b572fSBarry Smith        MatGetInfo_SeqAIJ,
2077cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2078cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2079cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2080cb5b572fSBarry Smith        MatNorm_SeqAIJ,
2081cb5b572fSBarry Smith        0,
2082cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
208317ab2063SBarry Smith        MatCompress_SeqAIJ,
2084cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2085cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2086cb5b572fSBarry Smith        MatZeroRows_SeqAIJ,
2087cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2088cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2089f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2090a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
2091273d9f13SBarry Smith        MatSetUpPreallocation_SeqAIJ,
2092cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2093861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
20946c0721eeSBarry Smith        MatGetArray_SeqAIJ,
20956c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2096be6bf707SBarry Smith        MatDuplicate_SeqAIJ,
2097cb5b572fSBarry Smith        0,
2098cb5b572fSBarry Smith        0,
2099cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2100cb5b572fSBarry Smith        0,
2101ac90fabeSBarry Smith        MatAXPY_SeqAIJ,
2102cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2103cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2104cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2105cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2106f0b747eeSBarry Smith        MatPrintHelp_SeqAIJ,
2107cb5b572fSBarry Smith        MatScale_SeqAIJ,
2108cb5b572fSBarry Smith        0,
2109cb5b572fSBarry Smith        0,
21106945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
21116945ee14SBarry Smith        MatGetBlockSize_SeqAIJ,
21123b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
21133b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
21143b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2115a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2116a93ec695SBarry Smith        MatFDColoringCreate_SeqAIJ,
2117b9617806SBarry Smith        0,
21180513a670SBarry Smith        0,
2119cda55fadSBarry Smith        MatPermute_SeqAIJ,
2120cda55fadSBarry Smith        0,
2121cda55fadSBarry Smith        0,
2122b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2123b9b97703SBarry Smith        MatView_SeqAIJ,
21248a124369SBarry Smith        MatGetPetscMaps_Petsc,
2125ee4f033dSBarry Smith        0,
2126ee4f033dSBarry Smith        0,
2127ee4f033dSBarry Smith        0,
2128ee4f033dSBarry Smith        0,
2129ee4f033dSBarry Smith        0,
2130ee4f033dSBarry Smith        0,
2131ee4f033dSBarry Smith        0,
2132ee4f033dSBarry Smith        0,
2133ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2134ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2135ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
2136ee4f033dSBarry Smith        MatFDColoringApply_SeqAIJ};
213717ab2063SBarry Smith 
2138fb2e594dSBarry Smith EXTERN_C_BEGIN
21394a2ae208SSatish Balay #undef __FUNCT__
21404a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
214115091d37SBarry Smith 
2142bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices)
2143bef8e0ddSBarry Smith {
2144bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2145bef8e0ddSBarry Smith   int        i,nz,n;
2146bef8e0ddSBarry Smith 
2147bef8e0ddSBarry Smith   PetscFunctionBegin;
2148bef8e0ddSBarry Smith 
2149bef8e0ddSBarry Smith   nz = aij->maxnz;
2150273d9f13SBarry Smith   n  = mat->n;
2151bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2152bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2153bef8e0ddSBarry Smith   }
2154bef8e0ddSBarry Smith   aij->nz = nz;
2155bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2156bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2157bef8e0ddSBarry Smith   }
2158bef8e0ddSBarry Smith 
2159bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2160bef8e0ddSBarry Smith }
2161fb2e594dSBarry Smith EXTERN_C_END
2162bef8e0ddSBarry Smith 
21634a2ae208SSatish Balay #undef __FUNCT__
21644a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2165bef8e0ddSBarry Smith /*@
2166bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2167bef8e0ddSBarry Smith        in the matrix.
2168bef8e0ddSBarry Smith 
2169bef8e0ddSBarry Smith   Input Parameters:
2170bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2171bef8e0ddSBarry Smith -  indices - the column indices
2172bef8e0ddSBarry Smith 
217315091d37SBarry Smith   Level: advanced
217415091d37SBarry Smith 
2175bef8e0ddSBarry Smith   Notes:
2176bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2177bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2178bef8e0ddSBarry Smith   of the MatSetValues() operation.
2179bef8e0ddSBarry Smith 
2180bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2181bef8e0ddSBarry Smith   MatCreateSeqAIJ().
2182bef8e0ddSBarry Smith 
2183bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2184bef8e0ddSBarry Smith 
2185b9617806SBarry Smith     The indices should start with zero, not one.
2186b9617806SBarry Smith 
2187bef8e0ddSBarry Smith @*/
2188bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices)
2189bef8e0ddSBarry Smith {
2190bef8e0ddSBarry Smith   int ierr,(*f)(Mat,int *);
2191bef8e0ddSBarry Smith 
2192bef8e0ddSBarry Smith   PetscFunctionBegin;
2193bef8e0ddSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2194c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2195bef8e0ddSBarry Smith   if (f) {
2196bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2197bef8e0ddSBarry Smith   } else {
219829bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to set column indices");
2199bef8e0ddSBarry Smith   }
2200bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2201bef8e0ddSBarry Smith }
2202bef8e0ddSBarry Smith 
2203be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2204be6bf707SBarry Smith 
2205fb2e594dSBarry Smith EXTERN_C_BEGIN
22064a2ae208SSatish Balay #undef __FUNCT__
22074a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2208be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat)
2209be6bf707SBarry Smith {
2210be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2211bfeeae90SHong Zhang   size_t       nz = aij->i[mat->m],ierr;
2212be6bf707SBarry Smith 
2213be6bf707SBarry Smith   PetscFunctionBegin;
2214be6bf707SBarry Smith   if (aij->nonew != 1) {
221529bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2216be6bf707SBarry Smith   }
2217be6bf707SBarry Smith 
2218be6bf707SBarry Smith   /* allocate space for values if not already there */
2219be6bf707SBarry Smith   if (!aij->saved_values) {
222087828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2221be6bf707SBarry Smith   }
2222be6bf707SBarry Smith 
2223be6bf707SBarry Smith   /* copy values over */
222487828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2225be6bf707SBarry Smith   PetscFunctionReturn(0);
2226be6bf707SBarry Smith }
2227fb2e594dSBarry Smith EXTERN_C_END
2228be6bf707SBarry Smith 
22294a2ae208SSatish Balay #undef __FUNCT__
2230b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2231be6bf707SBarry Smith /*@
2232be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2233be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2234be6bf707SBarry Smith        nonlinear portion.
2235be6bf707SBarry Smith 
2236be6bf707SBarry Smith    Collect on Mat
2237be6bf707SBarry Smith 
2238be6bf707SBarry Smith   Input Parameters:
2239be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2240be6bf707SBarry Smith 
224115091d37SBarry Smith   Level: advanced
224215091d37SBarry Smith 
2243be6bf707SBarry Smith   Common Usage, with SNESSolve():
2244be6bf707SBarry Smith $    Create Jacobian matrix
2245be6bf707SBarry Smith $    Set linear terms into matrix
2246be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2247be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2248be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2249be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2250be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2251be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2252be6bf707SBarry Smith $    In your Jacobian routine
2253be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2254be6bf707SBarry Smith $      Set nonlinear terms in matrix
2255be6bf707SBarry Smith 
2256be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2257be6bf707SBarry Smith $    // build linear portion of Jacobian
2258be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2259be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2260be6bf707SBarry Smith $    loop over nonlinear iterations
2261be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2262be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2263be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2264be6bf707SBarry Smith $       Solve linear system with Jacobian
2265be6bf707SBarry Smith $    endloop
2266be6bf707SBarry Smith 
2267be6bf707SBarry Smith   Notes:
2268be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2269be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2270be6bf707SBarry Smith     calling this routine.
2271be6bf707SBarry Smith 
2272be6bf707SBarry Smith .seealso: MatRetrieveValues()
2273be6bf707SBarry Smith 
2274be6bf707SBarry Smith @*/
2275be6bf707SBarry Smith int MatStoreValues(Mat mat)
2276be6bf707SBarry Smith {
2277be6bf707SBarry Smith   int ierr,(*f)(Mat);
2278be6bf707SBarry Smith 
2279be6bf707SBarry Smith   PetscFunctionBegin;
2280be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
228129bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
228229bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2283be6bf707SBarry Smith 
2284c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2285be6bf707SBarry Smith   if (f) {
2286be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2287be6bf707SBarry Smith   } else {
228829bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to store values");
2289be6bf707SBarry Smith   }
2290be6bf707SBarry Smith   PetscFunctionReturn(0);
2291be6bf707SBarry Smith }
2292be6bf707SBarry Smith 
2293fb2e594dSBarry Smith EXTERN_C_BEGIN
22944a2ae208SSatish Balay #undef __FUNCT__
22954a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2296be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat)
2297be6bf707SBarry Smith {
2298be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2299bfeeae90SHong Zhang   int        nz = aij->i[mat->m],ierr;
2300be6bf707SBarry Smith 
2301be6bf707SBarry Smith   PetscFunctionBegin;
2302be6bf707SBarry Smith   if (aij->nonew != 1) {
230329bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2304be6bf707SBarry Smith   }
2305be6bf707SBarry Smith   if (!aij->saved_values) {
230629bbc08cSBarry Smith     SETERRQ(1,"Must call MatStoreValues(A);first");
2307be6bf707SBarry Smith   }
2308be6bf707SBarry Smith 
2309be6bf707SBarry Smith   /* copy values over */
231087828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2311be6bf707SBarry Smith   PetscFunctionReturn(0);
2312be6bf707SBarry Smith }
2313fb2e594dSBarry Smith EXTERN_C_END
2314be6bf707SBarry Smith 
23154a2ae208SSatish Balay #undef __FUNCT__
23164a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2317be6bf707SBarry Smith /*@
2318be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2319be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2320be6bf707SBarry Smith        nonlinear portion.
2321be6bf707SBarry Smith 
2322be6bf707SBarry Smith    Collect on Mat
2323be6bf707SBarry Smith 
2324be6bf707SBarry Smith   Input Parameters:
2325be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2326be6bf707SBarry Smith 
232715091d37SBarry Smith   Level: advanced
232815091d37SBarry Smith 
2329be6bf707SBarry Smith .seealso: MatStoreValues()
2330be6bf707SBarry Smith 
2331be6bf707SBarry Smith @*/
2332be6bf707SBarry Smith int MatRetrieveValues(Mat mat)
2333be6bf707SBarry Smith {
2334be6bf707SBarry Smith   int ierr,(*f)(Mat);
2335be6bf707SBarry Smith 
2336be6bf707SBarry Smith   PetscFunctionBegin;
2337be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
233829bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
233929bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2340be6bf707SBarry Smith 
2341c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2342be6bf707SBarry Smith   if (f) {
2343be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2344be6bf707SBarry Smith   } else {
234529bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to retrieve values");
2346be6bf707SBarry Smith   }
2347be6bf707SBarry Smith   PetscFunctionReturn(0);
2348be6bf707SBarry Smith }
2349be6bf707SBarry Smith 
2350f83d6046SBarry Smith /*
2351f83d6046SBarry Smith    This allows SeqAIJ matrices to be passed to the matlab engine
2352f83d6046SBarry Smith */
235387828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2354f83d6046SBarry Smith #include "engine.h"   /* Matlab include file */
2355f83d6046SBarry Smith #include "mex.h"      /* Matlab include file */
2356f83d6046SBarry Smith EXTERN_C_BEGIN
23574a2ae208SSatish Balay #undef __FUNCT__
23584a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEnginePut_SeqAIJ"
235962aa7f4eSBarry Smith int MatMatlabEnginePut_SeqAIJ(PetscObject obj,void *mengine)
2360f83d6046SBarry Smith {
2361*25922d2bSSatish Balay   int         ierr;
2362f83d6046SBarry Smith   Mat         B = (Mat)obj;
2363f83d6046SBarry Smith   mxArray     *mat;
2364d79a51d8SBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)B->data;
2365d79a51d8SBarry Smith 
2366f83d6046SBarry Smith   PetscFunctionBegin;
236701820c62SBarry Smith   mat  = mxCreateSparse(B->n,B->m,aij->nz,mxREAL);
236887828ca2SBarry Smith   ierr = PetscMemcpy(mxGetPr(mat),aij->a,aij->nz*sizeof(PetscScalar));CHKERRQ(ierr);
2369d79a51d8SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
237066826eb6SBarry Smith   ierr = PetscMemcpy(mxGetIr(mat),aij->j,aij->nz*sizeof(int));CHKERRQ(ierr);
237166826eb6SBarry Smith   ierr = PetscMemcpy(mxGetJc(mat),aij->i,(B->m+1)*sizeof(int));CHKERRQ(ierr);
2372f83d6046SBarry Smith 
237301820c62SBarry Smith   /* Matlab indices start at 0 for sparse (what a surprise) */
2374bfeeae90SHong Zhang 
2375ca44d042SBarry Smith   ierr = PetscObjectName(obj);CHKERRQ(ierr);
2376f83d6046SBarry Smith   mxSetName(mat,obj->name);
237762aa7f4eSBarry Smith   engPutArray((Engine *)mengine,mat);
2378f83d6046SBarry Smith   PetscFunctionReturn(0);
2379f83d6046SBarry Smith }
2380f83d6046SBarry Smith EXTERN_C_END
238166826eb6SBarry Smith 
238266826eb6SBarry Smith EXTERN_C_BEGIN
23834a2ae208SSatish Balay #undef __FUNCT__
23844a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEngineGet_SeqAIJ"
238562aa7f4eSBarry Smith int MatMatlabEngineGet_SeqAIJ(PetscObject obj,void *mengine)
238666826eb6SBarry Smith {
238766826eb6SBarry Smith   int        ierr,ii;
238866826eb6SBarry Smith   Mat        mat = (Mat)obj;
238966826eb6SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
239066826eb6SBarry Smith   mxArray    *mmat;
239166826eb6SBarry Smith 
23926c0721eeSBarry Smith   PetscFunctionBegin;
239366826eb6SBarry Smith   ierr = PetscFree(aij->a);CHKERRQ(ierr);
239466826eb6SBarry Smith 
239562aa7f4eSBarry Smith   mmat = engGetArray((Engine *)mengine,obj->name);
239666826eb6SBarry Smith 
2397a65776f3SBarry Smith   aij->nz           = (mxGetJc(mmat))[mat->m];
2398a7ed9263SMatthew Knepley   ierr              = PetscMalloc(((size_t) aij->nz)*(sizeof(int)+sizeof(PetscScalar))+(mat->m+1)*sizeof(int),&aij->a);CHKERRQ(ierr);
239966826eb6SBarry Smith   aij->j            = (int*)(aij->a + aij->nz);
240066826eb6SBarry Smith   aij->i            = aij->j + aij->nz;
240166826eb6SBarry Smith   aij->singlemalloc = PETSC_TRUE;
240266826eb6SBarry Smith   aij->freedata     = PETSC_TRUE;
240366826eb6SBarry Smith 
240487828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,mxGetPr(mmat),aij->nz*sizeof(PetscScalar));CHKERRQ(ierr);
240566826eb6SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
240666826eb6SBarry Smith   ierr = PetscMemcpy(aij->j,mxGetIr(mmat),aij->nz*sizeof(int));CHKERRQ(ierr);
240766826eb6SBarry Smith   ierr = PetscMemcpy(aij->i,mxGetJc(mmat),(mat->m+1)*sizeof(int));CHKERRQ(ierr);
240866826eb6SBarry Smith 
240966826eb6SBarry Smith   for (ii=0; ii<mat->m; ii++) {
241066826eb6SBarry Smith     aij->ilen[ii] = aij->imax[ii] = aij->i[ii+1] - aij->i[ii];
241166826eb6SBarry Smith   }
241266826eb6SBarry Smith 
241366826eb6SBarry Smith   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
241466826eb6SBarry Smith   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
241566826eb6SBarry Smith 
241666826eb6SBarry Smith   PetscFunctionReturn(0);
241766826eb6SBarry Smith }
241866826eb6SBarry Smith EXTERN_C_END
2419f83d6046SBarry Smith #endif
2420f83d6046SBarry Smith 
2421be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
24224a2ae208SSatish Balay #undef __FUNCT__
24234a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
242417ab2063SBarry Smith /*@C
2425682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
24260d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
24276e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
242851c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
24292bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
243017ab2063SBarry Smith 
2431db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2432db81eaa0SLois Curfman McInnes 
243317ab2063SBarry Smith    Input Parameters:
2434db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
243517ab2063SBarry Smith .  m - number of rows
243617ab2063SBarry Smith .  n - number of columns
243717ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
243851c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
24392bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
244017ab2063SBarry Smith 
244117ab2063SBarry Smith    Output Parameter:
2442416022c9SBarry Smith .  A - the matrix
244317ab2063SBarry Smith 
2444b259b22eSLois Curfman McInnes    Notes:
244517ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
244617ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
24470002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
244844cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
244917ab2063SBarry Smith 
245017ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2451a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
24523d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
24536da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
245417ab2063SBarry Smith 
2455682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
24564fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2457682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
24586c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
24596c7ebb05SLois Curfman McInnes 
24606c7ebb05SLois Curfman McInnes    Options Database Keys:
2461db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2462db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2463db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2464db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2465db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
246617ab2063SBarry Smith 
2467027ccd11SLois Curfman McInnes    Level: intermediate
2468027ccd11SLois Curfman McInnes 
246936db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
247036db0b34SBarry Smith 
247117ab2063SBarry Smith @*/
2472ca01db9bSBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,const int nnz[],Mat *A)
247317ab2063SBarry Smith {
2474273d9f13SBarry Smith   int ierr;
24756945ee14SBarry Smith 
24763a40ed3dSBarry Smith   PetscFunctionBegin;
2477273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,m,n,A);CHKERRQ(ierr);
2478273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2479273d9f13SBarry Smith   ierr = MatSeqAIJSetPreallocation(*A,nz,nnz);CHKERRQ(ierr);
2480273d9f13SBarry Smith   PetscFunctionReturn(0);
2481273d9f13SBarry Smith }
2482273d9f13SBarry Smith 
24835da197adSKris Buschelman #define SKIP_ALLOCATION -4
24845da197adSKris Buschelman 
24854a2ae208SSatish Balay #undef __FUNCT__
24864a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2487273d9f13SBarry Smith /*@C
2488273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2489273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2490273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2491273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2492273d9f13SBarry Smith 
2493273d9f13SBarry Smith    Collective on MPI_Comm
2494273d9f13SBarry Smith 
2495273d9f13SBarry Smith    Input Parameters:
2496273d9f13SBarry Smith +  comm - MPI communicator, set to PETSC_COMM_SELF
2497273d9f13SBarry Smith .  m - number of rows
2498273d9f13SBarry Smith .  n - number of columns
2499273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2500273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2501273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2502273d9f13SBarry Smith 
2503273d9f13SBarry Smith    Output Parameter:
2504273d9f13SBarry Smith .  A - the matrix
2505273d9f13SBarry Smith 
2506273d9f13SBarry Smith    Notes:
2507273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2508273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2509273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2510273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2511273d9f13SBarry Smith 
2512273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2513273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2514273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2515273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2516273d9f13SBarry Smith 
2517273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2518273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2519273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2520273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2521273d9f13SBarry Smith 
2522273d9f13SBarry Smith    Options Database Keys:
2523273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2524273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2525273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2526273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2527273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2528273d9f13SBarry Smith 
2529273d9f13SBarry Smith    Level: intermediate
2530273d9f13SBarry Smith 
2531273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2532273d9f13SBarry Smith 
2533273d9f13SBarry Smith @*/
2534ca01db9bSBarry Smith int MatSeqAIJSetPreallocation(Mat B,int nz,const int nnz[])
2535273d9f13SBarry Smith {
2536ca01db9bSBarry Smith   int ierr,(*f)(Mat,int,const int[]);
2537a23d5eceSKris Buschelman 
2538a23d5eceSKris Buschelman   PetscFunctionBegin;
2539a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2540a23d5eceSKris Buschelman   if (f) {
2541a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2542a23d5eceSKris Buschelman   }
2543a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2544a23d5eceSKris Buschelman }
2545a23d5eceSKris Buschelman 
2546a23d5eceSKris Buschelman EXTERN_C_BEGIN
2547a23d5eceSKris Buschelman #undef __FUNCT__
2548a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2549a23d5eceSKris Buschelman int MatSeqAIJSetPreallocation_SeqAIJ(Mat B,int nz,int *nnz)
2550a23d5eceSKris Buschelman {
2551273d9f13SBarry Smith   Mat_SeqAIJ *b;
2552a7ed9263SMatthew Knepley   size_t     len = 0;
2553a43ee2ecSKris Buschelman   PetscTruth skipallocation = PETSC_FALSE;
2554a7ed9263SMatthew Knepley   int        i,ierr;
2555273d9f13SBarry Smith 
2556273d9f13SBarry Smith   PetscFunctionBegin;
2557d5d45c9bSBarry Smith 
2558c461c341SBarry Smith   if (nz == SKIP_ALLOCATION) {
2559c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2560c461c341SBarry Smith     nz             = 0;
2561c461c341SBarry Smith   }
2562c461c341SBarry Smith 
2563435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2564435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2565b73539f3SBarry Smith   if (nnz) {
2566273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
256729bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
25683a7fca6bSBarry Smith       if (nnz[i] > B->n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->n);
2569b73539f3SBarry Smith     }
2570b73539f3SBarry Smith   }
2571b73539f3SBarry Smith 
2572273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2573273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2574273d9f13SBarry Smith 
2575b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->imax);CHKERRQ(ierr);
2576273d9f13SBarry Smith   if (!nnz) {
2577435da068SBarry Smith     if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2578273d9f13SBarry Smith     else if (nz <= 0)        nz = 1;
2579273d9f13SBarry Smith     for (i=0; i<B->m; i++) b->imax[i] = nz;
2580273d9f13SBarry Smith     nz = nz*B->m;
2581273d9f13SBarry Smith   } else {
2582273d9f13SBarry Smith     nz = 0;
2583273d9f13SBarry Smith     for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2584273d9f13SBarry Smith   }
2585273d9f13SBarry Smith 
2586c461c341SBarry Smith   if (!skipallocation) {
2587273d9f13SBarry Smith     /* allocate the matrix space */
2588a7ed9263SMatthew Knepley     len             = ((size_t) nz)*(sizeof(int) + sizeof(PetscScalar)) + (B->m+1)*sizeof(int);
2589b0a32e0cSBarry Smith     ierr            = PetscMalloc(len,&b->a);CHKERRQ(ierr);
2590273d9f13SBarry Smith     b->j            = (int*)(b->a + nz);
2591273d9f13SBarry Smith     ierr            = PetscMemzero(b->j,nz*sizeof(int));CHKERRQ(ierr);
2592273d9f13SBarry Smith     b->i            = b->j + nz;
2593bfeeae90SHong Zhang     b->i[0] = 0;
25945da197adSKris Buschelman     for (i=1; i<B->m+1; i++) {
25955da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
25965da197adSKris Buschelman     }
2597273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2598273d9f13SBarry Smith     b->freedata     = PETSC_TRUE;
2599c461c341SBarry Smith   } else {
2600c461c341SBarry Smith     b->freedata     = PETSC_FALSE;
2601c461c341SBarry Smith   }
2602273d9f13SBarry Smith 
2603273d9f13SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
2604b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->ilen);CHKERRQ(ierr);
2605b0a32e0cSBarry Smith   PetscLogObjectMemory(B,len+2*(B->m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2606273d9f13SBarry Smith   for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2607273d9f13SBarry Smith 
2608273d9f13SBarry Smith   b->nz                = 0;
2609273d9f13SBarry Smith   b->maxnz             = nz;
2610273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2611273d9f13SBarry Smith   PetscFunctionReturn(0);
2612273d9f13SBarry Smith }
2613a23d5eceSKris Buschelman EXTERN_C_END
2614273d9f13SBarry Smith 
2615eb9c0419SKris Buschelman EXTERN int RegisterApplyPtAPRoutines_Private(Mat);
2616eb9c0419SKris Buschelman 
2617273d9f13SBarry Smith EXTERN_C_BEGIN
261805b94e36SKris Buschelman EXTERN int MatConvert_SeqAIJ_SeqSBAIJ(Mat,MatType,Mat*);
261905b94e36SKris Buschelman EXTERN int MatConvert_SeqAIJ_SeqBAIJ(Mat,MatType,Mat*);
262005b94e36SKris Buschelman EXTERN int MatReorderForNonzeroDiagonal_SeqAIJ(Mat,PetscReal,IS,IS);
26210a99d0a2SKris Buschelman EXTERN int MatAdjustForInodes_SeqAIJ(Mat,IS*,IS*);
2622d758967fSKris Buschelman EXTERN int MatSeqAIJGetInodeSizes_SeqAIJ(Mat,int*,int*[],int*);
2623a6175056SHong Zhang EXTERN_C_END
2624a6175056SHong Zhang 
2625a6175056SHong Zhang EXTERN_C_BEGIN
26264a2ae208SSatish Balay #undef __FUNCT__
26274a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2628273d9f13SBarry Smith int MatCreate_SeqAIJ(Mat B)
2629273d9f13SBarry Smith {
2630273d9f13SBarry Smith   Mat_SeqAIJ *b;
2631273d9f13SBarry Smith   int        ierr,size;
2632273d9f13SBarry Smith   PetscTruth flg;
2633273d9f13SBarry Smith 
2634273d9f13SBarry Smith   PetscFunctionBegin;
2635273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2636273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2637273d9f13SBarry Smith 
2638273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2639273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2640273d9f13SBarry Smith 
2641b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2642b0a32e0cSBarry Smith   B->data             = (void*)b;
2643549d3d68SSatish Balay   ierr = PetscMemzero(b,sizeof(Mat_SeqAIJ));CHKERRQ(ierr);
2644549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2645416022c9SBarry Smith   B->factor           = 0;
2646416022c9SBarry Smith   B->lupivotthreshold = 1.0;
264790f02eecSBarry Smith   B->mapping          = 0;
2648e82a3eeeSBarry Smith   ierr = PetscOptionsGetReal(B->prefix,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr);
2649e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2650416022c9SBarry Smith   b->row              = 0;
2651416022c9SBarry Smith   b->col              = 0;
265282bf6240SBarry Smith   b->icol             = 0;
2653b810aeb4SBarry Smith   b->reallocs         = 0;
265417ab2063SBarry Smith 
26558a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
26568a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2657a5ae1ecdSBarry Smith 
2658f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
265936db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2660f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2661416022c9SBarry Smith   b->nonew             = 0;
2662416022c9SBarry Smith   b->diag              = 0;
2663416022c9SBarry Smith   b->solve_work        = 0;
26642a1b7f2aSHong Zhang   B->spptr             = 0;
2665b65db4caSBarry Smith   b->inode.use         = PETSC_TRUE;
2666754ec7b1SSatish Balay   b->inode.node_count  = 0;
2667754ec7b1SSatish Balay   b->inode.size        = 0;
26686c7ebb05SLois Curfman McInnes   b->inode.limit       = 5;
26696c7ebb05SLois Curfman McInnes   b->inode.max_limit   = 5;
2670be6bf707SBarry Smith   b->saved_values      = 0;
2671d7f994e1SBarry Smith   b->idiag             = 0;
2672d7f994e1SBarry Smith   b->ssor              = 0;
2673f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
2674a30b2313SHong Zhang   b->xtoy              = 0;
2675a30b2313SHong Zhang   b->XtoY              = 0;
267617ab2063SBarry Smith 
267735d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
267835d8aa7fSBarry Smith 
2679e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_matlab",&flg);CHKERRQ(ierr);
2680ca44d042SBarry Smith   if (flg) {ierr = MatUseMatlab_SeqAIJ(B);CHKERRQ(ierr);}
2681f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2682bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2683bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2684f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2685be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2686bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2687f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2688be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2689bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2690a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
2691a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
2692a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
269385fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
269485fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
269585fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
2696cd0d46ebSvictorle   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsSymmetric_C",
2697cd0d46ebSvictorle                                      "MatIsSymmetric_SeqAIJ",
2698cd0d46ebSvictorle                                       MatIsSymmetric_SeqAIJ);CHKERRQ(ierr);
2699a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
2700a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
2701a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
270205b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
270305b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
270405b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
27050a99d0a2SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatAdjustForInodes_C",
27060a99d0a2SKris Buschelman                                      "MatAdjustForInodes_SeqAIJ",
27070a99d0a2SKris Buschelman                                       MatAdjustForInodes_SeqAIJ);CHKERRQ(ierr);
2708d758967fSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJGetInodeSizes_C",
2709d758967fSKris Buschelman                                      "MatSeqAIJGetInodeSizes_SeqAIJ",
2710d758967fSKris Buschelman                                       MatSeqAIJGetInodeSizes_SeqAIJ);CHKERRQ(ierr);
271187828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2712f83d6046SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatMatlabEnginePut_SeqAIJ",MatMatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
271366826eb6SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatMatlabEngineGet_SeqAIJ",MatMatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
2714f83d6046SBarry Smith #endif
2715eb9c0419SKris Buschelman   ierr = RegisterApplyPtAPRoutines_Private(B);CHKERRQ(ierr);
27163a40ed3dSBarry Smith   PetscFunctionReturn(0);
271717ab2063SBarry Smith }
2718273d9f13SBarry Smith EXTERN_C_END
271917ab2063SBarry Smith 
27204a2ae208SSatish Balay #undef __FUNCT__
27214a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2722be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
272317ab2063SBarry Smith {
2724416022c9SBarry Smith   Mat        C;
2725416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data;
2726bfeeae90SHong Zhang   int        i,m = A->m,ierr;
2727a7ed9263SMatthew Knepley   size_t     len;
272817ab2063SBarry Smith 
27293a40ed3dSBarry Smith   PetscFunctionBegin;
27304043dd9cSLois Curfman McInnes   *B = 0;
2731273d9f13SBarry Smith   ierr = MatCreate(A->comm,A->m,A->n,A->m,A->n,&C);CHKERRQ(ierr);
2732273d9f13SBarry Smith   ierr = MatSetType(C,MATSEQAIJ);CHKERRQ(ierr);
2733273d9f13SBarry Smith   c    = (Mat_SeqAIJ*)C->data;
2734273d9f13SBarry Smith 
2735416022c9SBarry Smith   C->factor         = A->factor;
2736416022c9SBarry Smith   c->row            = 0;
2737416022c9SBarry Smith   c->col            = 0;
273882bf6240SBarry Smith   c->icol           = 0;
2739f1e2ffcdSBarry Smith   c->keepzeroedrows = a->keepzeroedrows;
2740c456f294SBarry Smith   C->assembled      = PETSC_TRUE;
274117ab2063SBarry Smith 
2742273d9f13SBarry Smith   C->M          = A->m;
2743273d9f13SBarry Smith   C->N          = A->n;
274417ab2063SBarry Smith 
2745b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->imax);CHKERRQ(ierr);
2746b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->ilen);CHKERRQ(ierr);
274717ab2063SBarry Smith   for (i=0; i<m; i++) {
2748416022c9SBarry Smith     c->imax[i] = a->imax[i];
2749416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
275017ab2063SBarry Smith   }
275117ab2063SBarry Smith 
275217ab2063SBarry Smith   /* allocate the matrix space */
2753f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
2754a7ed9263SMatthew Knepley   len   = ((size_t) (m+1))*sizeof(int)+(a->i[m])*(sizeof(PetscScalar)+sizeof(int));
2755b0a32e0cSBarry Smith   ierr  = PetscMalloc(len,&c->a);CHKERRQ(ierr);
2756bfeeae90SHong Zhang   c->j  = (int*)(c->a + a->i[m] );
2757bfeeae90SHong Zhang   c->i  = c->j + a->i[m];
2758549d3d68SSatish Balay   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));CHKERRQ(ierr);
275917ab2063SBarry Smith   if (m > 0) {
2760bfeeae90SHong Zhang     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(int));CHKERRQ(ierr);
2761be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2762bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
2763be6bf707SBarry Smith     } else {
2764bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
276517ab2063SBarry Smith     }
276608480c60SBarry Smith   }
276717ab2063SBarry Smith 
2768b0a32e0cSBarry Smith   PetscLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2769416022c9SBarry Smith   c->sorted      = a->sorted;
2770416022c9SBarry Smith   c->roworiented = a->roworiented;
2771416022c9SBarry Smith   c->nonew       = a->nonew;
27727a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
2773be6bf707SBarry Smith   c->saved_values = 0;
2774d7f994e1SBarry Smith   c->idiag        = 0;
2775d7f994e1SBarry Smith   c->ssor         = 0;
277636db0b34SBarry Smith   c->ignorezeroentries = a->ignorezeroentries;
277736db0b34SBarry Smith   c->freedata     = PETSC_TRUE;
277817ab2063SBarry Smith 
2779416022c9SBarry Smith   if (a->diag) {
2780b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&c->diag);CHKERRQ(ierr);
2781b0a32e0cSBarry Smith     PetscLogObjectMemory(C,(m+1)*sizeof(int));
278217ab2063SBarry Smith     for (i=0; i<m; i++) {
2783416022c9SBarry Smith       c->diag[i] = a->diag[i];
278417ab2063SBarry Smith     }
27853a40ed3dSBarry Smith   } else c->diag        = 0;
2786b65db4caSBarry Smith   c->inode.use          = a->inode.use;
27876c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
27886c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
2789754ec7b1SSatish Balay   if (a->inode.size){
2790b0a32e0cSBarry Smith     ierr                = PetscMalloc((m+1)*sizeof(int),&c->inode.size);CHKERRQ(ierr);
2791754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
2792549d3d68SSatish Balay     ierr                = PetscMemcpy(c->inode.size,a->inode.size,(m+1)*sizeof(int));CHKERRQ(ierr);
2793754ec7b1SSatish Balay   } else {
2794754ec7b1SSatish Balay     c->inode.size       = 0;
2795754ec7b1SSatish Balay     c->inode.node_count = 0;
2796754ec7b1SSatish Balay   }
2797416022c9SBarry Smith   c->nz                 = a->nz;
2798416022c9SBarry Smith   c->maxnz              = a->maxnz;
2799416022c9SBarry Smith   c->solve_work         = 0;
28002a1b7f2aSHong Zhang   C->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
2801273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2802754ec7b1SSatish Balay 
2803416022c9SBarry Smith   *B = C;
2804b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
28053a40ed3dSBarry Smith   PetscFunctionReturn(0);
280617ab2063SBarry Smith }
280717ab2063SBarry Smith 
2808273d9f13SBarry Smith EXTERN_C_BEGIN
28094a2ae208SSatish Balay #undef __FUNCT__
28104a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
2811b0a32e0cSBarry Smith int MatLoad_SeqAIJ(PetscViewer viewer,MatType type,Mat *A)
281217ab2063SBarry Smith {
2813416022c9SBarry Smith   Mat_SeqAIJ   *a;
2814416022c9SBarry Smith   Mat          B;
2815efb16452SHong Zhang   int          i,nz,ierr,fd,header[4],size,*rowlengths = 0,M,N;
2816bcd2baecSBarry Smith   MPI_Comm     comm;
281717ab2063SBarry Smith 
28183a40ed3dSBarry Smith   PetscFunctionBegin;
2819e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2820d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
282129bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
2822b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
28230752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2824552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
282517ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
282617ab2063SBarry Smith 
2827d64ed03dSBarry Smith   if (nz < 0) {
282829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2829d64ed03dSBarry Smith   }
2830d64ed03dSBarry Smith 
283117ab2063SBarry Smith   /* read in row lengths */
2832b0a32e0cSBarry Smith   ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
28330752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
283417ab2063SBarry Smith 
283517ab2063SBarry Smith   /* create our matrix */
2836b3a1e11cSKris Buschelman   ierr = MatCreate(comm,PETSC_DECIDE,PETSC_DECIDE,M,N,&B);CHKERRQ(ierr);
2837b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
2838b3a1e11cSKris Buschelman   ierr = MatSeqAIJSetPreallocation(B,0,rowlengths);CHKERRQ(ierr);
2839416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
284017ab2063SBarry Smith 
284117ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
28420752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
284317ab2063SBarry Smith 
284417ab2063SBarry Smith   /* read in nonzero values */
28450752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
284617ab2063SBarry Smith 
284717ab2063SBarry Smith   /* set matrix "i" values */
2848efb16452SHong Zhang   a->i[0] = 0;
284917ab2063SBarry Smith   for (i=1; i<= M; i++) {
2850416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2851416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
285217ab2063SBarry Smith   }
2853606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
285417ab2063SBarry Smith 
28556d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
28566d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2857b3a1e11cSKris Buschelman   *A = B;
28583a40ed3dSBarry Smith   PetscFunctionReturn(0);
285917ab2063SBarry Smith }
2860273d9f13SBarry Smith EXTERN_C_END
286117ab2063SBarry Smith 
28624a2ae208SSatish Balay #undef __FUNCT__
2863b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
28648f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
28657264ac53SSatish Balay {
28667264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
28670f5bd95cSBarry Smith   int        ierr;
28687264ac53SSatish Balay 
28693a40ed3dSBarry Smith   PetscFunctionBegin;
2870bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
2871bfeeae90SHong Zhang   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)) {
2872ca44d042SBarry Smith     *flg = PETSC_FALSE;
2873ca44d042SBarry Smith     PetscFunctionReturn(0);
2874bcd2baecSBarry Smith   }
28757264ac53SSatish Balay 
28767264ac53SSatish Balay   /* if the a->i are the same */
2877273d9f13SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(int),flg);CHKERRQ(ierr);
28780f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
28797264ac53SSatish Balay 
28807264ac53SSatish Balay   /* if a->j are the same */
28810f5bd95cSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),flg);CHKERRQ(ierr);
28820f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
2883bcd2baecSBarry Smith 
2884bcd2baecSBarry Smith   /* if a->a are the same */
288587828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
28860f5bd95cSBarry Smith 
28873a40ed3dSBarry Smith   PetscFunctionReturn(0);
28887264ac53SSatish Balay 
28897264ac53SSatish Balay }
289036db0b34SBarry Smith 
28914a2ae208SSatish Balay #undef __FUNCT__
28924a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
289336db0b34SBarry Smith /*@C
289436db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
289536db0b34SBarry Smith               provided by the user.
289636db0b34SBarry Smith 
289736db0b34SBarry Smith       Coolective on MPI_Comm
289836db0b34SBarry Smith 
289936db0b34SBarry Smith    Input Parameters:
290036db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
290136db0b34SBarry Smith .   m - number of rows
290236db0b34SBarry Smith .   n - number of columns
290336db0b34SBarry Smith .   i - row indices
290436db0b34SBarry Smith .   j - column indices
290536db0b34SBarry Smith -   a - matrix values
290636db0b34SBarry Smith 
290736db0b34SBarry Smith    Output Parameter:
290836db0b34SBarry Smith .   mat - the matrix
290936db0b34SBarry Smith 
291036db0b34SBarry Smith    Level: intermediate
291136db0b34SBarry Smith 
291236db0b34SBarry Smith    Notes:
29130551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
291436db0b34SBarry Smith     once the matrix is destroyed
291536db0b34SBarry Smith 
291636db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
291736db0b34SBarry Smith 
2918bfeeae90SHong Zhang        The i and j indices are 0 based
291936db0b34SBarry Smith 
292036db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
292136db0b34SBarry Smith 
292236db0b34SBarry Smith @*/
292387828ca2SBarry Smith int MatCreateSeqAIJWithArrays(MPI_Comm comm,int m,int n,int* i,int*j,PetscScalar *a,Mat *mat)
292436db0b34SBarry Smith {
292536db0b34SBarry Smith   int        ierr,ii;
292636db0b34SBarry Smith   Mat_SeqAIJ *aij;
292736db0b34SBarry Smith 
292836db0b34SBarry Smith   PetscFunctionBegin;
2929c461c341SBarry Smith   ierr = MatCreateSeqAIJ(comm,m,n,SKIP_ALLOCATION,0,mat);CHKERRQ(ierr);
293036db0b34SBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
293136db0b34SBarry Smith 
2932bfeeae90SHong Zhang   if (i[0] != 0) {
2933bfeeae90SHong Zhang     SETERRQ(1,"i (row indices) must start with 0");
293436db0b34SBarry Smith   }
293536db0b34SBarry Smith   aij->i = i;
293636db0b34SBarry Smith   aij->j = j;
293736db0b34SBarry Smith   aij->a = a;
293836db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
293936db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
294036db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
294136db0b34SBarry Smith 
294236db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
294336db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
29449860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
294529bbc08cSBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(1,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
294636db0b34SBarry Smith #endif
294736db0b34SBarry Smith   }
29489860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
294936db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
2950bfeeae90SHong Zhang     if (j[ii] < 0) SETERRQ2(1,"Negative column index at location = %d index = %d",ii,j[ii]);
2951bfeeae90SHong Zhang     if (j[ii] > n - 1) SETERRQ2(1,"Column index to large at location = %d index = %d",ii,j[ii]);
295236db0b34SBarry Smith   }
295336db0b34SBarry Smith #endif
295436db0b34SBarry Smith 
2955b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2956b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
295736db0b34SBarry Smith   PetscFunctionReturn(0);
295836db0b34SBarry Smith }
295936db0b34SBarry Smith 
2960cc8ba8e1SBarry Smith #undef __FUNCT__
2961ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
2962ee4f033dSBarry Smith int MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
2963cc8ba8e1SBarry Smith {
2964cc8ba8e1SBarry Smith   int        ierr;
2965cc8ba8e1SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
296636db0b34SBarry Smith 
2967cc8ba8e1SBarry Smith   PetscFunctionBegin;
296812c595b3SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
2969cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
2970cc8ba8e1SBarry Smith     a->coloring = coloring;
297112c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
297208b6dcc0SBarry Smith     int             i,*larray;
297312c595b3SBarry Smith     ISColoring      ocoloring;
297408b6dcc0SBarry Smith     ISColoringValue *colors;
297512c595b3SBarry Smith 
297612c595b3SBarry Smith     /* set coloring for diagonal portion */
297712c595b3SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
297812c595b3SBarry Smith     for (i=0; i<A->n; i++) {
297912c595b3SBarry Smith       larray[i] = i;
298012c595b3SBarry Smith     }
298112c595b3SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
298208b6dcc0SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
298312c595b3SBarry Smith     for (i=0; i<A->n; i++) {
298412c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
298512c595b3SBarry Smith     }
298612c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
298712c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr);
298812c595b3SBarry Smith     a->coloring = ocoloring;
298912c595b3SBarry Smith   }
2990cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
2991cc8ba8e1SBarry Smith }
2992cc8ba8e1SBarry Smith 
299387828ca2SBarry Smith #if defined(PETSC_HAVE_ADIC) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2994ee4f033dSBarry Smith EXTERN_C_BEGIN
299529c1e371SBarry Smith #include "adic/ad_utils.h"
2996ee4f033dSBarry Smith EXTERN_C_END
2997cc8ba8e1SBarry Smith 
2998cc8ba8e1SBarry Smith #undef __FUNCT__
2999ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3000ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3001cc8ba8e1SBarry Smith {
3002cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
300308b6dcc0SBarry Smith   int             m = A->m,*ii = a->i,*jj = a->j,nz,i,j,nlen;
30044440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
300508b6dcc0SBarry Smith   ISColoringValue *color;
3006cc8ba8e1SBarry Smith 
3007cc8ba8e1SBarry Smith   PetscFunctionBegin;
3008cc8ba8e1SBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
30094440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3010cc8ba8e1SBarry Smith   color = a->coloring->colors;
3011cc8ba8e1SBarry Smith   /* loop over rows */
3012cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3013cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3014cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3015cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3016cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3017cc8ba8e1SBarry Smith     }
30184440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3019ee4f033dSBarry Smith   }
3020ee4f033dSBarry Smith   PetscFunctionReturn(0);
3021ee4f033dSBarry Smith }
3022ee4f033dSBarry Smith 
3023ee4f033dSBarry Smith #else
3024ee4f033dSBarry Smith 
3025ee4f033dSBarry Smith #undef __FUNCT__
3026ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3027ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3028ee4f033dSBarry Smith {
3029ee4f033dSBarry Smith   PetscFunctionBegin;
3030ee4f033dSBarry Smith   SETERRQ(1,"PETSc installed without ADIC");
3031ee4f033dSBarry Smith }
3032ee4f033dSBarry Smith 
3033ee4f033dSBarry Smith #endif
3034ee4f033dSBarry Smith 
3035ee4f033dSBarry Smith #undef __FUNCT__
3036ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
3037ee4f033dSBarry Smith int MatSetValuesAdifor_SeqAIJ(Mat A,int nl,void *advalues)
3038ee4f033dSBarry Smith {
3039ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
304008b6dcc0SBarry Smith   int             m = A->m,*ii = a->i,*jj = a->j,nz,i,j;
304187828ca2SBarry Smith   PetscScalar     *v = a->a,*values = (PetscScalar *)advalues;
304208b6dcc0SBarry Smith   ISColoringValue *color;
3043ee4f033dSBarry Smith 
3044ee4f033dSBarry Smith   PetscFunctionBegin;
3045ee4f033dSBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
3046ee4f033dSBarry Smith   color = a->coloring->colors;
3047ee4f033dSBarry Smith   /* loop over rows */
3048ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3049ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3050ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3051ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3052ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3053ee4f033dSBarry Smith     }
3054ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3055cc8ba8e1SBarry Smith   }
3056cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3057cc8ba8e1SBarry Smith }
305836db0b34SBarry Smith 
3059