xref: /petsc/src/mat/impls/aij/seq/aij.c (revision c14dc6b6670b013458f80652c6b8e042caee0f12)
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 
7a375949dSSatish Balay #include "petscsys.h"                           /*I "petscmat.h" I*/
870f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
9f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
10f5eb4b81SSatish Balay #include "src/inline/spops.h"
118d195f9aSBarry Smith #include "src/inline/dot.h"
120a835dfdSSatish Balay #include "petscbt.h"
1317ab2063SBarry Smith 
14a2ce50c7SBarry Smith 
15ca44d042SBarry Smith EXTERN int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**);
1617ab2063SBarry Smith 
174a2ae208SSatish Balay #undef __FUNCT__
184a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
1936db0b34SBarry Smith int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int **ia,int **ja,PetscTruth *done)
2017ab2063SBarry Smith {
21416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
226945ee14SBarry Smith   int        ierr,i,ishift;
2317ab2063SBarry Smith 
243a40ed3dSBarry Smith   PetscFunctionBegin;
2531625ec5SSatish Balay   *m     = A->m;
263a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
276945ee14SBarry Smith   ishift = a->indexshift;
2853e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
29273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
306945ee14SBarry Smith   } else if (oshift == 0 && ishift == -1) {
31435da068SBarry Smith     int nz = a->i[A->m] - 1;
323b2fbd54SBarry Smith     /* malloc space and  subtract 1 from i and j indices */
33b0a32e0cSBarry Smith     ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr);
34b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr);
353b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] - 1;
36273d9f13SBarry Smith     for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] - 1;
376945ee14SBarry Smith   } else if (oshift == 1 && ishift == 0) {
38435da068SBarry Smith     int nz = a->i[A->m];
393b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
401bc30dd5SBarry Smith     ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr);
41b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr);
423b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
43273d9f13SBarry Smith     for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1;
446945ee14SBarry Smith   } else {
456945ee14SBarry Smith     *ia = a->i; *ja = a->j;
46a2ce50c7SBarry Smith   }
473a40ed3dSBarry Smith   PetscFunctionReturn(0);
48a2744918SBarry Smith }
49a2744918SBarry Smith 
504a2ae208SSatish Balay #undef __FUNCT__
514a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
5236db0b34SBarry Smith int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,PetscTruth *done)
536945ee14SBarry Smith {
546945ee14SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
55606d414cSSatish Balay   int        ishift = a->indexshift,ierr;
566945ee14SBarry Smith 
573a40ed3dSBarry Smith   PetscFunctionBegin;
583a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
5953e63a63SBarry Smith   if ((symmetric && !A->structurally_symmetric) || (oshift == 0 && ishift == -1) || (oshift == 1 && ishift == 0)) {
60606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
61606d414cSSatish Balay     ierr = PetscFree(*ja);CHKERRQ(ierr);
62bcd2baecSBarry Smith   }
633a40ed3dSBarry Smith   PetscFunctionReturn(0);
6417ab2063SBarry Smith }
6517ab2063SBarry Smith 
664a2ae208SSatish Balay #undef __FUNCT__
674a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
6836db0b34SBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int **ia,int **ja,PetscTruth *done)
693b2fbd54SBarry Smith {
703b2fbd54SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
71a93ec695SBarry Smith   int        ierr,i,ishift = a->indexshift,*collengths,*cia,*cja,n = A->n,m = A->m;
72a93ec695SBarry Smith   int        nz = a->i[m]+ishift,row,*jj,mr,col;
733b2fbd54SBarry Smith 
743a40ed3dSBarry Smith   PetscFunctionBegin;
753b2fbd54SBarry Smith   *nn     = A->n;
763a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
773b2fbd54SBarry Smith   if (symmetric) {
78273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
793b2fbd54SBarry Smith   } else {
80b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&collengths);CHKERRQ(ierr);
81549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
82b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&cia);CHKERRQ(ierr);
83b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&cja);CHKERRQ(ierr);
843b2fbd54SBarry Smith     jj = a->j;
853b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
863b2fbd54SBarry Smith       collengths[jj[i] + ishift]++;
873b2fbd54SBarry Smith     }
883b2fbd54SBarry Smith     cia[0] = oshift;
893b2fbd54SBarry Smith     for (i=0; i<n; i++) {
903b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
913b2fbd54SBarry Smith     }
92549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
933b2fbd54SBarry Smith     jj   = a->j;
94a93ec695SBarry Smith     for (row=0; row<m; row++) {
95a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
96a93ec695SBarry Smith       for (i=0; i<mr; i++) {
973b2fbd54SBarry Smith         col = *jj++ + ishift;
983b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
993b2fbd54SBarry Smith       }
1003b2fbd54SBarry Smith     }
101606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1023b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1033b2fbd54SBarry Smith   }
1043a40ed3dSBarry Smith   PetscFunctionReturn(0);
1053b2fbd54SBarry Smith }
1063b2fbd54SBarry Smith 
1074a2ae208SSatish Balay #undef __FUNCT__
1084a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
10936db0b34SBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,PetscTruth *done)
1103b2fbd54SBarry Smith {
111606d414cSSatish Balay   int ierr;
112606d414cSSatish Balay 
1133a40ed3dSBarry Smith   PetscFunctionBegin;
1143a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1153b2fbd54SBarry Smith 
116606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
117606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1183b2fbd54SBarry Smith 
1193a40ed3dSBarry Smith   PetscFunctionReturn(0);
1203b2fbd54SBarry Smith }
1213b2fbd54SBarry Smith 
122227d817aSBarry Smith #define CHUNKSIZE   15
12317ab2063SBarry Smith 
1244a2ae208SSatish Balay #undef __FUNCT__
1254a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
12687828ca2SBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,PetscScalar *v,InsertMode is)
12717ab2063SBarry Smith {
128416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
129416022c9SBarry Smith   int         *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,sorted = a->sorted;
130273d9f13SBarry Smith   int         *imax = a->imax,*ai = a->i,*ailen = a->ilen;
131549d3d68SSatish Balay   int         *aj = a->j,nonew = a->nonew,shift = a->indexshift,ierr;
13287828ca2SBarry Smith   PetscScalar *ap,value,*aa = a->a;
13336db0b34SBarry Smith   PetscTruth  ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE);
134273d9f13SBarry Smith   PetscTruth  roworiented = a->roworiented;
13517ab2063SBarry Smith 
1363a40ed3dSBarry Smith   PetscFunctionBegin;
13717ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
138416022c9SBarry Smith     row  = im[k];
1395ef9f2a5SBarry Smith     if (row < 0) continue;
140aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
141273d9f13SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",row,A->m);
1423b2fbd54SBarry Smith #endif
14317ab2063SBarry Smith     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
14417ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
145416022c9SBarry Smith     low = 0;
14617ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1475ef9f2a5SBarry Smith       if (in[l] < 0) continue;
148aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
149273d9f13SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[l],A->n);
1503b2fbd54SBarry Smith #endif
1514b0e389bSBarry Smith       col = in[l] - shift;
1524b0e389bSBarry Smith       if (roworiented) {
1535ef9f2a5SBarry Smith         value = v[l + k*n];
154bef8e0ddSBarry Smith       } else {
1554b0e389bSBarry Smith         value = v[k + l*m];
1564b0e389bSBarry Smith       }
15736db0b34SBarry Smith       if (value == 0.0 && ignorezeroentries) continue;
15836db0b34SBarry Smith 
159416022c9SBarry Smith       if (!sorted) low = 0; high = nrow;
160416022c9SBarry Smith       while (high-low > 5) {
161416022c9SBarry Smith         t = (low+high)/2;
162416022c9SBarry Smith         if (rp[t] > col) high = t;
163416022c9SBarry Smith         else             low  = t;
16417ab2063SBarry Smith       }
165416022c9SBarry Smith       for (i=low; i<high; i++) {
16617ab2063SBarry Smith         if (rp[i] > col) break;
16717ab2063SBarry Smith         if (rp[i] == col) {
168416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
16917ab2063SBarry Smith           else                  ap[i] = value;
17017ab2063SBarry Smith           goto noinsert;
17117ab2063SBarry Smith         }
17217ab2063SBarry Smith       }
173c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
17429bbc08cSBarry Smith       else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix",row,col);
17517ab2063SBarry Smith       if (nrow >= rmax) {
17617ab2063SBarry Smith         /* there is no extra room in row, therefore enlarge */
177273d9f13SBarry Smith         int         new_nz = ai[A->m] + CHUNKSIZE,len,*new_i,*new_j;
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 */
18387828ca2SBarry Smith         len     = 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);
192416022c9SBarry Smith         len  = (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);
19487828ca2SBarry Smith         ierr = PetscMemcpy(new_a,aa,(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 
3044a2ae208SSatish Balay #undef __FUNCT__
3054a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
306b0a32e0cSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
307416022c9SBarry Smith {
308416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
309fb9695e5SSatish Balay   int               ierr,i,j,m = A->m,shift = a->indexshift;
310fb9695e5SSatish Balay   char              *name;
311f3ef73ceSBarry Smith   PetscViewerFormat format;
31217ab2063SBarry Smith 
3133a40ed3dSBarry Smith   PetscFunctionBegin;
314435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
315b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
316fb9695e5SSatish Balay   if (format == PETSC_VIEWER_ASCII_INFO_LONG || format == PETSC_VIEWER_ASCII_INFO) {
3176831982aSBarry Smith     if (a->inode.size) {
318b0a32e0cSBarry 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);
3196831982aSBarry Smith     } else {
320b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"not using I-node routines\n");CHKERRQ(ierr);
3216831982aSBarry Smith     }
322fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
323d00d2cf4SBarry Smith     int nofinalvalue = 0;
324273d9f13SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) {
325d00d2cf4SBarry Smith       nofinalvalue = 1;
326d00d2cf4SBarry Smith     }
327b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
328b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %d %d \n",m,A->n);CHKERRQ(ierr);
329b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %d \n",a->nz);CHKERRQ(ierr);
330b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
331b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
33217ab2063SBarry Smith 
33317ab2063SBarry Smith     for (i=0; i<m; i++) {
334416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
335aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
336b0a32e0cSBarry 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);
33717ab2063SBarry Smith #else
338b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
33917ab2063SBarry Smith #endif
34017ab2063SBarry Smith       }
34117ab2063SBarry Smith     }
342d00d2cf4SBarry Smith     if (nofinalvalue) {
343b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",m,A->n,0.0);CHKERRQ(ierr);
344d00d2cf4SBarry Smith     }
345fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
346b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
347fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
348b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
34944cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
350b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
35144cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
352aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
35336db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
354b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g + %g i",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
35536db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
356b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g - %g i",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
35736db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
358b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3596831982aSBarry Smith         }
36044cd7ae7SLois Curfman McInnes #else
361b0a32e0cSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
36244cd7ae7SLois Curfman McInnes #endif
36344cd7ae7SLois Curfman McInnes       }
364b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
36544cd7ae7SLois Curfman McInnes     }
366b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
367fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
368496be53dSLois Curfman McInnes     int nzd=0,fshift=1,*sptr;
369b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
370b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&sptr);CHKERRQ(ierr);
371496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
372496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
373496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
374496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
375aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
37636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
377496be53dSLois Curfman McInnes #else
378496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
379496be53dSLois Curfman McInnes #endif
380496be53dSLois Curfman McInnes         }
381496be53dSLois Curfman McInnes       }
382496be53dSLois Curfman McInnes     }
3832e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
384b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %d %d\n\n",m,nzd);CHKERRQ(ierr);
3852e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
386b0a32e0cSBarry 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);}
387b0a32e0cSBarry 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);}
388b0a32e0cSBarry 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);}
389b0a32e0cSBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
390b0a32e0cSBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %d %d\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
391b0a32e0cSBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %d\n",sptr[i]);CHKERRQ(ierr);}
392496be53dSLois Curfman McInnes     }
393b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
394606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
395496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
396496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
397b0a32e0cSBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %d ",a->j[j]+fshift);CHKERRQ(ierr);}
398496be53dSLois Curfman McInnes       }
399b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
400496be53dSLois Curfman McInnes     }
401b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
402496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
403496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
404496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
405aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
40636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
407b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4086831982aSBarry Smith           }
409496be53dSLois Curfman McInnes #else
410b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
411496be53dSLois Curfman McInnes #endif
412496be53dSLois Curfman McInnes         }
413496be53dSLois Curfman McInnes       }
414b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
415496be53dSLois Curfman McInnes     }
416b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
417fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
41802594712SBarry Smith     int         cnt = 0,jcnt;
41987828ca2SBarry Smith     PetscScalar value;
42002594712SBarry Smith 
421b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
42202594712SBarry Smith     for (i=0; i<m; i++) {
42302594712SBarry Smith       jcnt = 0;
424273d9f13SBarry Smith       for (j=0; j<A->n; j++) {
425e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
42602594712SBarry Smith           value = a->a[cnt++];
427e24b481bSBarry Smith           jcnt++;
42802594712SBarry Smith         } else {
42902594712SBarry Smith           value = 0.0;
43002594712SBarry Smith         }
431aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
432b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
43302594712SBarry Smith #else
434b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
43502594712SBarry Smith #endif
43602594712SBarry Smith       }
437b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
43802594712SBarry Smith     }
439b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4403a40ed3dSBarry Smith   } else {
441b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
44217ab2063SBarry Smith     for (i=0; i<m; i++) {
443b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
444416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
445aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
44636db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
447b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g + %g i",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
44836db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
449b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g - %g i",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4503a40ed3dSBarry Smith         } else {
451b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
45217ab2063SBarry Smith         }
45317ab2063SBarry Smith #else
454b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
45517ab2063SBarry Smith #endif
45617ab2063SBarry Smith       }
457b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45817ab2063SBarry Smith     }
459b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
46017ab2063SBarry Smith   }
461b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4623a40ed3dSBarry Smith   PetscFunctionReturn(0);
463416022c9SBarry Smith }
464416022c9SBarry Smith 
4654a2ae208SSatish Balay #undef __FUNCT__
4664a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
467b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
468416022c9SBarry Smith {
469480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
470416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
471273d9f13SBarry Smith   int               ierr,i,j,m = A->m,shift = a->indexshift,color;
47236db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
473b0a32e0cSBarry Smith   PetscViewer       viewer;
474f3ef73ceSBarry Smith   PetscViewerFormat format;
475cddf8d76SBarry Smith 
4763a40ed3dSBarry Smith   PetscFunctionBegin;
477480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
478b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
47919bcc07fSBarry Smith 
480b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
481416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4820513a670SBarry Smith 
483fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
4840513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
485b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
486416022c9SBarry Smith     for (i=0; i<m; i++) {
487cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
488416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
489cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
490aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
49136db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
492cddf8d76SBarry Smith #else
493cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
494cddf8d76SBarry Smith #endif
495b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
496cddf8d76SBarry Smith       }
497cddf8d76SBarry Smith     }
498b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
499cddf8d76SBarry Smith     for (i=0; i<m; i++) {
500cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
501cddf8d76SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
502cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
503cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
504b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
505cddf8d76SBarry Smith       }
506cddf8d76SBarry Smith     }
507b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
508cddf8d76SBarry Smith     for (i=0; i<m; i++) {
509cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
510cddf8d76SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
511cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
512aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
51336db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
514cddf8d76SBarry Smith #else
515cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
516cddf8d76SBarry Smith #endif
517b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
518416022c9SBarry Smith       }
519416022c9SBarry Smith     }
5200513a670SBarry Smith   } else {
5210513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5220513a670SBarry Smith     /* first determine max of all nonzero values */
5230513a670SBarry Smith     int    nz = a->nz,count;
524b0a32e0cSBarry Smith     PetscDraw   popup;
52536db0b34SBarry Smith     PetscReal scale;
5260513a670SBarry Smith 
5270513a670SBarry Smith     for (i=0; i<nz; i++) {
5280513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5290513a670SBarry Smith     }
530b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
531b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
532b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5330513a670SBarry Smith     count = 0;
5340513a670SBarry Smith     for (i=0; i<m; i++) {
5350513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
5360513a670SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5370513a670SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
538b0a32e0cSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count]));
539b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5400513a670SBarry Smith         count++;
5410513a670SBarry Smith       }
5420513a670SBarry Smith     }
5430513a670SBarry Smith   }
544480ef9eaSBarry Smith   PetscFunctionReturn(0);
545480ef9eaSBarry Smith }
546cddf8d76SBarry Smith 
5474a2ae208SSatish Balay #undef __FUNCT__
5484a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
549b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
550480ef9eaSBarry Smith {
551480ef9eaSBarry Smith   int        ierr;
552b0a32e0cSBarry Smith   PetscDraw  draw;
55336db0b34SBarry Smith   PetscReal  xr,yr,xl,yl,h,w;
554480ef9eaSBarry Smith   PetscTruth isnull;
555480ef9eaSBarry Smith 
556480ef9eaSBarry Smith   PetscFunctionBegin;
557b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
558b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
559480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
560480ef9eaSBarry Smith 
561480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
562273d9f13SBarry Smith   xr  = A->n; yr = A->m; h = yr/10.0; w = xr/10.0;
563480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
564b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
565b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
566480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5673a40ed3dSBarry Smith   PetscFunctionReturn(0);
568416022c9SBarry Smith }
569416022c9SBarry Smith 
5704a2ae208SSatish Balay #undef __FUNCT__
5714a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
572b0a32e0cSBarry Smith int MatView_SeqAIJ(Mat A,PetscViewer viewer)
573416022c9SBarry Smith {
574416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
575bcd2baecSBarry Smith   int         ierr;
5766831982aSBarry Smith   PetscTruth  issocket,isascii,isbinary,isdraw;
577416022c9SBarry Smith 
5783a40ed3dSBarry Smith   PetscFunctionBegin;
579b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
580b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
581fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
582fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
5830f5bd95cSBarry Smith   if (issocket) {
58401820c62SBarry Smith     if (a->indexshift) {
58529bbc08cSBarry Smith       SETERRQ(1,"Can only socket send sparse matrix with 0 based indexing");
58601820c62SBarry Smith     }
587b0a32e0cSBarry Smith     ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr);
5880f5bd95cSBarry Smith   } else if (isascii) {
5893a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5900f5bd95cSBarry Smith   } else if (isbinary) {
5913a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5920f5bd95cSBarry Smith   } else if (isdraw) {
5933a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
5945cd90555SBarry Smith   } else {
59529bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
59617ab2063SBarry Smith   }
5973a40ed3dSBarry Smith   PetscFunctionReturn(0);
59817ab2063SBarry Smith }
59919bcc07fSBarry Smith 
6003a7fca6bSBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat,PetscTruth);
6014a2ae208SSatish Balay #undef __FUNCT__
6024a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
6038f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
60417ab2063SBarry Smith {
605416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
60641c01911SSatish Balay   int          fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax,ierr;
607273d9f13SBarry Smith   int          m = A->m,*ip,N,*ailen = a->ilen,shift = a->indexshift,rmax = 0;
60887828ca2SBarry Smith   PetscScalar  *aa = a->a,*ap;
60917ab2063SBarry Smith 
6103a40ed3dSBarry Smith   PetscFunctionBegin;
6113a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
61217ab2063SBarry Smith 
61343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
61417ab2063SBarry Smith   for (i=1; i<m; i++) {
615416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
61617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
61794a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
61817ab2063SBarry Smith     if (fshift) {
6190f5bd95cSBarry Smith       ip = aj + ai[i] + shift;
6200f5bd95cSBarry Smith       ap = aa + ai[i] + shift;
62117ab2063SBarry Smith       N  = ailen[i];
62217ab2063SBarry Smith       for (j=0; j<N; j++) {
62317ab2063SBarry Smith         ip[j-fshift] = ip[j];
62417ab2063SBarry Smith         ap[j-fshift] = ap[j];
62517ab2063SBarry Smith       }
62617ab2063SBarry Smith     }
62717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
62817ab2063SBarry Smith   }
62917ab2063SBarry Smith   if (m) {
63017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
63117ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
63217ab2063SBarry Smith   }
63317ab2063SBarry Smith   /* reset ilen and imax for each row */
63417ab2063SBarry Smith   for (i=0; i<m; i++) {
63517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
63617ab2063SBarry Smith   }
637416022c9SBarry Smith   a->nz = ai[m] + shift;
63817ab2063SBarry Smith 
63917ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
640416022c9SBarry Smith   if (fshift && a->diag) {
641606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
642b0a32e0cSBarry Smith     PetscLogObjectMemory(A,-(m+1)*sizeof(int));
643416022c9SBarry Smith     a->diag = 0;
64417ab2063SBarry Smith   }
645b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded,%d used\n",m,A->n,fshift,a->nz);
646b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %d\n",a->reallocs);
647b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax);
648dd5f02e7SSatish Balay   a->reallocs          = 0;
6494e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
65036db0b34SBarry Smith   a->rmax              = rmax;
6514e220ebcSLois Curfman McInnes 
65276dd722bSSatish Balay   /* check out for identical nodes. If found, use inode functions */
6533a7fca6bSBarry Smith   ierr = Mat_AIJ_CheckInode(A,(PetscTruth)(!fshift));CHKERRQ(ierr);
6543a40ed3dSBarry Smith   PetscFunctionReturn(0);
65517ab2063SBarry Smith }
65617ab2063SBarry Smith 
6574a2ae208SSatish Balay #undef __FUNCT__
6584a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
6598f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A)
66017ab2063SBarry Smith {
661416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
662549d3d68SSatish Balay   int        ierr;
6633a40ed3dSBarry Smith 
6643a40ed3dSBarry Smith   PetscFunctionBegin;
66587828ca2SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->m]+a->indexshift)*sizeof(PetscScalar));CHKERRQ(ierr);
6663a40ed3dSBarry Smith   PetscFunctionReturn(0);
66717ab2063SBarry Smith }
668416022c9SBarry Smith 
6694a2ae208SSatish Balay #undef __FUNCT__
6704a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
671e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A)
67217ab2063SBarry Smith {
673416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
67482bf6240SBarry Smith   int        ierr;
675d5d45c9bSBarry Smith 
6763a40ed3dSBarry Smith   PetscFunctionBegin;
677aa482453SBarry Smith #if defined(PETSC_USE_LOG)
678b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",A->m,A->n,a->nz);
67917ab2063SBarry Smith #endif
68036db0b34SBarry Smith   if (a->freedata) {
681606d414cSSatish Balay     ierr = PetscFree(a->a);CHKERRQ(ierr);
682606d414cSSatish Balay     if (!a->singlemalloc) {
683606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
684606d414cSSatish Balay       ierr = PetscFree(a->j);CHKERRQ(ierr);
685606d414cSSatish Balay     }
68636db0b34SBarry Smith   }
687c38d4ed2SBarry Smith   if (a->row) {
688c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
689c38d4ed2SBarry Smith   }
690c38d4ed2SBarry Smith   if (a->col) {
691c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
692c38d4ed2SBarry Smith   }
693606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
694606d414cSSatish Balay   if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);}
695606d414cSSatish Balay   if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);}
696273d9f13SBarry Smith   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
697606d414cSSatish Balay   if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
698606d414cSSatish Balay   if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
69982bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
700606d414cSSatish Balay   if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);}
701cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
702606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
7033a40ed3dSBarry Smith   PetscFunctionReturn(0);
70417ab2063SBarry Smith }
70517ab2063SBarry Smith 
7064a2ae208SSatish Balay #undef __FUNCT__
7074a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
7088f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
70917ab2063SBarry Smith {
7103a40ed3dSBarry Smith   PetscFunctionBegin;
7113a40ed3dSBarry Smith   PetscFunctionReturn(0);
71217ab2063SBarry Smith }
71317ab2063SBarry Smith 
7144a2ae208SSatish Balay #undef __FUNCT__
7154a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7168f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
71717ab2063SBarry Smith {
718416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7193a40ed3dSBarry Smith 
7203a40ed3dSBarry Smith   PetscFunctionBegin;
721a65d3064SKris Buschelman   switch (op) {
722a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
723a65d3064SKris Buschelman       a->roworiented       = PETSC_TRUE;
724a65d3064SKris Buschelman       break;
725a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
726a65d3064SKris Buschelman       a->keepzeroedrows    = PETSC_TRUE;
727a65d3064SKris Buschelman       break;
728a65d3064SKris Buschelman     case MAT_COLUMN_ORIENTED:
729a65d3064SKris Buschelman       a->roworiented       = PETSC_FALSE;
730a65d3064SKris Buschelman       break;
731a65d3064SKris Buschelman     case MAT_COLUMNS_SORTED:
732a65d3064SKris Buschelman       a->sorted            = PETSC_TRUE;
733a65d3064SKris Buschelman       break;
734a65d3064SKris Buschelman     case MAT_COLUMNS_UNSORTED:
735a65d3064SKris Buschelman       a->sorted            = PETSC_FALSE;
736a65d3064SKris Buschelman       break;
737a65d3064SKris Buschelman     case MAT_NO_NEW_NONZERO_LOCATIONS:
738a65d3064SKris Buschelman       a->nonew             = 1;
739a65d3064SKris Buschelman       break;
740a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
741a65d3064SKris Buschelman       a->nonew             = -1;
742a65d3064SKris Buschelman       break;
743a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
744a65d3064SKris Buschelman       a->nonew             = -2;
745a65d3064SKris Buschelman       break;
746a65d3064SKris Buschelman     case MAT_YES_NEW_NONZERO_LOCATIONS:
747a65d3064SKris Buschelman       a->nonew             = 0;
748a65d3064SKris Buschelman       break;
749a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
750a65d3064SKris Buschelman       a->ignorezeroentries = PETSC_TRUE;
751a65d3064SKris Buschelman       break;
752a65d3064SKris Buschelman     case MAT_USE_INODES:
753a65d3064SKris Buschelman       a->inode.use         = PETSC_TRUE;
754a65d3064SKris Buschelman       break;
755a65d3064SKris Buschelman     case MAT_DO_NOT_USE_INODES:
756a65d3064SKris Buschelman       a->inode.use         = PETSC_FALSE;
757a65d3064SKris Buschelman       break;
758a65d3064SKris Buschelman     case MAT_ROWS_SORTED:
759a65d3064SKris Buschelman     case MAT_ROWS_UNSORTED:
760a65d3064SKris Buschelman     case MAT_YES_NEW_DIAGONALS:
761a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
762a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
763d03495bdSKris Buschelman     case MAT_USE_SINGLE_PRECISION_SOLVES:
764b0a32e0cSBarry Smith       PetscLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
765a65d3064SKris Buschelman       break;
766a65d3064SKris Buschelman     case MAT_NO_NEW_DIAGONALS:
76729bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
768a65d3064SKris Buschelman       break;
769a65d3064SKris Buschelman     case MAT_INODE_LIMIT_1:
770a65d3064SKris Buschelman       a->inode.limit  = 1;
771a65d3064SKris Buschelman       break;
772a65d3064SKris Buschelman     case MAT_INODE_LIMIT_2:
773a65d3064SKris Buschelman       a->inode.limit  = 2;
774a65d3064SKris Buschelman       break;
775a65d3064SKris Buschelman     case MAT_INODE_LIMIT_3:
776a65d3064SKris Buschelman       a->inode.limit  = 3;
777a65d3064SKris Buschelman       break;
778a65d3064SKris Buschelman     case MAT_INODE_LIMIT_4:
779a65d3064SKris Buschelman       a->inode.limit  = 4;
780a65d3064SKris Buschelman       break;
781a65d3064SKris Buschelman     case MAT_INODE_LIMIT_5:
782a65d3064SKris Buschelman       a->inode.limit  = 5;
783a65d3064SKris Buschelman       break;
784a65d3064SKris Buschelman     default:
785a65d3064SKris Buschelman       SETERRQ(PETSC_ERR_SUP,"unknown option");
786a65d3064SKris Buschelman       break;
787a65d3064SKris Buschelman   }
7883a40ed3dSBarry Smith   PetscFunctionReturn(0);
78917ab2063SBarry Smith }
79017ab2063SBarry Smith 
7914a2ae208SSatish Balay #undef __FUNCT__
7924a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
7938f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
79417ab2063SBarry Smith {
795416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
7963a40ed3dSBarry Smith   int          i,j,n,shift = a->indexshift,ierr;
79787828ca2SBarry Smith   PetscScalar  *x,zero = 0.0;
79817ab2063SBarry Smith 
7993a40ed3dSBarry Smith   PetscFunctionBegin;
8003a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
80136db0b34SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
80236db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
803273d9f13SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
804273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
805416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
806416022c9SBarry Smith       if (a->j[j]+shift == i) {
807416022c9SBarry Smith         x[i] = a->a[j];
80817ab2063SBarry Smith         break;
80917ab2063SBarry Smith       }
81017ab2063SBarry Smith     }
81117ab2063SBarry Smith   }
81236db0b34SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8133a40ed3dSBarry Smith   PetscFunctionReturn(0);
81417ab2063SBarry Smith }
81517ab2063SBarry Smith 
816d5d45c9bSBarry Smith 
8174a2ae208SSatish Balay #undef __FUNCT__
8184a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
8197c922b88SBarry Smith int MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
82017ab2063SBarry Smith {
821416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
8225c897100SBarry Smith   PetscScalar  *x,*y;
8235c897100SBarry Smith   int          ierr,m = A->m,shift = a->indexshift;
8245c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
8255c897100SBarry Smith   PetscScalar  *v,alpha;
8265c897100SBarry Smith   int          n,i,*idx;
8275c897100SBarry Smith #endif
82817ab2063SBarry Smith 
8293a40ed3dSBarry Smith   PetscFunctionBegin;
8302e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
831e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
832e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
83317ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
8345c897100SBarry Smith 
8355c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
8365c897100SBarry Smith   fortranmulttransposeaddaij_(&m,x,a->i,a->j+shift,a->a+shift,y);
8375c897100SBarry Smith #else
83817ab2063SBarry Smith   for (i=0; i<m; i++) {
839416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
840416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
841416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
84217ab2063SBarry Smith     alpha = x[i];
84317ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
84417ab2063SBarry Smith   }
8455c897100SBarry Smith #endif
846b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
847e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
848e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8493a40ed3dSBarry Smith   PetscFunctionReturn(0);
85017ab2063SBarry Smith }
85117ab2063SBarry Smith 
8524a2ae208SSatish Balay #undef __FUNCT__
8535c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
8545c897100SBarry Smith int MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8555c897100SBarry Smith {
8568d5b0100SBarry Smith   PetscScalar  zero = 0.0;
8575c897100SBarry Smith   int          ierr;
8585c897100SBarry Smith 
8595c897100SBarry Smith   PetscFunctionBegin;
8605c897100SBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
8615c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
8625c897100SBarry Smith   PetscFunctionReturn(0);
8635c897100SBarry Smith }
8645c897100SBarry Smith 
8655c897100SBarry Smith 
8665c897100SBarry Smith #undef __FUNCT__
8674a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
86844cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
86917ab2063SBarry Smith {
870416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
87187828ca2SBarry Smith   PetscScalar  *x,*y,*v,sum;
872273d9f13SBarry Smith   int          ierr,m = A->m,*idx,shift = a->indexshift,*ii;
873aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
874e36a17ebSSatish Balay   int          n,i,jrow,j;
875e36a17ebSSatish Balay #endif
87617ab2063SBarry Smith 
87747c8b664SMatthew Knepley #if defined(HAVE_PRAGMA_DISJOINT)
878fee21e36SBarry Smith #pragma disjoint(*x,*y,*v)
879fee21e36SBarry Smith #endif
880fee21e36SBarry Smith 
8813a40ed3dSBarry Smith   PetscFunctionBegin;
882e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
883e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
88417ab2063SBarry Smith   x    = x + shift;    /* shift for Fortran start by 1 indexing */
885416022c9SBarry Smith   idx  = a->j;
886d64ed03dSBarry Smith   v    = a->a;
887416022c9SBarry Smith   ii   = a->i;
888aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
88929b5ca8aSSatish Balay   fortranmultaij_(&m,x,ii,idx+shift,v+shift,y);
8908d195f9aSBarry Smith #else
891d64ed03dSBarry Smith   v    += shift; /* shift for Fortran start by 1 indexing */
892d64ed03dSBarry Smith   idx  += shift;
89317ab2063SBarry Smith   for (i=0; i<m; i++) {
8949ea0dfa2SSatish Balay     jrow = ii[i];
8959ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
89617ab2063SBarry Smith     sum  = 0.0;
8979ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
8989ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8999ea0dfa2SSatish Balay      }
90017ab2063SBarry Smith     y[i] = sum;
90117ab2063SBarry Smith   }
9028d195f9aSBarry Smith #endif
903b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz - m);
904e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
905e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9063a40ed3dSBarry Smith   PetscFunctionReturn(0);
90717ab2063SBarry Smith }
90817ab2063SBarry Smith 
9094a2ae208SSatish Balay #undef __FUNCT__
9104a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
91144cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
91217ab2063SBarry Smith {
913416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
91487828ca2SBarry Smith   PetscScalar  *x,*y,*z,*v,sum;
915273d9f13SBarry Smith   int          ierr,m = A->m,*idx,shift = a->indexshift,*ii;
916aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
917e36a17ebSSatish Balay   int          n,i,jrow,j;
918e36a17ebSSatish Balay #endif
9199ea0dfa2SSatish Balay 
9203a40ed3dSBarry Smith   PetscFunctionBegin;
921e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
922e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9232e8a6d31SBarry Smith   if (zz != yy) {
924e1311b90SBarry Smith     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9252e8a6d31SBarry Smith   } else {
9262e8a6d31SBarry Smith     z = y;
9272e8a6d31SBarry Smith   }
92817ab2063SBarry Smith   x    = x + shift; /* shift for Fortran start by 1 indexing */
929cddf8d76SBarry Smith   idx  = a->j;
930d64ed03dSBarry Smith   v    = a->a;
931cddf8d76SBarry Smith   ii   = a->i;
932aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
93329b5ca8aSSatish Balay   fortranmultaddaij_(&m,x,ii,idx+shift,v+shift,y,z);
93402ab625aSSatish Balay #else
935d64ed03dSBarry Smith   v   += shift; /* shift for Fortran start by 1 indexing */
936d64ed03dSBarry Smith   idx += shift;
93717ab2063SBarry Smith   for (i=0; i<m; i++) {
9389ea0dfa2SSatish Balay     jrow = ii[i];
9399ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
94017ab2063SBarry Smith     sum  = y[i];
9419ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
9429ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9439ea0dfa2SSatish Balay      }
94417ab2063SBarry Smith     z[i] = sum;
94517ab2063SBarry Smith   }
94602ab625aSSatish Balay #endif
947b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
948e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
949e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9502e8a6d31SBarry Smith   if (zz != yy) {
951e1311b90SBarry Smith     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
9522e8a6d31SBarry Smith   }
9533a40ed3dSBarry Smith   PetscFunctionReturn(0);
95417ab2063SBarry Smith }
95517ab2063SBarry Smith 
95617ab2063SBarry Smith /*
95717ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
95817ab2063SBarry Smith */
9594a2ae208SSatish Balay #undef __FUNCT__
9604a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
961f1e2ffcdSBarry Smith int MatMarkDiagonal_SeqAIJ(Mat A)
96217ab2063SBarry Smith {
963416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
964b0a32e0cSBarry Smith   int        i,j,*diag,m = A->m,shift = a->indexshift,ierr;
96517ab2063SBarry Smith 
9663a40ed3dSBarry Smith   PetscFunctionBegin;
967f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
968f1e2ffcdSBarry Smith 
969b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&diag);CHKERRQ(ierr);
970b0a32e0cSBarry Smith   PetscLogObjectMemory(A,(m+1)*sizeof(int));
971273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
97235b0346bSBarry Smith     diag[i] = a->i[i+1];
973416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
974416022c9SBarry Smith       if (a->j[j]+shift == i) {
97517ab2063SBarry Smith         diag[i] = j - shift;
97617ab2063SBarry Smith         break;
97717ab2063SBarry Smith       }
97817ab2063SBarry Smith     }
97917ab2063SBarry Smith   }
980416022c9SBarry Smith   a->diag = diag;
9813a40ed3dSBarry Smith   PetscFunctionReturn(0);
98217ab2063SBarry Smith }
98317ab2063SBarry Smith 
984be5855fcSBarry Smith /*
985be5855fcSBarry Smith      Checks for missing diagonals
986be5855fcSBarry Smith */
9874a2ae208SSatish Balay #undef __FUNCT__
9884a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
989f1e2ffcdSBarry Smith int MatMissingDiagonal_SeqAIJ(Mat A)
990be5855fcSBarry Smith {
991be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
992f1e2ffcdSBarry Smith   int        *diag,*jj = a->j,i,shift = a->indexshift,ierr;
993be5855fcSBarry Smith 
994be5855fcSBarry Smith   PetscFunctionBegin;
995f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
996f1e2ffcdSBarry Smith   diag = a->diag;
997273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
998be5855fcSBarry Smith     if (jj[diag[i]+shift] != i-shift) {
99929bbc08cSBarry Smith       SETERRQ1(1,"Matrix is missing diagonal number %d",i);
1000be5855fcSBarry Smith     }
1001be5855fcSBarry Smith   }
1002be5855fcSBarry Smith   PetscFunctionReturn(0);
1003be5855fcSBarry Smith }
1004be5855fcSBarry Smith 
10054a2ae208SSatish Balay #undef __FUNCT__
10064a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
1007*c14dc6b6SHong Zhang int MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
100817ab2063SBarry Smith {
1009416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
101087828ca2SBarry Smith   PetscScalar  *x,*b,*bs, d,*xs,sum,*v = a->a,*t=0,scale,*ts,*xb,*idiag=0;
1011273d9f13SBarry Smith   int          ierr,*idx,*diag,n = A->n,m = A->m,i,shift = a->indexshift;
101217ab2063SBarry Smith 
10133a40ed3dSBarry Smith   PetscFunctionBegin;
1014e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1015fb2e594dSBarry Smith   if (xx != bb) {
1016e1311b90SBarry Smith     ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
1017fb2e594dSBarry Smith   } else {
1018fb2e594dSBarry Smith     b = x;
1019fb2e594dSBarry Smith   }
1020fb2e594dSBarry Smith 
1021f1e2ffcdSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1022416022c9SBarry Smith   diag = a->diag;
1023416022c9SBarry Smith   xs   = x + shift; /* shifted by one for index start of a or a->j*/
102417ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
102517ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
102617ab2063SBarry Smith     bs = b + shift;
102717ab2063SBarry Smith     for (i=0; i<m; i++) {
1028416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1029416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1030b0a32e0cSBarry Smith 	PetscLogFlops(2*n-1);
1031416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1032416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
103317ab2063SBarry Smith         sum  = b[i]*d/omega;
103417ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
103517ab2063SBarry Smith         x[i] = sum;
103617ab2063SBarry Smith     }
1037cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1038fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
10393a40ed3dSBarry Smith     PetscFunctionReturn(0);
104017ab2063SBarry Smith   }
1041c783ea89SBarry Smith 
1042c783ea89SBarry Smith   /* setup workspace for Eisenstat */
1043c783ea89SBarry Smith   if (flag & SOR_EISENSTAT) {
1044c783ea89SBarry Smith     if (!a->idiag) {
104587828ca2SBarry Smith       ierr     = PetscMalloc(2*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
1046c783ea89SBarry Smith       a->ssor  = a->idiag + m;
1047c783ea89SBarry Smith       v        = a->a;
1048c783ea89SBarry Smith       for (i=0; i<m; i++) { a->idiag[i] = 1.0/v[diag[i]];}
1049c783ea89SBarry Smith     }
1050c783ea89SBarry Smith     t     = a->ssor;
1051c783ea89SBarry Smith     idiag = a->idiag;
1052c783ea89SBarry Smith   }
1053fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1054fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1055fc3d8934SBarry Smith 
1056fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1057fc3d8934SBarry Smith 
1058fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1059fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
106048af12d7SBarry Smith     is the relaxation factor.
1061fc3d8934SBarry Smith     */
1062fc3d8934SBarry Smith 
106348af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
106429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
106548af12d7SBarry Smith   } else if ((flag & SOR_EISENSTAT) && omega == 1.0 && shift == 0 && fshift == 0.0) {
106648af12d7SBarry Smith     /* special case for omega = 1.0 saves flops and some integer ops */
106787828ca2SBarry Smith     PetscScalar *v2;
106848af12d7SBarry Smith 
1069733d66baSBarry Smith     v2    = a->a;
1070fc3d8934SBarry Smith     /*  x = (E + U)^{-1} b */
1071fc3d8934SBarry Smith     for (i=m-1; i>=0; i--) {
1072fc3d8934SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1073fc3d8934SBarry Smith       idx  = a->j + diag[i] + 1;
1074fc3d8934SBarry Smith       v    = a->a + diag[i] + 1;
1075fc3d8934SBarry Smith       sum  = b[i];
1076fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1077b4632166SBarry Smith       x[i] = sum*idiag[i];
1078fc3d8934SBarry Smith 
1079fc3d8934SBarry Smith       /*  t = b - (2*E - D)x */
1080733d66baSBarry Smith       t[i] = b[i] - (v2[diag[i]])*x[i];
1081733d66baSBarry Smith     }
1082fc3d8934SBarry Smith 
1083fc3d8934SBarry Smith     /*  t = (E + L)^{-1}t */
1084fc3d8934SBarry Smith     diag = a->diag;
1085fc3d8934SBarry Smith     for (i=0; i<m; i++) {
1086fc3d8934SBarry Smith       n    = diag[i] - a->i[i];
1087fc3d8934SBarry Smith       idx  = a->j + a->i[i];
1088fc3d8934SBarry Smith       v    = a->a + a->i[i];
1089fc3d8934SBarry Smith       sum  = t[i];
1090fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,t,v,idx,n);
1091b4632166SBarry Smith       t[i]  = sum*idiag[i];
1092fc3d8934SBarry Smith 
1093fc3d8934SBarry Smith       /*  x = x + t */
1094733d66baSBarry Smith       x[i] += t[i];
1095733d66baSBarry Smith     }
1096733d66baSBarry Smith 
1097b0a32e0cSBarry Smith     PetscLogFlops(3*m-1 + 2*a->nz);
1098fc3d8934SBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1099fc3d8934SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
1100fc3d8934SBarry Smith     PetscFunctionReturn(0);
11013a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
110217ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
110317ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
110417ab2063SBarry Smith 
110517ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
110617ab2063SBarry Smith 
110717ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
110817ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
110917ab2063SBarry Smith     is the relaxation factor.
111017ab2063SBarry Smith     */
111117ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
111217ab2063SBarry Smith 
111317ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
111417ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1115416022c9SBarry Smith       d    = fshift + a->a[diag[i] + shift];
1116416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1117416022c9SBarry Smith       idx  = a->j + diag[i] + (!shift);
1118416022c9SBarry Smith       v    = a->a + diag[i] + (!shift);
111917ab2063SBarry Smith       sum  = b[i];
112017ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
112117ab2063SBarry Smith       x[i] = omega*(sum/d);
112217ab2063SBarry Smith     }
112317ab2063SBarry Smith 
112417ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1125416022c9SBarry Smith     v = a->a;
112617ab2063SBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; }
112717ab2063SBarry Smith 
112817ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1129416022c9SBarry Smith     ts = t + shift; /* shifted by one for index start of a or a->j*/
1130416022c9SBarry Smith     diag = a->diag;
113117ab2063SBarry Smith     for (i=0; i<m; i++) {
1132416022c9SBarry Smith       d    = fshift + a->a[diag[i]+shift];
1133416022c9SBarry Smith       n    = diag[i] - a->i[i];
1134416022c9SBarry Smith       idx  = a->j + a->i[i] + shift;
1135416022c9SBarry Smith       v    = a->a + a->i[i] + shift;
113617ab2063SBarry Smith       sum  = t[i];
113717ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
113817ab2063SBarry Smith       t[i] = omega*(sum/d);
1139733d66baSBarry Smith       /*  x = x + t */
1140733d66baSBarry Smith       x[i] += t[i];
114117ab2063SBarry Smith     }
114217ab2063SBarry Smith 
1143b0a32e0cSBarry Smith     PetscLogFlops(6*m-1 + 2*a->nz);
1144cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1145fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
11463a40ed3dSBarry Smith     PetscFunctionReturn(0);
114717ab2063SBarry Smith   }
114817ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
114917ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
115077d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
115177d8c4bbSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,diag,a->a,b);
115277d8c4bbSBarry Smith #else
115317ab2063SBarry Smith       for (i=0; i<m; i++) {
1154416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1155416022c9SBarry Smith         n    = diag[i] - a->i[i];
1156b0a32e0cSBarry Smith 	PetscLogFlops(2*n-1);
1157416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1158416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
115917ab2063SBarry Smith         sum  = b[i];
116017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
116117ab2063SBarry Smith         x[i] = omega*(sum/d);
116217ab2063SBarry Smith       }
116377d8c4bbSBarry Smith #endif
116417ab2063SBarry Smith       xb = x;
11653a40ed3dSBarry Smith     } else xb = b;
116617ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
116717ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
116817ab2063SBarry Smith       for (i=0; i<m; i++) {
1169416022c9SBarry Smith         x[i] *= a->a[diag[i]+shift];
117017ab2063SBarry Smith       }
1171b0a32e0cSBarry Smith       PetscLogFlops(m);
117217ab2063SBarry Smith     }
117317ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
117477d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
117577d8c4bbSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,diag,a->a,xb);
117677d8c4bbSBarry Smith #else
117717ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1178416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1179416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1180b0a32e0cSBarry Smith 	PetscLogFlops(2*n-1);
1181416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1182416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
118317ab2063SBarry Smith         sum  = xb[i];
118417ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
118517ab2063SBarry Smith         x[i] = omega*(sum/d);
118617ab2063SBarry Smith       }
118777d8c4bbSBarry Smith #endif
118817ab2063SBarry Smith     }
118917ab2063SBarry Smith     its--;
119017ab2063SBarry Smith   }
119117ab2063SBarry Smith   while (its--) {
119217ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
119377d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
119477d8c4bbSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,diag,a->a,b);
119577d8c4bbSBarry Smith #else
119617ab2063SBarry Smith       for (i=0; i<m; i++) {
1197416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1198416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1199b0a32e0cSBarry Smith 	PetscLogFlops(2*n-1);
1200416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1201416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
120217ab2063SBarry Smith         sum  = b[i];
120317ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
12047e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
120517ab2063SBarry Smith       }
120677d8c4bbSBarry Smith #endif
120717ab2063SBarry Smith     }
120817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
120977d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
121077d8c4bbSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,diag,a->a,b);
121177d8c4bbSBarry Smith #else
121217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1213416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1214416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1215b0a32e0cSBarry Smith 	PetscLogFlops(2*n-1);
1216416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1217416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
121817ab2063SBarry Smith         sum  = b[i];
121917ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
12207e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
122117ab2063SBarry Smith       }
122277d8c4bbSBarry Smith #endif
122317ab2063SBarry Smith     }
122417ab2063SBarry Smith   }
1225cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1226fb2e594dSBarry Smith   if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
12273a40ed3dSBarry Smith   PetscFunctionReturn(0);
122817ab2063SBarry Smith }
122917ab2063SBarry Smith 
12304a2ae208SSatish Balay #undef __FUNCT__
12314a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
12328f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
123317ab2063SBarry Smith {
1234416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12354e220ebcSLois Curfman McInnes 
12363a40ed3dSBarry Smith   PetscFunctionBegin;
1237273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1238273d9f13SBarry Smith   info->columns_global = (double)A->n;
1239273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1240273d9f13SBarry Smith   info->columns_local  = (double)A->n;
12414e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12424e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12434e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12444e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12454e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12464e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12474e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12484e220ebcSLois Curfman McInnes   if (A->factor) {
12494e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12504e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12514e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12524e220ebcSLois Curfman McInnes   } else {
12534e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12544e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12554e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12564e220ebcSLois Curfman McInnes   }
12573a40ed3dSBarry Smith   PetscFunctionReturn(0);
125817ab2063SBarry Smith }
125917ab2063SBarry Smith 
1260b9b97703SBarry Smith EXTERN int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,MatLUInfo*,Mat*);
1261ca44d042SBarry Smith EXTERN int MatLUFactorNumeric_SeqAIJ(Mat,Mat*);
1262b9b97703SBarry Smith EXTERN int MatLUFactor_SeqAIJ(Mat,IS,IS,MatLUInfo*);
1263ca44d042SBarry Smith EXTERN int MatSolve_SeqAIJ(Mat,Vec,Vec);
1264ca44d042SBarry Smith EXTERN int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
1265ca44d042SBarry Smith EXTERN int MatSolveTranspose_SeqAIJ(Mat,Vec,Vec);
1266ca44d042SBarry Smith EXTERN int MatSolveTransposeAdd_SeqAIJ(Mat,Vec,Vec,Vec);
126717ab2063SBarry Smith 
12684a2ae208SSatish Balay #undef __FUNCT__
12694a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
127087828ca2SBarry Smith int MatZeroRows_SeqAIJ(Mat A,IS is,PetscScalar *diag)
127117ab2063SBarry Smith {
1272416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1273273d9f13SBarry Smith   int         i,ierr,N,*rows,m = A->m - 1,shift = a->indexshift;
127417ab2063SBarry Smith 
12753a40ed3dSBarry Smith   PetscFunctionBegin;
1276b9b97703SBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
127717ab2063SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
1278f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1279f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
128029bbc08cSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
128187828ca2SBarry Smith       ierr = PetscMemzero(&a->a[a->i[rows[i]]+shift],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1282f1e2ffcdSBarry Smith     }
1283f1e2ffcdSBarry Smith     if (diag) {
1284f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1285f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1286f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1287f1e2ffcdSBarry Smith         a->a[a->diag[rows[i]]] = *diag;
1288f1e2ffcdSBarry Smith       }
1289f1e2ffcdSBarry Smith     }
1290f1e2ffcdSBarry Smith   } else {
129117ab2063SBarry Smith     if (diag) {
129217ab2063SBarry Smith       for (i=0; i<N; i++) {
129329bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
12947ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1295416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1296416022c9SBarry Smith           a->a[a->i[rows[i]]+shift] = *diag;
1297416022c9SBarry Smith           a->j[a->i[rows[i]]+shift] = rows[i]+shift;
12987ae801bdSBarry Smith         } else { /* in case row was completely empty */
1299d64ed03dSBarry Smith           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
130017ab2063SBarry Smith         }
130117ab2063SBarry Smith       }
13023a40ed3dSBarry Smith     } else {
130317ab2063SBarry Smith       for (i=0; i<N; i++) {
130429bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
1305416022c9SBarry Smith         a->ilen[rows[i]] = 0;
130617ab2063SBarry Smith       }
130717ab2063SBarry Smith     }
1308f1e2ffcdSBarry Smith   }
13097ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
131043a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13113a40ed3dSBarry Smith   PetscFunctionReturn(0);
131217ab2063SBarry Smith }
131317ab2063SBarry Smith 
13144a2ae208SSatish Balay #undef __FUNCT__
13154a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
131687828ca2SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v)
131717ab2063SBarry Smith {
1318416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1319b0a32e0cSBarry Smith   int        *itmp,i,shift = a->indexshift,ierr;
132017ab2063SBarry Smith 
13213a40ed3dSBarry Smith   PetscFunctionBegin;
1322273d9f13SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %d out of range",row);
132317ab2063SBarry Smith 
1324416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1325416022c9SBarry Smith   if (v) *v = a->a + a->i[row] + shift;
132617ab2063SBarry Smith   if (idx) {
1327416022c9SBarry Smith     itmp = a->j + a->i[row] + shift;
13284e093b46SBarry Smith     if (*nz && shift) {
1329b0a32e0cSBarry Smith       ierr = PetscMalloc((*nz)*sizeof(int),idx);CHKERRQ(ierr);
133017ab2063SBarry Smith       for (i=0; i<(*nz); i++) {(*idx)[i] = itmp[i] + shift;}
13314e093b46SBarry Smith     } else if (*nz) {
13324e093b46SBarry Smith       *idx = itmp;
133317ab2063SBarry Smith     }
133417ab2063SBarry Smith     else *idx = 0;
133517ab2063SBarry Smith   }
13363a40ed3dSBarry Smith   PetscFunctionReturn(0);
133717ab2063SBarry Smith }
133817ab2063SBarry Smith 
13394a2ae208SSatish Balay #undef __FUNCT__
13404a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
134187828ca2SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v)
134217ab2063SBarry Smith {
13434e093b46SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1344606d414cSSatish Balay   int ierr;
13453a40ed3dSBarry Smith 
13463a40ed3dSBarry Smith   PetscFunctionBegin;
1347606d414cSSatish Balay   if (idx) {if (*idx && a->indexshift) {ierr = PetscFree(*idx);CHKERRQ(ierr);}}
13483a40ed3dSBarry Smith   PetscFunctionReturn(0);
134917ab2063SBarry Smith }
135017ab2063SBarry Smith 
13514a2ae208SSatish Balay #undef __FUNCT__
13524a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
135336db0b34SBarry Smith int MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *norm)
135417ab2063SBarry Smith {
1355416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
135687828ca2SBarry Smith   PetscScalar  *v = a->a;
135736db0b34SBarry Smith   PetscReal    sum = 0.0;
1358549d3d68SSatish Balay   int          i,j,shift = a->indexshift,ierr;
135917ab2063SBarry Smith 
13603a40ed3dSBarry Smith   PetscFunctionBegin;
136117ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1362416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1363aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
136436db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
136517ab2063SBarry Smith #else
136617ab2063SBarry Smith       sum += (*v)*(*v); v++;
136717ab2063SBarry Smith #endif
136817ab2063SBarry Smith     }
136917ab2063SBarry Smith     *norm = sqrt(sum);
13703a40ed3dSBarry Smith   } else if (type == NORM_1) {
137136db0b34SBarry Smith     PetscReal *tmp;
1372416022c9SBarry Smith     int    *jj = a->j;
1373b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1374273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
137517ab2063SBarry Smith     *norm = 0.0;
1376416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1377a2744918SBarry Smith         tmp[*jj++ + shift] += PetscAbsScalar(*v);  v++;
137817ab2063SBarry Smith     }
1379273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
138017ab2063SBarry Smith       if (tmp[j] > *norm) *norm = tmp[j];
138117ab2063SBarry Smith     }
1382606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13833a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
138417ab2063SBarry Smith     *norm = 0.0;
1385273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1386416022c9SBarry Smith       v = a->a + a->i[j] + shift;
138717ab2063SBarry Smith       sum = 0.0;
1388416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1389cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
139017ab2063SBarry Smith       }
139117ab2063SBarry Smith       if (sum > *norm) *norm = sum;
139217ab2063SBarry Smith     }
13933a40ed3dSBarry Smith   } else {
139429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
139517ab2063SBarry Smith   }
13963a40ed3dSBarry Smith   PetscFunctionReturn(0);
139717ab2063SBarry Smith }
139817ab2063SBarry Smith 
13994a2ae208SSatish Balay #undef __FUNCT__
14004a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
14018f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
140217ab2063SBarry Smith {
1403416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
1404416022c9SBarry Smith   Mat          C;
1405273d9f13SBarry Smith   int          i,ierr,*aj = a->j,*ai = a->i,m = A->m,len,*col;
1406273d9f13SBarry Smith   int          shift = a->indexshift;
140787828ca2SBarry Smith   PetscScalar  *array = a->a;
140817ab2063SBarry Smith 
14093a40ed3dSBarry Smith   PetscFunctionBegin;
1410273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1411b0a32e0cSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(int),&col);CHKERRQ(ierr);
1412273d9f13SBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(int));CHKERRQ(ierr);
141317ab2063SBarry Smith   if (shift) {
141417ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] -= 1;
141517ab2063SBarry Smith   }
141617ab2063SBarry Smith   for (i=0; i<ai[m]+shift; i++) col[aj[i]] += 1;
1417273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,A->n,m,0,col,&C);CHKERRQ(ierr);
1418606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
141917ab2063SBarry Smith   for (i=0; i<m; i++) {
142017ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1421416022c9SBarry Smith     ierr   = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1422b9b97703SBarry Smith     array += len;
1423b9b97703SBarry Smith     aj    += len;
142417ab2063SBarry Smith   }
142517ab2063SBarry Smith   if (shift) {
142617ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] += 1;
142717ab2063SBarry Smith   }
142817ab2063SBarry Smith 
14296d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14306d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
143117ab2063SBarry Smith 
1432f1e2ffcdSBarry Smith   if (B) {
1433416022c9SBarry Smith     *B = C;
143417ab2063SBarry Smith   } else {
1435273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
143617ab2063SBarry Smith   }
14373a40ed3dSBarry Smith   PetscFunctionReturn(0);
143817ab2063SBarry Smith }
143917ab2063SBarry Smith 
14404a2ae208SSatish Balay #undef __FUNCT__
14414a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
14428f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
144317ab2063SBarry Smith {
1444416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
144587828ca2SBarry Smith   PetscScalar  *l,*r,x,*v;
1446273d9f13SBarry Smith   int          ierr,i,j,m = A->m,n = A->n,M,nz = a->nz,*jj,shift = a->indexshift;
144717ab2063SBarry Smith 
14483a40ed3dSBarry Smith   PetscFunctionBegin;
144917ab2063SBarry Smith   if (ll) {
14503ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14513ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1452e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1453273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
1454e1311b90SBarry Smith     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1455416022c9SBarry Smith     v = a->a;
145617ab2063SBarry Smith     for (i=0; i<m; i++) {
145717ab2063SBarry Smith       x = l[i];
1458416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
145917ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
146017ab2063SBarry Smith     }
1461e1311b90SBarry Smith     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1462b0a32e0cSBarry Smith     PetscLogFlops(nz);
146317ab2063SBarry Smith   }
146417ab2063SBarry Smith   if (rr) {
1465e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1466273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
1467e1311b90SBarry Smith     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1468416022c9SBarry Smith     v = a->a; jj = a->j;
146917ab2063SBarry Smith     for (i=0; i<nz; i++) {
147017ab2063SBarry Smith       (*v++) *= r[*jj++ + shift];
147117ab2063SBarry Smith     }
1472e1311b90SBarry Smith     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1473b0a32e0cSBarry Smith     PetscLogFlops(nz);
147417ab2063SBarry Smith   }
14753a40ed3dSBarry Smith   PetscFunctionReturn(0);
147617ab2063SBarry Smith }
147717ab2063SBarry Smith 
14784a2ae208SSatish Balay #undef __FUNCT__
14794a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
14807b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B)
148117ab2063SBarry Smith {
1482db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data,*c;
1483273d9f13SBarry Smith   int          *smap,i,k,kstart,kend,ierr,oldcols = A->n,*lens;
148436db0b34SBarry Smith   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
148599141d43SSatish Balay   int          *irow,*icol,nrows,ncols,shift = a->indexshift,*ssmap;
148699141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
148787828ca2SBarry Smith   PetscScalar  *a_new,*mat_a;
1488416022c9SBarry Smith   Mat          C;
1489fee21e36SBarry Smith   PetscTruth   stride;
149017ab2063SBarry Smith 
14913a40ed3dSBarry Smith   PetscFunctionBegin;
1492d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
149329bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1494d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
149529bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
149699141d43SSatish Balay 
149717ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1498b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1499b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
150017ab2063SBarry Smith 
1501fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1502fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1503fee21e36SBarry Smith   if (stride && step == 1) {
150402834360SBarry Smith     /* special case of contiguous rows */
150531ebf83bSSatish Balay     ierr   = PetscMalloc((2*nrows+1)*sizeof(int),&lens);CHKERRQ(ierr);
150631ebf83bSSatish Balay     starts = lens + nrows;
150702834360SBarry Smith     /* loop over new rows determining lens and starting points */
150802834360SBarry Smith     for (i=0; i<nrows; i++) {
1509a2744918SBarry Smith       kstart  = ai[irow[i]]+shift;
1510a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
151102834360SBarry Smith       for (k=kstart; k<kend; k++) {
1512d8ced48eSBarry Smith         if (aj[k]+shift >= first) {
151302834360SBarry Smith           starts[i] = k;
151402834360SBarry Smith           break;
151502834360SBarry Smith 	}
151602834360SBarry Smith       }
1517a2744918SBarry Smith       sum = 0;
151802834360SBarry Smith       while (k < kend) {
1519d8ced48eSBarry Smith         if (aj[k++]+shift >= first+ncols) break;
1520a2744918SBarry Smith         sum++;
152102834360SBarry Smith       }
1522a2744918SBarry Smith       lens[i] = sum;
152302834360SBarry Smith     }
152402834360SBarry Smith     /* create submatrix */
1525cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
152608480c60SBarry Smith       int n_cols,n_rows;
152708480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
152829bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1529d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
153008480c60SBarry Smith       C = *B;
15313a40ed3dSBarry Smith     } else {
153202834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
153308480c60SBarry Smith     }
1534db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1535db02288aSLois Curfman McInnes 
153602834360SBarry Smith     /* loop over rows inserting into submatrix */
1537db02288aSLois Curfman McInnes     a_new    = c->a;
1538db02288aSLois Curfman McInnes     j_new    = c->j;
1539db02288aSLois Curfman McInnes     i_new    = c->i;
1540db02288aSLois Curfman McInnes     i_new[0] = -shift;
154102834360SBarry Smith     for (i=0; i<nrows; i++) {
1542a2744918SBarry Smith       ii    = starts[i];
1543a2744918SBarry Smith       lensi = lens[i];
1544a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1545a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
154602834360SBarry Smith       }
154787828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1548a2744918SBarry Smith       a_new      += lensi;
1549a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1550a2744918SBarry Smith       c->ilen[i]  = lensi;
155102834360SBarry Smith     }
1552606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15533a40ed3dSBarry Smith   } else {
155402834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
1555b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(int),&smap);CHKERRQ(ierr);
155602834360SBarry Smith     ssmap = smap + shift;
1557b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(int),&lens);CHKERRQ(ierr);
1558549d3d68SSatish Balay     ierr  = PetscMemzero(smap,oldcols*sizeof(int));CHKERRQ(ierr);
155917ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
156002834360SBarry Smith     /* determine lens of each row */
156102834360SBarry Smith     for (i=0; i<nrows; i++) {
1562d8ced48eSBarry Smith       kstart  = ai[irow[i]]+shift;
156302834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
156402834360SBarry Smith       lens[i] = 0;
156502834360SBarry Smith       for (k=kstart; k<kend; k++) {
1566d8ced48eSBarry Smith         if (ssmap[aj[k]]) {
156702834360SBarry Smith           lens[i]++;
156802834360SBarry Smith         }
156902834360SBarry Smith       }
157002834360SBarry Smith     }
157117ab2063SBarry Smith     /* Create and fill new matrix */
1572a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
15730f5bd95cSBarry Smith       PetscTruth equal;
15740f5bd95cSBarry Smith 
157599141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1576273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1577273d9f13SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(int),&equal);CHKERRQ(ierr);
15780f5bd95cSBarry Smith       if (!equal) {
157929bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
158099141d43SSatish Balay       }
1581273d9f13SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(int));CHKERRQ(ierr);
158208480c60SBarry Smith       C = *B;
15833a40ed3dSBarry Smith     } else {
158402834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
158508480c60SBarry Smith     }
158699141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
158717ab2063SBarry Smith     for (i=0; i<nrows; i++) {
158899141d43SSatish Balay       row    = irow[i];
158999141d43SSatish Balay       kstart = ai[row]+shift;
159099141d43SSatish Balay       kend   = kstart + a->ilen[row];
159199141d43SSatish Balay       mat_i  = c->i[i]+shift;
159299141d43SSatish Balay       mat_j  = c->j + mat_i;
159399141d43SSatish Balay       mat_a  = c->a + mat_i;
159499141d43SSatish Balay       mat_ilen = c->ilen + i;
159517ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
159699141d43SSatish Balay         if ((tcol=ssmap[a->j[k]])) {
159799141d43SSatish Balay           *mat_j++ = tcol - (!shift);
159899141d43SSatish Balay           *mat_a++ = a->a[k];
159999141d43SSatish Balay           (*mat_ilen)++;
160099141d43SSatish Balay 
160117ab2063SBarry Smith         }
160217ab2063SBarry Smith       }
160317ab2063SBarry Smith     }
160402834360SBarry Smith     /* Free work space */
160502834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1606606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1607606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
160802834360SBarry Smith   }
16096d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16106d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
161117ab2063SBarry Smith 
161217ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1613416022c9SBarry Smith   *B = C;
16143a40ed3dSBarry Smith   PetscFunctionReturn(0);
161517ab2063SBarry Smith }
161617ab2063SBarry Smith 
1617a871dcd8SBarry Smith /*
1618a871dcd8SBarry Smith */
16194a2ae208SSatish Balay #undef __FUNCT__
16204a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
16215ef9f2a5SBarry Smith int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatILUInfo *info)
1622a871dcd8SBarry Smith {
162363b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
162408480c60SBarry Smith   int        ierr;
162563b91edcSBarry Smith   Mat        outA;
1626b8a78c4aSBarry Smith   PetscTruth row_identity,col_identity;
162763b91edcSBarry Smith 
16283a40ed3dSBarry Smith   PetscFunctionBegin;
162929bbc08cSBarry Smith   if (info && info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1630b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1631b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1632b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
163329bbc08cSBarry Smith     SETERRQ(1,"Row and column permutations must be identity for in-place ILU");
1634b8a78c4aSBarry Smith   }
1635a871dcd8SBarry Smith 
163663b91edcSBarry Smith   outA          = inA;
163763b91edcSBarry Smith   inA->factor   = FACTOR_LU;
163863b91edcSBarry Smith   a->row        = row;
163963b91edcSBarry Smith   a->col        = col;
1640c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1641c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
164263b91edcSBarry Smith 
164336db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1644b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
16454c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
1646b0a32e0cSBarry Smith   PetscLogObjectParent(inA,a->icol);
1647f0ec6fceSSatish Balay 
164894a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
164987828ca2SBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
165094a9d846SBarry Smith   }
165163b91edcSBarry Smith 
165208480c60SBarry Smith   if (!a->diag) {
1653f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
165463b91edcSBarry Smith   }
165563b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA);CHKERRQ(ierr);
16563a40ed3dSBarry Smith   PetscFunctionReturn(0);
1657a871dcd8SBarry Smith }
1658a871dcd8SBarry Smith 
1659d9eff348SSatish Balay #include "petscblaslapack.h"
16604a2ae208SSatish Balay #undef __FUNCT__
16614a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
166287828ca2SBarry Smith int MatScale_SeqAIJ(PetscScalar *alpha,Mat inA)
1663f0b747eeSBarry Smith {
1664f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
1665f0b747eeSBarry Smith   int        one = 1;
16663a40ed3dSBarry Smith 
16673a40ed3dSBarry Smith   PetscFunctionBegin;
1668f0b747eeSBarry Smith   BLscal_(&a->nz,alpha,a->a,&one);
1669b0a32e0cSBarry Smith   PetscLogFlops(a->nz);
16703a40ed3dSBarry Smith   PetscFunctionReturn(0);
1671f0b747eeSBarry Smith }
1672f0b747eeSBarry Smith 
16734a2ae208SSatish Balay #undef __FUNCT__
16744a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
16757b2a1423SBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n,IS *irow,IS *icol,MatReuse scall,Mat **B)
1676cddf8d76SBarry Smith {
1677cddf8d76SBarry Smith   int ierr,i;
1678cddf8d76SBarry Smith 
16793a40ed3dSBarry Smith   PetscFunctionBegin;
1680cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1681b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1682cddf8d76SBarry Smith   }
1683cddf8d76SBarry Smith 
1684cddf8d76SBarry Smith   for (i=0; i<n; i++) {
16856a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1686cddf8d76SBarry Smith   }
16873a40ed3dSBarry Smith   PetscFunctionReturn(0);
1688cddf8d76SBarry Smith }
1689cddf8d76SBarry Smith 
16904a2ae208SSatish Balay #undef __FUNCT__
16914a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_SeqAIJ"
16928f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A,int *bs)
16935a838052SSatish Balay {
1694f830108cSBarry Smith   PetscFunctionBegin;
16955a838052SSatish Balay   *bs = 1;
16963a40ed3dSBarry Smith   PetscFunctionReturn(0);
16975a838052SSatish Balay }
16985a838052SSatish Balay 
16994a2ae208SSatish Balay #undef __FUNCT__
17004a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
17018f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A,int is_max,IS *is,int ov)
17024dcbc457SBarry Smith {
1703e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
170406763907SSatish Balay   int        shift,row,i,j,k,l,m,n,*idx,ierr,*nidx,isz,val;
17058a047759SSatish Balay   int        start,end,*ai,*aj;
1706f1af5d2fSBarry Smith   PetscBT    table;
1707bbd702dbSSatish Balay 
17083a40ed3dSBarry Smith   PetscFunctionBegin;
17098a047759SSatish Balay   shift = a->indexshift;
1710273d9f13SBarry Smith   m     = A->m;
1711e4d965acSSatish Balay   ai    = a->i;
17128a047759SSatish Balay   aj    = a->j+shift;
17138a047759SSatish Balay 
171429bbc08cSBarry Smith   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal overlap value used");
171506763907SSatish Balay 
1716b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&nidx);CHKERRQ(ierr);
17176831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
171806763907SSatish Balay 
1719e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1720b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1721e4d965acSSatish Balay     isz  = 0;
17226831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1723e4d965acSSatish Balay 
1724e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17254dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1726b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1727e4d965acSSatish Balay 
1728dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1729e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1730f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
17314dcbc457SBarry Smith     }
173206763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
173306763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1734e4d965acSSatish Balay 
173504a348a9SBarry Smith     k = 0;
173604a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
173704a348a9SBarry Smith       n = isz;
173806763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1739e4d965acSSatish Balay         row   = nidx[k];
1740e4d965acSSatish Balay         start = ai[row];
1741e4d965acSSatish Balay         end   = ai[row+1];
174204a348a9SBarry Smith         for (l = start; l<end ; l++){
17438a047759SSatish Balay           val = aj[l] + shift;
1744f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1745e4d965acSSatish Balay         }
1746e4d965acSSatish Balay       }
1747e4d965acSSatish Balay     }
1748029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1749e4d965acSSatish Balay   }
17506831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1751606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17523a40ed3dSBarry Smith   PetscFunctionReturn(0);
17534dcbc457SBarry Smith }
175417ab2063SBarry Smith 
17550513a670SBarry Smith /* -------------------------------------------------------------- */
17564a2ae208SSatish Balay #undef __FUNCT__
17574a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
17580513a670SBarry Smith int MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
17590513a670SBarry Smith {
17600513a670SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
176187828ca2SBarry Smith   PetscScalar  *vwork;
1762273d9f13SBarry Smith   int          i,ierr,nz,m = A->m,n = A->n,*cwork;
17630513a670SBarry Smith   int          *row,*col,*cnew,j,*lens;
176456cd22aeSBarry Smith   IS           icolp,irowp;
17650513a670SBarry Smith 
17663a40ed3dSBarry Smith   PetscFunctionBegin;
17674c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
176856cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
17694c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
177056cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
17710513a670SBarry Smith 
17720513a670SBarry Smith   /* determine lengths of permuted rows */
1773b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&lens);CHKERRQ(ierr);
17740513a670SBarry Smith   for (i=0; i<m; i++) {
17750513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
17760513a670SBarry Smith   }
17770513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
1778606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
17790513a670SBarry Smith 
1780b0a32e0cSBarry Smith   ierr = PetscMalloc(n*sizeof(int),&cnew);CHKERRQ(ierr);
17810513a670SBarry Smith   for (i=0; i<m; i++) {
17820513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
17830513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
17840513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
17850513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
17860513a670SBarry Smith   }
1787606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
17880513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17890513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
179056cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
179156cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
179256cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
179356cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
17943a40ed3dSBarry Smith   PetscFunctionReturn(0);
17950513a670SBarry Smith }
17960513a670SBarry Smith 
17974a2ae208SSatish Balay #undef __FUNCT__
17984a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1799682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1800682d7d0cSBarry Smith {
1801c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1802682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1803d132466eSBarry Smith   int               ierr;
1804682d7d0cSBarry Smith 
18053a40ed3dSBarry Smith   PetscFunctionBegin;
1806c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1807d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1808d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1809d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
1810d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr);
1811d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr);
1812aa482453SBarry Smith #if defined(PETSC_HAVE_ESSL)
1813d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");CHKERRQ(ierr);
1814682d7d0cSBarry Smith #endif
1815cd5578b5SBarry Smith #if defined(PETSC_HAVE_LUSOL)
1816cd5578b5SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_lusol: Use the Stanford LUSOL sparse factorization and solve.\n");CHKERRQ(ierr);
1817cd5578b5SBarry Smith #endif
181887828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1819ca44d042SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_matlab: Use Matlab engine sparse LU factorization and solve.\n");CHKERRQ(ierr);
1820ca44d042SBarry Smith #endif
18213a40ed3dSBarry Smith   PetscFunctionReturn(0);
1822682d7d0cSBarry Smith }
1823ca44d042SBarry Smith EXTERN int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg);
1824ca44d042SBarry Smith EXTERN int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
1825ca44d042SBarry Smith EXTERN int MatILUDTFactor_SeqAIJ(Mat,MatILUInfo*,IS,IS,Mat*);
18264a2ae208SSatish Balay #undef __FUNCT__
18274a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1828cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1829cb5b572fSBarry Smith {
1830be6bf707SBarry Smith   int        ierr;
1831273d9f13SBarry Smith   PetscTruth flg;
1832cb5b572fSBarry Smith 
1833cb5b572fSBarry Smith   PetscFunctionBegin;
1834273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg);CHKERRQ(ierr);
1835273d9f13SBarry Smith   if (str == SAME_NONZERO_PATTERN && flg) {
1836be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1837be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1838be6bf707SBarry Smith 
1839273d9f13SBarry Smith     if (a->i[A->m]+a->indexshift != b->i[B->m]+a->indexshift) {
184029bbc08cSBarry Smith       SETERRQ(1,"Number of nonzeros in two matrices are different");
1841cb5b572fSBarry Smith     }
184287828ca2SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m]+a->indexshift)*sizeof(PetscScalar));CHKERRQ(ierr);
1843cb5b572fSBarry Smith   } else {
1844cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1845cb5b572fSBarry Smith   }
1846cb5b572fSBarry Smith   PetscFunctionReturn(0);
1847cb5b572fSBarry Smith }
1848cb5b572fSBarry Smith 
18494a2ae208SSatish Balay #undef __FUNCT__
18504a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1851273d9f13SBarry Smith int MatSetUpPreallocation_SeqAIJ(Mat A)
1852273d9f13SBarry Smith {
1853273d9f13SBarry Smith   int        ierr;
1854273d9f13SBarry Smith 
1855273d9f13SBarry Smith   PetscFunctionBegin;
1856273d9f13SBarry Smith   ierr =  MatSeqAIJSetPreallocation(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1857273d9f13SBarry Smith   PetscFunctionReturn(0);
1858273d9f13SBarry Smith }
1859273d9f13SBarry Smith 
18604a2ae208SSatish Balay #undef __FUNCT__
18614a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
186287828ca2SBarry Smith int MatGetArray_SeqAIJ(Mat A,PetscScalar **array)
18636c0721eeSBarry Smith {
18646c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
18656c0721eeSBarry Smith   PetscFunctionBegin;
18666c0721eeSBarry Smith   *array = a->a;
18676c0721eeSBarry Smith   PetscFunctionReturn(0);
18686c0721eeSBarry Smith }
18696c0721eeSBarry Smith 
18704a2ae208SSatish Balay #undef __FUNCT__
18714a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
187287828ca2SBarry Smith int MatRestoreArray_SeqAIJ(Mat A,PetscScalar **array)
18736c0721eeSBarry Smith {
18746c0721eeSBarry Smith   PetscFunctionBegin;
18756c0721eeSBarry Smith   PetscFunctionReturn(0);
18766c0721eeSBarry Smith }
1877273d9f13SBarry Smith 
1878ee4f033dSBarry Smith #undef __FUNCT__
1879ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1880ee4f033dSBarry Smith int MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1881ee4f033dSBarry Smith {
1882ee4f033dSBarry Smith   int           (*f)(void *,Vec,Vec,void*) = (int (*)(void *,Vec,Vec,void *))coloring->f;
1883ee4f033dSBarry Smith   int           k,ierr,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
188487828ca2SBarry Smith   PetscScalar   dx,mone = -1.0,*y,*xx,*w3_array;
188587828ca2SBarry Smith   PetscScalar   *vscale_array;
1886ee4f033dSBarry Smith   PetscReal     epsilon = coloring->error_rel,umin = coloring->umin;
1887ee4f033dSBarry Smith   Vec           w1,w2,w3;
1888ee4f033dSBarry Smith   void          *fctx = coloring->fctx;
1889ee4f033dSBarry Smith   PetscTruth    flg;
1890ee4f033dSBarry Smith 
1891ee4f033dSBarry Smith   PetscFunctionBegin;
1892ee4f033dSBarry Smith   if (!coloring->w1) {
1893ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
1894ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w1);
1895ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
1896ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w2);
1897ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
1898ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w3);
1899ee4f033dSBarry Smith   }
1900ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1901ee4f033dSBarry Smith 
1902ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1903ee4f033dSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1904ee4f033dSBarry Smith   if (flg) {
1905ee4f033dSBarry Smith     PetscLogInfo(coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n");
1906ee4f033dSBarry Smith   } else {
1907ee4f033dSBarry Smith     ierr = MatZeroEntries(J);CHKERRQ(ierr);
1908ee4f033dSBarry Smith   }
1909ee4f033dSBarry Smith 
1910ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1911ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1912ee4f033dSBarry Smith 
1913ee4f033dSBarry Smith   /*
1914ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1915ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1916ee4f033dSBarry Smith   */
1917ee4f033dSBarry Smith   if (coloring->F) {
1918ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1919ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1920ee4f033dSBarry Smith     if (m1 != m2) {
1921ee4f033dSBarry Smith       coloring->F = 0;
1922ee4f033dSBarry Smith     }
1923ee4f033dSBarry Smith   }
1924ee4f033dSBarry Smith 
1925ee4f033dSBarry Smith   if (coloring->F) {
1926ee4f033dSBarry Smith     w1          = coloring->F;
1927ee4f033dSBarry Smith     coloring->F = 0;
1928ee4f033dSBarry Smith   } else {
1929ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
1930ee4f033dSBarry Smith   }
1931ee4f033dSBarry Smith 
1932ee4f033dSBarry Smith   /*
1933ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
1934ee4f033dSBarry Smith   */
1935ee4f033dSBarry Smith   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
1936ee4f033dSBarry Smith   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
1937ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
1938ee4f033dSBarry Smith     /*
1939ee4f033dSBarry Smith        Loop over each column associated with color adding the
1940ee4f033dSBarry Smith        perturbation to the vector w3.
1941ee4f033dSBarry Smith     */
1942ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1943ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1944ee4f033dSBarry Smith       dx  = xx[col];
1945ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1946ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1947ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1948ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1949ee4f033dSBarry Smith #else
1950ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1951ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1952ee4f033dSBarry Smith #endif
1953ee4f033dSBarry Smith       dx                *= epsilon;
1954ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
1955ee4f033dSBarry Smith     }
1956ee4f033dSBarry Smith   }
1957ee4f033dSBarry Smith   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1958ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1959ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1960ee4f033dSBarry Smith 
1961ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
1962ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
1963ee4f033dSBarry Smith 
1964ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
1965ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
1966ee4f033dSBarry Smith 
1967ee4f033dSBarry Smith   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1968ee4f033dSBarry Smith   /*
1969ee4f033dSBarry Smith       Loop over each color
1970ee4f033dSBarry Smith   */
1971ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
1972ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
1973ee4f033dSBarry Smith     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
1974ee4f033dSBarry Smith     /*
1975ee4f033dSBarry Smith        Loop over each column associated with color adding the
1976ee4f033dSBarry Smith        perturbation to the vector w3.
1977ee4f033dSBarry Smith     */
1978ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1979ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1980ee4f033dSBarry Smith       dx  = xx[col];
1981ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1982ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1983ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1984ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1985ee4f033dSBarry Smith #else
1986ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1987ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1988ee4f033dSBarry Smith #endif
1989ee4f033dSBarry Smith       dx            *= epsilon;
1990ee4f033dSBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(1,"Computed 0 differencing parameter");
1991ee4f033dSBarry Smith       w3_array[col] += dx;
1992ee4f033dSBarry Smith     }
1993ee4f033dSBarry Smith     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
1994ee4f033dSBarry Smith 
1995ee4f033dSBarry Smith     /*
1996ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
1997ee4f033dSBarry Smith     */
1998ee4f033dSBarry Smith 
1999ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
2000ee4f033dSBarry Smith     ierr = VecAXPY(&mone,w1,w2);CHKERRQ(ierr);
2001ee4f033dSBarry Smith 
2002ee4f033dSBarry Smith     /*
2003ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2004ee4f033dSBarry Smith     */
2005ee4f033dSBarry Smith     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2006ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2007ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2008ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2009ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2010ee4f033dSBarry Smith       srow   = row + start;
2011ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2012ee4f033dSBarry Smith     }
2013ee4f033dSBarry Smith     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2014ee4f033dSBarry Smith   }
2015ee4f033dSBarry Smith   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2016ee4f033dSBarry Smith   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2017ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2018ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2019ee4f033dSBarry Smith   PetscFunctionReturn(0);
2020ee4f033dSBarry Smith }
2021ee4f033dSBarry Smith 
2022682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
20230a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2024cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2025cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2026cb5b572fSBarry Smith        MatMult_SeqAIJ,
2027cb5b572fSBarry Smith        MatMultAdd_SeqAIJ,
20287c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
20297c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2030cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2031cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
20327c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
20337c922b88SBarry Smith        MatSolveTransposeAdd_SeqAIJ,
2034cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2035cb5b572fSBarry Smith        0,
203617ab2063SBarry Smith        MatRelax_SeqAIJ,
203717ab2063SBarry Smith        MatTranspose_SeqAIJ,
2038cb5b572fSBarry Smith        MatGetInfo_SeqAIJ,
2039cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2040cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2041cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2042cb5b572fSBarry Smith        MatNorm_SeqAIJ,
2043cb5b572fSBarry Smith        0,
2044cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
204517ab2063SBarry Smith        MatCompress_SeqAIJ,
2046cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2047cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2048cb5b572fSBarry Smith        MatZeroRows_SeqAIJ,
2049cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2050cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2051cb5b572fSBarry Smith        0,
2052cb5b572fSBarry Smith        0,
2053273d9f13SBarry Smith        MatSetUpPreallocation_SeqAIJ,
2054cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2055cb5b572fSBarry Smith        0,
20566c0721eeSBarry Smith        MatGetArray_SeqAIJ,
20576c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2058be6bf707SBarry Smith        MatDuplicate_SeqAIJ,
2059cb5b572fSBarry Smith        0,
2060cb5b572fSBarry Smith        0,
2061cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2062cb5b572fSBarry Smith        0,
2063cb5b572fSBarry Smith        0,
2064cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2065cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2066cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2067cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2068f0b747eeSBarry Smith        MatPrintHelp_SeqAIJ,
2069cb5b572fSBarry Smith        MatScale_SeqAIJ,
2070cb5b572fSBarry Smith        0,
2071cb5b572fSBarry Smith        0,
20726945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
20736945ee14SBarry Smith        MatGetBlockSize_SeqAIJ,
20743b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
20753b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
20763b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2077a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2078a93ec695SBarry Smith        MatFDColoringCreate_SeqAIJ,
2079b9617806SBarry Smith        0,
20800513a670SBarry Smith        0,
2081cda55fadSBarry Smith        MatPermute_SeqAIJ,
2082cda55fadSBarry Smith        0,
2083cda55fadSBarry Smith        0,
2084b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2085b9b97703SBarry Smith        MatView_SeqAIJ,
20868a124369SBarry Smith        MatGetPetscMaps_Petsc,
2087ee4f033dSBarry Smith        0,
2088ee4f033dSBarry Smith        0,
2089ee4f033dSBarry Smith        0,
2090ee4f033dSBarry Smith        0,
2091ee4f033dSBarry Smith        0,
2092ee4f033dSBarry Smith        0,
2093ee4f033dSBarry Smith        0,
2094ee4f033dSBarry Smith        0,
2095ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2096ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2097ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
2098ee4f033dSBarry Smith        MatFDColoringApply_SeqAIJ};
209917ab2063SBarry Smith 
2100ca44d042SBarry Smith EXTERN int MatUseSuperLU_SeqAIJ(Mat);
2101ca44d042SBarry Smith EXTERN int MatUseEssl_SeqAIJ(Mat);
2102cd5578b5SBarry Smith EXTERN int MatUseLUSOL_SeqAIJ(Mat);
2103ca44d042SBarry Smith EXTERN int MatUseMatlab_SeqAIJ(Mat);
2104ca44d042SBarry Smith EXTERN int MatUseDXML_SeqAIJ(Mat);
210517ab2063SBarry Smith 
2106fb2e594dSBarry Smith EXTERN_C_BEGIN
21074a2ae208SSatish Balay #undef __FUNCT__
21084a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
210915091d37SBarry Smith 
2110bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices)
2111bef8e0ddSBarry Smith {
2112bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2113bef8e0ddSBarry Smith   int        i,nz,n;
2114bef8e0ddSBarry Smith 
2115bef8e0ddSBarry Smith   PetscFunctionBegin;
2116bef8e0ddSBarry Smith 
2117bef8e0ddSBarry Smith   nz = aij->maxnz;
2118273d9f13SBarry Smith   n  = mat->n;
2119bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2120bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2121bef8e0ddSBarry Smith   }
2122bef8e0ddSBarry Smith   aij->nz = nz;
2123bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2124bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2125bef8e0ddSBarry Smith   }
2126bef8e0ddSBarry Smith 
2127bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2128bef8e0ddSBarry Smith }
2129fb2e594dSBarry Smith EXTERN_C_END
2130bef8e0ddSBarry Smith 
21314a2ae208SSatish Balay #undef __FUNCT__
21324a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2133bef8e0ddSBarry Smith /*@
2134bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2135bef8e0ddSBarry Smith        in the matrix.
2136bef8e0ddSBarry Smith 
2137bef8e0ddSBarry Smith   Input Parameters:
2138bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2139bef8e0ddSBarry Smith -  indices - the column indices
2140bef8e0ddSBarry Smith 
214115091d37SBarry Smith   Level: advanced
214215091d37SBarry Smith 
2143bef8e0ddSBarry Smith   Notes:
2144bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2145bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2146bef8e0ddSBarry Smith   of the MatSetValues() operation.
2147bef8e0ddSBarry Smith 
2148bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2149bef8e0ddSBarry Smith   MatCreateSeqAIJ().
2150bef8e0ddSBarry Smith 
2151bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2152bef8e0ddSBarry Smith 
2153b9617806SBarry Smith     The indices should start with zero, not one.
2154b9617806SBarry Smith 
2155bef8e0ddSBarry Smith @*/
2156bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices)
2157bef8e0ddSBarry Smith {
2158bef8e0ddSBarry Smith   int ierr,(*f)(Mat,int *);
2159bef8e0ddSBarry Smith 
2160bef8e0ddSBarry Smith   PetscFunctionBegin;
2161bef8e0ddSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2162c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2163bef8e0ddSBarry Smith   if (f) {
2164bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2165bef8e0ddSBarry Smith   } else {
216629bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to set column indices");
2167bef8e0ddSBarry Smith   }
2168bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2169bef8e0ddSBarry Smith }
2170bef8e0ddSBarry Smith 
2171be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2172be6bf707SBarry Smith 
2173fb2e594dSBarry Smith EXTERN_C_BEGIN
21744a2ae208SSatish Balay #undef __FUNCT__
21754a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2176be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat)
2177be6bf707SBarry Smith {
2178be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2179273d9f13SBarry Smith   int        nz = aij->i[mat->m]+aij->indexshift,ierr;
2180be6bf707SBarry Smith 
2181be6bf707SBarry Smith   PetscFunctionBegin;
2182be6bf707SBarry Smith   if (aij->nonew != 1) {
218329bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2184be6bf707SBarry Smith   }
2185be6bf707SBarry Smith 
2186be6bf707SBarry Smith   /* allocate space for values if not already there */
2187be6bf707SBarry Smith   if (!aij->saved_values) {
218887828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2189be6bf707SBarry Smith   }
2190be6bf707SBarry Smith 
2191be6bf707SBarry Smith   /* copy values over */
219287828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2193be6bf707SBarry Smith   PetscFunctionReturn(0);
2194be6bf707SBarry Smith }
2195fb2e594dSBarry Smith EXTERN_C_END
2196be6bf707SBarry Smith 
21974a2ae208SSatish Balay #undef __FUNCT__
2198b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2199be6bf707SBarry Smith /*@
2200be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2201be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2202be6bf707SBarry Smith        nonlinear portion.
2203be6bf707SBarry Smith 
2204be6bf707SBarry Smith    Collect on Mat
2205be6bf707SBarry Smith 
2206be6bf707SBarry Smith   Input Parameters:
2207be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2208be6bf707SBarry Smith 
220915091d37SBarry Smith   Level: advanced
221015091d37SBarry Smith 
2211be6bf707SBarry Smith   Common Usage, with SNESSolve():
2212be6bf707SBarry Smith $    Create Jacobian matrix
2213be6bf707SBarry Smith $    Set linear terms into matrix
2214be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2215be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2216be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2217be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2218be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2219be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2220be6bf707SBarry Smith $    In your Jacobian routine
2221be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2222be6bf707SBarry Smith $      Set nonlinear terms in matrix
2223be6bf707SBarry Smith 
2224be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2225be6bf707SBarry Smith $    // build linear portion of Jacobian
2226be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2227be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2228be6bf707SBarry Smith $    loop over nonlinear iterations
2229be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2230be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2231be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2232be6bf707SBarry Smith $       Solve linear system with Jacobian
2233be6bf707SBarry Smith $    endloop
2234be6bf707SBarry Smith 
2235be6bf707SBarry Smith   Notes:
2236be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2237be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2238be6bf707SBarry Smith     calling this routine.
2239be6bf707SBarry Smith 
2240be6bf707SBarry Smith .seealso: MatRetrieveValues()
2241be6bf707SBarry Smith 
2242be6bf707SBarry Smith @*/
2243be6bf707SBarry Smith int MatStoreValues(Mat mat)
2244be6bf707SBarry Smith {
2245be6bf707SBarry Smith   int ierr,(*f)(Mat);
2246be6bf707SBarry Smith 
2247be6bf707SBarry Smith   PetscFunctionBegin;
2248be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
224929bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
225029bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2251be6bf707SBarry Smith 
2252c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2253be6bf707SBarry Smith   if (f) {
2254be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2255be6bf707SBarry Smith   } else {
225629bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to store values");
2257be6bf707SBarry Smith   }
2258be6bf707SBarry Smith   PetscFunctionReturn(0);
2259be6bf707SBarry Smith }
2260be6bf707SBarry Smith 
2261fb2e594dSBarry Smith EXTERN_C_BEGIN
22624a2ae208SSatish Balay #undef __FUNCT__
22634a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2264be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat)
2265be6bf707SBarry Smith {
2266be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2267273d9f13SBarry Smith   int        nz = aij->i[mat->m]+aij->indexshift,ierr;
2268be6bf707SBarry Smith 
2269be6bf707SBarry Smith   PetscFunctionBegin;
2270be6bf707SBarry Smith   if (aij->nonew != 1) {
227129bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2272be6bf707SBarry Smith   }
2273be6bf707SBarry Smith   if (!aij->saved_values) {
227429bbc08cSBarry Smith     SETERRQ(1,"Must call MatStoreValues(A);first");
2275be6bf707SBarry Smith   }
2276be6bf707SBarry Smith 
2277be6bf707SBarry Smith   /* copy values over */
227887828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2279be6bf707SBarry Smith   PetscFunctionReturn(0);
2280be6bf707SBarry Smith }
2281fb2e594dSBarry Smith EXTERN_C_END
2282be6bf707SBarry Smith 
22834a2ae208SSatish Balay #undef __FUNCT__
22844a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2285be6bf707SBarry Smith /*@
2286be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2287be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2288be6bf707SBarry Smith        nonlinear portion.
2289be6bf707SBarry Smith 
2290be6bf707SBarry Smith    Collect on Mat
2291be6bf707SBarry Smith 
2292be6bf707SBarry Smith   Input Parameters:
2293be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2294be6bf707SBarry Smith 
229515091d37SBarry Smith   Level: advanced
229615091d37SBarry Smith 
2297be6bf707SBarry Smith .seealso: MatStoreValues()
2298be6bf707SBarry Smith 
2299be6bf707SBarry Smith @*/
2300be6bf707SBarry Smith int MatRetrieveValues(Mat mat)
2301be6bf707SBarry Smith {
2302be6bf707SBarry Smith   int ierr,(*f)(Mat);
2303be6bf707SBarry Smith 
2304be6bf707SBarry Smith   PetscFunctionBegin;
2305be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
230629bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
230729bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2308be6bf707SBarry Smith 
2309c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2310be6bf707SBarry Smith   if (f) {
2311be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2312be6bf707SBarry Smith   } else {
231329bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to retrieve values");
2314be6bf707SBarry Smith   }
2315be6bf707SBarry Smith   PetscFunctionReturn(0);
2316be6bf707SBarry Smith }
2317be6bf707SBarry Smith 
2318f83d6046SBarry Smith /*
2319f83d6046SBarry Smith    This allows SeqAIJ matrices to be passed to the matlab engine
2320f83d6046SBarry Smith */
232187828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2322f83d6046SBarry Smith #include "engine.h"   /* Matlab include file */
2323f83d6046SBarry Smith #include "mex.h"      /* Matlab include file */
2324f83d6046SBarry Smith EXTERN_C_BEGIN
23254a2ae208SSatish Balay #undef __FUNCT__
23264a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEnginePut_SeqAIJ"
2327f83d6046SBarry Smith int MatMatlabEnginePut_SeqAIJ(PetscObject obj,void *engine)
2328f83d6046SBarry Smith {
232966826eb6SBarry Smith   int         ierr,i,*ai,*aj;
2330f83d6046SBarry Smith   Mat         B = (Mat)obj;
233187828ca2SBarry Smith   PetscScalar *array;
2332f83d6046SBarry Smith   mxArray     *mat;
2333d79a51d8SBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)B->data;
2334d79a51d8SBarry Smith 
2335f83d6046SBarry Smith   PetscFunctionBegin;
233601820c62SBarry Smith   mat  = mxCreateSparse(B->n,B->m,aij->nz,mxREAL);
233787828ca2SBarry Smith   ierr = PetscMemcpy(mxGetPr(mat),aij->a,aij->nz*sizeof(PetscScalar));CHKERRQ(ierr);
2338d79a51d8SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
233966826eb6SBarry Smith   ierr = PetscMemcpy(mxGetIr(mat),aij->j,aij->nz*sizeof(int));CHKERRQ(ierr);
234066826eb6SBarry Smith   ierr = PetscMemcpy(mxGetJc(mat),aij->i,(B->m+1)*sizeof(int));CHKERRQ(ierr);
2341f83d6046SBarry Smith 
234201820c62SBarry Smith   /* Matlab indices start at 0 for sparse (what a surprise) */
234301820c62SBarry Smith   if (aij->indexshift) {
234401820c62SBarry Smith     for (i=0; i<B->m+1; i++) {
234501820c62SBarry Smith       ai[i]--;
2346d79a51d8SBarry Smith     }
2347d79a51d8SBarry Smith     for (i=0; i<aij->nz; i++) {
234801820c62SBarry Smith       aj[i]--;
2349d79a51d8SBarry Smith     }
2350d79a51d8SBarry Smith   }
2351ca44d042SBarry Smith   ierr = PetscObjectName(obj);CHKERRQ(ierr);
2352f83d6046SBarry Smith   mxSetName(mat,obj->name);
2353f83d6046SBarry Smith   engPutArray((Engine *)engine,mat);
2354f83d6046SBarry Smith   PetscFunctionReturn(0);
2355f83d6046SBarry Smith }
2356f83d6046SBarry Smith EXTERN_C_END
235766826eb6SBarry Smith 
235866826eb6SBarry Smith EXTERN_C_BEGIN
23594a2ae208SSatish Balay #undef __FUNCT__
23604a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEngineGet_SeqAIJ"
236166826eb6SBarry Smith int MatMatlabEngineGet_SeqAIJ(PetscObject obj,void *engine)
236266826eb6SBarry Smith {
236366826eb6SBarry Smith   int        ierr,ii;
236466826eb6SBarry Smith   Mat        mat = (Mat)obj;
236566826eb6SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
236666826eb6SBarry Smith   mxArray    *mmat;
236766826eb6SBarry Smith 
23686c0721eeSBarry Smith   PetscFunctionBegin;
236966826eb6SBarry Smith   ierr = PetscFree(aij->a);CHKERRQ(ierr);
237066826eb6SBarry Smith   aij->indexshift = 0;
237166826eb6SBarry Smith 
237266826eb6SBarry Smith   mmat = engGetArray((Engine *)engine,obj->name);
237366826eb6SBarry Smith 
2374a65776f3SBarry Smith   aij->nz           = (mxGetJc(mmat))[mat->m];
237587828ca2SBarry Smith   ierr              = PetscMalloc(aij->nz*(sizeof(int)+sizeof(PetscScalar))+(mat->m+1)*sizeof(int),&aij->a);CHKERRQ(ierr);
237666826eb6SBarry Smith   aij->j            = (int*)(aij->a + aij->nz);
237766826eb6SBarry Smith   aij->i            = aij->j + aij->nz;
237866826eb6SBarry Smith   aij->singlemalloc = PETSC_TRUE;
237966826eb6SBarry Smith   aij->freedata     = PETSC_TRUE;
238066826eb6SBarry Smith 
238187828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,mxGetPr(mmat),aij->nz*sizeof(PetscScalar));CHKERRQ(ierr);
238266826eb6SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
238366826eb6SBarry Smith   ierr = PetscMemcpy(aij->j,mxGetIr(mmat),aij->nz*sizeof(int));CHKERRQ(ierr);
238466826eb6SBarry Smith   ierr = PetscMemcpy(aij->i,mxGetJc(mmat),(mat->m+1)*sizeof(int));CHKERRQ(ierr);
238566826eb6SBarry Smith 
238666826eb6SBarry Smith   for (ii=0; ii<mat->m; ii++) {
238766826eb6SBarry Smith     aij->ilen[ii] = aij->imax[ii] = aij->i[ii+1] - aij->i[ii];
238866826eb6SBarry Smith   }
238966826eb6SBarry Smith 
239066826eb6SBarry Smith   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
239166826eb6SBarry Smith   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
239266826eb6SBarry Smith 
239366826eb6SBarry Smith   PetscFunctionReturn(0);
239466826eb6SBarry Smith }
239566826eb6SBarry Smith EXTERN_C_END
2396f83d6046SBarry Smith #endif
2397f83d6046SBarry Smith 
2398be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
2399cb5b572fSBarry Smith 
24004a2ae208SSatish Balay #undef __FUNCT__
24014a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
240217ab2063SBarry Smith /*@C
2403682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
24040d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
24056e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
240651c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
24072bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
240817ab2063SBarry Smith 
2409db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2410db81eaa0SLois Curfman McInnes 
241117ab2063SBarry Smith    Input Parameters:
2412db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
241317ab2063SBarry Smith .  m - number of rows
241417ab2063SBarry Smith .  n - number of columns
241517ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
241651c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
24172bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
241817ab2063SBarry Smith 
241917ab2063SBarry Smith    Output Parameter:
2420416022c9SBarry Smith .  A - the matrix
242117ab2063SBarry Smith 
2422b259b22eSLois Curfman McInnes    Notes:
242317ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
242417ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
24250002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
242644cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
242717ab2063SBarry Smith 
242817ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2429a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
24303d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
24316da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
243217ab2063SBarry Smith 
2433682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
24344fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2435682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
24366c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
24376c7ebb05SLois Curfman McInnes 
24386c7ebb05SLois Curfman McInnes    Options Database Keys:
2439db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2440db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2441db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2442db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2443db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
244417ab2063SBarry Smith 
2445027ccd11SLois Curfman McInnes    Level: intermediate
2446027ccd11SLois Curfman McInnes 
244736db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
244836db0b34SBarry Smith 
244917ab2063SBarry Smith @*/
2450416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz,Mat *A)
245117ab2063SBarry Smith {
2452273d9f13SBarry Smith   int ierr;
24536945ee14SBarry Smith 
24543a40ed3dSBarry Smith   PetscFunctionBegin;
2455273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,m,n,A);CHKERRQ(ierr);
2456273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2457273d9f13SBarry Smith   ierr = MatSeqAIJSetPreallocation(*A,nz,nnz);CHKERRQ(ierr);
2458273d9f13SBarry Smith   PetscFunctionReturn(0);
2459273d9f13SBarry Smith }
2460273d9f13SBarry Smith 
24614a2ae208SSatish Balay #undef __FUNCT__
24624a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2463273d9f13SBarry Smith /*@C
2464273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2465273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2466273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2467273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2468273d9f13SBarry Smith 
2469273d9f13SBarry Smith    Collective on MPI_Comm
2470273d9f13SBarry Smith 
2471273d9f13SBarry Smith    Input Parameters:
2472273d9f13SBarry Smith +  comm - MPI communicator, set to PETSC_COMM_SELF
2473273d9f13SBarry Smith .  m - number of rows
2474273d9f13SBarry Smith .  n - number of columns
2475273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2476273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2477273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2478273d9f13SBarry Smith 
2479273d9f13SBarry Smith    Output Parameter:
2480273d9f13SBarry Smith .  A - the matrix
2481273d9f13SBarry Smith 
2482273d9f13SBarry Smith    Notes:
2483273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2484273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2485273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2486273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2487273d9f13SBarry Smith 
2488273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2489273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2490273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2491273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2492273d9f13SBarry Smith 
2493273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2494273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2495273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2496273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2497273d9f13SBarry Smith 
2498273d9f13SBarry Smith    Options Database Keys:
2499273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2500273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2501273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2502273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2503273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2504273d9f13SBarry Smith 
2505273d9f13SBarry Smith    Level: intermediate
2506273d9f13SBarry Smith 
2507273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2508273d9f13SBarry Smith 
2509273d9f13SBarry Smith @*/
2510273d9f13SBarry Smith int MatSeqAIJSetPreallocation(Mat B,int nz,int *nnz)
2511273d9f13SBarry Smith {
2512273d9f13SBarry Smith   Mat_SeqAIJ *b;
2513273d9f13SBarry Smith   int        i,len,ierr;
2514273d9f13SBarry Smith   PetscTruth flg2;
2515273d9f13SBarry Smith 
2516273d9f13SBarry Smith   PetscFunctionBegin;
2517273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg2);CHKERRQ(ierr);
2518273d9f13SBarry Smith   if (!flg2) PetscFunctionReturn(0);
2519d5d45c9bSBarry Smith 
2520435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2521435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2522b73539f3SBarry Smith   if (nnz) {
2523273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
252429bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
25253a7fca6bSBarry 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);
2526b73539f3SBarry Smith     }
2527b73539f3SBarry Smith   }
2528b73539f3SBarry Smith 
2529273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2530273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2531273d9f13SBarry Smith 
2532b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->imax);CHKERRQ(ierr);
2533273d9f13SBarry Smith   if (!nnz) {
2534435da068SBarry Smith     if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2535273d9f13SBarry Smith     else if (nz <= 0)        nz = 1;
2536273d9f13SBarry Smith     for (i=0; i<B->m; i++) b->imax[i] = nz;
2537273d9f13SBarry Smith     nz = nz*B->m;
2538273d9f13SBarry Smith   } else {
2539273d9f13SBarry Smith     nz = 0;
2540273d9f13SBarry Smith     for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2541273d9f13SBarry Smith   }
2542273d9f13SBarry Smith 
2543273d9f13SBarry Smith   /* allocate the matrix space */
254487828ca2SBarry Smith   len             = nz*(sizeof(int) + sizeof(PetscScalar)) + (B->m+1)*sizeof(int);
2545b0a32e0cSBarry Smith   ierr            = PetscMalloc(len,&b->a);CHKERRQ(ierr);
2546273d9f13SBarry Smith   b->j            = (int*)(b->a + nz);
2547273d9f13SBarry Smith   ierr            = PetscMemzero(b->j,nz*sizeof(int));CHKERRQ(ierr);
2548273d9f13SBarry Smith   b->i            = b->j + nz;
2549273d9f13SBarry Smith   b->singlemalloc = PETSC_TRUE;
2550273d9f13SBarry Smith   b->freedata     = PETSC_TRUE;
2551273d9f13SBarry Smith 
2552273d9f13SBarry Smith   b->i[0] = -b->indexshift;
2553273d9f13SBarry Smith   for (i=1; i<B->m+1; i++) {
2554273d9f13SBarry Smith     b->i[i] = b->i[i-1] + b->imax[i-1];
2555273d9f13SBarry Smith   }
2556273d9f13SBarry Smith 
2557273d9f13SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
2558b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->ilen);CHKERRQ(ierr);
2559b0a32e0cSBarry Smith   PetscLogObjectMemory(B,len+2*(B->m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2560273d9f13SBarry Smith   for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2561273d9f13SBarry Smith 
2562273d9f13SBarry Smith   b->nz                = 0;
2563273d9f13SBarry Smith   b->maxnz             = nz;
2564273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2565273d9f13SBarry Smith   PetscFunctionReturn(0);
2566273d9f13SBarry Smith }
2567273d9f13SBarry Smith 
2568273d9f13SBarry Smith EXTERN_C_BEGIN
25694a2ae208SSatish Balay #undef __FUNCT__
25704a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2571273d9f13SBarry Smith int MatCreate_SeqAIJ(Mat B)
2572273d9f13SBarry Smith {
2573273d9f13SBarry Smith   Mat_SeqAIJ *b;
2574273d9f13SBarry Smith   int        ierr,size;
2575273d9f13SBarry Smith   PetscTruth flg;
2576273d9f13SBarry Smith 
2577273d9f13SBarry Smith   PetscFunctionBegin;
2578273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2579273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2580273d9f13SBarry Smith 
2581273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2582273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2583273d9f13SBarry Smith 
2584b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2585b0a32e0cSBarry Smith   B->data             = (void*)b;
2586549d3d68SSatish Balay   ierr = PetscMemzero(b,sizeof(Mat_SeqAIJ));CHKERRQ(ierr);
2587549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2588416022c9SBarry Smith   B->factor           = 0;
2589416022c9SBarry Smith   B->lupivotthreshold = 1.0;
259090f02eecSBarry Smith   B->mapping          = 0;
259187828ca2SBarry Smith   ierr = PetscOptionsGetReal(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr);
2592b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2593416022c9SBarry Smith   b->row              = 0;
2594416022c9SBarry Smith   b->col              = 0;
259582bf6240SBarry Smith   b->icol             = 0;
2596416022c9SBarry Smith   b->indexshift       = 0;
2597b810aeb4SBarry Smith   b->reallocs         = 0;
2598b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_oneindex",&flg);CHKERRQ(ierr);
259969957df2SSatish Balay   if (flg) b->indexshift = -1;
260017ab2063SBarry Smith 
26018a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
26028a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2603a5ae1ecdSBarry Smith 
2604f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
260536db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2606f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2607416022c9SBarry Smith   b->nonew             = 0;
2608416022c9SBarry Smith   b->diag              = 0;
2609416022c9SBarry Smith   b->solve_work        = 0;
261071bd300dSLois Curfman McInnes   b->spptr             = 0;
2611b65db4caSBarry Smith   b->inode.use         = PETSC_TRUE;
2612754ec7b1SSatish Balay   b->inode.node_count  = 0;
2613754ec7b1SSatish Balay   b->inode.size        = 0;
26146c7ebb05SLois Curfman McInnes   b->inode.limit       = 5;
26156c7ebb05SLois Curfman McInnes   b->inode.max_limit   = 5;
2616be6bf707SBarry Smith   b->saved_values      = 0;
2617d7f994e1SBarry Smith   b->idiag             = 0;
2618d7f994e1SBarry Smith   b->ssor              = 0;
2619f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
262017ab2063SBarry Smith 
262135d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
262235d8aa7fSBarry Smith 
2623aa482453SBarry Smith #if defined(PETSC_HAVE_SUPERLU)
2624b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_superlu",&flg);CHKERRQ(ierr);
262569957df2SSatish Balay   if (flg) { ierr = MatUseSuperLU_SeqAIJ(B);CHKERRQ(ierr); }
26264b14c69eSBarry Smith #endif
2627b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_essl",&flg);CHKERRQ(ierr);
262869957df2SSatish Balay   if (flg) { ierr = MatUseEssl_SeqAIJ(B);CHKERRQ(ierr); }
2629b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_lusol",&flg);CHKERRQ(ierr);
2630cd5578b5SBarry Smith   if (flg) { ierr = MatUseLUSOL_SeqAIJ(B);CHKERRQ(ierr); }
2631b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_matlab",&flg);CHKERRQ(ierr);
2632ca44d042SBarry Smith   if (flg) {ierr = MatUseMatlab_SeqAIJ(B);CHKERRQ(ierr);}
2633b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_dxml",&flg);CHKERRQ(ierr);
263469957df2SSatish Balay   if (flg) {
263529bbc08cSBarry Smith     if (!b->indexshift) SETERRQ(PETSC_ERR_LIB,"need -mat_aij_oneindex with -mat_aij_dxml");
2636416022c9SBarry Smith     ierr = MatUseDXML_SeqAIJ(B);CHKERRQ(ierr);
263717ab2063SBarry Smith   }
2638f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2639bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2640bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2641f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2642be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2643bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2644f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2645be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2646bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
264787828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2648f83d6046SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatMatlabEnginePut_SeqAIJ",MatMatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
264966826eb6SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatMatlabEngineGet_SeqAIJ",MatMatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
2650f83d6046SBarry Smith #endif
26513a40ed3dSBarry Smith   PetscFunctionReturn(0);
265217ab2063SBarry Smith }
2653273d9f13SBarry Smith EXTERN_C_END
265417ab2063SBarry Smith 
26554a2ae208SSatish Balay #undef __FUNCT__
26564a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2657be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
265817ab2063SBarry Smith {
2659416022c9SBarry Smith   Mat        C;
2660416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data;
2661273d9f13SBarry Smith   int        i,len,m = A->m,shift = a->indexshift,ierr;
266217ab2063SBarry Smith 
26633a40ed3dSBarry Smith   PetscFunctionBegin;
26644043dd9cSLois Curfman McInnes   *B = 0;
2665273d9f13SBarry Smith   ierr = MatCreate(A->comm,A->m,A->n,A->m,A->n,&C);CHKERRQ(ierr);
2666273d9f13SBarry Smith   ierr = MatSetType(C,MATSEQAIJ);CHKERRQ(ierr);
2667273d9f13SBarry Smith   c    = (Mat_SeqAIJ*)C->data;
2668273d9f13SBarry Smith 
2669416022c9SBarry Smith   C->factor         = A->factor;
2670416022c9SBarry Smith   c->row            = 0;
2671416022c9SBarry Smith   c->col            = 0;
267282bf6240SBarry Smith   c->icol           = 0;
2673416022c9SBarry Smith   c->indexshift     = shift;
2674f1e2ffcdSBarry Smith   c->keepzeroedrows = a->keepzeroedrows;
2675c456f294SBarry Smith   C->assembled      = PETSC_TRUE;
267617ab2063SBarry Smith 
2677273d9f13SBarry Smith   C->M          = A->m;
2678273d9f13SBarry Smith   C->N          = A->n;
267917ab2063SBarry Smith 
2680b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->imax);CHKERRQ(ierr);
2681b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->ilen);CHKERRQ(ierr);
268217ab2063SBarry Smith   for (i=0; i<m; i++) {
2683416022c9SBarry Smith     c->imax[i] = a->imax[i];
2684416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
268517ab2063SBarry Smith   }
268617ab2063SBarry Smith 
268717ab2063SBarry Smith   /* allocate the matrix space */
2688f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
268987828ca2SBarry Smith   len   = (m+1)*sizeof(int)+(a->i[m])*(sizeof(PetscScalar)+sizeof(int));
2690b0a32e0cSBarry Smith   ierr  = PetscMalloc(len,&c->a);CHKERRQ(ierr);
2691416022c9SBarry Smith   c->j  = (int*)(c->a + a->i[m] + shift);
2692416022c9SBarry Smith   c->i  = c->j + a->i[m] + shift;
2693549d3d68SSatish Balay   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));CHKERRQ(ierr);
269417ab2063SBarry Smith   if (m > 0) {
2695549d3d68SSatish Balay     ierr = PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int));CHKERRQ(ierr);
2696be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
269787828ca2SBarry Smith       ierr = PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(PetscScalar));CHKERRQ(ierr);
2698be6bf707SBarry Smith     } else {
269987828ca2SBarry Smith       ierr = PetscMemzero(c->a,(a->i[m]+shift)*sizeof(PetscScalar));CHKERRQ(ierr);
270017ab2063SBarry Smith     }
270108480c60SBarry Smith   }
270217ab2063SBarry Smith 
2703b0a32e0cSBarry Smith   PetscLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2704416022c9SBarry Smith   c->sorted      = a->sorted;
2705416022c9SBarry Smith   c->roworiented = a->roworiented;
2706416022c9SBarry Smith   c->nonew       = a->nonew;
27077a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
2708be6bf707SBarry Smith   c->saved_values = 0;
2709d7f994e1SBarry Smith   c->idiag        = 0;
2710d7f994e1SBarry Smith   c->ssor         = 0;
271136db0b34SBarry Smith   c->ignorezeroentries = a->ignorezeroentries;
271236db0b34SBarry Smith   c->freedata     = PETSC_TRUE;
271317ab2063SBarry Smith 
2714416022c9SBarry Smith   if (a->diag) {
2715b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&c->diag);CHKERRQ(ierr);
2716b0a32e0cSBarry Smith     PetscLogObjectMemory(C,(m+1)*sizeof(int));
271717ab2063SBarry Smith     for (i=0; i<m; i++) {
2718416022c9SBarry Smith       c->diag[i] = a->diag[i];
271917ab2063SBarry Smith     }
27203a40ed3dSBarry Smith   } else c->diag        = 0;
2721b65db4caSBarry Smith   c->inode.use          = a->inode.use;
27226c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
27236c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
2724754ec7b1SSatish Balay   if (a->inode.size){
2725b0a32e0cSBarry Smith     ierr                = PetscMalloc((m+1)*sizeof(int),&c->inode.size);CHKERRQ(ierr);
2726754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
2727549d3d68SSatish Balay     ierr                = PetscMemcpy(c->inode.size,a->inode.size,(m+1)*sizeof(int));CHKERRQ(ierr);
2728754ec7b1SSatish Balay   } else {
2729754ec7b1SSatish Balay     c->inode.size       = 0;
2730754ec7b1SSatish Balay     c->inode.node_count = 0;
2731754ec7b1SSatish Balay   }
2732416022c9SBarry Smith   c->nz                 = a->nz;
2733416022c9SBarry Smith   c->maxnz              = a->maxnz;
2734416022c9SBarry Smith   c->solve_work         = 0;
273576dd722bSSatish Balay   c->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
2736273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2737754ec7b1SSatish Balay 
2738416022c9SBarry Smith   *B = C;
2739b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
27403a40ed3dSBarry Smith   PetscFunctionReturn(0);
274117ab2063SBarry Smith }
274217ab2063SBarry Smith 
2743273d9f13SBarry Smith EXTERN_C_BEGIN
27444a2ae208SSatish Balay #undef __FUNCT__
27454a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
2746b0a32e0cSBarry Smith int MatLoad_SeqAIJ(PetscViewer viewer,MatType type,Mat *A)
274717ab2063SBarry Smith {
2748416022c9SBarry Smith   Mat_SeqAIJ   *a;
2749416022c9SBarry Smith   Mat          B;
275017699dbbSLois Curfman McInnes   int          i,nz,ierr,fd,header[4],size,*rowlengths = 0,M,N,shift;
2751bcd2baecSBarry Smith   MPI_Comm     comm;
275217ab2063SBarry Smith 
27533a40ed3dSBarry Smith   PetscFunctionBegin;
2754e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2755d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
275629bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
2757b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
27580752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2759552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
276017ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
276117ab2063SBarry Smith 
2762d64ed03dSBarry Smith   if (nz < 0) {
276329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2764d64ed03dSBarry Smith   }
2765d64ed03dSBarry Smith 
276617ab2063SBarry Smith   /* read in row lengths */
2767b0a32e0cSBarry Smith   ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
27680752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
276917ab2063SBarry Smith 
277017ab2063SBarry Smith   /* create our matrix */
2771416022c9SBarry Smith   ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A);CHKERRQ(ierr);
2772416022c9SBarry Smith   B = *A;
2773416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
2774416022c9SBarry Smith   shift = a->indexshift;
277517ab2063SBarry Smith 
277617ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
27770752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
277817ab2063SBarry Smith   if (shift) {
277917ab2063SBarry Smith     for (i=0; i<nz; i++) {
2780416022c9SBarry Smith       a->j[i] += 1;
278117ab2063SBarry Smith     }
278217ab2063SBarry Smith   }
278317ab2063SBarry Smith 
278417ab2063SBarry Smith   /* read in nonzero values */
27850752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
278617ab2063SBarry Smith 
278717ab2063SBarry Smith   /* set matrix "i" values */
2788416022c9SBarry Smith   a->i[0] = -shift;
278917ab2063SBarry Smith   for (i=1; i<= M; i++) {
2790416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2791416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
279217ab2063SBarry Smith   }
2793606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
279417ab2063SBarry Smith 
27956d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27966d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27973a40ed3dSBarry Smith   PetscFunctionReturn(0);
279817ab2063SBarry Smith }
2799273d9f13SBarry Smith EXTERN_C_END
280017ab2063SBarry Smith 
28014a2ae208SSatish Balay #undef __FUNCT__
2802b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
28038f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
28047264ac53SSatish Balay {
28057264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
28060f5bd95cSBarry Smith   int        ierr;
2807273d9f13SBarry Smith   PetscTruth flag;
28087264ac53SSatish Balay 
28093a40ed3dSBarry Smith   PetscFunctionBegin;
2810273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flag);CHKERRQ(ierr);
2811273d9f13SBarry Smith   if (!flag) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type");
28127264ac53SSatish Balay 
28137264ac53SSatish Balay   /* If the  matrix dimensions are not equal,or no of nonzeros or shift */
2814273d9f13SBarry Smith   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)|| (a->indexshift != b->indexshift)) {
2815ca44d042SBarry Smith     *flg = PETSC_FALSE;
2816ca44d042SBarry Smith     PetscFunctionReturn(0);
2817bcd2baecSBarry Smith   }
28187264ac53SSatish Balay 
28197264ac53SSatish Balay   /* if the a->i are the same */
2820273d9f13SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(int),flg);CHKERRQ(ierr);
28210f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
28227264ac53SSatish Balay 
28237264ac53SSatish Balay   /* if a->j are the same */
28240f5bd95cSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),flg);CHKERRQ(ierr);
28250f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
2826bcd2baecSBarry Smith 
2827bcd2baecSBarry Smith   /* if a->a are the same */
282887828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
28290f5bd95cSBarry Smith 
28303a40ed3dSBarry Smith   PetscFunctionReturn(0);
28317264ac53SSatish Balay 
28327264ac53SSatish Balay }
283336db0b34SBarry Smith 
28344a2ae208SSatish Balay #undef __FUNCT__
28354a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
283636db0b34SBarry Smith /*@C
283736db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
283836db0b34SBarry Smith               provided by the user.
283936db0b34SBarry Smith 
284036db0b34SBarry Smith       Coolective on MPI_Comm
284136db0b34SBarry Smith 
284236db0b34SBarry Smith    Input Parameters:
284336db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
284436db0b34SBarry Smith .   m - number of rows
284536db0b34SBarry Smith .   n - number of columns
284636db0b34SBarry Smith .   i - row indices
284736db0b34SBarry Smith .   j - column indices
284836db0b34SBarry Smith -   a - matrix values
284936db0b34SBarry Smith 
285036db0b34SBarry Smith    Output Parameter:
285136db0b34SBarry Smith .   mat - the matrix
285236db0b34SBarry Smith 
285336db0b34SBarry Smith    Level: intermediate
285436db0b34SBarry Smith 
285536db0b34SBarry Smith    Notes:
28560551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
285736db0b34SBarry Smith     once the matrix is destroyed
285836db0b34SBarry Smith 
285936db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
286036db0b34SBarry Smith 
286136db0b34SBarry Smith        The i and j indices can be either 0- or 1 based
286236db0b34SBarry Smith 
286336db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
286436db0b34SBarry Smith 
286536db0b34SBarry Smith @*/
286687828ca2SBarry Smith int MatCreateSeqAIJWithArrays(MPI_Comm comm,int m,int n,int* i,int*j,PetscScalar *a,Mat *mat)
286736db0b34SBarry Smith {
286836db0b34SBarry Smith   int        ierr,ii;
286936db0b34SBarry Smith   Mat_SeqAIJ *aij;
287036db0b34SBarry Smith 
287136db0b34SBarry Smith   PetscFunctionBegin;
287236db0b34SBarry Smith   ierr = MatCreateSeqAIJ(comm,m,n,0,0,mat);CHKERRQ(ierr);
287336db0b34SBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
287436db0b34SBarry Smith   ierr = PetscFree(aij->a);CHKERRQ(ierr);
287536db0b34SBarry Smith 
287636db0b34SBarry Smith   if (i[0] == 1) {
287736db0b34SBarry Smith     aij->indexshift = -1;
287836db0b34SBarry Smith   } else if (i[0]) {
287929bbc08cSBarry Smith     SETERRQ(1,"i (row indices) do not start with 0 or 1");
288036db0b34SBarry Smith   }
288136db0b34SBarry Smith   aij->i = i;
288236db0b34SBarry Smith   aij->j = j;
288336db0b34SBarry Smith   aij->a = a;
288436db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
288536db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
288636db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
288736db0b34SBarry Smith 
288836db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
288936db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
28909860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
289129bbc08cSBarry 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]);
289236db0b34SBarry Smith #endif
289336db0b34SBarry Smith   }
28949860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
289536db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
289629bbc08cSBarry Smith     if (j[ii] < -aij->indexshift) SETERRQ2(1,"Negative column index at location = %d index = %d",ii,j[ii]);
289729bbc08cSBarry Smith     if (j[ii] > n - 1 -aij->indexshift) SETERRQ2(1,"Column index to large at location = %d index = %d",ii,j[ii]);
289836db0b34SBarry Smith   }
289936db0b34SBarry Smith #endif
290036db0b34SBarry Smith 
2901b65db4caSBarry Smith   /* changes indices to start at 0 */
2902b65db4caSBarry Smith   if (i[0]) {
2903b65db4caSBarry Smith     aij->indexshift = 0;
2904b65db4caSBarry Smith     for (ii=0; ii<m; ii++) {
2905b65db4caSBarry Smith       i[ii]--;
2906b65db4caSBarry Smith     }
2907b65db4caSBarry Smith     for (ii=0; ii<i[m]; ii++) {
2908b65db4caSBarry Smith       j[ii]--;
2909b65db4caSBarry Smith     }
2910b65db4caSBarry Smith   }
2911b65db4caSBarry Smith 
2912b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2913b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
291436db0b34SBarry Smith   PetscFunctionReturn(0);
291536db0b34SBarry Smith }
291636db0b34SBarry Smith 
2917cc8ba8e1SBarry Smith #undef __FUNCT__
2918ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
2919ee4f033dSBarry Smith int MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
2920cc8ba8e1SBarry Smith {
2921cc8ba8e1SBarry Smith   int        ierr;
2922cc8ba8e1SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
292336db0b34SBarry Smith 
2924cc8ba8e1SBarry Smith   PetscFunctionBegin;
2925cc8ba8e1SBarry Smith   ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
2926cc8ba8e1SBarry Smith   a->coloring = coloring;
2927cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
2928cc8ba8e1SBarry Smith }
2929cc8ba8e1SBarry Smith 
293087828ca2SBarry Smith #if defined(PETSC_HAVE_ADIC) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2931ee4f033dSBarry Smith EXTERN_C_BEGIN
2932cc8ba8e1SBarry Smith #include "adic_utils.h"
2933ee4f033dSBarry Smith EXTERN_C_END
2934cc8ba8e1SBarry Smith 
2935cc8ba8e1SBarry Smith #undef __FUNCT__
2936ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
2937ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
2938cc8ba8e1SBarry Smith {
2939cc8ba8e1SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
2940ee4f033dSBarry Smith   int         m = A->m,*ii = a->i,*jj = a->j,nz,i,*color,j,nlen;
294187828ca2SBarry Smith   PetscScalar *v = a->a,*values;
2942ee4f033dSBarry Smith   char        *cadvalues = (char *)advalues;
2943cc8ba8e1SBarry Smith 
2944cc8ba8e1SBarry Smith   PetscFunctionBegin;
2945cc8ba8e1SBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
2946ee4f033dSBarry Smith   nlen  = my_AD_GetDerivTypeSize();
2947cc8ba8e1SBarry Smith   color = a->coloring->colors;
2948cc8ba8e1SBarry Smith   /* loop over rows */
2949cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
2950cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
2951cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
2952ee4f033dSBarry Smith     values = my_AD_GetGradArray(cadvalues);
2953cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
2954cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
2955cc8ba8e1SBarry Smith     }
2956ee4f033dSBarry Smith     cadvalues += nlen; /* jump to next row of derivatives */
2957ee4f033dSBarry Smith   }
2958ee4f033dSBarry Smith   PetscFunctionReturn(0);
2959ee4f033dSBarry Smith }
2960ee4f033dSBarry Smith 
2961ee4f033dSBarry Smith #else
2962ee4f033dSBarry Smith 
2963ee4f033dSBarry Smith #undef __FUNCT__
2964ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
2965ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
2966ee4f033dSBarry Smith {
2967ee4f033dSBarry Smith   PetscFunctionBegin;
2968ee4f033dSBarry Smith   SETERRQ(1,"PETSc installed without ADIC");
2969ee4f033dSBarry Smith }
2970ee4f033dSBarry Smith 
2971ee4f033dSBarry Smith #endif
2972ee4f033dSBarry Smith 
2973ee4f033dSBarry Smith #undef __FUNCT__
2974ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
2975ee4f033dSBarry Smith int MatSetValuesAdifor_SeqAIJ(Mat A,int nl,void *advalues)
2976ee4f033dSBarry Smith {
2977ee4f033dSBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
2978ee4f033dSBarry Smith   int          m = A->m,*ii = a->i,*jj = a->j,nz,i,*color,j;
297987828ca2SBarry Smith   PetscScalar  *v = a->a,*values = (PetscScalar *)advalues;
2980ee4f033dSBarry Smith 
2981ee4f033dSBarry Smith   PetscFunctionBegin;
2982ee4f033dSBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
2983ee4f033dSBarry Smith   color = a->coloring->colors;
2984ee4f033dSBarry Smith   /* loop over rows */
2985ee4f033dSBarry Smith   for (i=0; i<m; i++) {
2986ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
2987ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
2988ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
2989ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
2990ee4f033dSBarry Smith     }
2991ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
2992cc8ba8e1SBarry Smith   }
2993cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
2994cc8ba8e1SBarry Smith }
299536db0b34SBarry Smith 
2996