xref: /petsc/src/mat/impls/aij/seq/aij.c (revision c537a17644e75ddee54e85f4b8155d17b42d28e5)
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"
1836db0b34SBarry 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);
266945ee14SBarry Smith   ishift = a->indexshift;
2753e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
28273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
296945ee14SBarry Smith   } else if (oshift == 0 && ishift == -1) {
30435da068SBarry Smith     int nz = a->i[A->m] - 1;
313b2fbd54SBarry Smith     /* malloc space and  subtract 1 from i and j indices */
32b0a32e0cSBarry 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 if (oshift == 1 && ishift == 0) {
37435da068SBarry Smith     int nz = a->i[A->m];
383b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
391bc30dd5SBarry Smith     ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr);
40b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr);
413b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
42273d9f13SBarry Smith     for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1;
436945ee14SBarry Smith   } else {
446945ee14SBarry Smith     *ia = a->i; *ja = a->j;
45a2ce50c7SBarry Smith   }
463a40ed3dSBarry Smith   PetscFunctionReturn(0);
47a2744918SBarry Smith }
48a2744918SBarry Smith 
494a2ae208SSatish Balay #undef __FUNCT__
504a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
5136db0b34SBarry Smith int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,PetscTruth *done)
526945ee14SBarry Smith {
536945ee14SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
54606d414cSSatish Balay   int        ishift = a->indexshift,ierr;
556945ee14SBarry Smith 
563a40ed3dSBarry Smith   PetscFunctionBegin;
573a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
5853e63a63SBarry Smith   if ((symmetric && !A->structurally_symmetric) || (oshift == 0 && ishift == -1) || (oshift == 1 && ishift == 0)) {
59606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
60606d414cSSatish Balay     ierr = PetscFree(*ja);CHKERRQ(ierr);
61bcd2baecSBarry Smith   }
623a40ed3dSBarry Smith   PetscFunctionReturn(0);
6317ab2063SBarry Smith }
6417ab2063SBarry Smith 
654a2ae208SSatish Balay #undef __FUNCT__
664a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
6736db0b34SBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int **ia,int **ja,PetscTruth *done)
683b2fbd54SBarry Smith {
693b2fbd54SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
70a93ec695SBarry Smith   int        ierr,i,ishift = a->indexshift,*collengths,*cia,*cja,n = A->n,m = A->m;
71a93ec695SBarry Smith   int        nz = a->i[m]+ishift,row,*jj,mr,col;
723b2fbd54SBarry Smith 
733a40ed3dSBarry Smith   PetscFunctionBegin;
743b2fbd54SBarry Smith   *nn     = A->n;
753a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
763b2fbd54SBarry Smith   if (symmetric) {
77273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
783b2fbd54SBarry Smith   } else {
79b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&collengths);CHKERRQ(ierr);
80549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
81b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&cia);CHKERRQ(ierr);
82b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&cja);CHKERRQ(ierr);
833b2fbd54SBarry Smith     jj = a->j;
843b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
853b2fbd54SBarry Smith       collengths[jj[i] + ishift]++;
863b2fbd54SBarry Smith     }
873b2fbd54SBarry Smith     cia[0] = oshift;
883b2fbd54SBarry Smith     for (i=0; i<n; i++) {
893b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
903b2fbd54SBarry Smith     }
91549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
923b2fbd54SBarry Smith     jj   = a->j;
93a93ec695SBarry Smith     for (row=0; row<m; row++) {
94a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
95a93ec695SBarry Smith       for (i=0; i<mr; i++) {
963b2fbd54SBarry Smith         col = *jj++ + ishift;
973b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
983b2fbd54SBarry Smith       }
993b2fbd54SBarry Smith     }
100606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1013b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1023b2fbd54SBarry Smith   }
1033a40ed3dSBarry Smith   PetscFunctionReturn(0);
1043b2fbd54SBarry Smith }
1053b2fbd54SBarry Smith 
1064a2ae208SSatish Balay #undef __FUNCT__
1074a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
10836db0b34SBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,PetscTruth *done)
1093b2fbd54SBarry Smith {
110606d414cSSatish Balay   int ierr;
111606d414cSSatish Balay 
1123a40ed3dSBarry Smith   PetscFunctionBegin;
1133a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1143b2fbd54SBarry Smith 
115606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
116606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1173b2fbd54SBarry Smith 
1183a40ed3dSBarry Smith   PetscFunctionReturn(0);
1193b2fbd54SBarry Smith }
1203b2fbd54SBarry Smith 
121227d817aSBarry Smith #define CHUNKSIZE   15
12217ab2063SBarry Smith 
1234a2ae208SSatish Balay #undef __FUNCT__
1244a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
12587828ca2SBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,PetscScalar *v,InsertMode is)
12617ab2063SBarry Smith {
127416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
128416022c9SBarry Smith   int         *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,sorted = a->sorted;
129273d9f13SBarry Smith   int         *imax = a->imax,*ai = a->i,*ailen = a->ilen;
130549d3d68SSatish Balay   int         *aj = a->j,nonew = a->nonew,shift = a->indexshift,ierr;
13187828ca2SBarry Smith   PetscScalar *ap,value,*aa = a->a;
13236db0b34SBarry Smith   PetscTruth  ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE);
133273d9f13SBarry Smith   PetscTruth  roworiented = a->roworiented;
13417ab2063SBarry Smith 
1353a40ed3dSBarry Smith   PetscFunctionBegin;
13617ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
137416022c9SBarry Smith     row  = im[k];
1385ef9f2a5SBarry Smith     if (row < 0) continue;
139aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
140273d9f13SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",row,A->m);
1413b2fbd54SBarry Smith #endif
14217ab2063SBarry Smith     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
14317ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
144416022c9SBarry Smith     low = 0;
14517ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1465ef9f2a5SBarry Smith       if (in[l] < 0) continue;
147aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
148273d9f13SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[l],A->n);
1493b2fbd54SBarry Smith #endif
1504b0e389bSBarry Smith       col = in[l] - shift;
1514b0e389bSBarry Smith       if (roworiented) {
1525ef9f2a5SBarry Smith         value = v[l + k*n];
153bef8e0ddSBarry Smith       } else {
1544b0e389bSBarry Smith         value = v[k + l*m];
1554b0e389bSBarry Smith       }
15636db0b34SBarry Smith       if (value == 0.0 && ignorezeroentries) continue;
15736db0b34SBarry Smith 
158416022c9SBarry Smith       if (!sorted) low = 0; high = nrow;
159416022c9SBarry Smith       while (high-low > 5) {
160416022c9SBarry Smith         t = (low+high)/2;
161416022c9SBarry Smith         if (rp[t] > col) high = t;
162416022c9SBarry Smith         else             low  = t;
16317ab2063SBarry Smith       }
164416022c9SBarry Smith       for (i=low; i<high; i++) {
16517ab2063SBarry Smith         if (rp[i] > col) break;
16617ab2063SBarry Smith         if (rp[i] == col) {
167416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
16817ab2063SBarry Smith           else                  ap[i] = value;
16917ab2063SBarry Smith           goto noinsert;
17017ab2063SBarry Smith         }
17117ab2063SBarry Smith       }
172c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
17329bbc08cSBarry Smith       else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix",row,col);
17417ab2063SBarry Smith       if (nrow >= rmax) {
17517ab2063SBarry Smith         /* there is no extra room in row, therefore enlarge */
176a7ed9263SMatthew Knepley         int         new_nz = ai[A->m] + CHUNKSIZE,*new_i,*new_j;
177a7ed9263SMatthew Knepley         size_t      len;
17887828ca2SBarry Smith         PetscScalar *new_a;
17917ab2063SBarry Smith 
18029bbc08cSBarry Smith         if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix requiring new malloc()",row,col);
18196854ed6SLois Curfman McInnes 
18217ab2063SBarry Smith         /* malloc new storage space */
183a7ed9263SMatthew Knepley         len     = ((size_t) new_nz)*(sizeof(int)+sizeof(PetscScalar))+(A->m+1)*sizeof(int);
184b0a32e0cSBarry Smith 	ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr);
18517ab2063SBarry Smith         new_j   = (int*)(new_a + new_nz);
18617ab2063SBarry Smith         new_i   = new_j + new_nz;
18717ab2063SBarry Smith 
18817ab2063SBarry Smith         /* copy over old data into new slots */
18917ab2063SBarry Smith         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];}
190273d9f13SBarry Smith         for (ii=row+1; ii<A->m+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;}
191549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr);
192a7ed9263SMatthew Knepley         len  = (((size_t) new_nz) - CHUNKSIZE - ai[row] - nrow - shift);
193549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow,len*sizeof(int));CHKERRQ(ierr);
194a7ed9263SMatthew Knepley         ierr = PetscMemcpy(new_a,aa,(((size_t) ai[row])+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr);
19587828ca2SBarry Smith         ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow,len*sizeof(PetscScalar));CHKERRQ(ierr);
19617ab2063SBarry Smith         /* free up old matrix storage */
197606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);
198606d414cSSatish Balay         if (!a->singlemalloc) {
199606d414cSSatish Balay           ierr = PetscFree(a->i);CHKERRQ(ierr);
200606d414cSSatish Balay           ierr = PetscFree(a->j);CHKERRQ(ierr);
201606d414cSSatish Balay         }
202416022c9SBarry Smith         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;
203f1e2ffcdSBarry Smith         a->singlemalloc = PETSC_TRUE;
20417ab2063SBarry Smith 
20517ab2063SBarry Smith         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
206416022c9SBarry Smith         rmax = imax[row] = imax[row] + CHUNKSIZE;
20787828ca2SBarry Smith         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar)));
208416022c9SBarry Smith         a->maxnz += CHUNKSIZE;
209b810aeb4SBarry Smith         a->reallocs++;
21017ab2063SBarry Smith       }
211416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
212416022c9SBarry Smith       /* shift up all the later entries in this row */
213416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
21417ab2063SBarry Smith         rp[ii+1] = rp[ii];
21517ab2063SBarry Smith         ap[ii+1] = ap[ii];
21617ab2063SBarry Smith       }
21717ab2063SBarry Smith       rp[i] = col;
21817ab2063SBarry Smith       ap[i] = value;
21917ab2063SBarry Smith       noinsert:;
220416022c9SBarry Smith       low = i + 1;
22117ab2063SBarry Smith     }
22217ab2063SBarry Smith     ailen[row] = nrow;
22317ab2063SBarry Smith   }
2243a40ed3dSBarry Smith   PetscFunctionReturn(0);
22517ab2063SBarry Smith }
22617ab2063SBarry Smith 
2274a2ae208SSatish Balay #undef __FUNCT__
2284a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
22987828ca2SBarry Smith int MatGetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,PetscScalar *v)
2307eb43aa7SLois Curfman McInnes {
2317eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
232b49de8d1SLois Curfman McInnes   int          *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
2337eb43aa7SLois Curfman McInnes   int          *ai = a->i,*ailen = a->ilen,shift = a->indexshift;
23487828ca2SBarry Smith   PetscScalar  *ap,*aa = a->a,zero = 0.0;
2357eb43aa7SLois Curfman McInnes 
2363a40ed3dSBarry Smith   PetscFunctionBegin;
2377eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2387eb43aa7SLois Curfman McInnes     row  = im[k];
23929bbc08cSBarry Smith     if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",row);
240273d9f13SBarry Smith     if (row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: %d",row);
2417eb43aa7SLois Curfman McInnes     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
2427eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2437eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
24429bbc08cSBarry Smith       if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",in[l]);
245273d9f13SBarry Smith       if (in[l] >= A->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: %d",in[l]);
2467eb43aa7SLois Curfman McInnes       col = in[l] - shift;
2477eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2487eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2497eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2507eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2517eb43aa7SLois Curfman McInnes         else             low  = t;
2527eb43aa7SLois Curfman McInnes       }
2537eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2547eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2557eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
256b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2577eb43aa7SLois Curfman McInnes           goto finished;
2587eb43aa7SLois Curfman McInnes         }
2597eb43aa7SLois Curfman McInnes       }
260b49de8d1SLois Curfman McInnes       *v++ = zero;
2617eb43aa7SLois Curfman McInnes       finished:;
2627eb43aa7SLois Curfman McInnes     }
2637eb43aa7SLois Curfman McInnes   }
2643a40ed3dSBarry Smith   PetscFunctionReturn(0);
2657eb43aa7SLois Curfman McInnes }
2667eb43aa7SLois Curfman McInnes 
26717ab2063SBarry Smith 
2684a2ae208SSatish Balay #undef __FUNCT__
2694a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
270b0a32e0cSBarry Smith int MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
27117ab2063SBarry Smith {
272416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
273416022c9SBarry Smith   int        i,fd,*col_lens,ierr;
27417ab2063SBarry Smith 
2753a40ed3dSBarry Smith   PetscFunctionBegin;
276b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
277b0a32e0cSBarry Smith   ierr = PetscMalloc((4+A->m)*sizeof(int),&col_lens);CHKERRQ(ierr);
278552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
279273d9f13SBarry Smith   col_lens[1] = A->m;
280273d9f13SBarry Smith   col_lens[2] = A->n;
281416022c9SBarry Smith   col_lens[3] = a->nz;
282416022c9SBarry Smith 
283416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
284273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
285416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
28617ab2063SBarry Smith   }
287273d9f13SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->m,PETSC_INT,1);CHKERRQ(ierr);
288606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
289416022c9SBarry Smith 
290416022c9SBarry Smith   /* store column indices (zero start index) */
291416022c9SBarry Smith   if (a->indexshift) {
292416022c9SBarry Smith     for (i=0; i<a->nz; i++) a->j[i]--;
29317ab2063SBarry Smith   }
2940752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,0);CHKERRQ(ierr);
295416022c9SBarry Smith   if (a->indexshift) {
296416022c9SBarry Smith     for (i=0; i<a->nz; i++) a->j[i]++;
29717ab2063SBarry Smith   }
298416022c9SBarry Smith 
299416022c9SBarry Smith   /* store nonzero values */
3000752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,0);CHKERRQ(ierr);
3013a40ed3dSBarry Smith   PetscFunctionReturn(0);
30217ab2063SBarry Smith }
303416022c9SBarry Smith 
3047f43a090SHong Zhang extern int MatSeqAIJFactorInfo_SuperLU(Mat,PetscViewer);
305cd155464SBarry Smith extern int MatMPIAIJFactorInfo_SuperLu(Mat,PetscViewer);
306a072f7f7SHong Zhang extern int MatFactorInfo_Spooles(Mat,PetscViewer);
307e7420845SHong Zhang extern int MatSeqAIJFactorInfo_UMFPACK(Mat,PetscViewer);
3084ffef66eSHong Zhang extern int MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
309cd155464SBarry Smith 
3104a2ae208SSatish Balay #undef __FUNCT__
3114a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
312b0a32e0cSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
313416022c9SBarry Smith {
314416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
315fb9695e5SSatish Balay   int               ierr,i,j,m = A->m,shift = a->indexshift;
316fb9695e5SSatish Balay   char              *name;
317f3ef73ceSBarry Smith   PetscViewerFormat format;
31817ab2063SBarry Smith 
3193a40ed3dSBarry Smith   PetscFunctionBegin;
320435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
321b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
322456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL || format == PETSC_VIEWER_ASCII_INFO) {
3236831982aSBarry Smith     if (a->inode.size) {
324b0a32e0cSBarry 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);
3256831982aSBarry Smith     } else {
326b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"not using I-node routines\n");CHKERRQ(ierr);
3276831982aSBarry Smith     }
328fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
329d00d2cf4SBarry Smith     int nofinalvalue = 0;
330273d9f13SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) {
331d00d2cf4SBarry Smith       nofinalvalue = 1;
332d00d2cf4SBarry Smith     }
333b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
334b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %d %d \n",m,A->n);CHKERRQ(ierr);
335b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %d \n",a->nz);CHKERRQ(ierr);
336b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
337b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
33817ab2063SBarry Smith 
33917ab2063SBarry Smith     for (i=0; i<m; i++) {
340416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
341aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
342b0a32e0cSBarry 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);
34317ab2063SBarry Smith #else
344b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
34517ab2063SBarry Smith #endif
34617ab2063SBarry Smith       }
34717ab2063SBarry Smith     }
348d00d2cf4SBarry Smith     if (nofinalvalue) {
349b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",m,A->n,0.0);CHKERRQ(ierr);
350d00d2cf4SBarry Smith     }
351fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
352b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
353cd155464SBarry Smith   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
3546ea74821SHong Zhang #if defined(PETSC_HAVE_SUPERLU) && !defined(PETSC_USE_SINGLE)
3557f43a090SHong Zhang      ierr = MatSeqAIJFactorInfo_SuperLU(A,viewer);CHKERRQ(ierr);
3567f43a090SHong Zhang #endif
3576ea74821SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST) && !defined(PETSC_USE_SINGLE)
358cd155464SBarry Smith      ierr = MatMPIAIJFactorInfo_SuperLu(A,viewer);CHKERRQ(ierr);
359cd155464SBarry Smith #endif
3606ea74821SHong Zhang #if defined(PETSC_HAVE_SPOOLES) && !defined(PETSC_USE_SINGLE)
361a072f7f7SHong Zhang      ierr = MatFactorInfo_Spooles(A,viewer);CHKERRQ(ierr);
362174d842dSHong Zhang #endif
363e7420845SHong Zhang #if defined(PETSC_HAVE_UMFPACK) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_COMPLEX)
364e7420845SHong Zhang      ierr = MatSeqAIJFactorInfo_UMFPACK(A,viewer);CHKERRQ(ierr);
365e7420845SHong Zhang #endif
3664ffef66eSHong Zhang #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_COMPLEX)
3674ffef66eSHong Zhang      ierr = MatSeqAIJFactorInfo_Matlab(A,viewer);CHKERRQ(ierr);
3684ffef66eSHong Zhang #endif
3694ffef66eSHong Zhang 
370cd155464SBarry Smith      PetscFunctionReturn(0);
371fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
372b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
37344cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
374b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
37544cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
376aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
37736db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
37862b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
37936db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
38062b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
38136db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
38262b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3836831982aSBarry Smith         }
38444cd7ae7SLois Curfman McInnes #else
38562b941d6SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
38644cd7ae7SLois Curfman McInnes #endif
38744cd7ae7SLois Curfman McInnes       }
388b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
38944cd7ae7SLois Curfman McInnes     }
390b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
391fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
392496be53dSLois Curfman McInnes     int nzd=0,fshift=1,*sptr;
393b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
394b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&sptr);CHKERRQ(ierr);
395496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
396496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
397496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
398496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
399aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
40036db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
401496be53dSLois Curfman McInnes #else
402496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
403496be53dSLois Curfman McInnes #endif
404496be53dSLois Curfman McInnes         }
405496be53dSLois Curfman McInnes       }
406496be53dSLois Curfman McInnes     }
4072e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
408b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %d %d\n\n",m,nzd);CHKERRQ(ierr);
4092e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
410b0a32e0cSBarry 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);}
411b0a32e0cSBarry 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);}
412b0a32e0cSBarry 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);}
413b0a32e0cSBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
414b0a32e0cSBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %d %d\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
415b0a32e0cSBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %d\n",sptr[i]);CHKERRQ(ierr);}
416496be53dSLois Curfman McInnes     }
417b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
418606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
419496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
420496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
421b0a32e0cSBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %d ",a->j[j]+fshift);CHKERRQ(ierr);}
422496be53dSLois Curfman McInnes       }
423b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
424496be53dSLois Curfman McInnes     }
425b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
426496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
427496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
428496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
429aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
43036db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
431b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4326831982aSBarry Smith           }
433496be53dSLois Curfman McInnes #else
434b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
435496be53dSLois Curfman McInnes #endif
436496be53dSLois Curfman McInnes         }
437496be53dSLois Curfman McInnes       }
438b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
439496be53dSLois Curfman McInnes     }
440b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
441fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
44202594712SBarry Smith     int         cnt = 0,jcnt;
44387828ca2SBarry Smith     PetscScalar value;
44402594712SBarry Smith 
445b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
44602594712SBarry Smith     for (i=0; i<m; i++) {
44702594712SBarry Smith       jcnt = 0;
448273d9f13SBarry Smith       for (j=0; j<A->n; j++) {
449e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
45002594712SBarry Smith           value = a->a[cnt++];
451e24b481bSBarry Smith           jcnt++;
45202594712SBarry Smith         } else {
45302594712SBarry Smith           value = 0.0;
45402594712SBarry Smith         }
455aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
456b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
45702594712SBarry Smith #else
458b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
45902594712SBarry Smith #endif
46002594712SBarry Smith       }
461b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
46202594712SBarry Smith     }
463b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4643a40ed3dSBarry Smith   } else {
465b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
46617ab2063SBarry Smith     for (i=0; i<m; i++) {
467b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
468416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
469aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
47036db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
47162b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
47236db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
47362b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4743a40ed3dSBarry Smith         } else {
47562b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
47617ab2063SBarry Smith         }
47717ab2063SBarry Smith #else
47862b941d6SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
47917ab2063SBarry Smith #endif
48017ab2063SBarry Smith       }
481b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
48217ab2063SBarry Smith     }
483b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
48417ab2063SBarry Smith   }
485b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4863a40ed3dSBarry Smith   PetscFunctionReturn(0);
487416022c9SBarry Smith }
488416022c9SBarry Smith 
4894a2ae208SSatish Balay #undef __FUNCT__
4904a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
491b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
492416022c9SBarry Smith {
493480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
494416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
495273d9f13SBarry Smith   int               ierr,i,j,m = A->m,shift = a->indexshift,color;
49636db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
497b0a32e0cSBarry Smith   PetscViewer       viewer;
498f3ef73ceSBarry Smith   PetscViewerFormat format;
499cddf8d76SBarry Smith 
5003a40ed3dSBarry Smith   PetscFunctionBegin;
501480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
502b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
50319bcc07fSBarry Smith 
504b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
505416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
5060513a670SBarry Smith 
507fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
5080513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
509b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
510416022c9SBarry Smith     for (i=0; i<m; i++) {
511cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
512416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
513cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
514aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
51536db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
516cddf8d76SBarry Smith #else
517cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
518cddf8d76SBarry Smith #endif
519b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
520cddf8d76SBarry Smith       }
521cddf8d76SBarry Smith     }
522b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
523cddf8d76SBarry Smith     for (i=0; i<m; i++) {
524cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
525cddf8d76SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
526cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
527cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
528b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
529cddf8d76SBarry Smith       }
530cddf8d76SBarry Smith     }
531b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
532cddf8d76SBarry Smith     for (i=0; i<m; i++) {
533cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
534cddf8d76SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
535cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
536aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
53736db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
538cddf8d76SBarry Smith #else
539cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
540cddf8d76SBarry Smith #endif
541b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
542416022c9SBarry Smith       }
543416022c9SBarry Smith     }
5440513a670SBarry Smith   } else {
5450513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5460513a670SBarry Smith     /* first determine max of all nonzero values */
5470513a670SBarry Smith     int    nz = a->nz,count;
548b0a32e0cSBarry Smith     PetscDraw   popup;
54936db0b34SBarry Smith     PetscReal scale;
5500513a670SBarry Smith 
5510513a670SBarry Smith     for (i=0; i<nz; i++) {
5520513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5530513a670SBarry Smith     }
554b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
555b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
556b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5570513a670SBarry Smith     count = 0;
5580513a670SBarry Smith     for (i=0; i<m; i++) {
5590513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
5600513a670SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5610513a670SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
562b0a32e0cSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count]));
563b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5640513a670SBarry Smith         count++;
5650513a670SBarry Smith       }
5660513a670SBarry Smith     }
5670513a670SBarry Smith   }
568480ef9eaSBarry Smith   PetscFunctionReturn(0);
569480ef9eaSBarry Smith }
570cddf8d76SBarry Smith 
5714a2ae208SSatish Balay #undef __FUNCT__
5724a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
573b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
574480ef9eaSBarry Smith {
575480ef9eaSBarry Smith   int        ierr;
576b0a32e0cSBarry Smith   PetscDraw  draw;
57736db0b34SBarry Smith   PetscReal  xr,yr,xl,yl,h,w;
578480ef9eaSBarry Smith   PetscTruth isnull;
579480ef9eaSBarry Smith 
580480ef9eaSBarry Smith   PetscFunctionBegin;
581b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
582b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
583480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
584480ef9eaSBarry Smith 
585480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
586273d9f13SBarry Smith   xr  = A->n; yr = A->m; h = yr/10.0; w = xr/10.0;
587480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
588b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
589b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
590480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5913a40ed3dSBarry Smith   PetscFunctionReturn(0);
592416022c9SBarry Smith }
593416022c9SBarry Smith 
5944a2ae208SSatish Balay #undef __FUNCT__
5954a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
596b0a32e0cSBarry Smith int MatView_SeqAIJ(Mat A,PetscViewer viewer)
597416022c9SBarry Smith {
598416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
599bcd2baecSBarry Smith   int         ierr;
6006831982aSBarry Smith   PetscTruth  issocket,isascii,isbinary,isdraw;
601416022c9SBarry Smith 
6023a40ed3dSBarry Smith   PetscFunctionBegin;
603b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
604b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
605fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
606fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
6070f5bd95cSBarry Smith   if (issocket) {
60801820c62SBarry Smith     if (a->indexshift) {
60929bbc08cSBarry Smith       SETERRQ(1,"Can only socket send sparse matrix with 0 based indexing");
61001820c62SBarry Smith     }
611b0a32e0cSBarry Smith     ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr);
6120f5bd95cSBarry Smith   } else if (isascii) {
6133a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
6140f5bd95cSBarry Smith   } else if (isbinary) {
6153a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
6160f5bd95cSBarry Smith   } else if (isdraw) {
6173a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6185cd90555SBarry Smith   } else {
61929bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
62017ab2063SBarry Smith   }
6213a40ed3dSBarry Smith   PetscFunctionReturn(0);
62217ab2063SBarry Smith }
62319bcc07fSBarry Smith 
6243a7fca6bSBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat,PetscTruth);
6254a2ae208SSatish Balay #undef __FUNCT__
6264a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
6278f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
62817ab2063SBarry Smith {
629416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
63041c01911SSatish Balay   int          fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax,ierr;
631273d9f13SBarry Smith   int          m = A->m,*ip,N,*ailen = a->ilen,shift = a->indexshift,rmax = 0;
63287828ca2SBarry Smith   PetscScalar  *aa = a->a,*ap;
633564e58d6SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST) || defined(PETSC_HAVE_SPOOLES) || defined(PETSC_HAVE_UMFPACK)
6349b9367d5SHong Zhang   PetscTruth   flag;
6359e070d67SMatthew Knepley #endif
63617ab2063SBarry Smith 
6373a40ed3dSBarry Smith   PetscFunctionBegin;
6383a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
63917ab2063SBarry Smith 
64043ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
64117ab2063SBarry Smith   for (i=1; i<m; i++) {
642416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
64317ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
64494a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
64517ab2063SBarry Smith     if (fshift) {
6460f5bd95cSBarry Smith       ip = aj + ai[i] + shift;
6470f5bd95cSBarry Smith       ap = aa + ai[i] + shift;
64817ab2063SBarry Smith       N  = ailen[i];
64917ab2063SBarry Smith       for (j=0; j<N; j++) {
65017ab2063SBarry Smith         ip[j-fshift] = ip[j];
65117ab2063SBarry Smith         ap[j-fshift] = ap[j];
65217ab2063SBarry Smith       }
65317ab2063SBarry Smith     }
65417ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
65517ab2063SBarry Smith   }
65617ab2063SBarry Smith   if (m) {
65717ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
65817ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
65917ab2063SBarry Smith   }
66017ab2063SBarry Smith   /* reset ilen and imax for each row */
66117ab2063SBarry Smith   for (i=0; i<m; i++) {
66217ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
66317ab2063SBarry Smith   }
664416022c9SBarry Smith   a->nz = ai[m] + shift;
66517ab2063SBarry Smith 
66617ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
667416022c9SBarry Smith   if (fshift && a->diag) {
668606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
669b0a32e0cSBarry Smith     PetscLogObjectMemory(A,-(m+1)*sizeof(int));
670416022c9SBarry Smith     a->diag = 0;
67117ab2063SBarry Smith   }
672b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded,%d used\n",m,A->n,fshift,a->nz);
673b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %d\n",a->reallocs);
674b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax);
675dd5f02e7SSatish Balay   a->reallocs          = 0;
6764e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
67736db0b34SBarry Smith   a->rmax              = rmax;
6784e220ebcSLois Curfman McInnes 
67976dd722bSSatish Balay   /* check out for identical nodes. If found, use inode functions */
6803a7fca6bSBarry Smith   ierr = Mat_AIJ_CheckInode(A,(PetscTruth)(!fshift));CHKERRQ(ierr);
681cfef3cccSHong Zhang 
6829b9367d5SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
683e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(A->prefix,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr);
6849b9367d5SHong Zhang   if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(A);CHKERRQ(ierr); }
6859b9367d5SHong Zhang #endif
6869b9367d5SHong Zhang 
687cfef3cccSHong Zhang #if defined(PETSC_HAVE_SPOOLES)
688e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(A->prefix,"-mat_aij_spooles",&flag);CHKERRQ(ierr);
689cfef3cccSHong Zhang   if (flag) { ierr = MatUseSpooles_SeqAIJ(A);CHKERRQ(ierr); }
690cfef3cccSHong Zhang #endif
691cfef3cccSHong Zhang 
692564e58d6SHong Zhang #if defined(PETSC_HAVE_UMFPACK)
693e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(A->prefix,"-mat_aij_umfpack",&flag);CHKERRQ(ierr);
694564e58d6SHong Zhang   if (flag) { ierr = MatUseUMFPACK_SeqAIJ(A);CHKERRQ(ierr); }
695564e58d6SHong Zhang #endif
696564e58d6SHong Zhang 
6973a40ed3dSBarry Smith   PetscFunctionReturn(0);
69817ab2063SBarry Smith }
69917ab2063SBarry Smith 
7004a2ae208SSatish Balay #undef __FUNCT__
7014a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
7028f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A)
70317ab2063SBarry Smith {
704416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
705549d3d68SSatish Balay   int        ierr;
7063a40ed3dSBarry Smith 
7073a40ed3dSBarry Smith   PetscFunctionBegin;
70887828ca2SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->m]+a->indexshift)*sizeof(PetscScalar));CHKERRQ(ierr);
7093a40ed3dSBarry Smith   PetscFunctionReturn(0);
71017ab2063SBarry Smith }
711416022c9SBarry Smith 
7124a2ae208SSatish Balay #undef __FUNCT__
7134a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
714e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A)
71517ab2063SBarry Smith {
716416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
71782bf6240SBarry Smith   int        ierr;
718d5d45c9bSBarry Smith 
7193a40ed3dSBarry Smith   PetscFunctionBegin;
720aa482453SBarry Smith #if defined(PETSC_USE_LOG)
721b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",A->m,A->n,a->nz);
72217ab2063SBarry Smith #endif
72336db0b34SBarry Smith   if (a->freedata) {
724606d414cSSatish Balay     ierr = PetscFree(a->a);CHKERRQ(ierr);
725606d414cSSatish Balay     if (!a->singlemalloc) {
726606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
727606d414cSSatish Balay       ierr = PetscFree(a->j);CHKERRQ(ierr);
728606d414cSSatish Balay     }
72936db0b34SBarry Smith   }
730c38d4ed2SBarry Smith   if (a->row) {
731c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
732c38d4ed2SBarry Smith   }
733c38d4ed2SBarry Smith   if (a->col) {
734c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
735c38d4ed2SBarry Smith   }
736606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
737606d414cSSatish Balay   if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);}
738606d414cSSatish Balay   if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);}
739273d9f13SBarry Smith   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
740606d414cSSatish Balay   if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
741606d414cSSatish Balay   if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
74282bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
743606d414cSSatish Balay   if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);}
744cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
745606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
7463a40ed3dSBarry Smith   PetscFunctionReturn(0);
74717ab2063SBarry Smith }
74817ab2063SBarry Smith 
7494a2ae208SSatish Balay #undef __FUNCT__
7504a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
7518f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
75217ab2063SBarry Smith {
7533a40ed3dSBarry Smith   PetscFunctionBegin;
7543a40ed3dSBarry Smith   PetscFunctionReturn(0);
75517ab2063SBarry Smith }
75617ab2063SBarry Smith 
7574a2ae208SSatish Balay #undef __FUNCT__
7584a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7598f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
76017ab2063SBarry Smith {
761416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7623a40ed3dSBarry Smith 
7633a40ed3dSBarry Smith   PetscFunctionBegin;
764a65d3064SKris Buschelman   switch (op) {
765a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
766a65d3064SKris Buschelman       a->roworiented       = PETSC_TRUE;
767a65d3064SKris Buschelman       break;
768a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
769a65d3064SKris Buschelman       a->keepzeroedrows    = PETSC_TRUE;
770a65d3064SKris Buschelman       break;
771a65d3064SKris Buschelman     case MAT_COLUMN_ORIENTED:
772a65d3064SKris Buschelman       a->roworiented       = PETSC_FALSE;
773a65d3064SKris Buschelman       break;
774a65d3064SKris Buschelman     case MAT_COLUMNS_SORTED:
775a65d3064SKris Buschelman       a->sorted            = PETSC_TRUE;
776a65d3064SKris Buschelman       break;
777a65d3064SKris Buschelman     case MAT_COLUMNS_UNSORTED:
778a65d3064SKris Buschelman       a->sorted            = PETSC_FALSE;
779a65d3064SKris Buschelman       break;
780a65d3064SKris Buschelman     case MAT_NO_NEW_NONZERO_LOCATIONS:
781a65d3064SKris Buschelman       a->nonew             = 1;
782a65d3064SKris Buschelman       break;
783a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
784a65d3064SKris Buschelman       a->nonew             = -1;
785a65d3064SKris Buschelman       break;
786a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
787a65d3064SKris Buschelman       a->nonew             = -2;
788a65d3064SKris Buschelman       break;
789a65d3064SKris Buschelman     case MAT_YES_NEW_NONZERO_LOCATIONS:
790a65d3064SKris Buschelman       a->nonew             = 0;
791a65d3064SKris Buschelman       break;
792a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
793a65d3064SKris Buschelman       a->ignorezeroentries = PETSC_TRUE;
794a65d3064SKris Buschelman       break;
795a65d3064SKris Buschelman     case MAT_USE_INODES:
796a65d3064SKris Buschelman       a->inode.use         = PETSC_TRUE;
797a65d3064SKris Buschelman       break;
798a65d3064SKris Buschelman     case MAT_DO_NOT_USE_INODES:
799a65d3064SKris Buschelman       a->inode.use         = PETSC_FALSE;
800a65d3064SKris Buschelman       break;
801a65d3064SKris Buschelman     case MAT_ROWS_SORTED:
802a65d3064SKris Buschelman     case MAT_ROWS_UNSORTED:
803a65d3064SKris Buschelman     case MAT_YES_NEW_DIAGONALS:
804a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
805a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
806b0a32e0cSBarry Smith       PetscLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
807a65d3064SKris Buschelman       break;
808a65d3064SKris Buschelman     case MAT_NO_NEW_DIAGONALS:
80929bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
810a65d3064SKris Buschelman     case MAT_INODE_LIMIT_1:
811a65d3064SKris Buschelman       a->inode.limit  = 1;
812a65d3064SKris Buschelman       break;
813a65d3064SKris Buschelman     case MAT_INODE_LIMIT_2:
814a65d3064SKris Buschelman       a->inode.limit  = 2;
815a65d3064SKris Buschelman       break;
816a65d3064SKris Buschelman     case MAT_INODE_LIMIT_3:
817a65d3064SKris Buschelman       a->inode.limit  = 3;
818a65d3064SKris Buschelman       break;
819a65d3064SKris Buschelman     case MAT_INODE_LIMIT_4:
820a65d3064SKris Buschelman       a->inode.limit  = 4;
821a65d3064SKris Buschelman       break;
822a65d3064SKris Buschelman     case MAT_INODE_LIMIT_5:
823a65d3064SKris Buschelman       a->inode.limit  = 5;
824a65d3064SKris Buschelman       break;
825a65d3064SKris Buschelman     default:
826a65d3064SKris Buschelman       SETERRQ(PETSC_ERR_SUP,"unknown option");
827a65d3064SKris Buschelman   }
8283a40ed3dSBarry Smith   PetscFunctionReturn(0);
82917ab2063SBarry Smith }
83017ab2063SBarry Smith 
8314a2ae208SSatish Balay #undef __FUNCT__
8324a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
8338f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
83417ab2063SBarry Smith {
835416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
8363a40ed3dSBarry Smith   int          i,j,n,shift = a->indexshift,ierr;
83787828ca2SBarry Smith   PetscScalar  *x,zero = 0.0;
83817ab2063SBarry Smith 
8393a40ed3dSBarry Smith   PetscFunctionBegin;
8403a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
841ed480e8bSBarry Smith   ierr = VecGetArrayFast(v,&x);CHKERRQ(ierr);
84236db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
843273d9f13SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
844273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
845416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
846416022c9SBarry Smith       if (a->j[j]+shift == i) {
847416022c9SBarry Smith         x[i] = a->a[j];
84817ab2063SBarry Smith         break;
84917ab2063SBarry Smith       }
85017ab2063SBarry Smith     }
85117ab2063SBarry Smith   }
852ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(v,&x);CHKERRQ(ierr);
8533a40ed3dSBarry Smith   PetscFunctionReturn(0);
85417ab2063SBarry Smith }
85517ab2063SBarry Smith 
856d5d45c9bSBarry Smith 
8574a2ae208SSatish Balay #undef __FUNCT__
8584a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
8597c922b88SBarry Smith int MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
86017ab2063SBarry Smith {
861416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
8625c897100SBarry Smith   PetscScalar  *x,*y;
8635c897100SBarry Smith   int          ierr,m = A->m,shift = a->indexshift;
8645c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
8655c897100SBarry Smith   PetscScalar  *v,alpha;
8665c897100SBarry Smith   int          n,i,*idx;
8675c897100SBarry Smith #endif
86817ab2063SBarry Smith 
8693a40ed3dSBarry Smith   PetscFunctionBegin;
8702e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
871ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
872ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
87317ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
8745c897100SBarry Smith 
8755c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
8765c897100SBarry Smith   fortranmulttransposeaddaij_(&m,x,a->i,a->j+shift,a->a+shift,y);
8775c897100SBarry Smith #else
87817ab2063SBarry Smith   for (i=0; i<m; i++) {
879416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
880416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
881416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
88217ab2063SBarry Smith     alpha = x[i];
88317ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
88417ab2063SBarry Smith   }
8855c897100SBarry Smith #endif
886b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
887ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
888ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
8893a40ed3dSBarry Smith   PetscFunctionReturn(0);
89017ab2063SBarry Smith }
89117ab2063SBarry Smith 
8924a2ae208SSatish Balay #undef __FUNCT__
8935c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
8945c897100SBarry Smith int MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8955c897100SBarry Smith {
8968d5b0100SBarry Smith   PetscScalar  zero = 0.0;
8975c897100SBarry Smith   int          ierr;
8985c897100SBarry Smith 
8995c897100SBarry Smith   PetscFunctionBegin;
9005c897100SBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
9015c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9025c897100SBarry Smith   PetscFunctionReturn(0);
9035c897100SBarry Smith }
9045c897100SBarry Smith 
9055c897100SBarry Smith 
9065c897100SBarry Smith #undef __FUNCT__
9074a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
90844cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
90917ab2063SBarry Smith {
910416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
911362ced78SSatish Balay   PetscScalar  *x,*y,*v;
912273d9f13SBarry Smith   int          ierr,m = A->m,*idx,shift = a->indexshift,*ii;
913aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
914e36a17ebSSatish Balay   int          n,i,jrow,j;
915362ced78SSatish Balay   PetscScalar  sum;
916e36a17ebSSatish Balay #endif
91717ab2063SBarry Smith 
918b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
919fee21e36SBarry Smith #pragma disjoint(*x,*y,*v)
920fee21e36SBarry Smith #endif
921fee21e36SBarry Smith 
9223a40ed3dSBarry Smith   PetscFunctionBegin;
923ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
924ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
92517ab2063SBarry Smith   x    = x + shift;    /* shift for Fortran start by 1 indexing */
926416022c9SBarry Smith   idx  = a->j;
927d64ed03dSBarry Smith   v    = a->a;
928416022c9SBarry Smith   ii   = a->i;
929aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
93029b5ca8aSSatish Balay   fortranmultaij_(&m,x,ii,idx+shift,v+shift,y);
9318d195f9aSBarry Smith #else
932d64ed03dSBarry Smith   v    += shift; /* shift for Fortran start by 1 indexing */
933d64ed03dSBarry Smith   idx  += shift;
93417ab2063SBarry Smith   for (i=0; i<m; i++) {
9359ea0dfa2SSatish Balay     jrow = ii[i];
9369ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
93717ab2063SBarry Smith     sum  = 0.0;
9389ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
9399ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9409ea0dfa2SSatish Balay      }
94117ab2063SBarry Smith     y[i] = sum;
94217ab2063SBarry Smith   }
9438d195f9aSBarry Smith #endif
944b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz - m);
945ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
946ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
9473a40ed3dSBarry Smith   PetscFunctionReturn(0);
94817ab2063SBarry Smith }
94917ab2063SBarry Smith 
9504a2ae208SSatish Balay #undef __FUNCT__
9514a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
95244cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
95317ab2063SBarry Smith {
954416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
955362ced78SSatish Balay   PetscScalar  *x,*y,*z,*v;
956273d9f13SBarry Smith   int          ierr,m = A->m,*idx,shift = a->indexshift,*ii;
957aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
958e36a17ebSSatish Balay   int          n,i,jrow,j;
959362ced78SSatish Balay PetscScalar    sum;
960e36a17ebSSatish Balay #endif
9619ea0dfa2SSatish Balay 
9623a40ed3dSBarry Smith   PetscFunctionBegin;
963ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
964ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
9652e8a6d31SBarry Smith   if (zz != yy) {
966ed480e8bSBarry Smith     ierr = VecGetArrayFast(zz,&z);CHKERRQ(ierr);
9672e8a6d31SBarry Smith   } else {
9682e8a6d31SBarry Smith     z = y;
9692e8a6d31SBarry Smith   }
97017ab2063SBarry Smith   x    = x + shift; /* shift for Fortran start by 1 indexing */
971cddf8d76SBarry Smith   idx  = a->j;
972d64ed03dSBarry Smith   v    = a->a;
973cddf8d76SBarry Smith   ii   = a->i;
974aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
97529b5ca8aSSatish Balay   fortranmultaddaij_(&m,x,ii,idx+shift,v+shift,y,z);
97602ab625aSSatish Balay #else
977d64ed03dSBarry Smith   v   += shift; /* shift for Fortran start by 1 indexing */
978d64ed03dSBarry Smith   idx += shift;
97917ab2063SBarry Smith   for (i=0; i<m; i++) {
9809ea0dfa2SSatish Balay     jrow = ii[i];
9819ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
98217ab2063SBarry Smith     sum  = y[i];
9839ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
9849ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9859ea0dfa2SSatish Balay      }
98617ab2063SBarry Smith     z[i] = sum;
98717ab2063SBarry Smith   }
98802ab625aSSatish Balay #endif
989b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
990ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
991ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
9922e8a6d31SBarry Smith   if (zz != yy) {
993ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(zz,&z);CHKERRQ(ierr);
9942e8a6d31SBarry Smith   }
9953a40ed3dSBarry Smith   PetscFunctionReturn(0);
99617ab2063SBarry Smith }
99717ab2063SBarry Smith 
99817ab2063SBarry Smith /*
99917ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
100017ab2063SBarry Smith */
10014a2ae208SSatish Balay #undef __FUNCT__
10024a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1003f1e2ffcdSBarry Smith int MatMarkDiagonal_SeqAIJ(Mat A)
100417ab2063SBarry Smith {
1005416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1006b0a32e0cSBarry Smith   int        i,j,*diag,m = A->m,shift = a->indexshift,ierr;
100717ab2063SBarry Smith 
10083a40ed3dSBarry Smith   PetscFunctionBegin;
1009f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
1010f1e2ffcdSBarry Smith 
1011b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&diag);CHKERRQ(ierr);
1012b0a32e0cSBarry Smith   PetscLogObjectMemory(A,(m+1)*sizeof(int));
1013273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
101435b0346bSBarry Smith     diag[i] = a->i[i+1];
1015416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
1016416022c9SBarry Smith       if (a->j[j]+shift == i) {
101717ab2063SBarry Smith         diag[i] = j - shift;
101817ab2063SBarry Smith         break;
101917ab2063SBarry Smith       }
102017ab2063SBarry Smith     }
102117ab2063SBarry Smith   }
1022416022c9SBarry Smith   a->diag = diag;
10233a40ed3dSBarry Smith   PetscFunctionReturn(0);
102417ab2063SBarry Smith }
102517ab2063SBarry Smith 
1026be5855fcSBarry Smith /*
1027be5855fcSBarry Smith      Checks for missing diagonals
1028be5855fcSBarry Smith */
10294a2ae208SSatish Balay #undef __FUNCT__
10304a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1031f1e2ffcdSBarry Smith int MatMissingDiagonal_SeqAIJ(Mat A)
1032be5855fcSBarry Smith {
1033be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1034f1e2ffcdSBarry Smith   int        *diag,*jj = a->j,i,shift = a->indexshift,ierr;
1035be5855fcSBarry Smith 
1036be5855fcSBarry Smith   PetscFunctionBegin;
1037f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1038f1e2ffcdSBarry Smith   diag = a->diag;
1039273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
1040be5855fcSBarry Smith     if (jj[diag[i]+shift] != i-shift) {
104129bbc08cSBarry Smith       SETERRQ1(1,"Matrix is missing diagonal number %d",i);
1042be5855fcSBarry Smith     }
1043be5855fcSBarry Smith   }
1044be5855fcSBarry Smith   PetscFunctionReturn(0);
1045be5855fcSBarry Smith }
1046be5855fcSBarry Smith 
10474a2ae208SSatish Balay #undef __FUNCT__
10484a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
1049c14dc6b6SHong Zhang int MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
105017ab2063SBarry Smith {
1051416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
1052ed480e8bSBarry Smith   PetscScalar  *x,*b,*bs, d,*xs,sum,*v = a->a,*t=0,scale,*ts,*xb,*idiag=0,*mdiag;
1053ed480e8bSBarry Smith   int          ierr,*idx,*diag,n = A->n,m = A->m,i;
105417ab2063SBarry Smith 
10553a40ed3dSBarry Smith   PetscFunctionBegin;
1056b965ef7fSBarry Smith   its = its*lits;
105791723122SBarry Smith   if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
105891723122SBarry Smith 
1059ed480e8bSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1060ed480e8bSBarry Smith   diag = a->diag;
1061ed480e8bSBarry Smith   if (!a->idiag) {
1062ed480e8bSBarry Smith     ierr     = PetscMalloc(3*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
1063ed480e8bSBarry Smith     a->ssor  = a->idiag + m;
1064ed480e8bSBarry Smith     mdiag    = a->ssor + m;
1065ed480e8bSBarry Smith 
1066ed480e8bSBarry Smith     v        = a->a;
1067ed480e8bSBarry Smith 
1068ed480e8bSBarry Smith     /* this is wrong when fshift omega changes each iteration */
1069ed480e8bSBarry Smith     if (omega == 1.0 && fshift == 0.0) {
1070ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1071ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1072ed480e8bSBarry Smith         a->idiag[i] = 1.0/v[diag[i]];
1073ed480e8bSBarry Smith       }
1074ed480e8bSBarry Smith     } else {
1075ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1076ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1077ed480e8bSBarry Smith         a->idiag[i] = omega/(fshift + v[diag[i]]);}
1078ed480e8bSBarry Smith     }
1079ed480e8bSBarry Smith 
1080ed480e8bSBarry Smith   }
1081ed480e8bSBarry Smith   t     = a->ssor;
1082ed480e8bSBarry Smith   idiag = a->idiag;
1083ed480e8bSBarry Smith   mdiag = a->idiag + 2*m;
1084ed480e8bSBarry Smith 
1085ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
1086fb2e594dSBarry Smith   if (xx != bb) {
1087ed480e8bSBarry Smith     ierr = VecGetArrayFast(bb,&b);CHKERRQ(ierr);
1088fb2e594dSBarry Smith   } else {
1089fb2e594dSBarry Smith     b = x;
1090fb2e594dSBarry Smith   }
1091fb2e594dSBarry Smith 
1092ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1093ed480e8bSBarry Smith   xs   = x;
109417ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
109517ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1096ed480e8bSBarry Smith     bs = b;
109717ab2063SBarry Smith     for (i=0; i<m; i++) {
1098ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1099416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1100ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1101ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
110217ab2063SBarry Smith         sum  = b[i]*d/omega;
110317ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
110417ab2063SBarry Smith         x[i] = sum;
110517ab2063SBarry Smith     }
1106ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1107ed480e8bSBarry Smith     if (bb != xx) {ierr = VecRestoreArrayFast(bb,&b);CHKERRQ(ierr);}
1108ed480e8bSBarry Smith     PetscLogFlops(a->nz);
11093a40ed3dSBarry Smith     PetscFunctionReturn(0);
111017ab2063SBarry Smith   }
1111c783ea89SBarry Smith 
1112ed480e8bSBarry Smith 
1113fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1114fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1115fc3d8934SBarry Smith 
1116fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1117fc3d8934SBarry Smith 
1118fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1119fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
112048af12d7SBarry Smith     is the relaxation factor.
1121fc3d8934SBarry Smith     */
1122fc3d8934SBarry Smith 
112348af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
112429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11253a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
112617ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
112717ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
112817ab2063SBarry Smith 
112917ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
113017ab2063SBarry Smith 
113117ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
113217ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
113317ab2063SBarry Smith     is the relaxation factor.
113417ab2063SBarry Smith     */
113517ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
113617ab2063SBarry Smith 
113717ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
113817ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1139416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1140ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1141ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
114217ab2063SBarry Smith       sum  = b[i];
114317ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1144ed480e8bSBarry Smith       x[i] = sum*idiag[i];
114517ab2063SBarry Smith     }
114617ab2063SBarry Smith 
114717ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1148416022c9SBarry Smith     v = a->a;
1149ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
115017ab2063SBarry Smith 
115117ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1152ed480e8bSBarry Smith     ts = t;
1153416022c9SBarry Smith     diag = a->diag;
115417ab2063SBarry Smith     for (i=0; i<m; i++) {
1155416022c9SBarry Smith       n    = diag[i] - a->i[i];
1156ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1157ed480e8bSBarry Smith       v    = a->a + a->i[i];
115817ab2063SBarry Smith       sum  = t[i];
115917ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1160ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1161733d66baSBarry Smith       /*  x = x + t */
1162733d66baSBarry Smith       x[i] += t[i];
116317ab2063SBarry Smith     }
116417ab2063SBarry Smith 
1165b0a32e0cSBarry Smith     PetscLogFlops(6*m-1 + 2*a->nz);
1166ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1167ed480e8bSBarry Smith     if (bb != xx) {ierr = VecRestoreArrayFast(bb,&b);CHKERRQ(ierr);}
11683a40ed3dSBarry Smith     PetscFunctionReturn(0);
116917ab2063SBarry Smith   }
117017ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
117117ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
117277d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
117377d8c4bbSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,diag,a->a,b);
117477d8c4bbSBarry Smith #else
117517ab2063SBarry Smith       for (i=0; i<m; i++) {
1176416022c9SBarry Smith         n    = diag[i] - a->i[i];
1177ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1178ed480e8bSBarry Smith         v    = a->a + a->i[i];
117917ab2063SBarry Smith         sum  = b[i];
118017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1181ed480e8bSBarry Smith         x[i] = sum*idiag[i];
118217ab2063SBarry Smith       }
118377d8c4bbSBarry Smith #endif
118417ab2063SBarry Smith       xb = x;
1185ed480e8bSBarry Smith       PetscLogFlops(a->nz);
11863a40ed3dSBarry Smith     } else xb = b;
118717ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
118817ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
118917ab2063SBarry Smith       for (i=0; i<m; i++) {
1190ed480e8bSBarry Smith         x[i] *= mdiag[i];
119117ab2063SBarry Smith       }
1192b0a32e0cSBarry Smith       PetscLogFlops(m);
119317ab2063SBarry Smith     }
119417ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
119577d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
119677d8c4bbSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,diag,a->a,xb);
119777d8c4bbSBarry Smith #else
119817ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1199416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1200ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1201ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
120217ab2063SBarry Smith         sum  = xb[i];
120317ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1204ed480e8bSBarry Smith         x[i] = sum*idiag[i];
120517ab2063SBarry Smith       }
120677d8c4bbSBarry Smith #endif
1207ed480e8bSBarry Smith       PetscLogFlops(a->nz);
120817ab2063SBarry Smith     }
120917ab2063SBarry Smith     its--;
121017ab2063SBarry Smith   }
121117ab2063SBarry Smith   while (its--) {
121217ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
121377d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
121477d8c4bbSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,diag,a->a,b);
121577d8c4bbSBarry Smith #else
121617ab2063SBarry Smith       for (i=0; i<m; i++) {
1217ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1218416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1219ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1220ed480e8bSBarry Smith         v    = a->a + a->i[i];
122117ab2063SBarry Smith         sum  = b[i];
122217ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1223ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
122417ab2063SBarry Smith       }
122577d8c4bbSBarry Smith #endif
1226ed480e8bSBarry Smith       PetscLogFlops(a->nz);
122717ab2063SBarry Smith     }
122817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
122977d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
123077d8c4bbSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,diag,a->a,b);
123177d8c4bbSBarry Smith #else
123217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1233ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1234416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1235ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1236ed480e8bSBarry Smith         v    = a->a + a->i[i];
123717ab2063SBarry Smith         sum  = b[i];
123817ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1239ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
124017ab2063SBarry Smith       }
124177d8c4bbSBarry Smith #endif
1242ed480e8bSBarry Smith       PetscLogFlops(a->nz);
124317ab2063SBarry Smith     }
124417ab2063SBarry Smith   }
1245ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1246ed480e8bSBarry Smith   if (bb != xx) {ierr = VecRestoreArrayFast(bb,&b);CHKERRQ(ierr);}
12473a40ed3dSBarry Smith   PetscFunctionReturn(0);
124817ab2063SBarry Smith }
124917ab2063SBarry Smith 
12504a2ae208SSatish Balay #undef __FUNCT__
12514a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
12528f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
125317ab2063SBarry Smith {
1254416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12554e220ebcSLois Curfman McInnes 
12563a40ed3dSBarry Smith   PetscFunctionBegin;
1257273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1258273d9f13SBarry Smith   info->columns_global = (double)A->n;
1259273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1260273d9f13SBarry Smith   info->columns_local  = (double)A->n;
12614e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12624e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12634e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12644e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12654e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12664e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12674e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12684e220ebcSLois Curfman McInnes   if (A->factor) {
12694e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12704e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12714e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12724e220ebcSLois Curfman McInnes   } else {
12734e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12744e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12754e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12764e220ebcSLois Curfman McInnes   }
12773a40ed3dSBarry Smith   PetscFunctionReturn(0);
127817ab2063SBarry Smith }
127917ab2063SBarry Smith 
1280b380c88cSHong Zhang EXTERN int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,MatFactorInfo*,Mat*);
1281ca44d042SBarry Smith EXTERN int MatLUFactorNumeric_SeqAIJ(Mat,Mat*);
1282b380c88cSHong Zhang EXTERN int MatLUFactor_SeqAIJ(Mat,IS,IS,MatFactorInfo*);
1283ca44d042SBarry Smith EXTERN int MatSolve_SeqAIJ(Mat,Vec,Vec);
1284ca44d042SBarry Smith EXTERN int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
1285ca44d042SBarry Smith EXTERN int MatSolveTranspose_SeqAIJ(Mat,Vec,Vec);
1286ca44d042SBarry Smith EXTERN int MatSolveTransposeAdd_SeqAIJ(Mat,Vec,Vec,Vec);
128717ab2063SBarry Smith 
12884a2ae208SSatish Balay #undef __FUNCT__
12894a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
129087828ca2SBarry Smith int MatZeroRows_SeqAIJ(Mat A,IS is,PetscScalar *diag)
129117ab2063SBarry Smith {
1292416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1293273d9f13SBarry Smith   int         i,ierr,N,*rows,m = A->m - 1,shift = a->indexshift;
129417ab2063SBarry Smith 
12953a40ed3dSBarry Smith   PetscFunctionBegin;
1296b9b97703SBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
129717ab2063SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
1298f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1299f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
130029bbc08cSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
130187828ca2SBarry Smith       ierr = PetscMemzero(&a->a[a->i[rows[i]]+shift],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1302f1e2ffcdSBarry Smith     }
1303f1e2ffcdSBarry Smith     if (diag) {
1304f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1305f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1306f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1307f1e2ffcdSBarry Smith         a->a[a->diag[rows[i]]] = *diag;
1308f1e2ffcdSBarry Smith       }
1309f1e2ffcdSBarry Smith     }
1310f1e2ffcdSBarry Smith   } else {
131117ab2063SBarry Smith     if (diag) {
131217ab2063SBarry Smith       for (i=0; i<N; i++) {
131329bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
13147ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1315416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1316416022c9SBarry Smith           a->a[a->i[rows[i]]+shift] = *diag;
1317416022c9SBarry Smith           a->j[a->i[rows[i]]+shift] = rows[i]+shift;
13187ae801bdSBarry Smith         } else { /* in case row was completely empty */
1319d64ed03dSBarry Smith           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
132017ab2063SBarry Smith         }
132117ab2063SBarry Smith       }
13223a40ed3dSBarry Smith     } else {
132317ab2063SBarry Smith       for (i=0; i<N; i++) {
132429bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
1325416022c9SBarry Smith         a->ilen[rows[i]] = 0;
132617ab2063SBarry Smith       }
132717ab2063SBarry Smith     }
1328f1e2ffcdSBarry Smith   }
13297ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
133043a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13313a40ed3dSBarry Smith   PetscFunctionReturn(0);
133217ab2063SBarry Smith }
133317ab2063SBarry Smith 
13344a2ae208SSatish Balay #undef __FUNCT__
13354a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
133687828ca2SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v)
133717ab2063SBarry Smith {
1338416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1339b0a32e0cSBarry Smith   int        *itmp,i,shift = a->indexshift,ierr;
134017ab2063SBarry Smith 
13413a40ed3dSBarry Smith   PetscFunctionBegin;
1342273d9f13SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %d out of range",row);
134317ab2063SBarry Smith 
1344416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1345416022c9SBarry Smith   if (v) *v = a->a + a->i[row] + shift;
134617ab2063SBarry Smith   if (idx) {
1347416022c9SBarry Smith     itmp = a->j + a->i[row] + shift;
13484e093b46SBarry Smith     if (*nz && shift) {
1349b0a32e0cSBarry Smith       ierr = PetscMalloc((*nz)*sizeof(int),idx);CHKERRQ(ierr);
135017ab2063SBarry Smith       for (i=0; i<(*nz); i++) {(*idx)[i] = itmp[i] + shift;}
13514e093b46SBarry Smith     } else if (*nz) {
13524e093b46SBarry Smith       *idx = itmp;
135317ab2063SBarry Smith     }
135417ab2063SBarry Smith     else *idx = 0;
135517ab2063SBarry Smith   }
13563a40ed3dSBarry Smith   PetscFunctionReturn(0);
135717ab2063SBarry Smith }
135817ab2063SBarry Smith 
13594a2ae208SSatish Balay #undef __FUNCT__
13604a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
136187828ca2SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v)
136217ab2063SBarry Smith {
13634e093b46SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1364606d414cSSatish Balay   int ierr;
13653a40ed3dSBarry Smith 
13663a40ed3dSBarry Smith   PetscFunctionBegin;
1367606d414cSSatish Balay   if (idx) {if (*idx && a->indexshift) {ierr = PetscFree(*idx);CHKERRQ(ierr);}}
13683a40ed3dSBarry Smith   PetscFunctionReturn(0);
136917ab2063SBarry Smith }
137017ab2063SBarry Smith 
13714a2ae208SSatish Balay #undef __FUNCT__
13724a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1373064f8208SBarry Smith int MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
137417ab2063SBarry Smith {
1375416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
137687828ca2SBarry Smith   PetscScalar  *v = a->a;
137736db0b34SBarry Smith   PetscReal    sum = 0.0;
1378549d3d68SSatish Balay   int          i,j,shift = a->indexshift,ierr;
137917ab2063SBarry Smith 
13803a40ed3dSBarry Smith   PetscFunctionBegin;
138117ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1382416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1383aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
138436db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
138517ab2063SBarry Smith #else
138617ab2063SBarry Smith       sum += (*v)*(*v); v++;
138717ab2063SBarry Smith #endif
138817ab2063SBarry Smith     }
1389064f8208SBarry Smith     *nrm = sqrt(sum);
13903a40ed3dSBarry Smith   } else if (type == NORM_1) {
139136db0b34SBarry Smith     PetscReal *tmp;
1392416022c9SBarry Smith     int    *jj = a->j;
1393b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1394273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
1395064f8208SBarry Smith     *nrm = 0.0;
1396416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1397a2744918SBarry Smith         tmp[*jj++ + shift] += PetscAbsScalar(*v);  v++;
139817ab2063SBarry Smith     }
1399273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
1400064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
140117ab2063SBarry Smith     }
1402606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14033a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1404064f8208SBarry Smith     *nrm = 0.0;
1405273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1406416022c9SBarry Smith       v = a->a + a->i[j] + shift;
140717ab2063SBarry Smith       sum = 0.0;
1408416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1409cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
141017ab2063SBarry Smith       }
1411064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
141217ab2063SBarry Smith     }
14133a40ed3dSBarry Smith   } else {
141429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
141517ab2063SBarry Smith   }
14163a40ed3dSBarry Smith   PetscFunctionReturn(0);
141717ab2063SBarry Smith }
141817ab2063SBarry Smith 
14194a2ae208SSatish Balay #undef __FUNCT__
14204a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
14218f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
142217ab2063SBarry Smith {
1423416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
1424416022c9SBarry Smith   Mat          C;
1425273d9f13SBarry Smith   int          i,ierr,*aj = a->j,*ai = a->i,m = A->m,len,*col;
1426273d9f13SBarry Smith   int          shift = a->indexshift;
142787828ca2SBarry Smith   PetscScalar  *array = a->a;
142817ab2063SBarry Smith 
14293a40ed3dSBarry Smith   PetscFunctionBegin;
1430273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1431b0a32e0cSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(int),&col);CHKERRQ(ierr);
1432273d9f13SBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(int));CHKERRQ(ierr);
143317ab2063SBarry Smith   if (shift) {
143417ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] -= 1;
143517ab2063SBarry Smith   }
143617ab2063SBarry Smith   for (i=0; i<ai[m]+shift; i++) col[aj[i]] += 1;
1437273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,A->n,m,0,col,&C);CHKERRQ(ierr);
1438606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
143917ab2063SBarry Smith   for (i=0; i<m; i++) {
144017ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1441416022c9SBarry Smith     ierr   = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1442b9b97703SBarry Smith     array += len;
1443b9b97703SBarry Smith     aj    += len;
144417ab2063SBarry Smith   }
144517ab2063SBarry Smith   if (shift) {
144617ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] += 1;
144717ab2063SBarry Smith   }
144817ab2063SBarry Smith 
14496d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14506d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
145117ab2063SBarry Smith 
1452f1e2ffcdSBarry Smith   if (B) {
1453416022c9SBarry Smith     *B = C;
145417ab2063SBarry Smith   } else {
1455273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
145617ab2063SBarry Smith   }
14573a40ed3dSBarry Smith   PetscFunctionReturn(0);
145817ab2063SBarry Smith }
145917ab2063SBarry Smith 
14604a2ae208SSatish Balay #undef __FUNCT__
14614a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
14628f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
146317ab2063SBarry Smith {
1464416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
146587828ca2SBarry Smith   PetscScalar  *l,*r,x,*v;
1466273d9f13SBarry Smith   int          ierr,i,j,m = A->m,n = A->n,M,nz = a->nz,*jj,shift = a->indexshift;
146717ab2063SBarry Smith 
14683a40ed3dSBarry Smith   PetscFunctionBegin;
146917ab2063SBarry Smith   if (ll) {
14703ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14713ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1472e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1473273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
1474ed480e8bSBarry Smith     ierr = VecGetArrayFast(ll,&l);CHKERRQ(ierr);
1475416022c9SBarry Smith     v = a->a;
147617ab2063SBarry Smith     for (i=0; i<m; i++) {
147717ab2063SBarry Smith       x = l[i];
1478416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
147917ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
148017ab2063SBarry Smith     }
1481ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(ll,&l);CHKERRQ(ierr);
1482b0a32e0cSBarry Smith     PetscLogFlops(nz);
148317ab2063SBarry Smith   }
148417ab2063SBarry Smith   if (rr) {
1485e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1486273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
1487ed480e8bSBarry Smith     ierr = VecGetArrayFast(rr,&r);CHKERRQ(ierr);
1488416022c9SBarry Smith     v = a->a; jj = a->j;
148917ab2063SBarry Smith     for (i=0; i<nz; i++) {
149017ab2063SBarry Smith       (*v++) *= r[*jj++ + shift];
149117ab2063SBarry Smith     }
1492ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(rr,&r);CHKERRQ(ierr);
1493b0a32e0cSBarry Smith     PetscLogFlops(nz);
149417ab2063SBarry Smith   }
14953a40ed3dSBarry Smith   PetscFunctionReturn(0);
149617ab2063SBarry Smith }
149717ab2063SBarry Smith 
14984a2ae208SSatish Balay #undef __FUNCT__
14994a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
15007b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B)
150117ab2063SBarry Smith {
1502db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data,*c;
1503273d9f13SBarry Smith   int          *smap,i,k,kstart,kend,ierr,oldcols = A->n,*lens;
150436db0b34SBarry Smith   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
150599141d43SSatish Balay   int          *irow,*icol,nrows,ncols,shift = a->indexshift,*ssmap;
150699141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
150787828ca2SBarry Smith   PetscScalar  *a_new,*mat_a;
1508416022c9SBarry Smith   Mat          C;
1509fee21e36SBarry Smith   PetscTruth   stride;
151017ab2063SBarry Smith 
15113a40ed3dSBarry Smith   PetscFunctionBegin;
1512d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
151329bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1514d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
151529bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
151699141d43SSatish Balay 
151717ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1518b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1519b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
152017ab2063SBarry Smith 
1521fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1522fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1523fee21e36SBarry Smith   if (stride && step == 1) {
152402834360SBarry Smith     /* special case of contiguous rows */
152531ebf83bSSatish Balay     ierr   = PetscMalloc((2*nrows+1)*sizeof(int),&lens);CHKERRQ(ierr);
152631ebf83bSSatish Balay     starts = lens + nrows;
152702834360SBarry Smith     /* loop over new rows determining lens and starting points */
152802834360SBarry Smith     for (i=0; i<nrows; i++) {
1529a2744918SBarry Smith       kstart  = ai[irow[i]]+shift;
1530a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
153102834360SBarry Smith       for (k=kstart; k<kend; k++) {
1532d8ced48eSBarry Smith         if (aj[k]+shift >= first) {
153302834360SBarry Smith           starts[i] = k;
153402834360SBarry Smith           break;
153502834360SBarry Smith 	}
153602834360SBarry Smith       }
1537a2744918SBarry Smith       sum = 0;
153802834360SBarry Smith       while (k < kend) {
1539d8ced48eSBarry Smith         if (aj[k++]+shift >= first+ncols) break;
1540a2744918SBarry Smith         sum++;
154102834360SBarry Smith       }
1542a2744918SBarry Smith       lens[i] = sum;
154302834360SBarry Smith     }
154402834360SBarry Smith     /* create submatrix */
1545cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
154608480c60SBarry Smith       int n_cols,n_rows;
154708480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
154829bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1549d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
155008480c60SBarry Smith       C = *B;
15513a40ed3dSBarry Smith     } else {
155202834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
155308480c60SBarry Smith     }
1554db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1555db02288aSLois Curfman McInnes 
155602834360SBarry Smith     /* loop over rows inserting into submatrix */
1557db02288aSLois Curfman McInnes     a_new    = c->a;
1558db02288aSLois Curfman McInnes     j_new    = c->j;
1559db02288aSLois Curfman McInnes     i_new    = c->i;
1560db02288aSLois Curfman McInnes     i_new[0] = -shift;
156102834360SBarry Smith     for (i=0; i<nrows; i++) {
1562a2744918SBarry Smith       ii    = starts[i];
1563a2744918SBarry Smith       lensi = lens[i];
1564a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1565a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
156602834360SBarry Smith       }
156787828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1568a2744918SBarry Smith       a_new      += lensi;
1569a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1570a2744918SBarry Smith       c->ilen[i]  = lensi;
157102834360SBarry Smith     }
1572606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15733a40ed3dSBarry Smith   } else {
157402834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
1575b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(int),&smap);CHKERRQ(ierr);
157602834360SBarry Smith     ssmap = smap + shift;
1577b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(int),&lens);CHKERRQ(ierr);
1578549d3d68SSatish Balay     ierr  = PetscMemzero(smap,oldcols*sizeof(int));CHKERRQ(ierr);
157917ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
158002834360SBarry Smith     /* determine lens of each row */
158102834360SBarry Smith     for (i=0; i<nrows; i++) {
1582d8ced48eSBarry Smith       kstart  = ai[irow[i]]+shift;
158302834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
158402834360SBarry Smith       lens[i] = 0;
158502834360SBarry Smith       for (k=kstart; k<kend; k++) {
1586d8ced48eSBarry Smith         if (ssmap[aj[k]]) {
158702834360SBarry Smith           lens[i]++;
158802834360SBarry Smith         }
158902834360SBarry Smith       }
159002834360SBarry Smith     }
159117ab2063SBarry Smith     /* Create and fill new matrix */
1592a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
15930f5bd95cSBarry Smith       PetscTruth equal;
15940f5bd95cSBarry Smith 
159599141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1596273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1597273d9f13SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(int),&equal);CHKERRQ(ierr);
15980f5bd95cSBarry Smith       if (!equal) {
159929bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
160099141d43SSatish Balay       }
1601273d9f13SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(int));CHKERRQ(ierr);
160208480c60SBarry Smith       C = *B;
16033a40ed3dSBarry Smith     } else {
160402834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
160508480c60SBarry Smith     }
160699141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
160717ab2063SBarry Smith     for (i=0; i<nrows; i++) {
160899141d43SSatish Balay       row    = irow[i];
160999141d43SSatish Balay       kstart = ai[row]+shift;
161099141d43SSatish Balay       kend   = kstart + a->ilen[row];
161199141d43SSatish Balay       mat_i  = c->i[i]+shift;
161299141d43SSatish Balay       mat_j  = c->j + mat_i;
161399141d43SSatish Balay       mat_a  = c->a + mat_i;
161499141d43SSatish Balay       mat_ilen = c->ilen + i;
161517ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
161699141d43SSatish Balay         if ((tcol=ssmap[a->j[k]])) {
1617ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
161899141d43SSatish Balay           *mat_a++ = a->a[k];
161999141d43SSatish Balay           (*mat_ilen)++;
162099141d43SSatish Balay 
162117ab2063SBarry Smith         }
162217ab2063SBarry Smith       }
162317ab2063SBarry Smith     }
162402834360SBarry Smith     /* Free work space */
162502834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1626606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1627606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
162802834360SBarry Smith   }
16296d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16306d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
163117ab2063SBarry Smith 
163217ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1633416022c9SBarry Smith   *B = C;
16343a40ed3dSBarry Smith   PetscFunctionReturn(0);
163517ab2063SBarry Smith }
163617ab2063SBarry Smith 
1637a871dcd8SBarry Smith /*
1638a871dcd8SBarry Smith */
16394a2ae208SSatish Balay #undef __FUNCT__
16404a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1641b380c88cSHong Zhang int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1642a871dcd8SBarry Smith {
164363b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
164408480c60SBarry Smith   int        ierr;
164563b91edcSBarry Smith   Mat        outA;
1646b8a78c4aSBarry Smith   PetscTruth row_identity,col_identity;
164763b91edcSBarry Smith 
16483a40ed3dSBarry Smith   PetscFunctionBegin;
1649d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1650b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1651b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1652b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
165329bbc08cSBarry Smith     SETERRQ(1,"Row and column permutations must be identity for in-place ILU");
1654b8a78c4aSBarry Smith   }
1655a871dcd8SBarry Smith 
165663b91edcSBarry Smith   outA          = inA;
165763b91edcSBarry Smith   inA->factor   = FACTOR_LU;
165863b91edcSBarry Smith   a->row        = row;
165963b91edcSBarry Smith   a->col        = col;
1660c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1661c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
166263b91edcSBarry Smith 
166336db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1664b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
16654c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
1666b0a32e0cSBarry Smith   PetscLogObjectParent(inA,a->icol);
1667f0ec6fceSSatish Balay 
166894a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
166987828ca2SBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
167094a9d846SBarry Smith   }
167163b91edcSBarry Smith 
167208480c60SBarry Smith   if (!a->diag) {
1673f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
167463b91edcSBarry Smith   }
167563b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA);CHKERRQ(ierr);
16763a40ed3dSBarry Smith   PetscFunctionReturn(0);
1677a871dcd8SBarry Smith }
1678a871dcd8SBarry Smith 
1679d9eff348SSatish Balay #include "petscblaslapack.h"
16804a2ae208SSatish Balay #undef __FUNCT__
16814a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
168287828ca2SBarry Smith int MatScale_SeqAIJ(PetscScalar *alpha,Mat inA)
1683f0b747eeSBarry Smith {
1684f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
1685f0b747eeSBarry Smith   int        one = 1;
16863a40ed3dSBarry Smith 
16873a40ed3dSBarry Smith   PetscFunctionBegin;
1688f0b747eeSBarry Smith   BLscal_(&a->nz,alpha,a->a,&one);
1689b0a32e0cSBarry Smith   PetscLogFlops(a->nz);
16903a40ed3dSBarry Smith   PetscFunctionReturn(0);
1691f0b747eeSBarry Smith }
1692f0b747eeSBarry Smith 
16934a2ae208SSatish Balay #undef __FUNCT__
16944a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
16957b2a1423SBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n,IS *irow,IS *icol,MatReuse scall,Mat **B)
1696cddf8d76SBarry Smith {
1697cddf8d76SBarry Smith   int ierr,i;
1698cddf8d76SBarry Smith 
16993a40ed3dSBarry Smith   PetscFunctionBegin;
1700cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1701b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1702cddf8d76SBarry Smith   }
1703cddf8d76SBarry Smith 
1704cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17056a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1706cddf8d76SBarry Smith   }
17073a40ed3dSBarry Smith   PetscFunctionReturn(0);
1708cddf8d76SBarry Smith }
1709cddf8d76SBarry Smith 
17104a2ae208SSatish Balay #undef __FUNCT__
17114a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_SeqAIJ"
17128f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A,int *bs)
17135a838052SSatish Balay {
1714f830108cSBarry Smith   PetscFunctionBegin;
17155a838052SSatish Balay   *bs = 1;
17163a40ed3dSBarry Smith   PetscFunctionReturn(0);
17175a838052SSatish Balay }
17185a838052SSatish Balay 
17194a2ae208SSatish Balay #undef __FUNCT__
17204a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
17218f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A,int is_max,IS *is,int ov)
17224dcbc457SBarry Smith {
1723e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
172406763907SSatish Balay   int        shift,row,i,j,k,l,m,n,*idx,ierr,*nidx,isz,val;
17258a047759SSatish Balay   int        start,end,*ai,*aj;
1726f1af5d2fSBarry Smith   PetscBT    table;
1727bbd702dbSSatish Balay 
17283a40ed3dSBarry Smith   PetscFunctionBegin;
17298a047759SSatish Balay   shift = a->indexshift;
1730273d9f13SBarry Smith   m     = A->m;
1731e4d965acSSatish Balay   ai    = a->i;
17328a047759SSatish Balay   aj    = a->j+shift;
17338a047759SSatish Balay 
173429bbc08cSBarry Smith   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal overlap value used");
173506763907SSatish Balay 
1736b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&nidx);CHKERRQ(ierr);
17376831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
173806763907SSatish Balay 
1739e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1740b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1741e4d965acSSatish Balay     isz  = 0;
17426831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1743e4d965acSSatish Balay 
1744e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17454dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1746b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1747e4d965acSSatish Balay 
1748dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1749e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1750f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
17514dcbc457SBarry Smith     }
175206763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
175306763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1754e4d965acSSatish Balay 
175504a348a9SBarry Smith     k = 0;
175604a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
175704a348a9SBarry Smith       n = isz;
175806763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1759e4d965acSSatish Balay         row   = nidx[k];
1760e4d965acSSatish Balay         start = ai[row];
1761e4d965acSSatish Balay         end   = ai[row+1];
176204a348a9SBarry Smith         for (l = start; l<end ; l++){
17638a047759SSatish Balay           val = aj[l] + shift;
1764f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1765e4d965acSSatish Balay         }
1766e4d965acSSatish Balay       }
1767e4d965acSSatish Balay     }
1768029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1769e4d965acSSatish Balay   }
17706831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1771606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17723a40ed3dSBarry Smith   PetscFunctionReturn(0);
17734dcbc457SBarry Smith }
177417ab2063SBarry Smith 
17750513a670SBarry Smith /* -------------------------------------------------------------- */
17764a2ae208SSatish Balay #undef __FUNCT__
17774a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
17780513a670SBarry Smith int MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
17790513a670SBarry Smith {
17800513a670SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
178187828ca2SBarry Smith   PetscScalar  *vwork;
1782273d9f13SBarry Smith   int          i,ierr,nz,m = A->m,n = A->n,*cwork;
17830513a670SBarry Smith   int          *row,*col,*cnew,j,*lens;
178456cd22aeSBarry Smith   IS           icolp,irowp;
17850513a670SBarry Smith 
17863a40ed3dSBarry Smith   PetscFunctionBegin;
17874c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
178856cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
17894c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
179056cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
17910513a670SBarry Smith 
17920513a670SBarry Smith   /* determine lengths of permuted rows */
1793b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&lens);CHKERRQ(ierr);
17940513a670SBarry Smith   for (i=0; i<m; i++) {
17950513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
17960513a670SBarry Smith   }
17970513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
1798606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
17990513a670SBarry Smith 
1800b0a32e0cSBarry Smith   ierr = PetscMalloc(n*sizeof(int),&cnew);CHKERRQ(ierr);
18010513a670SBarry Smith   for (i=0; i<m; i++) {
18020513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18030513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
18040513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
18050513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18060513a670SBarry Smith   }
1807606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18080513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18090513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
181056cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
181156cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
181256cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
181356cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18143a40ed3dSBarry Smith   PetscFunctionReturn(0);
18150513a670SBarry Smith }
18160513a670SBarry Smith 
18174a2ae208SSatish Balay #undef __FUNCT__
18184a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1819682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1820682d7d0cSBarry Smith {
1821c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1822682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1823d132466eSBarry Smith   int               ierr;
1824682d7d0cSBarry Smith 
18253a40ed3dSBarry Smith   PetscFunctionBegin;
1826c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1827d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1828d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1829d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
1830d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr);
1831d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr);
1832aa482453SBarry Smith #if defined(PETSC_HAVE_ESSL)
1833d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");CHKERRQ(ierr);
1834682d7d0cSBarry Smith #endif
1835cd5578b5SBarry Smith #if defined(PETSC_HAVE_LUSOL)
1836cd5578b5SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_lusol: Use the Stanford LUSOL sparse factorization and solve.\n");CHKERRQ(ierr);
1837cd5578b5SBarry Smith #endif
183887828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1839ca44d042SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_matlab: Use Matlab engine sparse LU factorization and solve.\n");CHKERRQ(ierr);
1840ca44d042SBarry Smith #endif
18413a40ed3dSBarry Smith   PetscFunctionReturn(0);
1842682d7d0cSBarry Smith }
1843ca44d042SBarry Smith EXTERN int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg);
1844ca44d042SBarry Smith EXTERN int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
1845b380c88cSHong Zhang EXTERN int MatILUDTFactor_SeqAIJ(Mat,MatFactorInfo*,IS,IS,Mat*);
18464a2ae208SSatish Balay #undef __FUNCT__
18474a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1848cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1849cb5b572fSBarry Smith {
1850be6bf707SBarry Smith   int        ierr;
1851273d9f13SBarry Smith   PetscTruth flg;
1852cb5b572fSBarry Smith 
1853cb5b572fSBarry Smith   PetscFunctionBegin;
1854273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg);CHKERRQ(ierr);
1855273d9f13SBarry Smith   if (str == SAME_NONZERO_PATTERN && flg) {
1856be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1857be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1858be6bf707SBarry Smith 
1859273d9f13SBarry Smith     if (a->i[A->m]+a->indexshift != b->i[B->m]+a->indexshift) {
186029bbc08cSBarry Smith       SETERRQ(1,"Number of nonzeros in two matrices are different");
1861cb5b572fSBarry Smith     }
186287828ca2SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m]+a->indexshift)*sizeof(PetscScalar));CHKERRQ(ierr);
1863cb5b572fSBarry Smith   } else {
1864cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1865cb5b572fSBarry Smith   }
1866cb5b572fSBarry Smith   PetscFunctionReturn(0);
1867cb5b572fSBarry Smith }
1868cb5b572fSBarry Smith 
18694a2ae208SSatish Balay #undef __FUNCT__
18704a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1871273d9f13SBarry Smith int MatSetUpPreallocation_SeqAIJ(Mat A)
1872273d9f13SBarry Smith {
1873273d9f13SBarry Smith   int        ierr;
1874273d9f13SBarry Smith 
1875273d9f13SBarry Smith   PetscFunctionBegin;
1876273d9f13SBarry Smith   ierr =  MatSeqAIJSetPreallocation(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1877273d9f13SBarry Smith   PetscFunctionReturn(0);
1878273d9f13SBarry Smith }
1879273d9f13SBarry Smith 
18804a2ae208SSatish Balay #undef __FUNCT__
18814a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
188287828ca2SBarry Smith int MatGetArray_SeqAIJ(Mat A,PetscScalar **array)
18836c0721eeSBarry Smith {
18846c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
18856c0721eeSBarry Smith   PetscFunctionBegin;
18866c0721eeSBarry Smith   *array = a->a;
18876c0721eeSBarry Smith   PetscFunctionReturn(0);
18886c0721eeSBarry Smith }
18896c0721eeSBarry Smith 
18904a2ae208SSatish Balay #undef __FUNCT__
18914a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
189287828ca2SBarry Smith int MatRestoreArray_SeqAIJ(Mat A,PetscScalar **array)
18936c0721eeSBarry Smith {
18946c0721eeSBarry Smith   PetscFunctionBegin;
18956c0721eeSBarry Smith   PetscFunctionReturn(0);
18966c0721eeSBarry Smith }
1897273d9f13SBarry Smith 
1898ee4f033dSBarry Smith #undef __FUNCT__
1899ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1900ee4f033dSBarry Smith int MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1901ee4f033dSBarry Smith {
1902ee4f033dSBarry Smith   int           (*f)(void *,Vec,Vec,void*) = (int (*)(void *,Vec,Vec,void *))coloring->f;
1903ee4f033dSBarry Smith   int           k,ierr,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
190487828ca2SBarry Smith   PetscScalar   dx,mone = -1.0,*y,*xx,*w3_array;
190587828ca2SBarry Smith   PetscScalar   *vscale_array;
1906ee4f033dSBarry Smith   PetscReal     epsilon = coloring->error_rel,umin = coloring->umin;
1907ee4f033dSBarry Smith   Vec           w1,w2,w3;
1908ee4f033dSBarry Smith   void          *fctx = coloring->fctx;
1909ee4f033dSBarry Smith   PetscTruth    flg;
1910ee4f033dSBarry Smith 
1911ee4f033dSBarry Smith   PetscFunctionBegin;
1912ee4f033dSBarry Smith   if (!coloring->w1) {
1913ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
1914ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w1);
1915ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
1916ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w2);
1917ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
1918ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w3);
1919ee4f033dSBarry Smith   }
1920ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1921ee4f033dSBarry Smith 
1922ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1923e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1924ee4f033dSBarry Smith   if (flg) {
1925ee4f033dSBarry Smith     PetscLogInfo(coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n");
1926ee4f033dSBarry Smith   } else {
1927ee4f033dSBarry Smith     ierr = MatZeroEntries(J);CHKERRQ(ierr);
1928ee4f033dSBarry Smith   }
1929ee4f033dSBarry Smith 
1930ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1931ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1932ee4f033dSBarry Smith 
1933ee4f033dSBarry Smith   /*
1934ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1935ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1936ee4f033dSBarry Smith   */
1937ee4f033dSBarry Smith   if (coloring->F) {
1938ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1939ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1940ee4f033dSBarry Smith     if (m1 != m2) {
1941ee4f033dSBarry Smith       coloring->F = 0;
1942ee4f033dSBarry Smith     }
1943ee4f033dSBarry Smith   }
1944ee4f033dSBarry Smith 
1945ee4f033dSBarry Smith   if (coloring->F) {
1946ee4f033dSBarry Smith     w1          = coloring->F;
1947ee4f033dSBarry Smith     coloring->F = 0;
1948ee4f033dSBarry Smith   } else {
194966f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1950ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
195166f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1952ee4f033dSBarry Smith   }
1953ee4f033dSBarry Smith 
1954ee4f033dSBarry Smith   /*
1955ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
1956ee4f033dSBarry Smith   */
1957ed480e8bSBarry Smith   ierr = VecGetArrayFast(x1,&xx);CHKERRQ(ierr);xx = xx - start;
1958ed480e8bSBarry Smith   ierr = VecGetArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
1959ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
1960ee4f033dSBarry Smith     /*
1961ee4f033dSBarry Smith        Loop over each column associated with color adding the
1962ee4f033dSBarry Smith        perturbation to the vector w3.
1963ee4f033dSBarry Smith     */
1964ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1965ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1966ee4f033dSBarry Smith       dx  = xx[col];
1967ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1968ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1969ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1970ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1971ee4f033dSBarry Smith #else
1972ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1973ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1974ee4f033dSBarry Smith #endif
1975ee4f033dSBarry Smith       dx                *= epsilon;
1976ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
1977ee4f033dSBarry Smith     }
1978ee4f033dSBarry Smith   }
1979ed480e8bSBarry Smith   vscale_array = vscale_array + start;ierr = VecRestoreArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1980ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1981ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1982ee4f033dSBarry Smith 
1983ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
1984ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
1985ee4f033dSBarry Smith 
1986ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
1987ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
1988ee4f033dSBarry Smith 
1989ed480e8bSBarry Smith   ierr = VecGetArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1990ee4f033dSBarry Smith   /*
1991ee4f033dSBarry Smith       Loop over each color
1992ee4f033dSBarry Smith   */
1993ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
199449b058dcSBarry Smith     coloring->currentcolor = k;
1995ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
1996ed480e8bSBarry Smith     ierr = VecGetArrayFast(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
1997ee4f033dSBarry Smith     /*
1998ee4f033dSBarry Smith        Loop over each column associated with color adding the
1999ee4f033dSBarry Smith        perturbation to the vector w3.
2000ee4f033dSBarry Smith     */
2001ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2002ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2003ee4f033dSBarry Smith       dx  = xx[col];
2004ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2005ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2006ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2007ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2008ee4f033dSBarry Smith #else
2009ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2010ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2011ee4f033dSBarry Smith #endif
2012ee4f033dSBarry Smith       dx            *= epsilon;
2013ee4f033dSBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(1,"Computed 0 differencing parameter");
2014ee4f033dSBarry Smith       w3_array[col] += dx;
2015ee4f033dSBarry Smith     }
2016ed480e8bSBarry Smith     w3_array = w3_array + start; ierr = VecRestoreArrayFast(w3,&w3_array);CHKERRQ(ierr);
2017ee4f033dSBarry Smith 
2018ee4f033dSBarry Smith     /*
2019ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2020ee4f033dSBarry Smith     */
2021ee4f033dSBarry Smith 
202266f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2023ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
202466f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2025ee4f033dSBarry Smith     ierr = VecAXPY(&mone,w1,w2);CHKERRQ(ierr);
2026ee4f033dSBarry Smith 
2027ee4f033dSBarry Smith     /*
2028ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2029ee4f033dSBarry Smith     */
2030ed480e8bSBarry Smith     ierr = VecGetArrayFast(w2,&y);CHKERRQ(ierr);
2031ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2032ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2033ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2034ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2035ee4f033dSBarry Smith       srow   = row + start;
2036ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2037ee4f033dSBarry Smith     }
2038ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(w2,&y);CHKERRQ(ierr);
2039ee4f033dSBarry Smith   }
204049b058dcSBarry Smith   coloring->currentcolor = k;
2041ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2042ed480e8bSBarry Smith   xx = xx + start; ierr  = VecRestoreArrayFast(x1,&xx);CHKERRQ(ierr);
2043ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2044ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2045ee4f033dSBarry Smith   PetscFunctionReturn(0);
2046ee4f033dSBarry Smith }
2047ee4f033dSBarry Smith 
2048ac90fabeSBarry Smith #include "petscblaslapack.h"
2049ac90fabeSBarry Smith #undef __FUNCT__
2050ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2051ac90fabeSBarry Smith int MatAXPY_SeqAIJ(PetscScalar *a,Mat X,Mat Y,MatStructure str)
2052ac90fabeSBarry Smith {
2053*c537a176SHong Zhang   int        ierr,one=1,i;
2054ac90fabeSBarry Smith   Mat_SeqAIJ *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
2055*c537a176SHong Zhang   int        *xtoy,nz,row,xcol,ycol,jx,jy,*xi=x->i,*xj=x->j,*yi=y->i,*yj=y->j;
2056ac90fabeSBarry Smith 
2057ac90fabeSBarry Smith   PetscFunctionBegin;
2058ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2059ac90fabeSBarry Smith     BLaxpy_(&x->nz,a,x->a,&one,y->a,&one);
2060*c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2061*c537a176SHong Zhang 
2062*c537a176SHong Zhang     ierr = PetscMalloc(x->nz*sizeof(int),&xtoy);CHKERRQ(ierr);
2063*c537a176SHong Zhang     i = 0;
2064*c537a176SHong Zhang     for (row=0; row<X->m; row++){
2065*c537a176SHong Zhang       nz = xi[1] - xi[0];
2066*c537a176SHong Zhang       jy = 0;
2067*c537a176SHong Zhang       for (jx=0; jx<nz; jx++,jy++){
2068*c537a176SHong Zhang         xcol = xj[*xi + jx];
2069*c537a176SHong Zhang         ycol = yj[*yi + jy];  /* col index for y */
2070*c537a176SHong Zhang         while ( ycol < xcol ) {
2071*c537a176SHong Zhang           jy++;
2072*c537a176SHong Zhang           ycol = yj[*yi + jy];
2073*c537a176SHong Zhang         }
2074*c537a176SHong Zhang         if (xcol != ycol) SETERRQ2(PETSC_ERR_ARG_WRONG,"X matrix entry (%d,%d) is not in Y matrix",row,ycol);
2075*c537a176SHong Zhang         xtoy[i++] = *yi + jy;
2076*c537a176SHong Zhang       }
2077*c537a176SHong Zhang       xi++; yi++;
2078*c537a176SHong Zhang     }
2079*c537a176SHong Zhang 
2080*c537a176SHong Zhang     for (i=0; i<x->nz; i++) {
2081*c537a176SHong Zhang       y->a[xtoy[i]] += (*a)*(x->a[i]);
2082*c537a176SHong Zhang     }
2083*c537a176SHong Zhang     ierr =  PetscFree(xtoy);CHKERRQ(ierr);
2084ac90fabeSBarry Smith   } else {
2085ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
2086ac90fabeSBarry Smith   }
2087ac90fabeSBarry Smith   PetscFunctionReturn(0);
2088ac90fabeSBarry Smith }
2089ac90fabeSBarry Smith 
2090ac90fabeSBarry Smith 
2091682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
20920a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2093cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2094cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2095cb5b572fSBarry Smith        MatMult_SeqAIJ,
2096cb5b572fSBarry Smith        MatMultAdd_SeqAIJ,
20977c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
20987c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2099cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2100cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
21017c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
21027c922b88SBarry Smith        MatSolveTransposeAdd_SeqAIJ,
2103cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2104cb5b572fSBarry Smith        0,
210517ab2063SBarry Smith        MatRelax_SeqAIJ,
210617ab2063SBarry Smith        MatTranspose_SeqAIJ,
2107cb5b572fSBarry Smith        MatGetInfo_SeqAIJ,
2108cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2109cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2110cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2111cb5b572fSBarry Smith        MatNorm_SeqAIJ,
2112cb5b572fSBarry Smith        0,
2113cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
211417ab2063SBarry Smith        MatCompress_SeqAIJ,
2115cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2116cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2117cb5b572fSBarry Smith        MatZeroRows_SeqAIJ,
2118cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2119cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2120f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2121a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
2122273d9f13SBarry Smith        MatSetUpPreallocation_SeqAIJ,
2123cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2124861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
21256c0721eeSBarry Smith        MatGetArray_SeqAIJ,
21266c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2127be6bf707SBarry Smith        MatDuplicate_SeqAIJ,
2128cb5b572fSBarry Smith        0,
2129cb5b572fSBarry Smith        0,
2130cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2131cb5b572fSBarry Smith        0,
2132ac90fabeSBarry Smith        MatAXPY_SeqAIJ,
2133cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2134cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2135cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2136cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2137f0b747eeSBarry Smith        MatPrintHelp_SeqAIJ,
2138cb5b572fSBarry Smith        MatScale_SeqAIJ,
2139cb5b572fSBarry Smith        0,
2140cb5b572fSBarry Smith        0,
21416945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
21426945ee14SBarry Smith        MatGetBlockSize_SeqAIJ,
21433b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
21443b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
21453b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2146a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2147a93ec695SBarry Smith        MatFDColoringCreate_SeqAIJ,
2148b9617806SBarry Smith        0,
21490513a670SBarry Smith        0,
2150cda55fadSBarry Smith        MatPermute_SeqAIJ,
2151cda55fadSBarry Smith        0,
2152cda55fadSBarry Smith        0,
2153b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2154b9b97703SBarry Smith        MatView_SeqAIJ,
21558a124369SBarry Smith        MatGetPetscMaps_Petsc,
2156ee4f033dSBarry Smith        0,
2157ee4f033dSBarry Smith        0,
2158ee4f033dSBarry Smith        0,
2159ee4f033dSBarry Smith        0,
2160ee4f033dSBarry Smith        0,
2161ee4f033dSBarry Smith        0,
2162ee4f033dSBarry Smith        0,
2163ee4f033dSBarry Smith        0,
2164ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2165ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2166ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
2167ee4f033dSBarry Smith        MatFDColoringApply_SeqAIJ};
216817ab2063SBarry Smith 
2169fb2e594dSBarry Smith EXTERN_C_BEGIN
21704a2ae208SSatish Balay #undef __FUNCT__
21714a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
217215091d37SBarry Smith 
2173bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices)
2174bef8e0ddSBarry Smith {
2175bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2176bef8e0ddSBarry Smith   int        i,nz,n;
2177bef8e0ddSBarry Smith 
2178bef8e0ddSBarry Smith   PetscFunctionBegin;
2179bef8e0ddSBarry Smith 
2180bef8e0ddSBarry Smith   nz = aij->maxnz;
2181273d9f13SBarry Smith   n  = mat->n;
2182bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2183bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2184bef8e0ddSBarry Smith   }
2185bef8e0ddSBarry Smith   aij->nz = nz;
2186bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2187bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2188bef8e0ddSBarry Smith   }
2189bef8e0ddSBarry Smith 
2190bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2191bef8e0ddSBarry Smith }
2192fb2e594dSBarry Smith EXTERN_C_END
2193bef8e0ddSBarry Smith 
21944a2ae208SSatish Balay #undef __FUNCT__
21954a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2196bef8e0ddSBarry Smith /*@
2197bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2198bef8e0ddSBarry Smith        in the matrix.
2199bef8e0ddSBarry Smith 
2200bef8e0ddSBarry Smith   Input Parameters:
2201bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2202bef8e0ddSBarry Smith -  indices - the column indices
2203bef8e0ddSBarry Smith 
220415091d37SBarry Smith   Level: advanced
220515091d37SBarry Smith 
2206bef8e0ddSBarry Smith   Notes:
2207bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2208bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2209bef8e0ddSBarry Smith   of the MatSetValues() operation.
2210bef8e0ddSBarry Smith 
2211bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2212bef8e0ddSBarry Smith   MatCreateSeqAIJ().
2213bef8e0ddSBarry Smith 
2214bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2215bef8e0ddSBarry Smith 
2216b9617806SBarry Smith     The indices should start with zero, not one.
2217b9617806SBarry Smith 
2218bef8e0ddSBarry Smith @*/
2219bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices)
2220bef8e0ddSBarry Smith {
2221bef8e0ddSBarry Smith   int ierr,(*f)(Mat,int *);
2222bef8e0ddSBarry Smith 
2223bef8e0ddSBarry Smith   PetscFunctionBegin;
2224bef8e0ddSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2225c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2226bef8e0ddSBarry Smith   if (f) {
2227bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2228bef8e0ddSBarry Smith   } else {
222929bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to set column indices");
2230bef8e0ddSBarry Smith   }
2231bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2232bef8e0ddSBarry Smith }
2233bef8e0ddSBarry Smith 
2234be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2235be6bf707SBarry Smith 
2236fb2e594dSBarry Smith EXTERN_C_BEGIN
22374a2ae208SSatish Balay #undef __FUNCT__
22384a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2239be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat)
2240be6bf707SBarry Smith {
2241be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2242a7ed9263SMatthew Knepley   size_t       nz = aij->i[mat->m]+aij->indexshift,ierr;
2243be6bf707SBarry Smith 
2244be6bf707SBarry Smith   PetscFunctionBegin;
2245be6bf707SBarry Smith   if (aij->nonew != 1) {
224629bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2247be6bf707SBarry Smith   }
2248be6bf707SBarry Smith 
2249be6bf707SBarry Smith   /* allocate space for values if not already there */
2250be6bf707SBarry Smith   if (!aij->saved_values) {
225187828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2252be6bf707SBarry Smith   }
2253be6bf707SBarry Smith 
2254be6bf707SBarry Smith   /* copy values over */
225587828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2256be6bf707SBarry Smith   PetscFunctionReturn(0);
2257be6bf707SBarry Smith }
2258fb2e594dSBarry Smith EXTERN_C_END
2259be6bf707SBarry Smith 
22604a2ae208SSatish Balay #undef __FUNCT__
2261b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2262be6bf707SBarry Smith /*@
2263be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2264be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2265be6bf707SBarry Smith        nonlinear portion.
2266be6bf707SBarry Smith 
2267be6bf707SBarry Smith    Collect on Mat
2268be6bf707SBarry Smith 
2269be6bf707SBarry Smith   Input Parameters:
2270be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2271be6bf707SBarry Smith 
227215091d37SBarry Smith   Level: advanced
227315091d37SBarry Smith 
2274be6bf707SBarry Smith   Common Usage, with SNESSolve():
2275be6bf707SBarry Smith $    Create Jacobian matrix
2276be6bf707SBarry Smith $    Set linear terms into matrix
2277be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2278be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2279be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2280be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2281be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2282be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2283be6bf707SBarry Smith $    In your Jacobian routine
2284be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2285be6bf707SBarry Smith $      Set nonlinear terms in matrix
2286be6bf707SBarry Smith 
2287be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2288be6bf707SBarry Smith $    // build linear portion of Jacobian
2289be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2290be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2291be6bf707SBarry Smith $    loop over nonlinear iterations
2292be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2293be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2294be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2295be6bf707SBarry Smith $       Solve linear system with Jacobian
2296be6bf707SBarry Smith $    endloop
2297be6bf707SBarry Smith 
2298be6bf707SBarry Smith   Notes:
2299be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2300be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2301be6bf707SBarry Smith     calling this routine.
2302be6bf707SBarry Smith 
2303be6bf707SBarry Smith .seealso: MatRetrieveValues()
2304be6bf707SBarry Smith 
2305be6bf707SBarry Smith @*/
2306be6bf707SBarry Smith int MatStoreValues(Mat mat)
2307be6bf707SBarry Smith {
2308be6bf707SBarry Smith   int ierr,(*f)(Mat);
2309be6bf707SBarry Smith 
2310be6bf707SBarry Smith   PetscFunctionBegin;
2311be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
231229bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
231329bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2314be6bf707SBarry Smith 
2315c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2316be6bf707SBarry Smith   if (f) {
2317be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2318be6bf707SBarry Smith   } else {
231929bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to store values");
2320be6bf707SBarry Smith   }
2321be6bf707SBarry Smith   PetscFunctionReturn(0);
2322be6bf707SBarry Smith }
2323be6bf707SBarry Smith 
2324fb2e594dSBarry Smith EXTERN_C_BEGIN
23254a2ae208SSatish Balay #undef __FUNCT__
23264a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2327be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat)
2328be6bf707SBarry Smith {
2329be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2330273d9f13SBarry Smith   int        nz = aij->i[mat->m]+aij->indexshift,ierr;
2331be6bf707SBarry Smith 
2332be6bf707SBarry Smith   PetscFunctionBegin;
2333be6bf707SBarry Smith   if (aij->nonew != 1) {
233429bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2335be6bf707SBarry Smith   }
2336be6bf707SBarry Smith   if (!aij->saved_values) {
233729bbc08cSBarry Smith     SETERRQ(1,"Must call MatStoreValues(A);first");
2338be6bf707SBarry Smith   }
2339be6bf707SBarry Smith 
2340be6bf707SBarry Smith   /* copy values over */
234187828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2342be6bf707SBarry Smith   PetscFunctionReturn(0);
2343be6bf707SBarry Smith }
2344fb2e594dSBarry Smith EXTERN_C_END
2345be6bf707SBarry Smith 
23464a2ae208SSatish Balay #undef __FUNCT__
23474a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2348be6bf707SBarry Smith /*@
2349be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2350be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2351be6bf707SBarry Smith        nonlinear portion.
2352be6bf707SBarry Smith 
2353be6bf707SBarry Smith    Collect on Mat
2354be6bf707SBarry Smith 
2355be6bf707SBarry Smith   Input Parameters:
2356be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2357be6bf707SBarry Smith 
235815091d37SBarry Smith   Level: advanced
235915091d37SBarry Smith 
2360be6bf707SBarry Smith .seealso: MatStoreValues()
2361be6bf707SBarry Smith 
2362be6bf707SBarry Smith @*/
2363be6bf707SBarry Smith int MatRetrieveValues(Mat mat)
2364be6bf707SBarry Smith {
2365be6bf707SBarry Smith   int ierr,(*f)(Mat);
2366be6bf707SBarry Smith 
2367be6bf707SBarry Smith   PetscFunctionBegin;
2368be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
236929bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
237029bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2371be6bf707SBarry Smith 
2372c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2373be6bf707SBarry Smith   if (f) {
2374be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2375be6bf707SBarry Smith   } else {
237629bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to retrieve values");
2377be6bf707SBarry Smith   }
2378be6bf707SBarry Smith   PetscFunctionReturn(0);
2379be6bf707SBarry Smith }
2380be6bf707SBarry Smith 
2381f83d6046SBarry Smith /*
2382f83d6046SBarry Smith    This allows SeqAIJ matrices to be passed to the matlab engine
2383f83d6046SBarry Smith */
238487828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2385f83d6046SBarry Smith #include "engine.h"   /* Matlab include file */
2386f83d6046SBarry Smith #include "mex.h"      /* Matlab include file */
2387f83d6046SBarry Smith EXTERN_C_BEGIN
23884a2ae208SSatish Balay #undef __FUNCT__
23894a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEnginePut_SeqAIJ"
239062aa7f4eSBarry Smith int MatMatlabEnginePut_SeqAIJ(PetscObject obj,void *mengine)
2391f83d6046SBarry Smith {
23924a4478b9SSatish Balay   int         ierr,i,*ai,*aj;
2393f83d6046SBarry Smith   Mat         B = (Mat)obj;
2394f83d6046SBarry Smith   mxArray     *mat;
2395d79a51d8SBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)B->data;
2396d79a51d8SBarry Smith 
2397f83d6046SBarry Smith   PetscFunctionBegin;
239801820c62SBarry Smith   mat  = mxCreateSparse(B->n,B->m,aij->nz,mxREAL);
239987828ca2SBarry Smith   ierr = PetscMemcpy(mxGetPr(mat),aij->a,aij->nz*sizeof(PetscScalar));CHKERRQ(ierr);
2400d79a51d8SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
240166826eb6SBarry Smith   ierr = PetscMemcpy(mxGetIr(mat),aij->j,aij->nz*sizeof(int));CHKERRQ(ierr);
240266826eb6SBarry Smith   ierr = PetscMemcpy(mxGetJc(mat),aij->i,(B->m+1)*sizeof(int));CHKERRQ(ierr);
2403f83d6046SBarry Smith 
240401820c62SBarry Smith   /* Matlab indices start at 0 for sparse (what a surprise) */
240501820c62SBarry Smith   if (aij->indexshift) {
2406a1c0a87aSBarry Smith     ai = mxGetJc(mat);
240701820c62SBarry Smith     for (i=0; i<B->m+1; i++) {
240801820c62SBarry Smith       ai[i]--;
2409d79a51d8SBarry Smith     }
2410a1c0a87aSBarry Smith     aj = mxGetIr(mat);
2411d79a51d8SBarry Smith     for (i=0; i<aij->nz; i++) {
241201820c62SBarry Smith       aj[i]--;
2413d79a51d8SBarry Smith     }
2414d79a51d8SBarry Smith   }
2415ca44d042SBarry Smith   ierr = PetscObjectName(obj);CHKERRQ(ierr);
2416f83d6046SBarry Smith   mxSetName(mat,obj->name);
241762aa7f4eSBarry Smith   engPutArray((Engine *)mengine,mat);
2418f83d6046SBarry Smith   PetscFunctionReturn(0);
2419f83d6046SBarry Smith }
2420f83d6046SBarry Smith EXTERN_C_END
242166826eb6SBarry Smith 
242266826eb6SBarry Smith EXTERN_C_BEGIN
24234a2ae208SSatish Balay #undef __FUNCT__
24244a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEngineGet_SeqAIJ"
242562aa7f4eSBarry Smith int MatMatlabEngineGet_SeqAIJ(PetscObject obj,void *mengine)
242666826eb6SBarry Smith {
242766826eb6SBarry Smith   int        ierr,ii;
242866826eb6SBarry Smith   Mat        mat = (Mat)obj;
242966826eb6SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
243066826eb6SBarry Smith   mxArray    *mmat;
243166826eb6SBarry Smith 
24326c0721eeSBarry Smith   PetscFunctionBegin;
243366826eb6SBarry Smith   ierr = PetscFree(aij->a);CHKERRQ(ierr);
243466826eb6SBarry Smith   aij->indexshift = 0;
243566826eb6SBarry Smith 
243662aa7f4eSBarry Smith   mmat = engGetArray((Engine *)mengine,obj->name);
243766826eb6SBarry Smith 
2438a65776f3SBarry Smith   aij->nz           = (mxGetJc(mmat))[mat->m];
2439a7ed9263SMatthew Knepley   ierr              = PetscMalloc(((size_t) aij->nz)*(sizeof(int)+sizeof(PetscScalar))+(mat->m+1)*sizeof(int),&aij->a);CHKERRQ(ierr);
244066826eb6SBarry Smith   aij->j            = (int*)(aij->a + aij->nz);
244166826eb6SBarry Smith   aij->i            = aij->j + aij->nz;
244266826eb6SBarry Smith   aij->singlemalloc = PETSC_TRUE;
244366826eb6SBarry Smith   aij->freedata     = PETSC_TRUE;
244466826eb6SBarry Smith 
244587828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,mxGetPr(mmat),aij->nz*sizeof(PetscScalar));CHKERRQ(ierr);
244666826eb6SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
244766826eb6SBarry Smith   ierr = PetscMemcpy(aij->j,mxGetIr(mmat),aij->nz*sizeof(int));CHKERRQ(ierr);
244866826eb6SBarry Smith   ierr = PetscMemcpy(aij->i,mxGetJc(mmat),(mat->m+1)*sizeof(int));CHKERRQ(ierr);
244966826eb6SBarry Smith 
245066826eb6SBarry Smith   for (ii=0; ii<mat->m; ii++) {
245166826eb6SBarry Smith     aij->ilen[ii] = aij->imax[ii] = aij->i[ii+1] - aij->i[ii];
245266826eb6SBarry Smith   }
245366826eb6SBarry Smith 
245466826eb6SBarry Smith   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
245566826eb6SBarry Smith   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
245666826eb6SBarry Smith 
245766826eb6SBarry Smith   PetscFunctionReturn(0);
245866826eb6SBarry Smith }
245966826eb6SBarry Smith EXTERN_C_END
2460f83d6046SBarry Smith #endif
2461f83d6046SBarry Smith 
2462be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
24634a2ae208SSatish Balay #undef __FUNCT__
24644a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
246517ab2063SBarry Smith /*@C
2466682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
24670d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
24686e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
246951c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
24702bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
247117ab2063SBarry Smith 
2472db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2473db81eaa0SLois Curfman McInnes 
247417ab2063SBarry Smith    Input Parameters:
2475db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
247617ab2063SBarry Smith .  m - number of rows
247717ab2063SBarry Smith .  n - number of columns
247817ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
247951c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
24802bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
248117ab2063SBarry Smith 
248217ab2063SBarry Smith    Output Parameter:
2483416022c9SBarry Smith .  A - the matrix
248417ab2063SBarry Smith 
2485b259b22eSLois Curfman McInnes    Notes:
248617ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
248717ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
24880002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
248944cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
249017ab2063SBarry Smith 
249117ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2492a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
24933d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
24946da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
249517ab2063SBarry Smith 
2496682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
24974fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2498682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
24996c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
25006c7ebb05SLois Curfman McInnes 
25016c7ebb05SLois Curfman McInnes    Options Database Keys:
2502db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2503db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2504db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2505db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2506db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
250717ab2063SBarry Smith 
2508027ccd11SLois Curfman McInnes    Level: intermediate
2509027ccd11SLois Curfman McInnes 
251036db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
251136db0b34SBarry Smith 
251217ab2063SBarry Smith @*/
2513416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz,Mat *A)
251417ab2063SBarry Smith {
2515273d9f13SBarry Smith   int ierr;
25166945ee14SBarry Smith 
25173a40ed3dSBarry Smith   PetscFunctionBegin;
2518273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,m,n,A);CHKERRQ(ierr);
2519273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2520273d9f13SBarry Smith   ierr = MatSeqAIJSetPreallocation(*A,nz,nnz);CHKERRQ(ierr);
2521273d9f13SBarry Smith   PetscFunctionReturn(0);
2522273d9f13SBarry Smith }
2523273d9f13SBarry Smith 
25245da197adSKris Buschelman #define SKIP_ALLOCATION -4
25255da197adSKris Buschelman 
25264a2ae208SSatish Balay #undef __FUNCT__
25274a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2528273d9f13SBarry Smith /*@C
2529273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2530273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2531273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2532273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2533273d9f13SBarry Smith 
2534273d9f13SBarry Smith    Collective on MPI_Comm
2535273d9f13SBarry Smith 
2536273d9f13SBarry Smith    Input Parameters:
2537273d9f13SBarry Smith +  comm - MPI communicator, set to PETSC_COMM_SELF
2538273d9f13SBarry Smith .  m - number of rows
2539273d9f13SBarry Smith .  n - number of columns
2540273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2541273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2542273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2543273d9f13SBarry Smith 
2544273d9f13SBarry Smith    Output Parameter:
2545273d9f13SBarry Smith .  A - the matrix
2546273d9f13SBarry Smith 
2547273d9f13SBarry Smith    Notes:
2548273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2549273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2550273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2551273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2552273d9f13SBarry Smith 
2553273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2554273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2555273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2556273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2557273d9f13SBarry Smith 
2558273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2559273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2560273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2561273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2562273d9f13SBarry Smith 
2563273d9f13SBarry Smith    Options Database Keys:
2564273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2565273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2566273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2567273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2568273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2569273d9f13SBarry Smith 
2570273d9f13SBarry Smith    Level: intermediate
2571273d9f13SBarry Smith 
2572273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2573273d9f13SBarry Smith 
2574273d9f13SBarry Smith @*/
2575273d9f13SBarry Smith int MatSeqAIJSetPreallocation(Mat B,int nz,int *nnz)
2576273d9f13SBarry Smith {
2577273d9f13SBarry Smith   Mat_SeqAIJ *b;
2578a7ed9263SMatthew Knepley   size_t     len = 0;
2579c461c341SBarry Smith   PetscTruth flg2,skipallocation = PETSC_FALSE;
2580a7ed9263SMatthew Knepley   int        i,ierr;
2581273d9f13SBarry Smith 
2582273d9f13SBarry Smith   PetscFunctionBegin;
2583273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg2);CHKERRQ(ierr);
2584273d9f13SBarry Smith   if (!flg2) PetscFunctionReturn(0);
2585d5d45c9bSBarry Smith 
2586c461c341SBarry Smith   if (nz == SKIP_ALLOCATION) {
2587c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2588c461c341SBarry Smith     nz             = 0;
2589c461c341SBarry Smith   }
2590c461c341SBarry Smith 
2591435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2592435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2593b73539f3SBarry Smith   if (nnz) {
2594273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
259529bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
25963a7fca6bSBarry 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);
2597b73539f3SBarry Smith     }
2598b73539f3SBarry Smith   }
2599b73539f3SBarry Smith 
2600273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2601273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2602273d9f13SBarry Smith 
2603b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->imax);CHKERRQ(ierr);
2604273d9f13SBarry Smith   if (!nnz) {
2605435da068SBarry Smith     if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2606273d9f13SBarry Smith     else if (nz <= 0)        nz = 1;
2607273d9f13SBarry Smith     for (i=0; i<B->m; i++) b->imax[i] = nz;
2608273d9f13SBarry Smith     nz = nz*B->m;
2609273d9f13SBarry Smith   } else {
2610273d9f13SBarry Smith     nz = 0;
2611273d9f13SBarry Smith     for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2612273d9f13SBarry Smith   }
2613273d9f13SBarry Smith 
2614c461c341SBarry Smith   if (!skipallocation) {
2615273d9f13SBarry Smith     /* allocate the matrix space */
2616a7ed9263SMatthew Knepley     len             = ((size_t) nz)*(sizeof(int) + sizeof(PetscScalar)) + (B->m+1)*sizeof(int);
2617b0a32e0cSBarry Smith     ierr            = PetscMalloc(len,&b->a);CHKERRQ(ierr);
2618273d9f13SBarry Smith     b->j            = (int*)(b->a + nz);
2619273d9f13SBarry Smith     ierr            = PetscMemzero(b->j,nz*sizeof(int));CHKERRQ(ierr);
2620273d9f13SBarry Smith     b->i            = b->j + nz;
26215da197adSKris Buschelman     b->i[0] = -b->indexshift;
26225da197adSKris Buschelman     for (i=1; i<B->m+1; i++) {
26235da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
26245da197adSKris Buschelman     }
2625273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2626273d9f13SBarry Smith     b->freedata     = PETSC_TRUE;
2627c461c341SBarry Smith   } else {
2628c461c341SBarry Smith     b->freedata     = PETSC_FALSE;
2629c461c341SBarry Smith   }
2630273d9f13SBarry Smith 
2631273d9f13SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
2632b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->ilen);CHKERRQ(ierr);
2633b0a32e0cSBarry Smith   PetscLogObjectMemory(B,len+2*(B->m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2634273d9f13SBarry Smith   for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2635273d9f13SBarry Smith 
2636273d9f13SBarry Smith   b->nz                = 0;
2637273d9f13SBarry Smith   b->maxnz             = nz;
2638273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2639273d9f13SBarry Smith   PetscFunctionReturn(0);
2640273d9f13SBarry Smith }
2641273d9f13SBarry Smith 
2642eb9c0419SKris Buschelman EXTERN int RegisterApplyPtAPRoutines_Private(Mat);
2643eb9c0419SKris Buschelman 
2644273d9f13SBarry Smith EXTERN_C_BEGIN
2645a6175056SHong Zhang extern int MatConvert_SeqAIJ_SeqSBAIJ(Mat,MatType,Mat*);
264685fc7724SBarry Smith extern int MatConvert_SeqAIJ_SeqBAIJ(Mat,MatType,Mat*);
2647a6175056SHong Zhang EXTERN_C_END
2648a6175056SHong Zhang 
2649a6175056SHong Zhang EXTERN_C_BEGIN
26504a2ae208SSatish Balay #undef __FUNCT__
26514a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2652273d9f13SBarry Smith int MatCreate_SeqAIJ(Mat B)
2653273d9f13SBarry Smith {
2654273d9f13SBarry Smith   Mat_SeqAIJ *b;
2655273d9f13SBarry Smith   int        ierr,size;
2656273d9f13SBarry Smith   PetscTruth flg;
2657273d9f13SBarry Smith 
2658273d9f13SBarry Smith   PetscFunctionBegin;
2659273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2660273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2661273d9f13SBarry Smith 
2662273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2663273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2664273d9f13SBarry Smith 
2665b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2666b0a32e0cSBarry Smith   B->data             = (void*)b;
2667549d3d68SSatish Balay   ierr = PetscMemzero(b,sizeof(Mat_SeqAIJ));CHKERRQ(ierr);
2668549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2669416022c9SBarry Smith   B->factor           = 0;
2670416022c9SBarry Smith   B->lupivotthreshold = 1.0;
267190f02eecSBarry Smith   B->mapping          = 0;
2672e82a3eeeSBarry Smith   ierr = PetscOptionsGetReal(B->prefix,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr);
2673e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2674416022c9SBarry Smith   b->row              = 0;
2675416022c9SBarry Smith   b->col              = 0;
267682bf6240SBarry Smith   b->icol             = 0;
2677416022c9SBarry Smith   b->indexshift       = 0;
2678b810aeb4SBarry Smith   b->reallocs         = 0;
2679e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_oneindex",&flg);CHKERRQ(ierr);
268069957df2SSatish Balay   if (flg) b->indexshift = -1;
268117ab2063SBarry Smith 
26828a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
26838a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2684a5ae1ecdSBarry Smith 
2685f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
268636db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2687f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2688416022c9SBarry Smith   b->nonew             = 0;
2689416022c9SBarry Smith   b->diag              = 0;
2690416022c9SBarry Smith   b->solve_work        = 0;
26912a1b7f2aSHong Zhang   B->spptr             = 0;
2692b65db4caSBarry Smith   b->inode.use         = PETSC_TRUE;
2693754ec7b1SSatish Balay   b->inode.node_count  = 0;
2694754ec7b1SSatish Balay   b->inode.size        = 0;
26956c7ebb05SLois Curfman McInnes   b->inode.limit       = 5;
26966c7ebb05SLois Curfman McInnes   b->inode.max_limit   = 5;
2697be6bf707SBarry Smith   b->saved_values      = 0;
2698d7f994e1SBarry Smith   b->idiag             = 0;
2699d7f994e1SBarry Smith   b->ssor              = 0;
2700f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
270117ab2063SBarry Smith 
270235d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
270335d8aa7fSBarry Smith 
2704aa482453SBarry Smith #if defined(PETSC_HAVE_SUPERLU)
2705e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_superlu",&flg);CHKERRQ(ierr);
270669957df2SSatish Balay   if (flg) { ierr = MatUseSuperLU_SeqAIJ(B);CHKERRQ(ierr); }
27074b14c69eSBarry Smith #endif
2708564e58d6SHong Zhang 
2709e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_essl",&flg);CHKERRQ(ierr);
271069957df2SSatish Balay   if (flg) { ierr = MatUseEssl_SeqAIJ(B);CHKERRQ(ierr); }
2711e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_lusol",&flg);CHKERRQ(ierr);
2712cd5578b5SBarry Smith   if (flg) { ierr = MatUseLUSOL_SeqAIJ(B);CHKERRQ(ierr); }
2713e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_matlab",&flg);CHKERRQ(ierr);
2714ca44d042SBarry Smith   if (flg) {ierr = MatUseMatlab_SeqAIJ(B);CHKERRQ(ierr);}
2715e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_dxml",&flg);CHKERRQ(ierr);
271669957df2SSatish Balay   if (flg) {
271729bbc08cSBarry Smith     if (!b->indexshift) SETERRQ(PETSC_ERR_LIB,"need -mat_aij_oneindex with -mat_aij_dxml");
2718416022c9SBarry Smith     ierr = MatUseDXML_SeqAIJ(B);CHKERRQ(ierr);
271917ab2063SBarry Smith   }
2720f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2721bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2722bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2723f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2724be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2725bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2726f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2727be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2728bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2729a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
2730a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
2731a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
273285fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
273385fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
273485fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
273587828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2736f83d6046SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatMatlabEnginePut_SeqAIJ",MatMatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
273766826eb6SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatMatlabEngineGet_SeqAIJ",MatMatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
2738f83d6046SBarry Smith #endif
2739eb9c0419SKris Buschelman   ierr = RegisterApplyPtAPRoutines_Private(B);CHKERRQ(ierr);
27403a40ed3dSBarry Smith   PetscFunctionReturn(0);
274117ab2063SBarry Smith }
2742273d9f13SBarry Smith EXTERN_C_END
274317ab2063SBarry Smith 
27444a2ae208SSatish Balay #undef __FUNCT__
27454a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2746be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
274717ab2063SBarry Smith {
2748416022c9SBarry Smith   Mat        C;
2749416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data;
2750a7ed9263SMatthew Knepley   int        i,m = A->m,shift = a->indexshift,ierr;
2751a7ed9263SMatthew Knepley   size_t     len;
275217ab2063SBarry Smith 
27533a40ed3dSBarry Smith   PetscFunctionBegin;
27544043dd9cSLois Curfman McInnes   *B = 0;
2755273d9f13SBarry Smith   ierr = MatCreate(A->comm,A->m,A->n,A->m,A->n,&C);CHKERRQ(ierr);
2756273d9f13SBarry Smith   ierr = MatSetType(C,MATSEQAIJ);CHKERRQ(ierr);
2757273d9f13SBarry Smith   c    = (Mat_SeqAIJ*)C->data;
2758273d9f13SBarry Smith 
2759416022c9SBarry Smith   C->factor         = A->factor;
2760416022c9SBarry Smith   c->row            = 0;
2761416022c9SBarry Smith   c->col            = 0;
276282bf6240SBarry Smith   c->icol           = 0;
2763416022c9SBarry Smith   c->indexshift     = shift;
2764f1e2ffcdSBarry Smith   c->keepzeroedrows = a->keepzeroedrows;
2765c456f294SBarry Smith   C->assembled      = PETSC_TRUE;
276617ab2063SBarry Smith 
2767273d9f13SBarry Smith   C->M          = A->m;
2768273d9f13SBarry Smith   C->N          = A->n;
276917ab2063SBarry Smith 
2770b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->imax);CHKERRQ(ierr);
2771b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->ilen);CHKERRQ(ierr);
277217ab2063SBarry Smith   for (i=0; i<m; i++) {
2773416022c9SBarry Smith     c->imax[i] = a->imax[i];
2774416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
277517ab2063SBarry Smith   }
277617ab2063SBarry Smith 
277717ab2063SBarry Smith   /* allocate the matrix space */
2778f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
2779a7ed9263SMatthew Knepley   len   = ((size_t) (m+1))*sizeof(int)+(a->i[m])*(sizeof(PetscScalar)+sizeof(int));
2780b0a32e0cSBarry Smith   ierr  = PetscMalloc(len,&c->a);CHKERRQ(ierr);
2781416022c9SBarry Smith   c->j  = (int*)(c->a + a->i[m] + shift);
2782416022c9SBarry Smith   c->i  = c->j + a->i[m] + shift;
2783549d3d68SSatish Balay   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));CHKERRQ(ierr);
278417ab2063SBarry Smith   if (m > 0) {
2785549d3d68SSatish Balay     ierr = PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int));CHKERRQ(ierr);
2786be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
278787828ca2SBarry Smith       ierr = PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(PetscScalar));CHKERRQ(ierr);
2788be6bf707SBarry Smith     } else {
278987828ca2SBarry Smith       ierr = PetscMemzero(c->a,(a->i[m]+shift)*sizeof(PetscScalar));CHKERRQ(ierr);
279017ab2063SBarry Smith     }
279108480c60SBarry Smith   }
279217ab2063SBarry Smith 
2793b0a32e0cSBarry Smith   PetscLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2794416022c9SBarry Smith   c->sorted      = a->sorted;
2795416022c9SBarry Smith   c->roworiented = a->roworiented;
2796416022c9SBarry Smith   c->nonew       = a->nonew;
27977a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
2798be6bf707SBarry Smith   c->saved_values = 0;
2799d7f994e1SBarry Smith   c->idiag        = 0;
2800d7f994e1SBarry Smith   c->ssor         = 0;
280136db0b34SBarry Smith   c->ignorezeroentries = a->ignorezeroentries;
280236db0b34SBarry Smith   c->freedata     = PETSC_TRUE;
280317ab2063SBarry Smith 
2804416022c9SBarry Smith   if (a->diag) {
2805b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&c->diag);CHKERRQ(ierr);
2806b0a32e0cSBarry Smith     PetscLogObjectMemory(C,(m+1)*sizeof(int));
280717ab2063SBarry Smith     for (i=0; i<m; i++) {
2808416022c9SBarry Smith       c->diag[i] = a->diag[i];
280917ab2063SBarry Smith     }
28103a40ed3dSBarry Smith   } else c->diag        = 0;
2811b65db4caSBarry Smith   c->inode.use          = a->inode.use;
28126c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
28136c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
2814754ec7b1SSatish Balay   if (a->inode.size){
2815b0a32e0cSBarry Smith     ierr                = PetscMalloc((m+1)*sizeof(int),&c->inode.size);CHKERRQ(ierr);
2816754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
2817549d3d68SSatish Balay     ierr                = PetscMemcpy(c->inode.size,a->inode.size,(m+1)*sizeof(int));CHKERRQ(ierr);
2818754ec7b1SSatish Balay   } else {
2819754ec7b1SSatish Balay     c->inode.size       = 0;
2820754ec7b1SSatish Balay     c->inode.node_count = 0;
2821754ec7b1SSatish Balay   }
2822416022c9SBarry Smith   c->nz                 = a->nz;
2823416022c9SBarry Smith   c->maxnz              = a->maxnz;
2824416022c9SBarry Smith   c->solve_work         = 0;
28252a1b7f2aSHong Zhang   C->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
2826273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2827754ec7b1SSatish Balay 
2828416022c9SBarry Smith   *B = C;
2829b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
28303a40ed3dSBarry Smith   PetscFunctionReturn(0);
283117ab2063SBarry Smith }
283217ab2063SBarry Smith 
2833273d9f13SBarry Smith EXTERN_C_BEGIN
28344a2ae208SSatish Balay #undef __FUNCT__
28354a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
2836b0a32e0cSBarry Smith int MatLoad_SeqAIJ(PetscViewer viewer,MatType type,Mat *A)
283717ab2063SBarry Smith {
2838416022c9SBarry Smith   Mat_SeqAIJ   *a;
2839416022c9SBarry Smith   Mat          B;
284017699dbbSLois Curfman McInnes   int          i,nz,ierr,fd,header[4],size,*rowlengths = 0,M,N,shift;
2841bcd2baecSBarry Smith   MPI_Comm     comm;
284227fa2452SHong Zhang #if defined(PETSC_HAVE_SPOOLES) || defined(PETSC_HAVE_SUPERLU) || defined(PETSC_HAVE_SUPERLUDIST) || defined(PETSC_HAVE_UMFPACK)
284327fa2452SHong Zhang   PetscTruth   flag;
284427fa2452SHong Zhang #endif
284517ab2063SBarry Smith 
28463a40ed3dSBarry Smith   PetscFunctionBegin;
2847e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2848d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
284929bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
2850b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
28510752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2852552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
285317ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
285417ab2063SBarry Smith 
2855d64ed03dSBarry Smith   if (nz < 0) {
285629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2857d64ed03dSBarry Smith   }
2858d64ed03dSBarry Smith 
285917ab2063SBarry Smith   /* read in row lengths */
2860b0a32e0cSBarry Smith   ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
28610752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
286217ab2063SBarry Smith 
286317ab2063SBarry Smith   /* create our matrix */
2864416022c9SBarry Smith   ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A);CHKERRQ(ierr);
2865416022c9SBarry Smith   B = *A;
2866416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
2867416022c9SBarry Smith   shift = a->indexshift;
286817ab2063SBarry Smith 
286917ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
28700752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
287117ab2063SBarry Smith   if (shift) {
287217ab2063SBarry Smith     for (i=0; i<nz; i++) {
2873416022c9SBarry Smith       a->j[i] += 1;
287417ab2063SBarry Smith     }
287517ab2063SBarry Smith   }
287617ab2063SBarry Smith 
287717ab2063SBarry Smith   /* read in nonzero values */
28780752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
287917ab2063SBarry Smith 
288017ab2063SBarry Smith   /* set matrix "i" values */
2881416022c9SBarry Smith   a->i[0] = -shift;
288217ab2063SBarry Smith   for (i=1; i<= M; i++) {
2883416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2884416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
288517ab2063SBarry Smith   }
2886606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
288717ab2063SBarry Smith 
28886d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
28896d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
289027fa2452SHong Zhang #if defined(PETSC_HAVE_SPOOLES)
289127fa2452SHong Zhang   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_spooles",&flag);CHKERRQ(ierr);
289227fa2452SHong Zhang   if (flag) { ierr = MatUseSpooles_SeqAIJ(B);CHKERRQ(ierr); }
289327fa2452SHong Zhang #endif
289427fa2452SHong Zhang #if defined(PETSC_HAVE_SUPERLU)
289527fa2452SHong Zhang   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_superlu",&flag);CHKERRQ(ierr);
289627fa2452SHong Zhang   if (flag) { ierr = MatUseSuperLU_SeqAIJ(B);CHKERRQ(ierr); }
289727fa2452SHong Zhang #endif
289827fa2452SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
289927fa2452SHong Zhang   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr);
290027fa2452SHong Zhang   if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(B);CHKERRQ(ierr); }
290127fa2452SHong Zhang #endif
290227fa2452SHong Zhang #if defined(PETSC_HAVE_UMFPACK)
290327fa2452SHong Zhang   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_umfpack",&flag);CHKERRQ(ierr);
290427fa2452SHong Zhang   if (flag) { ierr = MatUseUMFPACK_SeqAIJ(B);CHKERRQ(ierr); }
290527fa2452SHong Zhang #endif
29063a40ed3dSBarry Smith   PetscFunctionReturn(0);
290717ab2063SBarry Smith }
2908273d9f13SBarry Smith EXTERN_C_END
290917ab2063SBarry Smith 
29104a2ae208SSatish Balay #undef __FUNCT__
2911b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
29128f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
29137264ac53SSatish Balay {
29147264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
29150f5bd95cSBarry Smith   int        ierr;
2916273d9f13SBarry Smith   PetscTruth flag;
29177264ac53SSatish Balay 
29183a40ed3dSBarry Smith   PetscFunctionBegin;
2919273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flag);CHKERRQ(ierr);
2920273d9f13SBarry Smith   if (!flag) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type");
29217264ac53SSatish Balay 
29227264ac53SSatish Balay   /* If the  matrix dimensions are not equal,or no of nonzeros or shift */
2923273d9f13SBarry Smith   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)|| (a->indexshift != b->indexshift)) {
2924ca44d042SBarry Smith     *flg = PETSC_FALSE;
2925ca44d042SBarry Smith     PetscFunctionReturn(0);
2926bcd2baecSBarry Smith   }
29277264ac53SSatish Balay 
29287264ac53SSatish Balay   /* if the a->i are the same */
2929273d9f13SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(int),flg);CHKERRQ(ierr);
29300f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
29317264ac53SSatish Balay 
29327264ac53SSatish Balay   /* if a->j are the same */
29330f5bd95cSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),flg);CHKERRQ(ierr);
29340f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
2935bcd2baecSBarry Smith 
2936bcd2baecSBarry Smith   /* if a->a are the same */
293787828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
29380f5bd95cSBarry Smith 
29393a40ed3dSBarry Smith   PetscFunctionReturn(0);
29407264ac53SSatish Balay 
29417264ac53SSatish Balay }
294236db0b34SBarry Smith 
29434a2ae208SSatish Balay #undef __FUNCT__
29444a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
294536db0b34SBarry Smith /*@C
294636db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
294736db0b34SBarry Smith               provided by the user.
294836db0b34SBarry Smith 
294936db0b34SBarry Smith       Coolective on MPI_Comm
295036db0b34SBarry Smith 
295136db0b34SBarry Smith    Input Parameters:
295236db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
295336db0b34SBarry Smith .   m - number of rows
295436db0b34SBarry Smith .   n - number of columns
295536db0b34SBarry Smith .   i - row indices
295636db0b34SBarry Smith .   j - column indices
295736db0b34SBarry Smith -   a - matrix values
295836db0b34SBarry Smith 
295936db0b34SBarry Smith    Output Parameter:
296036db0b34SBarry Smith .   mat - the matrix
296136db0b34SBarry Smith 
296236db0b34SBarry Smith    Level: intermediate
296336db0b34SBarry Smith 
296436db0b34SBarry Smith    Notes:
29650551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
296636db0b34SBarry Smith     once the matrix is destroyed
296736db0b34SBarry Smith 
296836db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
296936db0b34SBarry Smith 
297036db0b34SBarry Smith        The i and j indices can be either 0- or 1 based
297136db0b34SBarry Smith 
297236db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
297336db0b34SBarry Smith 
297436db0b34SBarry Smith @*/
297587828ca2SBarry Smith int MatCreateSeqAIJWithArrays(MPI_Comm comm,int m,int n,int* i,int*j,PetscScalar *a,Mat *mat)
297636db0b34SBarry Smith {
297736db0b34SBarry Smith   int        ierr,ii;
297836db0b34SBarry Smith   Mat_SeqAIJ *aij;
297936db0b34SBarry Smith 
298036db0b34SBarry Smith   PetscFunctionBegin;
2981c461c341SBarry Smith   ierr = MatCreateSeqAIJ(comm,m,n,SKIP_ALLOCATION,0,mat);CHKERRQ(ierr);
298236db0b34SBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
298336db0b34SBarry Smith 
298436db0b34SBarry Smith   if (i[0] == 1) {
298536db0b34SBarry Smith     aij->indexshift = -1;
298636db0b34SBarry Smith   } else if (i[0]) {
298729bbc08cSBarry Smith     SETERRQ(1,"i (row indices) do not start with 0 or 1");
298836db0b34SBarry Smith   }
298936db0b34SBarry Smith   aij->i = i;
299036db0b34SBarry Smith   aij->j = j;
299136db0b34SBarry Smith   aij->a = a;
299236db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
299336db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
299436db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
299536db0b34SBarry Smith 
299636db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
299736db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
29989860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
299929bbc08cSBarry 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]);
300036db0b34SBarry Smith #endif
300136db0b34SBarry Smith   }
30029860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
300336db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
300429bbc08cSBarry Smith     if (j[ii] < -aij->indexshift) SETERRQ2(1,"Negative column index at location = %d index = %d",ii,j[ii]);
300529bbc08cSBarry Smith     if (j[ii] > n - 1 -aij->indexshift) SETERRQ2(1,"Column index to large at location = %d index = %d",ii,j[ii]);
300636db0b34SBarry Smith   }
300736db0b34SBarry Smith #endif
300836db0b34SBarry Smith 
3009b65db4caSBarry Smith   /* changes indices to start at 0 */
3010b65db4caSBarry Smith   if (i[0]) {
3011b65db4caSBarry Smith     aij->indexshift = 0;
3012b65db4caSBarry Smith     for (ii=0; ii<m; ii++) {
3013b65db4caSBarry Smith       i[ii]--;
3014b65db4caSBarry Smith     }
3015b65db4caSBarry Smith     for (ii=0; ii<i[m]; ii++) {
3016b65db4caSBarry Smith       j[ii]--;
3017b65db4caSBarry Smith     }
3018b65db4caSBarry Smith   }
3019b65db4caSBarry Smith 
3020b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3021b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
302236db0b34SBarry Smith   PetscFunctionReturn(0);
302336db0b34SBarry Smith }
302436db0b34SBarry Smith 
3025cc8ba8e1SBarry Smith #undef __FUNCT__
3026ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3027ee4f033dSBarry Smith int MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3028cc8ba8e1SBarry Smith {
3029cc8ba8e1SBarry Smith   int        ierr;
3030cc8ba8e1SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
303136db0b34SBarry Smith 
3032cc8ba8e1SBarry Smith   PetscFunctionBegin;
303312c595b3SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
3034cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3035cc8ba8e1SBarry Smith     a->coloring = coloring;
303612c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
303708b6dcc0SBarry Smith     int             i,*larray;
303812c595b3SBarry Smith     ISColoring      ocoloring;
303908b6dcc0SBarry Smith     ISColoringValue *colors;
304012c595b3SBarry Smith 
304112c595b3SBarry Smith     /* set coloring for diagonal portion */
304212c595b3SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
304312c595b3SBarry Smith     for (i=0; i<A->n; i++) {
304412c595b3SBarry Smith       larray[i] = i;
304512c595b3SBarry Smith     }
304612c595b3SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
304708b6dcc0SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
304812c595b3SBarry Smith     for (i=0; i<A->n; i++) {
304912c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
305012c595b3SBarry Smith     }
305112c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
305212c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr);
305312c595b3SBarry Smith     a->coloring = ocoloring;
305412c595b3SBarry Smith   }
3055cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3056cc8ba8e1SBarry Smith }
3057cc8ba8e1SBarry Smith 
305887828ca2SBarry Smith #if defined(PETSC_HAVE_ADIC) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
3059ee4f033dSBarry Smith EXTERN_C_BEGIN
306029c1e371SBarry Smith #include "adic/ad_utils.h"
3061ee4f033dSBarry Smith EXTERN_C_END
3062cc8ba8e1SBarry Smith 
3063cc8ba8e1SBarry Smith #undef __FUNCT__
3064ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3065ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3066cc8ba8e1SBarry Smith {
3067cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
306808b6dcc0SBarry Smith   int             m = A->m,*ii = a->i,*jj = a->j,nz,i,j,nlen;
30694440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
307008b6dcc0SBarry Smith   ISColoringValue *color;
3071cc8ba8e1SBarry Smith 
3072cc8ba8e1SBarry Smith   PetscFunctionBegin;
3073cc8ba8e1SBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
30744440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3075cc8ba8e1SBarry Smith   color = a->coloring->colors;
3076cc8ba8e1SBarry Smith   /* loop over rows */
3077cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3078cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3079cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3080cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3081cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3082cc8ba8e1SBarry Smith     }
30834440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3084ee4f033dSBarry Smith   }
3085ee4f033dSBarry Smith   PetscFunctionReturn(0);
3086ee4f033dSBarry Smith }
3087ee4f033dSBarry Smith 
3088ee4f033dSBarry Smith #else
3089ee4f033dSBarry Smith 
3090ee4f033dSBarry Smith #undef __FUNCT__
3091ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3092ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3093ee4f033dSBarry Smith {
3094ee4f033dSBarry Smith   PetscFunctionBegin;
3095ee4f033dSBarry Smith   SETERRQ(1,"PETSc installed without ADIC");
3096ee4f033dSBarry Smith }
3097ee4f033dSBarry Smith 
3098ee4f033dSBarry Smith #endif
3099ee4f033dSBarry Smith 
3100ee4f033dSBarry Smith #undef __FUNCT__
3101ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
3102ee4f033dSBarry Smith int MatSetValuesAdifor_SeqAIJ(Mat A,int nl,void *advalues)
3103ee4f033dSBarry Smith {
3104ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
310508b6dcc0SBarry Smith   int             m = A->m,*ii = a->i,*jj = a->j,nz,i,j;
310687828ca2SBarry Smith   PetscScalar     *v = a->a,*values = (PetscScalar *)advalues;
310708b6dcc0SBarry Smith   ISColoringValue *color;
3108ee4f033dSBarry Smith 
3109ee4f033dSBarry Smith   PetscFunctionBegin;
3110ee4f033dSBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
3111ee4f033dSBarry Smith   color = a->coloring->colors;
3112ee4f033dSBarry Smith   /* loop over rows */
3113ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3114ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3115ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3116ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3117ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3118ee4f033dSBarry Smith     }
3119ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3120cc8ba8e1SBarry Smith   }
3121cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3122cc8ba8e1SBarry Smith }
312336db0b34SBarry Smith 
3124