xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 35d8aa7f675fa4ee5c80aa031e65d3ef307a4891)
1*35d8aa7fSBarry Smith /*$Id: aij.c,v 1.357 2000/10/24 20:25:32 bsmith Exp bsmith $*/
2d5d45c9bSBarry Smith /*
33369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
4d5d45c9bSBarry Smith   matrix storage format.
5d5d45c9bSBarry Smith */
63369ce9aSBarry Smith 
7e090d566SSatish Balay #include "petscsys.h"
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 
175615d1e5SSatish Balay #undef __FUNC__
18ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetRowIJ_SeqAIJ"></a>*/"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;
286945ee14SBarry Smith   if (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) {
31273d9f13SBarry Smith     int nz = a->i[A->m];
323b2fbd54SBarry Smith     /* malloc space and  subtract 1 from i and j indices */
33273d9f13SBarry Smith     *ia = (int*)PetscMalloc((A->m+1)*sizeof(int));CHKPTRQ(*ia);
343b2fbd54SBarry Smith     *ja = (int*)PetscMalloc((nz+1)*sizeof(int));CHKPTRQ(*ja);
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) {
38273d9f13SBarry Smith     int nz = a->i[A->m] + 1;
393b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
40273d9f13SBarry Smith     *ia = (int*)PetscMalloc((A->m+1)*sizeof(int));CHKPTRQ(*ia);
413b2fbd54SBarry Smith     *ja = (int*)PetscMalloc((nz+1)*sizeof(int));CHKPTRQ(*ja);
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 
505615d1e5SSatish Balay #undef __FUNC__
51ca44d042SBarry Smith #define __FUNC__ /*<a name="MatRestoreRowIJ_SeqAIJ"></a>*/"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);
593b2fbd54SBarry Smith   if (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 
665615d1e5SSatish Balay #undef __FUNC__
67ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetColumnIJ_SeqAIJ"></a>*/"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 {
8061d2ded1SBarry Smith     collengths = (int*)PetscMalloc((n+1)*sizeof(int));CHKPTRQ(collengths);
81549d3d68SSatish Balay     ierr       = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
823b2fbd54SBarry Smith     cia        = (int*)PetscMalloc((n+1)*sizeof(int));CHKPTRQ(cia);
83a93ec695SBarry Smith     cja        = (int*)PetscMalloc((nz+1)*sizeof(int));CHKPTRQ(cja);
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 
1075615d1e5SSatish Balay #undef __FUNC__
108ca44d042SBarry Smith #define __FUNC__ /*<a name="MatRestoreColumnIJ_SeqAIJ"></a>*/"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 
1245615d1e5SSatish Balay #undef __FUNC__
125ca44d042SBarry Smith #define __FUNC__ /*<a name="MatSetValues_SeqAIJ"></a>*/"MatSetValues_SeqAIJ"
12661d2ded1SBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *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;
132416022c9SBarry Smith   Scalar     *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;
17817ab2063SBarry Smith         Scalar *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 */
183273d9f13SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(A->m+1)*sizeof(int);
1840452661fSBarry Smith         new_a   = (Scalar*)PetscMalloc(len);CHKPTRQ(new_a);
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);
194549d3d68SSatish Balay         ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar));CHKERRQ(ierr);
195549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow,len*sizeof(Scalar));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;
207416022c9SBarry Smith         PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar)));
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 
2275615d1e5SSatish Balay #undef __FUNC__
228ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetValues_SeqAIJ"></a>*/"MatGetValues_SeqAIJ"
2298f6be9afSLois Curfman McInnes int MatGetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *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;
2347eb43aa7SLois Curfman McInnes   Scalar     *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 
2685615d1e5SSatish Balay #undef __FUNC__
269ca44d042SBarry Smith #define __FUNC__ /*<a name="MatView_SeqAIJ_Binary"></a>*/"MatView_SeqAIJ_Binary"
270480ef9eaSBarry Smith int MatView_SeqAIJ_Binary(Mat A,Viewer 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;
27690ace30eSBarry Smith   ierr = ViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
277273d9f13SBarry Smith   col_lens = (int*)PetscMalloc((4+A->m)*sizeof(int));CHKPTRQ(col_lens);
278416022c9SBarry Smith   col_lens[0] = MAT_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 
3045615d1e5SSatish Balay #undef __FUNC__
305ca44d042SBarry Smith #define __FUNC__ /*<a name="MatView_SeqAIJ_ASCII"></a>*/"MatView_SeqAIJ_ASCII"
306480ef9eaSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,Viewer viewer)
307416022c9SBarry Smith {
308416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
309273d9f13SBarry Smith   int         ierr,i,j,m = A->m,shift = a->indexshift,format;
31017ab2063SBarry Smith   char        *outputname;
31117ab2063SBarry Smith 
3123a40ed3dSBarry Smith   PetscFunctionBegin;
313fb4b0f7fSBarry Smith   ierr = ViewerGetOutputname(viewer,&outputname);CHKERRQ(ierr);
314888f2ed8SSatish Balay   ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr);
3156831982aSBarry Smith   if (format == VIEWER_FORMAT_ASCII_INFO_LONG || format == VIEWER_FORMAT_ASCII_INFO) {
3166831982aSBarry Smith     if (a->inode.size) {
3176831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"using I-node routines: found %d nodes, limit used is %d\n",a->inode.node_count,a->inode.limit);CHKERRQ(ierr);
3186831982aSBarry Smith     } else {
3196831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"not using I-node routines\n");CHKERRQ(ierr);
3206831982aSBarry Smith     }
3213a40ed3dSBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_MATLAB) {
322d00d2cf4SBarry Smith     int nofinalvalue = 0;
323273d9f13SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) {
324d00d2cf4SBarry Smith       nofinalvalue = 1;
325d00d2cf4SBarry Smith     }
3266831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
327273d9f13SBarry Smith     ierr = ViewerASCIIPrintf(viewer,"%% Size = %d %d \n",m,A->n);CHKERRQ(ierr);
3286831982aSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"%% Nonzeros = %d \n",a->nz);CHKERRQ(ierr);
3296831982aSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
3306831982aSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
33117ab2063SBarry Smith 
33217ab2063SBarry Smith     for (i=0; i<m; i++) {
333416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
334aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
33536db0b34SBarry Smith         ierr = ViewerASCIIPrintf(viewer,"%d %d  %18.16e + %18.16ei \n",i+1,a->j[j]+!shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
33617ab2063SBarry Smith #else
3376831982aSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
33817ab2063SBarry Smith #endif
33917ab2063SBarry Smith       }
34017ab2063SBarry Smith     }
341d00d2cf4SBarry Smith     if (nofinalvalue) {
342273d9f13SBarry Smith       ierr = ViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",m,A->n,0.0);CHKERRQ(ierr);
343d00d2cf4SBarry Smith     }
3446831982aSBarry Smith     if (outputname) {ierr = ViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",outputname);CHKERRQ(ierr);}
3456831982aSBarry Smith     else            {ierr = ViewerASCIIPrintf(viewer,"];\n M = spconvert(zzz);\n");CHKERRQ(ierr);}
3466831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
3473a40ed3dSBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_COMMON) {
3486831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
34944cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
3506831982aSBarry Smith       ierr = ViewerASCIIPrintf(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) {
35436db0b34SBarry Smith           ierr = ViewerASCIIPrintf(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) {
35636db0b34SBarry Smith           ierr = ViewerASCIIPrintf(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) {
35836db0b34SBarry Smith           ierr = ViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3596831982aSBarry Smith         }
36044cd7ae7SLois Curfman McInnes #else
3616831982aSBarry Smith         if (a->a[j] != 0.0) {ierr = ViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
36244cd7ae7SLois Curfman McInnes #endif
36344cd7ae7SLois Curfman McInnes       }
3646831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
36544cd7ae7SLois Curfman McInnes     }
3666831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
36702594712SBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_SYMMODU) {
368496be53dSLois Curfman McInnes     int nzd=0,fshift=1,*sptr;
3696831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
3702e44a96cSLois Curfman McInnes     sptr = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(sptr);
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;
3846831982aSBarry Smith     ierr = ViewerASCIIPrintf(viewer," %d %d\n\n",m,nzd);CHKERRQ(ierr);
3852e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
3866831982aSBarry Smith       if (i+4<m) {ierr = ViewerASCIIPrintf(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);}
3876831982aSBarry Smith       else if (i+3<m) {ierr = ViewerASCIIPrintf(viewer," %d %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);CHKERRQ(ierr);}
3886831982aSBarry Smith       else if (i+2<m) {ierr = ViewerASCIIPrintf(viewer," %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);}
3896831982aSBarry Smith       else if (i+1<m) {ierr = ViewerASCIIPrintf(viewer," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
3906831982aSBarry Smith       else if (i<m)   {ierr = ViewerASCIIPrintf(viewer," %d %d\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
3916831982aSBarry Smith       else            {ierr = ViewerASCIIPrintf(viewer," %d\n",sptr[i]);CHKERRQ(ierr);}
392496be53dSLois Curfman McInnes     }
3936831982aSBarry Smith     ierr = ViewerASCIIPrintf(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++) {
3976831982aSBarry Smith         if (a->j[j] >= i) {ierr = ViewerASCIIPrintf(viewer," %d ",a->j[j]+fshift);CHKERRQ(ierr);}
398496be53dSLois Curfman McInnes       }
3996831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
400496be53dSLois Curfman McInnes     }
4016831982aSBarry Smith     ierr = ViewerASCIIPrintf(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) {
40736db0b34SBarry Smith             ierr = ViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4086831982aSBarry Smith           }
409496be53dSLois Curfman McInnes #else
4106831982aSBarry Smith           if (a->a[j] != 0.0) {ierr = ViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
411496be53dSLois Curfman McInnes #endif
412496be53dSLois Curfman McInnes         }
413496be53dSLois Curfman McInnes       }
4146831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
415496be53dSLois Curfman McInnes     }
4166831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
41702594712SBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_DENSE) {
41802594712SBarry Smith     int    cnt = 0,jcnt;
41902594712SBarry Smith     Scalar value;
42002594712SBarry Smith 
4216831982aSBarry Smith     ierr = ViewerASCIIUseTabs(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)
43236db0b34SBarry Smith         ierr = ViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
43302594712SBarry Smith #else
4346831982aSBarry Smith         ierr = ViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
43502594712SBarry Smith #endif
43602594712SBarry Smith       }
4376831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
43802594712SBarry Smith     }
4396831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4403a40ed3dSBarry Smith   } else {
4416831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
44217ab2063SBarry Smith     for (i=0; i<m; i++) {
4436831982aSBarry Smith       ierr = ViewerASCIIPrintf(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) {
44736db0b34SBarry Smith           ierr = ViewerASCIIPrintf(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) {
44936db0b34SBarry Smith           ierr = ViewerASCIIPrintf(viewer," %d %g - %g i",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4503a40ed3dSBarry Smith         } else {
45136db0b34SBarry Smith           ierr = ViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
45217ab2063SBarry Smith         }
45317ab2063SBarry Smith #else
4546831982aSBarry Smith         ierr = ViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
45517ab2063SBarry Smith #endif
45617ab2063SBarry Smith       }
4576831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45817ab2063SBarry Smith     }
4596831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
46017ab2063SBarry Smith   }
4616831982aSBarry Smith   ierr = ViewerFlush(viewer);CHKERRQ(ierr);
4623a40ed3dSBarry Smith   PetscFunctionReturn(0);
463416022c9SBarry Smith }
464416022c9SBarry Smith 
4655615d1e5SSatish Balay #undef __FUNC__
466ca44d042SBarry Smith #define __FUNC__ /*<a name="MatView_SeqAIJ_Draw_Zoom"></a>*/"MatView_SeqAIJ_Draw_Zoom"
467480ef9eaSBarry Smith int MatView_SeqAIJ_Draw_Zoom(Draw 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;
4720513a670SBarry Smith   int         format;
47336db0b34SBarry Smith   PetscReal   xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
474480ef9eaSBarry Smith   Viewer      viewer;
475cddf8d76SBarry Smith 
4763a40ed3dSBarry Smith   PetscFunctionBegin;
477480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
4780513a670SBarry Smith   ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr);
47919bcc07fSBarry Smith 
480480ef9eaSBarry Smith   ierr = DrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
481416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4820513a670SBarry Smith 
4830513a670SBarry Smith   if (format != VIEWER_FORMAT_DRAW_CONTOUR) {
4840513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
485cddf8d76SBarry Smith     color = 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
495480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
496cddf8d76SBarry Smith       }
497cddf8d76SBarry Smith     }
498cddf8d76SBarry Smith     color = 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;
504480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
505cddf8d76SBarry Smith       }
506cddf8d76SBarry Smith     }
507cddf8d76SBarry Smith     color = 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
517480ef9eaSBarry Smith         ierr = DrawRectangle(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;
5240513a670SBarry Smith     Draw   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     }
530480ef9eaSBarry Smith     scale = (245.0 - DRAW_BASIC_COLORS)/maxv;
531522c5e43SBarry Smith     ierr  = DrawGetPopup(draw,&popup);CHKERRQ(ierr);
532f1e2ffcdSBarry Smith     if (popup) {ierr  = DrawScalePopup(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;
5386d6b6c30SSatish Balay         color = DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count]));
539480ef9eaSBarry Smith         ierr  = DrawRectangle(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 
547480ef9eaSBarry Smith #undef __FUNC__
548ca44d042SBarry Smith #define __FUNC__ /*<a name="MatView_SeqAIJ_Draw"></a>*/"MatView_SeqAIJ_Draw"
549480ef9eaSBarry Smith int MatView_SeqAIJ_Draw(Mat A,Viewer viewer)
550480ef9eaSBarry Smith {
551480ef9eaSBarry Smith   int        ierr;
552480ef9eaSBarry Smith   Draw       draw;
55336db0b34SBarry Smith   PetscReal  xr,yr,xl,yl,h,w;
554480ef9eaSBarry Smith   PetscTruth isnull;
555480ef9eaSBarry Smith 
556480ef9eaSBarry Smith   PetscFunctionBegin;
55777ed5343SBarry Smith   ierr = ViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
558480ef9eaSBarry Smith   ierr = DrawIsNull(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;
564cddf8d76SBarry Smith   ierr = DrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
565480ef9eaSBarry Smith   ierr = DrawZoom(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 
5705615d1e5SSatish Balay #undef __FUNC__
571ca44d042SBarry Smith #define __FUNC__ /*<a name="MatView_SeqAIJ"></a>*/"MatView_SeqAIJ"
572e1311b90SBarry Smith int MatView_SeqAIJ(Mat A,Viewer 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;
5796831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,SOCKET_VIEWER,&issocket);CHKERRQ(ierr);
5806831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr);
5816831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,BINARY_VIEWER,&isbinary);CHKERRQ(ierr);
5826831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,DRAW_VIEWER,&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     }
587273d9f13SBarry Smith     ierr = ViewerSocketPutSparse_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 
600ca44d042SBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat);
6015615d1e5SSatish Balay #undef __FUNC__
602ca44d042SBarry Smith #define __FUNC__ /*<a name="MatAssemblyEnd_SeqAIJ"></a>*/"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;
608416022c9SBarry Smith   Scalar     *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);
642416022c9SBarry Smith     PLogObjectMemory(A,-(m+1)*sizeof(int));
643416022c9SBarry Smith     a->diag = 0;
64417ab2063SBarry Smith   }
645273d9f13SBarry Smith   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded,%d used\n",m,A->n,fshift,a->nz);
646c38d4ed2SBarry Smith   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %d\n",a->reallocs);
64794a9d846SBarry Smith   PLogInfo(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 */
65341c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(A);CHKERRQ(ierr);
6543a40ed3dSBarry Smith   PetscFunctionReturn(0);
65517ab2063SBarry Smith }
65617ab2063SBarry Smith 
6575615d1e5SSatish Balay #undef __FUNC__
658ca44d042SBarry Smith #define __FUNC__ /*<a name="atZeroEntries_SeqAIJ"></a>*/"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;
665273d9f13SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->m]+a->indexshift)*sizeof(Scalar));CHKERRQ(ierr);
6663a40ed3dSBarry Smith   PetscFunctionReturn(0);
66717ab2063SBarry Smith }
668416022c9SBarry Smith 
6695615d1e5SSatish Balay #undef __FUNC__
670ca44d042SBarry Smith #define __FUNC__ /*<a name="MatDestroy_SeqAIJ"></a>*/"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)
678273d9f13SBarry Smith   PLogObjectState((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);}
701606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
7023a40ed3dSBarry Smith   PetscFunctionReturn(0);
70317ab2063SBarry Smith }
70417ab2063SBarry Smith 
7055615d1e5SSatish Balay #undef __FUNC__
706ca44d042SBarry Smith #define __FUNC__ /*<a name="MatCompress_SeqAIJ"></a>*/"MatCompress_SeqAIJ"
7078f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
70817ab2063SBarry Smith {
7093a40ed3dSBarry Smith   PetscFunctionBegin;
7103a40ed3dSBarry Smith   PetscFunctionReturn(0);
71117ab2063SBarry Smith }
71217ab2063SBarry Smith 
7135615d1e5SSatish Balay #undef __FUNC__
714ca44d042SBarry Smith #define __FUNC__ /*<a name="MatSetOption_SeqAIJ"></a>*/"MatSetOption_SeqAIJ"
7158f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
71617ab2063SBarry Smith {
717416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7183a40ed3dSBarry Smith 
7193a40ed3dSBarry Smith   PetscFunctionBegin;
720f1e2ffcdSBarry Smith   if      (op == MAT_ROW_ORIENTED)                 a->roworiented       = PETSC_TRUE;
721f1e2ffcdSBarry Smith   else if (op == MAT_KEEP_ZEROED_ROWS)             a->keepzeroedrows    = PETSC_TRUE;
722f1e2ffcdSBarry Smith   else if (op == MAT_COLUMN_ORIENTED)              a->roworiented       = PETSC_FALSE;
723f1e2ffcdSBarry Smith   else if (op == MAT_COLUMNS_SORTED)               a->sorted            = PETSC_TRUE;
724f1e2ffcdSBarry Smith   else if (op == MAT_COLUMNS_UNSORTED)             a->sorted            = PETSC_FALSE;
7256d4a8577SBarry Smith   else if (op == MAT_NO_NEW_NONZERO_LOCATIONS)     a->nonew             = 1;
7264787f768SSatish Balay   else if (op == MAT_NEW_NONZERO_LOCATION_ERR)     a->nonew             = -1;
7274787f768SSatish Balay   else if (op == MAT_NEW_NONZERO_ALLOCATION_ERR)   a->nonew             = -2;
7286d4a8577SBarry Smith   else if (op == MAT_YES_NEW_NONZERO_LOCATIONS)    a->nonew             = 0;
72936db0b34SBarry Smith   else if (op == MAT_IGNORE_ZERO_ENTRIES)          a->ignorezeroentries = PETSC_TRUE;
730b65db4caSBarry Smith   else if (op == MAT_USE_INODES)                   a->inode.use         = PETSC_TRUE;
731b65db4caSBarry Smith   else if (op == MAT_DO_NOT_USE_INODES)            a->inode.use         = PETSC_FALSE;
7326d4a8577SBarry Smith   else if (op == MAT_ROWS_SORTED ||
733219d9a1aSLois Curfman McInnes            op == MAT_ROWS_UNSORTED ||
7346d4a8577SBarry Smith            op == MAT_SYMMETRIC ||
7356d4a8577SBarry Smith            op == MAT_STRUCTURALLY_SYMMETRIC ||
73690f02eecSBarry Smith            op == MAT_YES_NEW_DIAGONALS ||
737b51ba29fSSatish Balay            op == MAT_IGNORE_OFF_PROC_ENTRIES||
738b51ba29fSSatish Balay            op == MAT_USE_HASH_TABLE)
739981c4779SBarry Smith     PLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
7403a40ed3dSBarry Smith   else if (op == MAT_NO_NEW_DIAGONALS) {
74129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
7423a40ed3dSBarry Smith   } else if (op == MAT_INODE_LIMIT_1)          a->inode.limit  = 1;
7436d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_2)            a->inode.limit  = 2;
7446d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_3)            a->inode.limit  = 3;
7456d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_4)            a->inode.limit  = 4;
7466d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_5)            a->inode.limit  = 5;
74729bbc08cSBarry Smith   else SETERRQ(PETSC_ERR_SUP,"unknown option");
7483a40ed3dSBarry Smith   PetscFunctionReturn(0);
74917ab2063SBarry Smith }
75017ab2063SBarry Smith 
7515615d1e5SSatish Balay #undef __FUNC__
752ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetDiagonal_SeqAIJ"></a>*/"MatGetDiagonal_SeqAIJ"
7538f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
75417ab2063SBarry Smith {
755416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7563a40ed3dSBarry Smith   int        i,j,n,shift = a->indexshift,ierr;
75717ab2063SBarry Smith   Scalar     *x,zero = 0.0;
75817ab2063SBarry Smith 
7593a40ed3dSBarry Smith   PetscFunctionBegin;
7603a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
76136db0b34SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
76236db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
763273d9f13SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
764273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
765416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
766416022c9SBarry Smith       if (a->j[j]+shift == i) {
767416022c9SBarry Smith         x[i] = a->a[j];
76817ab2063SBarry Smith         break;
76917ab2063SBarry Smith       }
77017ab2063SBarry Smith     }
77117ab2063SBarry Smith   }
77236db0b34SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
7733a40ed3dSBarry Smith   PetscFunctionReturn(0);
77417ab2063SBarry Smith }
77517ab2063SBarry Smith 
7765615d1e5SSatish Balay #undef __FUNC__
777ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMultTranspose_SeqAIJ"></a>*/"MatMultTranspose_SeqAIJ"
7787c922b88SBarry Smith int MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
77917ab2063SBarry Smith {
780416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
781f1af5d2fSBarry Smith   Scalar     *x,*y,*v,alpha,zero = 0.0;
782273d9f13SBarry Smith   int        ierr,m = A->m,n,i,*idx,shift = a->indexshift;
78317ab2063SBarry Smith 
7843a40ed3dSBarry Smith   PetscFunctionBegin;
785f1af5d2fSBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
786e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
787e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
78817ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
78917ab2063SBarry Smith   for (i=0; i<m; i++) {
790416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
791416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
792416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
79317ab2063SBarry Smith     alpha = x[i];
79417ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
79517ab2063SBarry Smith   }
796273d9f13SBarry Smith   PLogFlops(2*a->nz - A->n);
797e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
798e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
7993a40ed3dSBarry Smith   PetscFunctionReturn(0);
80017ab2063SBarry Smith }
801d5d45c9bSBarry Smith 
8025615d1e5SSatish Balay #undef __FUNC__
803ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMultTransposeAdd_SeqAIJ"></a>*/"MatMultTransposeAdd_SeqAIJ"
8047c922b88SBarry Smith int MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
80517ab2063SBarry Smith {
806416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
80717ab2063SBarry Smith   Scalar     *x,*y,*v,alpha;
808273d9f13SBarry Smith   int        ierr,m = A->m,n,i,*idx,shift = a->indexshift;
80917ab2063SBarry Smith 
8103a40ed3dSBarry Smith   PetscFunctionBegin;
8112e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
812e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
813e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
81417ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
81517ab2063SBarry Smith   for (i=0; i<m; i++) {
816416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
817416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
818416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
81917ab2063SBarry Smith     alpha = x[i];
82017ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
82117ab2063SBarry Smith   }
82290f02eecSBarry Smith   PLogFlops(2*a->nz);
823e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
824e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8253a40ed3dSBarry Smith   PetscFunctionReturn(0);
82617ab2063SBarry Smith }
82717ab2063SBarry Smith 
8285615d1e5SSatish Balay #undef __FUNC__
829ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMult_SeqAIJ"></a>*/"MatMult_SeqAIJ"
83044cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
83117ab2063SBarry Smith {
832416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
83317ab2063SBarry Smith   Scalar     *x,*y,*v,sum;
834273d9f13SBarry Smith   int        ierr,m = A->m,*idx,shift = a->indexshift,*ii;
835aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
836e36a17ebSSatish Balay   int        n,i,jrow,j;
837e36a17ebSSatish Balay #endif
83817ab2063SBarry Smith 
839aa482453SBarry Smith #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
840fee21e36SBarry Smith #pragma disjoint(*x,*y,*v)
841fee21e36SBarry Smith #endif
842fee21e36SBarry Smith 
8433a40ed3dSBarry Smith   PetscFunctionBegin;
844e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
845e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
84617ab2063SBarry Smith   x    = x + shift;    /* shift for Fortran start by 1 indexing */
847416022c9SBarry Smith   idx  = a->j;
848d64ed03dSBarry Smith   v    = a->a;
849416022c9SBarry Smith   ii   = a->i;
850aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
85129b5ca8aSSatish Balay   fortranmultaij_(&m,x,ii,idx+shift,v+shift,y);
8528d195f9aSBarry Smith #else
853d64ed03dSBarry Smith   v    += shift; /* shift for Fortran start by 1 indexing */
854d64ed03dSBarry Smith   idx  += shift;
85517ab2063SBarry Smith   for (i=0; i<m; i++) {
8569ea0dfa2SSatish Balay     jrow = ii[i];
8579ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
85817ab2063SBarry Smith     sum  = 0.0;
8599ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
8609ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8619ea0dfa2SSatish Balay      }
86217ab2063SBarry Smith     y[i] = sum;
86317ab2063SBarry Smith   }
8648d195f9aSBarry Smith #endif
865416022c9SBarry Smith   PLogFlops(2*a->nz - m);
866e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
867e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8683a40ed3dSBarry Smith   PetscFunctionReturn(0);
86917ab2063SBarry Smith }
87017ab2063SBarry Smith 
8715615d1e5SSatish Balay #undef __FUNC__
872ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMultAdd_SeqAIJ"></a>*/"MatMultAdd_SeqAIJ"
87344cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
87417ab2063SBarry Smith {
875416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
87617ab2063SBarry Smith   Scalar     *x,*y,*z,*v,sum;
877273d9f13SBarry Smith   int        ierr,m = A->m,*idx,shift = a->indexshift,*ii;
878aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
879e36a17ebSSatish Balay   int        n,i,jrow,j;
880e36a17ebSSatish Balay #endif
8819ea0dfa2SSatish Balay 
8823a40ed3dSBarry Smith   PetscFunctionBegin;
883e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
884e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8852e8a6d31SBarry Smith   if (zz != yy) {
886e1311b90SBarry Smith     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
8872e8a6d31SBarry Smith   } else {
8882e8a6d31SBarry Smith     z = y;
8892e8a6d31SBarry Smith   }
89017ab2063SBarry Smith   x    = x + shift; /* shift for Fortran start by 1 indexing */
891cddf8d76SBarry Smith   idx  = a->j;
892d64ed03dSBarry Smith   v    = a->a;
893cddf8d76SBarry Smith   ii   = a->i;
894aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
89529b5ca8aSSatish Balay   fortranmultaddaij_(&m,x,ii,idx+shift,v+shift,y,z);
89602ab625aSSatish Balay #else
897d64ed03dSBarry Smith   v   += shift; /* shift for Fortran start by 1 indexing */
898d64ed03dSBarry Smith   idx += shift;
89917ab2063SBarry Smith   for (i=0; i<m; i++) {
9009ea0dfa2SSatish Balay     jrow = ii[i];
9019ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
90217ab2063SBarry Smith     sum  = y[i];
9039ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
9049ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9059ea0dfa2SSatish Balay      }
90617ab2063SBarry Smith     z[i] = sum;
90717ab2063SBarry Smith   }
90802ab625aSSatish Balay #endif
909416022c9SBarry Smith   PLogFlops(2*a->nz);
910e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
911e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9122e8a6d31SBarry Smith   if (zz != yy) {
913e1311b90SBarry Smith     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
9142e8a6d31SBarry Smith   }
9153a40ed3dSBarry Smith   PetscFunctionReturn(0);
91617ab2063SBarry Smith }
91717ab2063SBarry Smith 
91817ab2063SBarry Smith /*
91917ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
92017ab2063SBarry Smith */
9215615d1e5SSatish Balay #undef __FUNC__
922ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMarkDiagonal_SeqAIJ"></a>*/"MatMarkDiagonal_SeqAIJ"
923f1e2ffcdSBarry Smith int MatMarkDiagonal_SeqAIJ(Mat A)
92417ab2063SBarry Smith {
925416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
926273d9f13SBarry Smith   int        i,j,*diag,m = A->m,shift = a->indexshift;
92717ab2063SBarry Smith 
9283a40ed3dSBarry Smith   PetscFunctionBegin;
929f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
930f1e2ffcdSBarry Smith 
9310452661fSBarry Smith   diag = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(diag);
932416022c9SBarry Smith   PLogObjectMemory(A,(m+1)*sizeof(int));
933273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
93435b0346bSBarry Smith     diag[i] = a->i[i+1];
935416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
936416022c9SBarry Smith       if (a->j[j]+shift == i) {
93717ab2063SBarry Smith         diag[i] = j - shift;
93817ab2063SBarry Smith         break;
93917ab2063SBarry Smith       }
94017ab2063SBarry Smith     }
94117ab2063SBarry Smith   }
942416022c9SBarry Smith   a->diag = diag;
9433a40ed3dSBarry Smith   PetscFunctionReturn(0);
94417ab2063SBarry Smith }
94517ab2063SBarry Smith 
946be5855fcSBarry Smith /*
947be5855fcSBarry Smith      Checks for missing diagonals
948be5855fcSBarry Smith */
949be5855fcSBarry Smith #undef __FUNC__
950ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMissingDiagonal_SeqAIJ"></a>*/"MatMissingDiagonal_SeqAIJ"
951f1e2ffcdSBarry Smith int MatMissingDiagonal_SeqAIJ(Mat A)
952be5855fcSBarry Smith {
953be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
954f1e2ffcdSBarry Smith   int        *diag,*jj = a->j,i,shift = a->indexshift,ierr;
955be5855fcSBarry Smith 
956be5855fcSBarry Smith   PetscFunctionBegin;
957f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
958f1e2ffcdSBarry Smith   diag = a->diag;
959273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
960be5855fcSBarry Smith     if (jj[diag[i]+shift] != i-shift) {
96129bbc08cSBarry Smith       SETERRQ1(1,"Matrix is missing diagonal number %d",i);
962be5855fcSBarry Smith     }
963be5855fcSBarry Smith   }
964be5855fcSBarry Smith   PetscFunctionReturn(0);
965be5855fcSBarry Smith }
966be5855fcSBarry Smith 
9675615d1e5SSatish Balay #undef __FUNC__
968ca44d042SBarry Smith #define __FUNC__ /*<a name="MatRelax_SeqAIJ"></a>*/"MatRelax_SeqAIJ"
96936db0b34SBarry Smith int MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,Vec xx)
97017ab2063SBarry Smith {
971416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
972d6ed3216SSatish Balay   Scalar     *x,*b,*bs, d,*xs,sum,*v = a->a,*t=0,scale,*ts,*xb,*idiag=0;
973273d9f13SBarry Smith   int        ierr,*idx,*diag,n = A->n,m = A->m,i,shift = a->indexshift;
97417ab2063SBarry Smith 
9753a40ed3dSBarry Smith   PetscFunctionBegin;
976e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
977fb2e594dSBarry Smith   if (xx != bb) {
978e1311b90SBarry Smith     ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
979fb2e594dSBarry Smith   } else {
980fb2e594dSBarry Smith     b = x;
981fb2e594dSBarry Smith   }
982fb2e594dSBarry Smith 
983f1e2ffcdSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
984416022c9SBarry Smith   diag = a->diag;
985416022c9SBarry Smith   xs   = x + shift; /* shifted by one for index start of a or a->j*/
98617ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
98717ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
98817ab2063SBarry Smith     bs = b + shift;
98917ab2063SBarry Smith     for (i=0; i<m; i++) {
990416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
991416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
992488ecbafSBarry Smith 	PLogFlops(2*n-1);
993416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
994416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
99517ab2063SBarry Smith         sum  = b[i]*d/omega;
99617ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
99717ab2063SBarry Smith         x[i] = sum;
99817ab2063SBarry Smith     }
999cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1000fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
10013a40ed3dSBarry Smith     PetscFunctionReturn(0);
100217ab2063SBarry Smith   }
1003c783ea89SBarry Smith 
1004c783ea89SBarry Smith   /* setup workspace for Eisenstat */
1005c783ea89SBarry Smith   if (flag & SOR_EISENSTAT) {
1006c783ea89SBarry Smith     if (!a->idiag) {
1007c783ea89SBarry Smith       a->idiag = (Scalar*)PetscMalloc(2*m*sizeof(Scalar));CHKPTRQ(a->idiag);
1008c783ea89SBarry Smith       a->ssor  = a->idiag + m;
1009c783ea89SBarry Smith       v        = a->a;
1010c783ea89SBarry Smith       for (i=0; i<m; i++) { a->idiag[i] = 1.0/v[diag[i]];}
1011c783ea89SBarry Smith     }
1012c783ea89SBarry Smith     t     = a->ssor;
1013c783ea89SBarry Smith     idiag = a->idiag;
1014c783ea89SBarry Smith   }
1015fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1016fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1017fc3d8934SBarry Smith 
1018fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1019fc3d8934SBarry Smith 
1020fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1021fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
102248af12d7SBarry Smith     is the relaxation factor.
1023fc3d8934SBarry Smith     */
1024fc3d8934SBarry Smith 
102548af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
102629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
102748af12d7SBarry Smith   } else if ((flag & SOR_EISENSTAT) && omega == 1.0 && shift == 0 && fshift == 0.0) {
102848af12d7SBarry Smith     /* special case for omega = 1.0 saves flops and some integer ops */
1029733d66baSBarry Smith     Scalar *v2;
103048af12d7SBarry Smith 
1031733d66baSBarry Smith     v2    = a->a;
1032fc3d8934SBarry Smith     /*  x = (E + U)^{-1} b */
1033fc3d8934SBarry Smith     for (i=m-1; i>=0; i--) {
1034fc3d8934SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1035fc3d8934SBarry Smith       idx  = a->j + diag[i] + 1;
1036fc3d8934SBarry Smith       v    = a->a + diag[i] + 1;
1037fc3d8934SBarry Smith       sum  = b[i];
1038fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1039b4632166SBarry Smith       x[i] = sum*idiag[i];
1040fc3d8934SBarry Smith 
1041fc3d8934SBarry Smith       /*  t = b - (2*E - D)x */
1042733d66baSBarry Smith       t[i] = b[i] - (v2[diag[i]])*x[i];
1043733d66baSBarry Smith     }
1044fc3d8934SBarry Smith 
1045fc3d8934SBarry Smith     /*  t = (E + L)^{-1}t */
1046fc3d8934SBarry Smith     diag = a->diag;
1047fc3d8934SBarry Smith     for (i=0; i<m; i++) {
1048fc3d8934SBarry Smith       n    = diag[i] - a->i[i];
1049fc3d8934SBarry Smith       idx  = a->j + a->i[i];
1050fc3d8934SBarry Smith       v    = a->a + a->i[i];
1051fc3d8934SBarry Smith       sum  = t[i];
1052fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,t,v,idx,n);
1053b4632166SBarry Smith       t[i]  = sum*idiag[i];
1054fc3d8934SBarry Smith 
1055fc3d8934SBarry Smith       /*  x = x + t */
1056733d66baSBarry Smith       x[i] += t[i];
1057733d66baSBarry Smith     }
1058733d66baSBarry Smith 
1059a4d9cbf6SBarry Smith     PLogFlops(3*m-1 + 2*a->nz);
1060fc3d8934SBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1061fc3d8934SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
1062fc3d8934SBarry Smith     PetscFunctionReturn(0);
10633a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
106417ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
106517ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
106617ab2063SBarry Smith 
106717ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
106817ab2063SBarry Smith 
106917ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
107017ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
107117ab2063SBarry Smith     is the relaxation factor.
107217ab2063SBarry Smith     */
107317ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
107417ab2063SBarry Smith 
107517ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
107617ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1077416022c9SBarry Smith       d    = fshift + a->a[diag[i] + shift];
1078416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1079416022c9SBarry Smith       idx  = a->j + diag[i] + (!shift);
1080416022c9SBarry Smith       v    = a->a + diag[i] + (!shift);
108117ab2063SBarry Smith       sum  = b[i];
108217ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
108317ab2063SBarry Smith       x[i] = omega*(sum/d);
108417ab2063SBarry Smith     }
108517ab2063SBarry Smith 
108617ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1087416022c9SBarry Smith     v = a->a;
108817ab2063SBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; }
108917ab2063SBarry Smith 
109017ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1091416022c9SBarry Smith     ts = t + shift; /* shifted by one for index start of a or a->j*/
1092416022c9SBarry Smith     diag = a->diag;
109317ab2063SBarry Smith     for (i=0; i<m; i++) {
1094416022c9SBarry Smith       d    = fshift + a->a[diag[i]+shift];
1095416022c9SBarry Smith       n    = diag[i] - a->i[i];
1096416022c9SBarry Smith       idx  = a->j + a->i[i] + shift;
1097416022c9SBarry Smith       v    = a->a + a->i[i] + shift;
109817ab2063SBarry Smith       sum  = t[i];
109917ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
110017ab2063SBarry Smith       t[i] = omega*(sum/d);
1101733d66baSBarry Smith       /*  x = x + t */
1102733d66baSBarry Smith       x[i] += t[i];
110317ab2063SBarry Smith     }
110417ab2063SBarry Smith 
1105c783ea89SBarry Smith     PLogFlops(6*m-1 + 2*a->nz);
1106cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1107fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
11083a40ed3dSBarry Smith     PetscFunctionReturn(0);
110917ab2063SBarry Smith   }
111017ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
111117ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
111217ab2063SBarry Smith       for (i=0; i<m; i++) {
1113416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1114416022c9SBarry Smith         n    = diag[i] - a->i[i];
1115488ecbafSBarry Smith 	PLogFlops(2*n-1);
1116416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1117416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
111817ab2063SBarry Smith         sum  = b[i];
111917ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
112017ab2063SBarry Smith         x[i] = omega*(sum/d);
112117ab2063SBarry Smith       }
112217ab2063SBarry Smith       xb = x;
11233a40ed3dSBarry Smith     } else xb = b;
112417ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
112517ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
112617ab2063SBarry Smith       for (i=0; i<m; i++) {
1127416022c9SBarry Smith         x[i] *= a->a[diag[i]+shift];
112817ab2063SBarry Smith       }
1129488ecbafSBarry Smith       PLogFlops(m);
113017ab2063SBarry Smith     }
113117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
113217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1133416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1134416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1135488ecbafSBarry Smith 	PLogFlops(2*n-1);
1136416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1137416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
113817ab2063SBarry Smith         sum  = xb[i];
113917ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
114017ab2063SBarry Smith         x[i] = omega*(sum/d);
114117ab2063SBarry Smith       }
114217ab2063SBarry Smith     }
114317ab2063SBarry Smith     its--;
114417ab2063SBarry Smith   }
114517ab2063SBarry Smith   while (its--) {
114617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
114717ab2063SBarry Smith       for (i=0; i<m; i++) {
1148416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1149416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1150488ecbafSBarry Smith 	PLogFlops(2*n-1);
1151416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1152416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
115317ab2063SBarry Smith         sum  = b[i];
115417ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
11557e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
115617ab2063SBarry Smith       }
115717ab2063SBarry Smith     }
115817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
115917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1160416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1161416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1162488ecbafSBarry Smith 	PLogFlops(2*n-1);
1163416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1164416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
116517ab2063SBarry Smith         sum  = b[i];
116617ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
11677e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
116817ab2063SBarry Smith       }
116917ab2063SBarry Smith     }
117017ab2063SBarry Smith   }
1171cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1172fb2e594dSBarry Smith   if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
11733a40ed3dSBarry Smith   PetscFunctionReturn(0);
117417ab2063SBarry Smith }
117517ab2063SBarry Smith 
11765615d1e5SSatish Balay #undef __FUNC__
1177ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetInfo_SeqAIJ"></a>*/"MatGetInfo_SeqAIJ"
11788f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
117917ab2063SBarry Smith {
1180416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
11814e220ebcSLois Curfman McInnes 
11823a40ed3dSBarry Smith   PetscFunctionBegin;
1183273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1184273d9f13SBarry Smith   info->columns_global = (double)A->n;
1185273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1186273d9f13SBarry Smith   info->columns_local  = (double)A->n;
11874e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
11884e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
11894e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
11904e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
11914e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
11924e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
11934e220ebcSLois Curfman McInnes   info->memory         = A->mem;
11944e220ebcSLois Curfman McInnes   if (A->factor) {
11954e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
11964e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
11974e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
11984e220ebcSLois Curfman McInnes   } else {
11994e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12004e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12014e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12024e220ebcSLois Curfman McInnes   }
12033a40ed3dSBarry Smith   PetscFunctionReturn(0);
120417ab2063SBarry Smith }
120517ab2063SBarry Smith 
1206b9b97703SBarry Smith EXTERN int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,MatLUInfo*,Mat*);
1207ca44d042SBarry Smith EXTERN int MatLUFactorNumeric_SeqAIJ(Mat,Mat*);
1208b9b97703SBarry Smith EXTERN int MatLUFactor_SeqAIJ(Mat,IS,IS,MatLUInfo*);
1209ca44d042SBarry Smith EXTERN int MatSolve_SeqAIJ(Mat,Vec,Vec);
1210ca44d042SBarry Smith EXTERN int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
1211ca44d042SBarry Smith EXTERN int MatSolveTranspose_SeqAIJ(Mat,Vec,Vec);
1212ca44d042SBarry Smith EXTERN int MatSolveTransposeAdd_SeqAIJ(Mat,Vec,Vec,Vec);
121317ab2063SBarry Smith 
12145615d1e5SSatish Balay #undef __FUNC__
1215e7592fafSBarry Smith #define __FUNC__ /*<a name="MatZeroRows_SeqAIJ"></a>*/"MatZeroRows_SeqAIJ"
12168f6be9afSLois Curfman McInnes int MatZeroRows_SeqAIJ(Mat A,IS is,Scalar *diag)
121717ab2063SBarry Smith {
1218416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1219273d9f13SBarry Smith   int         i,ierr,N,*rows,m = A->m - 1,shift = a->indexshift;
122017ab2063SBarry Smith 
12213a40ed3dSBarry Smith   PetscFunctionBegin;
1222b9b97703SBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
122317ab2063SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
1224f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1225f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
122629bbc08cSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
1227f1e2ffcdSBarry Smith       ierr = PetscMemzero(&a->a[a->i[rows[i]]+shift],a->ilen[rows[i]]*sizeof(Scalar));CHKERRQ(ierr);
1228f1e2ffcdSBarry Smith     }
1229f1e2ffcdSBarry Smith     if (diag) {
1230f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1231f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1232f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1233f1e2ffcdSBarry Smith         a->a[a->diag[rows[i]]] = *diag;
1234f1e2ffcdSBarry Smith       }
1235f1e2ffcdSBarry Smith     }
1236f1e2ffcdSBarry Smith   } else {
123717ab2063SBarry Smith     if (diag) {
123817ab2063SBarry Smith       for (i=0; i<N; i++) {
123929bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
12407ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1241416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1242416022c9SBarry Smith           a->a[a->i[rows[i]]+shift] = *diag;
1243416022c9SBarry Smith           a->j[a->i[rows[i]]+shift] = rows[i]+shift;
12447ae801bdSBarry Smith         } else { /* in case row was completely empty */
1245d64ed03dSBarry Smith           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
124617ab2063SBarry Smith         }
124717ab2063SBarry Smith       }
12483a40ed3dSBarry Smith     } else {
124917ab2063SBarry Smith       for (i=0; i<N; i++) {
125029bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
1251416022c9SBarry Smith         a->ilen[rows[i]] = 0;
125217ab2063SBarry Smith       }
125317ab2063SBarry Smith     }
1254f1e2ffcdSBarry Smith   }
12557ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
125643a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12573a40ed3dSBarry Smith   PetscFunctionReturn(0);
125817ab2063SBarry Smith }
125917ab2063SBarry Smith 
12605615d1e5SSatish Balay #undef __FUNC__
1261ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetOwnershipRange_SeqAIJ"></a>*/"MatGetOwnershipRange_SeqAIJ"
12628f6be9afSLois Curfman McInnes int MatGetOwnershipRange_SeqAIJ(Mat A,int *m,int *n)
126317ab2063SBarry Smith {
12643a40ed3dSBarry Smith   PetscFunctionBegin;
12654c49b128SBarry Smith   if (m) *m = 0;
1266273d9f13SBarry Smith   if (n) *n = A->m;
12673a40ed3dSBarry Smith   PetscFunctionReturn(0);
126817ab2063SBarry Smith }
1269026e39d0SSatish Balay 
12705615d1e5SSatish Balay #undef __FUNC__
1271ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetRow_SeqAIJ"></a>*/"MatGetRow_SeqAIJ"
12724e093b46SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
127317ab2063SBarry Smith {
1274416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1275c456f294SBarry Smith   int        *itmp,i,shift = a->indexshift;
127617ab2063SBarry Smith 
12773a40ed3dSBarry Smith   PetscFunctionBegin;
1278273d9f13SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %d out of range",row);
127917ab2063SBarry Smith 
1280416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1281416022c9SBarry Smith   if (v) *v = a->a + a->i[row] + shift;
128217ab2063SBarry Smith   if (idx) {
1283416022c9SBarry Smith     itmp = a->j + a->i[row] + shift;
12844e093b46SBarry Smith     if (*nz && shift) {
12850452661fSBarry Smith       *idx = (int*)PetscMalloc((*nz)*sizeof(int));CHKPTRQ(*idx);
128617ab2063SBarry Smith       for (i=0; i<(*nz); i++) {(*idx)[i] = itmp[i] + shift;}
12874e093b46SBarry Smith     } else if (*nz) {
12884e093b46SBarry Smith       *idx = itmp;
128917ab2063SBarry Smith     }
129017ab2063SBarry Smith     else *idx = 0;
129117ab2063SBarry Smith   }
12923a40ed3dSBarry Smith   PetscFunctionReturn(0);
129317ab2063SBarry Smith }
129417ab2063SBarry Smith 
12955615d1e5SSatish Balay #undef __FUNC__
1296ca44d042SBarry Smith #define __FUNC__ /*<a name="MatRestoreRow_SeqAIJ"></a>*/"MatRestoreRow_SeqAIJ"
12974e093b46SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
129817ab2063SBarry Smith {
12994e093b46SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1300606d414cSSatish Balay   int ierr;
13013a40ed3dSBarry Smith 
13023a40ed3dSBarry Smith   PetscFunctionBegin;
1303606d414cSSatish Balay   if (idx) {if (*idx && a->indexshift) {ierr = PetscFree(*idx);CHKERRQ(ierr);}}
13043a40ed3dSBarry Smith   PetscFunctionReturn(0);
130517ab2063SBarry Smith }
130617ab2063SBarry Smith 
13075615d1e5SSatish Balay #undef __FUNC__
1308ca44d042SBarry Smith #define __FUNC__ /*<a name="MatNorm_SeqAIJ"></a>*/"MatNorm_SeqAIJ"
130936db0b34SBarry Smith int MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *norm)
131017ab2063SBarry Smith {
1311416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1312416022c9SBarry Smith   Scalar     *v = a->a;
131336db0b34SBarry Smith   PetscReal  sum = 0.0;
1314549d3d68SSatish Balay   int        i,j,shift = a->indexshift,ierr;
131517ab2063SBarry Smith 
13163a40ed3dSBarry Smith   PetscFunctionBegin;
131717ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1318416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1319aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
132036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
132117ab2063SBarry Smith #else
132217ab2063SBarry Smith       sum += (*v)*(*v); v++;
132317ab2063SBarry Smith #endif
132417ab2063SBarry Smith     }
132517ab2063SBarry Smith     *norm = sqrt(sum);
13263a40ed3dSBarry Smith   } else if (type == NORM_1) {
132736db0b34SBarry Smith     PetscReal *tmp;
1328416022c9SBarry Smith     int    *jj = a->j;
1329273d9f13SBarry Smith     tmp = (PetscReal*)PetscMalloc((A->n+1)*sizeof(PetscReal));CHKPTRQ(tmp);
1330273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
133117ab2063SBarry Smith     *norm = 0.0;
1332416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1333a2744918SBarry Smith         tmp[*jj++ + shift] += PetscAbsScalar(*v);  v++;
133417ab2063SBarry Smith     }
1335273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
133617ab2063SBarry Smith       if (tmp[j] > *norm) *norm = tmp[j];
133717ab2063SBarry Smith     }
1338606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13393a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
134017ab2063SBarry Smith     *norm = 0.0;
1341273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1342416022c9SBarry Smith       v = a->a + a->i[j] + shift;
134317ab2063SBarry Smith       sum = 0.0;
1344416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1345cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
134617ab2063SBarry Smith       }
134717ab2063SBarry Smith       if (sum > *norm) *norm = sum;
134817ab2063SBarry Smith     }
13493a40ed3dSBarry Smith   } else {
135029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
135117ab2063SBarry Smith   }
13523a40ed3dSBarry Smith   PetscFunctionReturn(0);
135317ab2063SBarry Smith }
135417ab2063SBarry Smith 
13555615d1e5SSatish Balay #undef __FUNC__
1356ca44d042SBarry Smith #define __FUNC__ /*<a name="MatTranspose_SeqAIJ"></a>*/"MatTranspose_SeqAIJ"
13578f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
135817ab2063SBarry Smith {
1359416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1360416022c9SBarry Smith   Mat        C;
1361273d9f13SBarry Smith   int        i,ierr,*aj = a->j,*ai = a->i,m = A->m,len,*col;
1362273d9f13SBarry Smith   int        shift = a->indexshift;
1363d5d45c9bSBarry Smith   Scalar     *array = a->a;
136417ab2063SBarry Smith 
13653a40ed3dSBarry Smith   PetscFunctionBegin;
1366273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1367273d9f13SBarry Smith   col  = (int*)PetscMalloc((1+A->n)*sizeof(int));CHKPTRQ(col);
1368273d9f13SBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(int));CHKERRQ(ierr);
136917ab2063SBarry Smith   if (shift) {
137017ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] -= 1;
137117ab2063SBarry Smith   }
137217ab2063SBarry Smith   for (i=0; i<ai[m]+shift; i++) col[aj[i]] += 1;
1373273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,A->n,m,0,col,&C);CHKERRQ(ierr);
1374606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
137517ab2063SBarry Smith   for (i=0; i<m; i++) {
137617ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1377416022c9SBarry Smith     ierr   = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1378b9b97703SBarry Smith     array += len;
1379b9b97703SBarry Smith     aj    += len;
138017ab2063SBarry Smith   }
138117ab2063SBarry Smith   if (shift) {
138217ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] += 1;
138317ab2063SBarry Smith   }
138417ab2063SBarry Smith 
13856d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13866d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
138717ab2063SBarry Smith 
1388f1e2ffcdSBarry Smith   if (B) {
1389416022c9SBarry Smith     *B = C;
139017ab2063SBarry Smith   } else {
1391273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
139217ab2063SBarry Smith   }
13933a40ed3dSBarry Smith   PetscFunctionReturn(0);
139417ab2063SBarry Smith }
139517ab2063SBarry Smith 
13965615d1e5SSatish Balay #undef __FUNC__
1397ca44d042SBarry Smith #define __FUNC__ /*<a name="MatDiagonalScale_SeqAIJ"></a>*/"MatDiagonalScale_SeqAIJ"
13988f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
139917ab2063SBarry Smith {
1400416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
140117ab2063SBarry Smith   Scalar     *l,*r,x,*v;
1402273d9f13SBarry Smith   int        ierr,i,j,m = A->m,n = A->n,M,nz = a->nz,*jj,shift = a->indexshift;
140317ab2063SBarry Smith 
14043a40ed3dSBarry Smith   PetscFunctionBegin;
140517ab2063SBarry Smith   if (ll) {
14063ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14073ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1408e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1409273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
1410e1311b90SBarry Smith     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1411416022c9SBarry Smith     v = a->a;
141217ab2063SBarry Smith     for (i=0; i<m; i++) {
141317ab2063SBarry Smith       x = l[i];
1414416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
141517ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
141617ab2063SBarry Smith     }
1417e1311b90SBarry Smith     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
141844cd7ae7SLois Curfman McInnes     PLogFlops(nz);
141917ab2063SBarry Smith   }
142017ab2063SBarry Smith   if (rr) {
1421e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1422273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
1423e1311b90SBarry Smith     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1424416022c9SBarry Smith     v = a->a; jj = a->j;
142517ab2063SBarry Smith     for (i=0; i<nz; i++) {
142617ab2063SBarry Smith       (*v++) *= r[*jj++ + shift];
142717ab2063SBarry Smith     }
1428e1311b90SBarry Smith     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
142944cd7ae7SLois Curfman McInnes     PLogFlops(nz);
143017ab2063SBarry Smith   }
14313a40ed3dSBarry Smith   PetscFunctionReturn(0);
143217ab2063SBarry Smith }
143317ab2063SBarry Smith 
14345615d1e5SSatish Balay #undef __FUNC__
1435ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetSubMatrix_SeqAIJ"></a>*/"MatGetSubMatrix_SeqAIJ"
14367b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B)
143717ab2063SBarry Smith {
1438db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data,*c;
1439273d9f13SBarry Smith   int          *smap,i,k,kstart,kend,ierr,oldcols = A->n,*lens;
144036db0b34SBarry Smith   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
144199141d43SSatish Balay   int          *irow,*icol,nrows,ncols,shift = a->indexshift,*ssmap;
144299141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
144399141d43SSatish Balay   Scalar       *a_new,*mat_a;
1444416022c9SBarry Smith   Mat          C;
1445fee21e36SBarry Smith   PetscTruth   stride;
144617ab2063SBarry Smith 
14473a40ed3dSBarry Smith   PetscFunctionBegin;
1448d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
144929bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1450d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
145129bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
145299141d43SSatish Balay 
145317ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1454b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1455b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
145617ab2063SBarry Smith 
1457fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1458fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1459fee21e36SBarry Smith   if (stride && step == 1) {
146002834360SBarry Smith     /* special case of contiguous rows */
146157faeb66SBarry Smith     lens   = (int*)PetscMalloc((ncols+nrows+1)*sizeof(int));CHKPTRQ(lens);
146202834360SBarry Smith     starts = lens + ncols;
146302834360SBarry Smith     /* loop over new rows determining lens and starting points */
146402834360SBarry Smith     for (i=0; i<nrows; i++) {
1465a2744918SBarry Smith       kstart  = ai[irow[i]]+shift;
1466a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
146702834360SBarry Smith       for (k=kstart; k<kend; k++) {
1468d8ced48eSBarry Smith         if (aj[k]+shift >= first) {
146902834360SBarry Smith           starts[i] = k;
147002834360SBarry Smith           break;
147102834360SBarry Smith 	}
147202834360SBarry Smith       }
1473a2744918SBarry Smith       sum = 0;
147402834360SBarry Smith       while (k < kend) {
1475d8ced48eSBarry Smith         if (aj[k++]+shift >= first+ncols) break;
1476a2744918SBarry Smith         sum++;
147702834360SBarry Smith       }
1478a2744918SBarry Smith       lens[i] = sum;
147902834360SBarry Smith     }
148002834360SBarry Smith     /* create submatrix */
1481cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
148208480c60SBarry Smith       int n_cols,n_rows;
148308480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
148429bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1485d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
148608480c60SBarry Smith       C = *B;
14873a40ed3dSBarry Smith     } else {
148802834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
148908480c60SBarry Smith     }
1490db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1491db02288aSLois Curfman McInnes 
149202834360SBarry Smith     /* loop over rows inserting into submatrix */
1493db02288aSLois Curfman McInnes     a_new    = c->a;
1494db02288aSLois Curfman McInnes     j_new    = c->j;
1495db02288aSLois Curfman McInnes     i_new    = c->i;
1496db02288aSLois Curfman McInnes     i_new[0] = -shift;
149702834360SBarry Smith     for (i=0; i<nrows; i++) {
1498a2744918SBarry Smith       ii    = starts[i];
1499a2744918SBarry Smith       lensi = lens[i];
1500a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1501a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
150202834360SBarry Smith       }
1503549d3d68SSatish Balay       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(Scalar));CHKERRQ(ierr);
1504a2744918SBarry Smith       a_new      += lensi;
1505a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1506a2744918SBarry Smith       c->ilen[i]  = lensi;
150702834360SBarry Smith     }
1508606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15093a40ed3dSBarry Smith   } else {
151002834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
15110452661fSBarry Smith     smap  = (int*)PetscMalloc((1+oldcols)*sizeof(int));CHKPTRQ(smap);
151202834360SBarry Smith     ssmap = smap + shift;
151399141d43SSatish Balay     lens  = (int*)PetscMalloc((1+nrows)*sizeof(int));CHKPTRQ(lens);
1514549d3d68SSatish Balay     ierr  = PetscMemzero(smap,oldcols*sizeof(int));CHKERRQ(ierr);
151517ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
151602834360SBarry Smith     /* determine lens of each row */
151702834360SBarry Smith     for (i=0; i<nrows; i++) {
1518d8ced48eSBarry Smith       kstart  = ai[irow[i]]+shift;
151902834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
152002834360SBarry Smith       lens[i] = 0;
152102834360SBarry Smith       for (k=kstart; k<kend; k++) {
1522d8ced48eSBarry Smith         if (ssmap[aj[k]]) {
152302834360SBarry Smith           lens[i]++;
152402834360SBarry Smith         }
152502834360SBarry Smith       }
152602834360SBarry Smith     }
152717ab2063SBarry Smith     /* Create and fill new matrix */
1528a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
15290f5bd95cSBarry Smith       PetscTruth equal;
15300f5bd95cSBarry Smith 
153199141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1532273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1533273d9f13SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(int),&equal);CHKERRQ(ierr);
15340f5bd95cSBarry Smith       if (!equal) {
153529bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
153699141d43SSatish Balay       }
1537273d9f13SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(int));CHKERRQ(ierr);
153808480c60SBarry Smith       C = *B;
15393a40ed3dSBarry Smith     } else {
154002834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
154108480c60SBarry Smith     }
154299141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
154317ab2063SBarry Smith     for (i=0; i<nrows; i++) {
154499141d43SSatish Balay       row    = irow[i];
154599141d43SSatish Balay       kstart = ai[row]+shift;
154699141d43SSatish Balay       kend   = kstart + a->ilen[row];
154799141d43SSatish Balay       mat_i  = c->i[i]+shift;
154899141d43SSatish Balay       mat_j  = c->j + mat_i;
154999141d43SSatish Balay       mat_a  = c->a + mat_i;
155099141d43SSatish Balay       mat_ilen = c->ilen + i;
155117ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
155299141d43SSatish Balay         if ((tcol=ssmap[a->j[k]])) {
155399141d43SSatish Balay           *mat_j++ = tcol - (!shift);
155499141d43SSatish Balay           *mat_a++ = a->a[k];
155599141d43SSatish Balay           (*mat_ilen)++;
155699141d43SSatish Balay 
155717ab2063SBarry Smith         }
155817ab2063SBarry Smith       }
155917ab2063SBarry Smith     }
156002834360SBarry Smith     /* Free work space */
156102834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1562606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1563606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
156402834360SBarry Smith   }
15656d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15666d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
156717ab2063SBarry Smith 
156817ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1569416022c9SBarry Smith   *B = C;
15703a40ed3dSBarry Smith   PetscFunctionReturn(0);
157117ab2063SBarry Smith }
157217ab2063SBarry Smith 
1573a871dcd8SBarry Smith /*
1574a871dcd8SBarry Smith */
15755615d1e5SSatish Balay #undef __FUNC__
1576ca44d042SBarry Smith #define __FUNC__ /*<a name="MatILUFactor_SeqAIJ"></a>*/"MatILUFactor_SeqAIJ"
15775ef9f2a5SBarry Smith int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatILUInfo *info)
1578a871dcd8SBarry Smith {
157963b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
158008480c60SBarry Smith   int        ierr;
158163b91edcSBarry Smith   Mat        outA;
1582b8a78c4aSBarry Smith   PetscTruth row_identity,col_identity;
158363b91edcSBarry Smith 
15843a40ed3dSBarry Smith   PetscFunctionBegin;
158529bbc08cSBarry Smith   if (info && info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1586b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1587b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1588b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
158929bbc08cSBarry Smith     SETERRQ(1,"Row and column permutations must be identity for in-place ILU");
1590b8a78c4aSBarry Smith   }
1591a871dcd8SBarry Smith 
159263b91edcSBarry Smith   outA          = inA;
159363b91edcSBarry Smith   inA->factor   = FACTOR_LU;
159463b91edcSBarry Smith   a->row        = row;
159563b91edcSBarry Smith   a->col        = col;
1596c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1597c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
159863b91edcSBarry Smith 
159936db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1600b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
16014c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
16021e4dd532SSatish Balay   PLogObjectParent(inA,a->icol);
1603f0ec6fceSSatish Balay 
160494a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1605273d9f13SBarry Smith     a->solve_work = (Scalar*)PetscMalloc((inA->m+1)*sizeof(Scalar));CHKPTRQ(a->solve_work);
160694a9d846SBarry Smith   }
160763b91edcSBarry Smith 
160808480c60SBarry Smith   if (!a->diag) {
1609f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
161063b91edcSBarry Smith   }
161163b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA);CHKERRQ(ierr);
16123a40ed3dSBarry Smith   PetscFunctionReturn(0);
1613a871dcd8SBarry Smith }
1614a871dcd8SBarry Smith 
1615d9eff348SSatish Balay #include "petscblaslapack.h"
16165615d1e5SSatish Balay #undef __FUNC__
1617ca44d042SBarry Smith #define __FUNC__ /*<a name="MatScale_SeqAIJ"></a>*/"MatScale_SeqAIJ"
16188f6be9afSLois Curfman McInnes int MatScale_SeqAIJ(Scalar *alpha,Mat inA)
1619f0b747eeSBarry Smith {
1620f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
1621f0b747eeSBarry Smith   int        one = 1;
16223a40ed3dSBarry Smith 
16233a40ed3dSBarry Smith   PetscFunctionBegin;
1624f0b747eeSBarry Smith   BLscal_(&a->nz,alpha,a->a,&one);
1625f0b747eeSBarry Smith   PLogFlops(a->nz);
16263a40ed3dSBarry Smith   PetscFunctionReturn(0);
1627f0b747eeSBarry Smith }
1628f0b747eeSBarry Smith 
16295615d1e5SSatish Balay #undef __FUNC__
1630ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetSubMatrices_SeqAIJ"></a>*/"MatGetSubMatrices_SeqAIJ"
16317b2a1423SBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n,IS *irow,IS *icol,MatReuse scall,Mat **B)
1632cddf8d76SBarry Smith {
1633cddf8d76SBarry Smith   int ierr,i;
1634cddf8d76SBarry Smith 
16353a40ed3dSBarry Smith   PetscFunctionBegin;
1636cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
16370452661fSBarry Smith     *B = (Mat*)PetscMalloc((n+1)*sizeof(Mat));CHKPTRQ(*B);
1638cddf8d76SBarry Smith   }
1639cddf8d76SBarry Smith 
1640cddf8d76SBarry Smith   for (i=0; i<n; i++) {
16416a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1642cddf8d76SBarry Smith   }
16433a40ed3dSBarry Smith   PetscFunctionReturn(0);
1644cddf8d76SBarry Smith }
1645cddf8d76SBarry Smith 
16465615d1e5SSatish Balay #undef __FUNC__
1647ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetBlockSize_SeqAIJ"></a>*/"MatGetBlockSize_SeqAIJ"
16488f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A,int *bs)
16495a838052SSatish Balay {
1650f830108cSBarry Smith   PetscFunctionBegin;
16515a838052SSatish Balay   *bs = 1;
16523a40ed3dSBarry Smith   PetscFunctionReturn(0);
16535a838052SSatish Balay }
16545a838052SSatish Balay 
16555615d1e5SSatish Balay #undef __FUNC__
1656ca44d042SBarry Smith #define __FUNC__ /*<a name="MatIncreaseOverlap_SeqAIJ"></a>*/"MatIncreaseOverlap_SeqAIJ"
16578f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A,int is_max,IS *is,int ov)
16584dcbc457SBarry Smith {
1659e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
166006763907SSatish Balay   int        shift,row,i,j,k,l,m,n,*idx,ierr,*nidx,isz,val;
16618a047759SSatish Balay   int        start,end,*ai,*aj;
1662f1af5d2fSBarry Smith   PetscBT    table;
1663bbd702dbSSatish Balay 
16643a40ed3dSBarry Smith   PetscFunctionBegin;
16658a047759SSatish Balay   shift = a->indexshift;
1666273d9f13SBarry Smith   m     = A->m;
1667e4d965acSSatish Balay   ai    = a->i;
16688a047759SSatish Balay   aj    = a->j+shift;
16698a047759SSatish Balay 
167029bbc08cSBarry Smith   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal overlap value used");
167106763907SSatish Balay 
167206763907SSatish Balay   nidx  = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(nidx);
16736831982aSBarry Smith   ierr  = PetscBTCreate(m,table);CHKERRQ(ierr);
167406763907SSatish Balay 
1675e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1676b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1677e4d965acSSatish Balay     isz  = 0;
16786831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1679e4d965acSSatish Balay 
1680e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
16814dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1682b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1683e4d965acSSatish Balay 
1684dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1685e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1686f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
16874dcbc457SBarry Smith     }
168806763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
168906763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1690e4d965acSSatish Balay 
169104a348a9SBarry Smith     k = 0;
169204a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
169304a348a9SBarry Smith       n = isz;
169406763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1695e4d965acSSatish Balay         row   = nidx[k];
1696e4d965acSSatish Balay         start = ai[row];
1697e4d965acSSatish Balay         end   = ai[row+1];
169804a348a9SBarry Smith         for (l = start; l<end ; l++){
16998a047759SSatish Balay           val = aj[l] + shift;
1700f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1701e4d965acSSatish Balay         }
1702e4d965acSSatish Balay       }
1703e4d965acSSatish Balay     }
1704029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1705e4d965acSSatish Balay   }
17066831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1707606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17083a40ed3dSBarry Smith   PetscFunctionReturn(0);
17094dcbc457SBarry Smith }
171017ab2063SBarry Smith 
17110513a670SBarry Smith /* -------------------------------------------------------------- */
17120513a670SBarry Smith #undef __FUNC__
1713ca44d042SBarry Smith #define __FUNC__ /*<a name="MatPermute_SeqAIJ"></a>*/"MatPermute_SeqAIJ"
17140513a670SBarry Smith int MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
17150513a670SBarry Smith {
17160513a670SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
17170513a670SBarry Smith   Scalar     *vwork;
1718273d9f13SBarry Smith   int        i,ierr,nz,m = A->m,n = A->n,*cwork;
17190513a670SBarry Smith   int        *row,*col,*cnew,j,*lens;
172056cd22aeSBarry Smith   IS         icolp,irowp;
17210513a670SBarry Smith 
17223a40ed3dSBarry Smith   PetscFunctionBegin;
17234c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
172456cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
17254c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
172656cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
17270513a670SBarry Smith 
17280513a670SBarry Smith   /* determine lengths of permuted rows */
17290513a670SBarry Smith   lens = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(lens);
17300513a670SBarry Smith   for (i=0; i<m; i++) {
17310513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
17320513a670SBarry Smith   }
17330513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
1734606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
17350513a670SBarry Smith 
17360513a670SBarry Smith   cnew = (int*)PetscMalloc(n*sizeof(int));CHKPTRQ(cnew);
17370513a670SBarry Smith   for (i=0; i<m; i++) {
17380513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
17390513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
17400513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
17410513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
17420513a670SBarry Smith   }
1743606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
17440513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17450513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
174656cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
174756cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
174856cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
174956cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
17503a40ed3dSBarry Smith   PetscFunctionReturn(0);
17510513a670SBarry Smith }
17520513a670SBarry Smith 
17535615d1e5SSatish Balay #undef __FUNC__
1754ca44d042SBarry Smith #define __FUNC__ /*<a name="MatPrintHelp_SeqAIJ"></a>*/"MatPrintHelp_SeqAIJ"
1755682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1756682d7d0cSBarry Smith {
1757c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1758682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1759d132466eSBarry Smith   int               ierr;
1760682d7d0cSBarry Smith 
17613a40ed3dSBarry Smith   PetscFunctionBegin;
1762c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1763d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1764d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1765d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
1766d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr);
1767d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr);
1768aa482453SBarry Smith #if defined(PETSC_HAVE_ESSL)
1769d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");CHKERRQ(ierr);
1770682d7d0cSBarry Smith #endif
1771cd5578b5SBarry Smith #if defined(PETSC_HAVE_LUSOL)
1772cd5578b5SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_lusol: Use the Stanford LUSOL sparse factorization and solve.\n");CHKERRQ(ierr);
1773cd5578b5SBarry Smith #endif
1774b9b97703SBarry Smith #if defined(PETSC_HAVE_MATLAB) && !defined(PETSC_USE_COMPLEX)
1775ca44d042SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_matlab: Use Matlab engine sparse LU factorization and solve.\n");CHKERRQ(ierr);
1776ca44d042SBarry Smith #endif
17773a40ed3dSBarry Smith   PetscFunctionReturn(0);
1778682d7d0cSBarry Smith }
1779ca44d042SBarry Smith EXTERN int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg);
1780ca44d042SBarry Smith EXTERN int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
1781ca44d042SBarry Smith EXTERN int MatColoringPatch_SeqAIJ(Mat,int,int *,ISColoring *);
1782ca44d042SBarry Smith EXTERN int MatILUDTFactor_SeqAIJ(Mat,MatILUInfo*,IS,IS,Mat*);
1783cb5b572fSBarry Smith #undef __FUNC__
1784ca44d042SBarry Smith #define __FUNC__ /*<a name="MatCopy_SeqAIJ"></a>*/"MatCopy_SeqAIJ"
1785cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1786cb5b572fSBarry Smith {
1787be6bf707SBarry Smith   int        ierr;
1788273d9f13SBarry Smith   PetscTruth flg;
1789cb5b572fSBarry Smith 
1790cb5b572fSBarry Smith   PetscFunctionBegin;
1791273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg);CHKERRQ(ierr);
1792273d9f13SBarry Smith   if (str == SAME_NONZERO_PATTERN && flg) {
1793be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1794be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1795be6bf707SBarry Smith 
1796273d9f13SBarry Smith     if (a->i[A->m]+a->indexshift != b->i[B->m]+a->indexshift) {
179729bbc08cSBarry Smith       SETERRQ(1,"Number of nonzeros in two matrices are different");
1798cb5b572fSBarry Smith     }
1799273d9f13SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m]+a->indexshift)*sizeof(Scalar));CHKERRQ(ierr);
1800cb5b572fSBarry Smith   } else {
1801cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1802cb5b572fSBarry Smith   }
1803cb5b572fSBarry Smith   PetscFunctionReturn(0);
1804cb5b572fSBarry Smith }
1805cb5b572fSBarry Smith 
1806273d9f13SBarry Smith #undef __FUNC__
1807273d9f13SBarry Smith #define __FUNC__ /*<a name="MatSetUpPreallocation_SeqAIJ"></a>*/"MatSetUpPreallocation_SeqAIJ"
1808273d9f13SBarry Smith int MatSetUpPreallocation_SeqAIJ(Mat A)
1809273d9f13SBarry Smith {
1810273d9f13SBarry Smith   int        ierr;
1811273d9f13SBarry Smith 
1812273d9f13SBarry Smith   PetscFunctionBegin;
1813273d9f13SBarry Smith   ierr =  MatSeqAIJSetPreallocation(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1814273d9f13SBarry Smith   PetscFunctionReturn(0);
1815273d9f13SBarry Smith }
1816273d9f13SBarry Smith 
1817273d9f13SBarry Smith 
1818682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
18190a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
1820cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
1821cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
1822cb5b572fSBarry Smith        MatMult_SeqAIJ,
1823cb5b572fSBarry Smith        MatMultAdd_SeqAIJ,
18247c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
18257c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
1826cb5b572fSBarry Smith        MatSolve_SeqAIJ,
1827cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
18287c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
18297c922b88SBarry Smith        MatSolveTransposeAdd_SeqAIJ,
1830cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
1831cb5b572fSBarry Smith        0,
183217ab2063SBarry Smith        MatRelax_SeqAIJ,
183317ab2063SBarry Smith        MatTranspose_SeqAIJ,
1834cb5b572fSBarry Smith        MatGetInfo_SeqAIJ,
1835cb5b572fSBarry Smith        MatEqual_SeqAIJ,
1836cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
1837cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
1838cb5b572fSBarry Smith        MatNorm_SeqAIJ,
1839cb5b572fSBarry Smith        0,
1840cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
184117ab2063SBarry Smith        MatCompress_SeqAIJ,
1842cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
1843cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
1844cb5b572fSBarry Smith        MatZeroRows_SeqAIJ,
1845cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
1846cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
1847cb5b572fSBarry Smith        0,
1848cb5b572fSBarry Smith        0,
1849273d9f13SBarry Smith        MatSetUpPreallocation_SeqAIJ,
1850273d9f13SBarry Smith        0,
1851cb5b572fSBarry Smith        MatGetOwnershipRange_SeqAIJ,
1852cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
1853cb5b572fSBarry Smith        0,
1854cb5b572fSBarry Smith        0,
1855cb5b572fSBarry Smith        0,
1856be6bf707SBarry Smith        MatDuplicate_SeqAIJ,
1857cb5b572fSBarry Smith        0,
1858cb5b572fSBarry Smith        0,
1859cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
1860cb5b572fSBarry Smith        0,
1861cb5b572fSBarry Smith        0,
1862cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
1863cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
1864cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
1865cb5b572fSBarry Smith        MatCopy_SeqAIJ,
1866f0b747eeSBarry Smith        MatPrintHelp_SeqAIJ,
1867cb5b572fSBarry Smith        MatScale_SeqAIJ,
1868cb5b572fSBarry Smith        0,
1869cb5b572fSBarry Smith        0,
18706945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
18716945ee14SBarry Smith        MatGetBlockSize_SeqAIJ,
18723b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
18733b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
18743b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
1875a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
1876a93ec695SBarry Smith        MatFDColoringCreate_SeqAIJ,
18770513a670SBarry Smith        MatColoringPatch_SeqAIJ,
18780513a670SBarry Smith        0,
1879cda55fadSBarry Smith        MatPermute_SeqAIJ,
1880cda55fadSBarry Smith        0,
1881cda55fadSBarry Smith        0,
1882b9b97703SBarry Smith        MatDestroy_SeqAIJ,
1883b9b97703SBarry Smith        MatView_SeqAIJ,
1884cda55fadSBarry Smith        MatGetMaps_Petsc};
188517ab2063SBarry Smith 
1886ca44d042SBarry Smith EXTERN int MatUseSuperLU_SeqAIJ(Mat);
1887ca44d042SBarry Smith EXTERN int MatUseEssl_SeqAIJ(Mat);
1888cd5578b5SBarry Smith EXTERN int MatUseLUSOL_SeqAIJ(Mat);
1889ca44d042SBarry Smith EXTERN int MatUseMatlab_SeqAIJ(Mat);
1890ca44d042SBarry Smith EXTERN int MatUseDXML_SeqAIJ(Mat);
189117ab2063SBarry Smith 
1892fb2e594dSBarry Smith EXTERN_C_BEGIN
18935615d1e5SSatish Balay #undef __FUNC__
1894ca44d042SBarry Smith #define __FUNC__ /*<a name="MatSeqAIJSetColumnIndices_SeqAIJ"></a>*/"MatSeqAIJSetColumnIndices_SeqAIJ"
189515091d37SBarry Smith 
1896bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices)
1897bef8e0ddSBarry Smith {
1898bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
1899bef8e0ddSBarry Smith   int        i,nz,n;
1900bef8e0ddSBarry Smith 
1901bef8e0ddSBarry Smith   PetscFunctionBegin;
190229bbc08cSBarry Smith   if (aij->indexshift) SETERRQ(1,"No support with 1 based indexing");
1903bef8e0ddSBarry Smith 
1904bef8e0ddSBarry Smith   nz = aij->maxnz;
1905273d9f13SBarry Smith   n  = mat->n;
1906bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
1907bef8e0ddSBarry Smith     aij->j[i] = indices[i];
1908bef8e0ddSBarry Smith   }
1909bef8e0ddSBarry Smith   aij->nz = nz;
1910bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
1911bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
1912bef8e0ddSBarry Smith   }
1913bef8e0ddSBarry Smith 
1914bef8e0ddSBarry Smith   PetscFunctionReturn(0);
1915bef8e0ddSBarry Smith }
1916fb2e594dSBarry Smith EXTERN_C_END
1917bef8e0ddSBarry Smith 
1918bef8e0ddSBarry Smith #undef __FUNC__
1919ca44d042SBarry Smith #define __FUNC__ /*<a name="MatSeqAIJSetColumnIndices"></a>*/"MatSeqAIJSetColumnIndices"
1920bef8e0ddSBarry Smith /*@
1921bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
1922bef8e0ddSBarry Smith        in the matrix.
1923bef8e0ddSBarry Smith 
1924bef8e0ddSBarry Smith   Input Parameters:
1925bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
1926bef8e0ddSBarry Smith -  indices - the column indices
1927bef8e0ddSBarry Smith 
192815091d37SBarry Smith   Level: advanced
192915091d37SBarry Smith 
1930bef8e0ddSBarry Smith   Notes:
1931bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
1932bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
1933bef8e0ddSBarry Smith   of the MatSetValues() operation.
1934bef8e0ddSBarry Smith 
1935bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
1936bef8e0ddSBarry Smith   MatCreateSeqAIJ().
1937bef8e0ddSBarry Smith 
1938bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
1939bef8e0ddSBarry Smith 
1940bef8e0ddSBarry Smith @*/
1941bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices)
1942bef8e0ddSBarry Smith {
1943bef8e0ddSBarry Smith   int ierr,(*f)(Mat,int *);
1944bef8e0ddSBarry Smith 
1945bef8e0ddSBarry Smith   PetscFunctionBegin;
1946bef8e0ddSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1947549d3d68SSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void **)&f);CHKERRQ(ierr);
1948bef8e0ddSBarry Smith   if (f) {
1949bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
1950bef8e0ddSBarry Smith   } else {
195129bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to set column indices");
1952bef8e0ddSBarry Smith   }
1953bef8e0ddSBarry Smith   PetscFunctionReturn(0);
1954bef8e0ddSBarry Smith }
1955bef8e0ddSBarry Smith 
1956be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
1957be6bf707SBarry Smith 
1958fb2e594dSBarry Smith EXTERN_C_BEGIN
1959be6bf707SBarry Smith #undef __FUNC__
1960ca44d042SBarry Smith #define __FUNC__ /*<a name="MatStoreValues_SeqAIJ"></a>*/"MatStoreValues_SeqAIJ"
1961be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat)
1962be6bf707SBarry Smith {
1963be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
1964273d9f13SBarry Smith   int        nz = aij->i[mat->m]+aij->indexshift,ierr;
1965be6bf707SBarry Smith 
1966be6bf707SBarry Smith   PetscFunctionBegin;
1967be6bf707SBarry Smith   if (aij->nonew != 1) {
196829bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
1969be6bf707SBarry Smith   }
1970be6bf707SBarry Smith 
1971be6bf707SBarry Smith   /* allocate space for values if not already there */
1972be6bf707SBarry Smith   if (!aij->saved_values) {
1973be6bf707SBarry Smith     aij->saved_values = (Scalar*)PetscMalloc(nz*sizeof(Scalar));CHKPTRQ(aij->saved_values);
1974be6bf707SBarry Smith   }
1975be6bf707SBarry Smith 
1976be6bf707SBarry Smith   /* copy values over */
1977549d3d68SSatish Balay   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(Scalar));CHKERRQ(ierr);
1978be6bf707SBarry Smith   PetscFunctionReturn(0);
1979be6bf707SBarry Smith }
1980fb2e594dSBarry Smith EXTERN_C_END
1981be6bf707SBarry Smith 
1982be6bf707SBarry Smith #undef __FUNC__
1983ca44d042SBarry Smith #define __FUNC__ /*<a name="MatStoreValues""></a>*/"MatStoreValues"
1984be6bf707SBarry Smith /*@
1985be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
1986be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
1987be6bf707SBarry Smith        nonlinear portion.
1988be6bf707SBarry Smith 
1989be6bf707SBarry Smith    Collect on Mat
1990be6bf707SBarry Smith 
1991be6bf707SBarry Smith   Input Parameters:
1992be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
1993be6bf707SBarry Smith 
199415091d37SBarry Smith   Level: advanced
199515091d37SBarry Smith 
1996be6bf707SBarry Smith   Common Usage, with SNESSolve():
1997be6bf707SBarry Smith $    Create Jacobian matrix
1998be6bf707SBarry Smith $    Set linear terms into matrix
1999be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2000be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2001be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2002be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2003be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2004be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2005be6bf707SBarry Smith $    In your Jacobian routine
2006be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2007be6bf707SBarry Smith $      Set nonlinear terms in matrix
2008be6bf707SBarry Smith 
2009be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2010be6bf707SBarry Smith $    // build linear portion of Jacobian
2011be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2012be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2013be6bf707SBarry Smith $    loop over nonlinear iterations
2014be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2015be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2016be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2017be6bf707SBarry Smith $       Solve linear system with Jacobian
2018be6bf707SBarry Smith $    endloop
2019be6bf707SBarry Smith 
2020be6bf707SBarry Smith   Notes:
2021be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2022be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2023be6bf707SBarry Smith     calling this routine.
2024be6bf707SBarry Smith 
2025be6bf707SBarry Smith .seealso: MatRetrieveValues()
2026be6bf707SBarry Smith 
2027be6bf707SBarry Smith @*/
2028be6bf707SBarry Smith int MatStoreValues(Mat mat)
2029be6bf707SBarry Smith {
2030be6bf707SBarry Smith   int ierr,(*f)(Mat);
2031be6bf707SBarry Smith 
2032be6bf707SBarry Smith   PetscFunctionBegin;
2033be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
203429bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
203529bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2036be6bf707SBarry Smith 
2037c5d6004cSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void **)&f);CHKERRQ(ierr);
2038be6bf707SBarry Smith   if (f) {
2039be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2040be6bf707SBarry Smith   } else {
204129bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to store values");
2042be6bf707SBarry Smith   }
2043be6bf707SBarry Smith   PetscFunctionReturn(0);
2044be6bf707SBarry Smith }
2045be6bf707SBarry Smith 
2046fb2e594dSBarry Smith EXTERN_C_BEGIN
2047be6bf707SBarry Smith #undef __FUNC__
2048ca44d042SBarry Smith #define __FUNC__ /*<a name="MatRetrieveValues_SeqAIJ"></a>*/"MatRetrieveValues_SeqAIJ"
2049be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat)
2050be6bf707SBarry Smith {
2051be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2052273d9f13SBarry Smith   int        nz = aij->i[mat->m]+aij->indexshift,ierr;
2053be6bf707SBarry Smith 
2054be6bf707SBarry Smith   PetscFunctionBegin;
2055be6bf707SBarry Smith   if (aij->nonew != 1) {
205629bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2057be6bf707SBarry Smith   }
2058be6bf707SBarry Smith   if (!aij->saved_values) {
205929bbc08cSBarry Smith     SETERRQ(1,"Must call MatStoreValues(A);first");
2060be6bf707SBarry Smith   }
2061be6bf707SBarry Smith 
2062be6bf707SBarry Smith   /* copy values over */
2063549d3d68SSatish Balay   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(Scalar));CHKERRQ(ierr);
2064be6bf707SBarry Smith   PetscFunctionReturn(0);
2065be6bf707SBarry Smith }
2066fb2e594dSBarry Smith EXTERN_C_END
2067be6bf707SBarry Smith 
2068be6bf707SBarry Smith #undef __FUNC__
2069ca44d042SBarry Smith #define __FUNC__ /*<a name="MatRetrieveValues"></a>*/"MatRetrieveValues"
2070be6bf707SBarry Smith /*@
2071be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2072be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2073be6bf707SBarry Smith        nonlinear portion.
2074be6bf707SBarry Smith 
2075be6bf707SBarry Smith    Collect on Mat
2076be6bf707SBarry Smith 
2077be6bf707SBarry Smith   Input Parameters:
2078be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2079be6bf707SBarry Smith 
208015091d37SBarry Smith   Level: advanced
208115091d37SBarry Smith 
2082be6bf707SBarry Smith .seealso: MatStoreValues()
2083be6bf707SBarry Smith 
2084be6bf707SBarry Smith @*/
2085be6bf707SBarry Smith int MatRetrieveValues(Mat mat)
2086be6bf707SBarry Smith {
2087be6bf707SBarry Smith   int ierr,(*f)(Mat);
2088be6bf707SBarry Smith 
2089be6bf707SBarry Smith   PetscFunctionBegin;
2090be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
209129bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
209229bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2093be6bf707SBarry Smith 
2094c5d6004cSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void **)&f);CHKERRQ(ierr);
2095be6bf707SBarry Smith   if (f) {
2096be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2097be6bf707SBarry Smith   } else {
209829bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to retrieve values");
2099be6bf707SBarry Smith   }
2100be6bf707SBarry Smith   PetscFunctionReturn(0);
2101be6bf707SBarry Smith }
2102be6bf707SBarry Smith 
2103f83d6046SBarry Smith /*
2104f83d6046SBarry Smith    This allows SeqAIJ matrices to be passed to the matlab engine
2105f83d6046SBarry Smith */
2106b9b97703SBarry Smith #if defined(PETSC_HAVE_MATLAB) && !defined(PETSC_USE_COMPLEX)
2107f83d6046SBarry Smith #include "engine.h"   /* Matlab include file */
2108f83d6046SBarry Smith #include "mex.h"      /* Matlab include file */
2109f83d6046SBarry Smith EXTERN_C_BEGIN
2110f83d6046SBarry Smith #undef __FUNC__
2111f83d6046SBarry Smith #define __FUNC__ /*<a name="MatMatlabEnginePut_SeqAIJ"></a>*/"MatMatlabEnginePut_SeqAIJ"
2112f83d6046SBarry Smith int MatMatlabEnginePut_SeqAIJ(PetscObject obj,void *engine)
2113f83d6046SBarry Smith {
2114d79a51d8SBarry Smith   int        ierr,i,*aj,*ai;
2115f83d6046SBarry Smith   Mat        B = (Mat)obj;
2116f83d6046SBarry Smith   Scalar     *array;
2117f83d6046SBarry Smith   mxArray    *mat;
2118d79a51d8SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)B->data;
2119d79a51d8SBarry Smith 
2120f83d6046SBarry Smith 
2121f83d6046SBarry Smith   PetscFunctionBegin;
212201820c62SBarry Smith   mat  = mxCreateSparse(B->n,B->m,aij->nz,mxREAL);
2123d79a51d8SBarry Smith   ierr = PetscMemcpy(mxGetPr(mat),aij->a,aij->nz*sizeof(Scalar));CHKERRQ(ierr);
2124d79a51d8SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
2125d79a51d8SBarry Smith   ai   = mxGetJc(mat);
2126d79a51d8SBarry Smith   aj   = mxGetIr(mat);
2127d79a51d8SBarry Smith   ierr = PetscMemcpy(aj,aij->j,aij->nz*sizeof(int));CHKERRQ(ierr);
2128d79a51d8SBarry Smith   ierr = PetscMemcpy(ai,aij->i,(B->m+1)*sizeof(int));CHKERRQ(ierr);
2129f83d6046SBarry Smith 
213001820c62SBarry Smith   /* Matlab indices start at 0 for sparse (what a surprise) */
213101820c62SBarry Smith   if (aij->indexshift) {
213201820c62SBarry Smith     for (i=0; i<B->m+1; i++) {
213301820c62SBarry Smith       ai[i]--;
2134d79a51d8SBarry Smith     }
2135d79a51d8SBarry Smith     for (i=0; i<aij->nz; i++) {
213601820c62SBarry Smith       aj[i]--;
2137d79a51d8SBarry Smith     }
2138d79a51d8SBarry Smith   }
2139ca44d042SBarry Smith   ierr = PetscObjectName(obj);CHKERRQ(ierr);
2140f83d6046SBarry Smith   mxSetName(mat,obj->name);
2141f83d6046SBarry Smith   engPutArray((Engine *)engine,mat);
2142f83d6046SBarry Smith   PetscFunctionReturn(0);
2143f83d6046SBarry Smith }
2144f83d6046SBarry Smith EXTERN_C_END
2145f83d6046SBarry Smith #endif
2146f83d6046SBarry Smith 
2147be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
2148cb5b572fSBarry Smith 
2149bef8e0ddSBarry Smith #undef __FUNC__
2150ca44d042SBarry Smith #define __FUNC__ /*<a name="MatCreateSeqAIJ"></a>*/"MatCreateSeqAIJ"
215117ab2063SBarry Smith /*@C
2152682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
21530d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
21546e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
215551c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
21562bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
215717ab2063SBarry Smith 
2158db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2159db81eaa0SLois Curfman McInnes 
216017ab2063SBarry Smith    Input Parameters:
2161db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
216217ab2063SBarry Smith .  m - number of rows
216317ab2063SBarry Smith .  n - number of columns
216417ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
216551c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
21662bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
216717ab2063SBarry Smith 
216817ab2063SBarry Smith    Output Parameter:
2169416022c9SBarry Smith .  A - the matrix
217017ab2063SBarry Smith 
2171b259b22eSLois Curfman McInnes    Notes:
217217ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
217317ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
21740002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
217544cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
217617ab2063SBarry Smith 
217717ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2178a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
21793d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
21806da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
218117ab2063SBarry Smith 
2182682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
21834fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2184682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
21856c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
21866c7ebb05SLois Curfman McInnes 
21876c7ebb05SLois Curfman McInnes    Options Database Keys:
2188db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2189db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2190db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2191db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2192db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
219317ab2063SBarry Smith 
2194027ccd11SLois Curfman McInnes    Level: intermediate
2195027ccd11SLois Curfman McInnes 
219636db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
219736db0b34SBarry Smith 
219817ab2063SBarry Smith @*/
2199416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz,Mat *A)
220017ab2063SBarry Smith {
2201273d9f13SBarry Smith   int ierr;
22026945ee14SBarry Smith 
22033a40ed3dSBarry Smith   PetscFunctionBegin;
2204273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,m,n,A);CHKERRQ(ierr);
2205273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2206273d9f13SBarry Smith   ierr = MatSeqAIJSetPreallocation(*A,nz,nnz);CHKERRQ(ierr);
2207273d9f13SBarry Smith   PetscFunctionReturn(0);
2208273d9f13SBarry Smith }
2209273d9f13SBarry Smith 
2210273d9f13SBarry Smith #undef __FUNC__
2211273d9f13SBarry Smith #define __FUNC__ /*<a name="MatSeqAIJSetPreallocation"></a>*/"MatSeqAIJSetPreallocation"
2212273d9f13SBarry Smith /*@C
2213273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2214273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2215273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2216273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2217273d9f13SBarry Smith 
2218273d9f13SBarry Smith    Collective on MPI_Comm
2219273d9f13SBarry Smith 
2220273d9f13SBarry Smith    Input Parameters:
2221273d9f13SBarry Smith +  comm - MPI communicator, set to PETSC_COMM_SELF
2222273d9f13SBarry Smith .  m - number of rows
2223273d9f13SBarry Smith .  n - number of columns
2224273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2225273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2226273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2227273d9f13SBarry Smith 
2228273d9f13SBarry Smith    Output Parameter:
2229273d9f13SBarry Smith .  A - the matrix
2230273d9f13SBarry Smith 
2231273d9f13SBarry Smith    Notes:
2232273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2233273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2234273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2235273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2236273d9f13SBarry Smith 
2237273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2238273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2239273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2240273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2241273d9f13SBarry Smith 
2242273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2243273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2244273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2245273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2246273d9f13SBarry Smith 
2247273d9f13SBarry Smith    Options Database Keys:
2248273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2249273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2250273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2251273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2252273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2253273d9f13SBarry Smith 
2254273d9f13SBarry Smith    Level: intermediate
2255273d9f13SBarry Smith 
2256273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2257273d9f13SBarry Smith 
2258273d9f13SBarry Smith @*/
2259273d9f13SBarry Smith int MatSeqAIJSetPreallocation(Mat B,int nz,int *nnz)
2260273d9f13SBarry Smith {
2261273d9f13SBarry Smith   Mat_SeqAIJ *b;
2262273d9f13SBarry Smith   int        i,len,ierr;
2263273d9f13SBarry Smith   PetscTruth flg2;
2264273d9f13SBarry Smith 
2265273d9f13SBarry Smith   PetscFunctionBegin;
2266273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg2);CHKERRQ(ierr);
2267273d9f13SBarry Smith   if (!flg2) PetscFunctionReturn(0);
2268d5d45c9bSBarry Smith 
226929bbc08cSBarry Smith   if (nz < -2) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than -2: value %d",nz);
2270b73539f3SBarry Smith   if (nnz) {
2271273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
227229bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2273b73539f3SBarry Smith     }
2274b73539f3SBarry Smith   }
2275b73539f3SBarry Smith 
2276273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2277273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2278273d9f13SBarry Smith 
2279273d9f13SBarry Smith   b->imax = (int*)PetscMalloc((B->m+1)*sizeof(int));CHKPTRQ(b->imax);
2280273d9f13SBarry Smith   if (!nnz) {
2281273d9f13SBarry Smith     if (nz == PETSC_DEFAULT) nz = 10;
2282273d9f13SBarry Smith     else if (nz <= 0)        nz = 1;
2283273d9f13SBarry Smith     for (i=0; i<B->m; i++) b->imax[i] = nz;
2284273d9f13SBarry Smith     nz = nz*B->m;
2285273d9f13SBarry Smith   } else {
2286273d9f13SBarry Smith     nz = 0;
2287273d9f13SBarry Smith     for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2288273d9f13SBarry Smith   }
2289273d9f13SBarry Smith 
2290273d9f13SBarry Smith   /* allocate the matrix space */
2291273d9f13SBarry Smith   len             = nz*(sizeof(int) + sizeof(Scalar)) + (B->m+1)*sizeof(int);
2292273d9f13SBarry Smith   b->a            = (Scalar*)PetscMalloc(len);CHKPTRQ(b->a);
2293273d9f13SBarry Smith   b->j            = (int*)(b->a + nz);
2294273d9f13SBarry Smith   ierr            = PetscMemzero(b->j,nz*sizeof(int));CHKERRQ(ierr);
2295273d9f13SBarry Smith   b->i            = b->j + nz;
2296273d9f13SBarry Smith   b->singlemalloc = PETSC_TRUE;
2297273d9f13SBarry Smith   b->freedata     = PETSC_TRUE;
2298273d9f13SBarry Smith 
2299273d9f13SBarry Smith   b->i[0] = -b->indexshift;
2300273d9f13SBarry Smith   for (i=1; i<B->m+1; i++) {
2301273d9f13SBarry Smith     b->i[i] = b->i[i-1] + b->imax[i-1];
2302273d9f13SBarry Smith   }
2303273d9f13SBarry Smith 
2304273d9f13SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
2305273d9f13SBarry Smith   b->ilen = (int*)PetscMalloc((B->m+1)*sizeof(int));
2306273d9f13SBarry Smith   PLogObjectMemory(B,len+2*(B->m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2307273d9f13SBarry Smith   for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2308273d9f13SBarry Smith 
2309273d9f13SBarry Smith   b->nz                = 0;
2310273d9f13SBarry Smith   b->maxnz             = nz;
2311273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2312273d9f13SBarry Smith   PetscFunctionReturn(0);
2313273d9f13SBarry Smith }
2314273d9f13SBarry Smith 
2315273d9f13SBarry Smith EXTERN_C_BEGIN
2316273d9f13SBarry Smith #undef __FUNC__
2317273d9f13SBarry Smith #define __FUNC__ /*<a name="MatCreate_SeqAIJ"></a>*/"MatCreate_SeqAIJ"
2318273d9f13SBarry Smith int MatCreate_SeqAIJ(Mat B)
2319273d9f13SBarry Smith {
2320273d9f13SBarry Smith   Mat_SeqAIJ *b;
2321273d9f13SBarry Smith   int        ierr,size;
2322273d9f13SBarry Smith   PetscTruth flg;
2323273d9f13SBarry Smith 
2324273d9f13SBarry Smith   PetscFunctionBegin;
2325273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2326273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2327273d9f13SBarry Smith 
2328273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2329273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2330273d9f13SBarry Smith 
23310452661fSBarry Smith   B->data             = (void*)(b = PetscNew(Mat_SeqAIJ));CHKPTRQ(b);
2332549d3d68SSatish Balay   ierr = PetscMemzero(b,sizeof(Mat_SeqAIJ));CHKERRQ(ierr);
2333549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2334416022c9SBarry Smith   B->factor           = 0;
2335416022c9SBarry Smith   B->lupivotthreshold = 1.0;
233690f02eecSBarry Smith   B->mapping          = 0;
2337f1af5d2fSBarry Smith   ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr);
2338f1af5d2fSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2339416022c9SBarry Smith   b->row              = 0;
2340416022c9SBarry Smith   b->col              = 0;
234182bf6240SBarry Smith   b->icol             = 0;
2342416022c9SBarry Smith   b->indexshift       = 0;
2343b810aeb4SBarry Smith   b->reallocs         = 0;
234469957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_oneindex",&flg);CHKERRQ(ierr);
234569957df2SSatish Balay   if (flg) b->indexshift = -1;
234617ab2063SBarry Smith 
2347273d9f13SBarry Smith   ierr = MapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
2348273d9f13SBarry Smith   ierr = MapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2349a5ae1ecdSBarry Smith 
2350f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
235136db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2352f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2353416022c9SBarry Smith   b->nonew             = 0;
2354416022c9SBarry Smith   b->diag              = 0;
2355416022c9SBarry Smith   b->solve_work        = 0;
235671bd300dSLois Curfman McInnes   b->spptr             = 0;
2357b65db4caSBarry Smith   b->inode.use         = PETSC_TRUE;
2358754ec7b1SSatish Balay   b->inode.node_count  = 0;
2359754ec7b1SSatish Balay   b->inode.size        = 0;
23606c7ebb05SLois Curfman McInnes   b->inode.limit       = 5;
23616c7ebb05SLois Curfman McInnes   b->inode.max_limit   = 5;
2362be6bf707SBarry Smith   b->saved_values      = 0;
2363d7f994e1SBarry Smith   b->idiag             = 0;
2364d7f994e1SBarry Smith   b->ssor              = 0;
2365f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
236617ab2063SBarry Smith 
2367*35d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
2368*35d8aa7fSBarry Smith 
23694b14c69eSBarry Smith   /*  SuperLU is not currently supported through PETSc */
2370aa482453SBarry Smith #if defined(PETSC_HAVE_SUPERLU)
237169957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_superlu",&flg);CHKERRQ(ierr);
237269957df2SSatish Balay   if (flg) { ierr = MatUseSuperLU_SeqAIJ(B);CHKERRQ(ierr); }
23734b14c69eSBarry Smith #endif
237469957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_essl",&flg);CHKERRQ(ierr);
237569957df2SSatish Balay   if (flg) { ierr = MatUseEssl_SeqAIJ(B);CHKERRQ(ierr); }
2376cd5578b5SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_lusol",&flg);CHKERRQ(ierr);
2377cd5578b5SBarry Smith   if (flg) { ierr = MatUseLUSOL_SeqAIJ(B);CHKERRQ(ierr); }
2378ca44d042SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_matlab",&flg);CHKERRQ(ierr);
2379ca44d042SBarry Smith   if (flg) {ierr = MatUseMatlab_SeqAIJ(B);CHKERRQ(ierr);}
238069957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_dxml",&flg);CHKERRQ(ierr);
238169957df2SSatish Balay   if (flg) {
238229bbc08cSBarry Smith     if (!b->indexshift) SETERRQ(PETSC_ERR_LIB,"need -mat_aij_oneindex with -mat_aij_dxml");
2383416022c9SBarry Smith     ierr = MatUseDXML_SeqAIJ(B);CHKERRQ(ierr);
238417ab2063SBarry Smith   }
2385f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2386bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2387bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2388f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2389be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2390bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2391f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2392be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2393bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2394b9b97703SBarry Smith #if defined(PETSC_HAVE_MATLAB) && !defined(PETSC_USE_COMPLEX)
2395f83d6046SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatMatlabEnginePut_SeqAIJ",MatMatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
2396f83d6046SBarry Smith #endif
23973a40ed3dSBarry Smith   PetscFunctionReturn(0);
239817ab2063SBarry Smith }
2399273d9f13SBarry Smith EXTERN_C_END
240017ab2063SBarry Smith 
24015615d1e5SSatish Balay #undef __FUNC__
2402ca44d042SBarry Smith #define __FUNC__ /*<a name="MatDuplicate_SeqAIJ"></a>*/"MatDuplicate_SeqAIJ"
2403be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
240417ab2063SBarry Smith {
2405416022c9SBarry Smith   Mat        C;
2406416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data;
2407273d9f13SBarry Smith   int        i,len,m = A->m,shift = a->indexshift,ierr;
240817ab2063SBarry Smith 
24093a40ed3dSBarry Smith   PetscFunctionBegin;
24104043dd9cSLois Curfman McInnes   *B = 0;
2411273d9f13SBarry Smith   ierr = MatCreate(A->comm,A->m,A->n,A->m,A->n,&C);CHKERRQ(ierr);
2412273d9f13SBarry Smith   ierr = MatSetType(C,MATSEQAIJ);CHKERRQ(ierr);
2413273d9f13SBarry Smith   c    = (Mat_SeqAIJ*)C->data;
2414273d9f13SBarry Smith 
2415416022c9SBarry Smith   C->factor         = A->factor;
2416416022c9SBarry Smith   c->row            = 0;
2417416022c9SBarry Smith   c->col            = 0;
241882bf6240SBarry Smith   c->icol           = 0;
2419416022c9SBarry Smith   c->indexshift     = shift;
2420f1e2ffcdSBarry Smith   c->keepzeroedrows = a->keepzeroedrows;
2421c456f294SBarry Smith   C->assembled      = PETSC_TRUE;
242217ab2063SBarry Smith 
2423273d9f13SBarry Smith   C->M          = A->m;
2424273d9f13SBarry Smith   C->N          = A->n;
242517ab2063SBarry Smith 
24260452661fSBarry Smith   c->imax       = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->imax);
24270452661fSBarry Smith   c->ilen       = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->ilen);
242817ab2063SBarry Smith   for (i=0; i<m; i++) {
2429416022c9SBarry Smith     c->imax[i] = a->imax[i];
2430416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
243117ab2063SBarry Smith   }
243217ab2063SBarry Smith 
243317ab2063SBarry Smith   /* allocate the matrix space */
2434f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
2435416022c9SBarry Smith   len   = (m+1)*sizeof(int)+(a->i[m])*(sizeof(Scalar)+sizeof(int));
24360452661fSBarry Smith   c->a  = (Scalar*)PetscMalloc(len);CHKPTRQ(c->a);
2437416022c9SBarry Smith   c->j  = (int*)(c->a + a->i[m] + shift);
2438416022c9SBarry Smith   c->i  = c->j + a->i[m] + shift;
2439549d3d68SSatish Balay   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));CHKERRQ(ierr);
244017ab2063SBarry Smith   if (m > 0) {
2441549d3d68SSatish Balay     ierr = PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int));CHKERRQ(ierr);
2442be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2443549d3d68SSatish Balay       ierr = PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(Scalar));CHKERRQ(ierr);
2444be6bf707SBarry Smith     } else {
2445549d3d68SSatish Balay       ierr = PetscMemzero(c->a,(a->i[m]+shift)*sizeof(Scalar));CHKERRQ(ierr);
244617ab2063SBarry Smith     }
244708480c60SBarry Smith   }
244817ab2063SBarry Smith 
2449f09e8eb9SSatish Balay   PLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2450416022c9SBarry Smith   c->sorted      = a->sorted;
2451416022c9SBarry Smith   c->roworiented = a->roworiented;
2452416022c9SBarry Smith   c->nonew       = a->nonew;
24537a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
2454be6bf707SBarry Smith   c->saved_values = 0;
2455d7f994e1SBarry Smith   c->idiag        = 0;
2456d7f994e1SBarry Smith   c->ssor         = 0;
245736db0b34SBarry Smith   c->ignorezeroentries = a->ignorezeroentries;
245836db0b34SBarry Smith   c->freedata     = PETSC_TRUE;
245917ab2063SBarry Smith 
2460416022c9SBarry Smith   if (a->diag) {
24610452661fSBarry Smith     c->diag = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->diag);
2462416022c9SBarry Smith     PLogObjectMemory(C,(m+1)*sizeof(int));
246317ab2063SBarry Smith     for (i=0; i<m; i++) {
2464416022c9SBarry Smith       c->diag[i] = a->diag[i];
246517ab2063SBarry Smith     }
24663a40ed3dSBarry Smith   } else c->diag        = 0;
2467b65db4caSBarry Smith   c->inode.use          = a->inode.use;
24686c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
24696c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
2470754ec7b1SSatish Balay   if (a->inode.size){
2471daed632aSSatish Balay     c->inode.size       = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->inode.size);
2472754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
2473549d3d68SSatish Balay     ierr = PetscMemcpy(c->inode.size,a->inode.size,(m+1)*sizeof(int));CHKERRQ(ierr);
2474754ec7b1SSatish Balay   } else {
2475754ec7b1SSatish Balay     c->inode.size       = 0;
2476754ec7b1SSatish Balay     c->inode.node_count = 0;
2477754ec7b1SSatish Balay   }
2478416022c9SBarry Smith   c->nz                 = a->nz;
2479416022c9SBarry Smith   c->maxnz              = a->maxnz;
2480416022c9SBarry Smith   c->solve_work         = 0;
248176dd722bSSatish Balay   c->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
2482273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2483754ec7b1SSatish Balay 
2484416022c9SBarry Smith   *B = C;
24857b2a1423SBarry Smith   ierr = FListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
24863a40ed3dSBarry Smith   PetscFunctionReturn(0);
248717ab2063SBarry Smith }
248817ab2063SBarry Smith 
2489273d9f13SBarry Smith EXTERN_C_BEGIN
24905615d1e5SSatish Balay #undef __FUNC__
2491ca44d042SBarry Smith #define __FUNC__ /*<a name="MatLoad_SeqAIJ"></a>*/"MatLoad_SeqAIJ"
249219bcc07fSBarry Smith int MatLoad_SeqAIJ(Viewer viewer,MatType type,Mat *A)
249317ab2063SBarry Smith {
2494416022c9SBarry Smith   Mat_SeqAIJ   *a;
2495416022c9SBarry Smith   Mat          B;
249617699dbbSLois Curfman McInnes   int          i,nz,ierr,fd,header[4],size,*rowlengths = 0,M,N,shift;
2497bcd2baecSBarry Smith   MPI_Comm     comm;
249817ab2063SBarry Smith 
24993a40ed3dSBarry Smith   PetscFunctionBegin;
2500e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2501d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
250229bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
250390ace30eSBarry Smith   ierr = ViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
25040752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
250529bbc08cSBarry Smith   if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
250617ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
250717ab2063SBarry Smith 
2508d64ed03dSBarry Smith   if (nz < 0) {
250929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2510d64ed03dSBarry Smith   }
2511d64ed03dSBarry Smith 
251217ab2063SBarry Smith   /* read in row lengths */
25130452661fSBarry Smith   rowlengths = (int*)PetscMalloc(M*sizeof(int));CHKPTRQ(rowlengths);
25140752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
251517ab2063SBarry Smith 
251617ab2063SBarry Smith   /* create our matrix */
2517416022c9SBarry Smith   ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A);CHKERRQ(ierr);
2518416022c9SBarry Smith   B = *A;
2519416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
2520416022c9SBarry Smith   shift = a->indexshift;
252117ab2063SBarry Smith 
252217ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
25230752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
252417ab2063SBarry Smith   if (shift) {
252517ab2063SBarry Smith     for (i=0; i<nz; i++) {
2526416022c9SBarry Smith       a->j[i] += 1;
252717ab2063SBarry Smith     }
252817ab2063SBarry Smith   }
252917ab2063SBarry Smith 
253017ab2063SBarry Smith   /* read in nonzero values */
25310752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
253217ab2063SBarry Smith 
253317ab2063SBarry Smith   /* set matrix "i" values */
2534416022c9SBarry Smith   a->i[0] = -shift;
253517ab2063SBarry Smith   for (i=1; i<= M; i++) {
2536416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2537416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
253817ab2063SBarry Smith   }
2539606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
254017ab2063SBarry Smith 
25416d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
25426d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
25433a40ed3dSBarry Smith   PetscFunctionReturn(0);
254417ab2063SBarry Smith }
2545273d9f13SBarry Smith EXTERN_C_END
254617ab2063SBarry Smith 
25475615d1e5SSatish Balay #undef __FUNC__
2548ca44d042SBarry Smith #define __FUNC__ /*<a name="MatEqual_SeqAIJ""></a>*/"MatEqual_SeqAIJ"
25498f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
25507264ac53SSatish Balay {
25517264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
25520f5bd95cSBarry Smith   int        ierr;
2553273d9f13SBarry Smith   PetscTruth flag;
25547264ac53SSatish Balay 
25553a40ed3dSBarry Smith   PetscFunctionBegin;
2556273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flag);CHKERRQ(ierr);
2557273d9f13SBarry Smith   if (!flag) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type");
25587264ac53SSatish Balay 
25597264ac53SSatish Balay   /* If the  matrix dimensions are not equal,or no of nonzeros or shift */
2560273d9f13SBarry Smith   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)|| (a->indexshift != b->indexshift)) {
2561ca44d042SBarry Smith     *flg = PETSC_FALSE;
2562ca44d042SBarry Smith     PetscFunctionReturn(0);
2563bcd2baecSBarry Smith   }
25647264ac53SSatish Balay 
25657264ac53SSatish Balay   /* if the a->i are the same */
2566273d9f13SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(int),flg);CHKERRQ(ierr);
25670f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
25687264ac53SSatish Balay 
25697264ac53SSatish Balay   /* if a->j are the same */
25700f5bd95cSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),flg);CHKERRQ(ierr);
25710f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
2572bcd2baecSBarry Smith 
2573bcd2baecSBarry Smith   /* if a->a are the same */
25740f5bd95cSBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(Scalar),flg);CHKERRQ(ierr);
25750f5bd95cSBarry Smith 
25763a40ed3dSBarry Smith   PetscFunctionReturn(0);
25777264ac53SSatish Balay 
25787264ac53SSatish Balay }
257936db0b34SBarry Smith 
258036db0b34SBarry Smith #undef __FUNC__
2581ca44d042SBarry Smith #define __FUNC__ /*<a name="MatCreateSeqAIJWithArrays"></a>*/"MatCreateSeqAIJWithArrays"
258236db0b34SBarry Smith /*@C
258336db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
258436db0b34SBarry Smith               provided by the user.
258536db0b34SBarry Smith 
258636db0b34SBarry Smith       Coolective on MPI_Comm
258736db0b34SBarry Smith 
258836db0b34SBarry Smith    Input Parameters:
258936db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
259036db0b34SBarry Smith .   m - number of rows
259136db0b34SBarry Smith .   n - number of columns
259236db0b34SBarry Smith .   i - row indices
259336db0b34SBarry Smith .   j - column indices
259436db0b34SBarry Smith -   a - matrix values
259536db0b34SBarry Smith 
259636db0b34SBarry Smith    Output Parameter:
259736db0b34SBarry Smith .   mat - the matrix
259836db0b34SBarry Smith 
259936db0b34SBarry Smith    Level: intermediate
260036db0b34SBarry Smith 
260136db0b34SBarry Smith    Notes:
26020551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
260336db0b34SBarry Smith     once the matrix is destroyed
260436db0b34SBarry Smith 
260536db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
260636db0b34SBarry Smith 
260736db0b34SBarry Smith        The i and j indices can be either 0- or 1 based
260836db0b34SBarry Smith 
260936db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
261036db0b34SBarry Smith 
261136db0b34SBarry Smith @*/
261236db0b34SBarry Smith int MatCreateSeqAIJWithArrays(MPI_Comm comm,int m,int n,int* i,int*j,Scalar *a,Mat *mat)
261336db0b34SBarry Smith {
261436db0b34SBarry Smith   int        ierr,ii;
261536db0b34SBarry Smith   Mat_SeqAIJ *aij;
261636db0b34SBarry Smith 
261736db0b34SBarry Smith   PetscFunctionBegin;
261836db0b34SBarry Smith   ierr = MatCreateSeqAIJ(comm,m,n,0,0,mat);CHKERRQ(ierr);
261936db0b34SBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
262036db0b34SBarry Smith   ierr = PetscFree(aij->a);CHKERRQ(ierr);
262136db0b34SBarry Smith 
262236db0b34SBarry Smith   if (i[0] == 1) {
262336db0b34SBarry Smith     aij->indexshift = -1;
262436db0b34SBarry Smith   } else if (i[0]) {
262529bbc08cSBarry Smith     SETERRQ(1,"i (row indices) do not start with 0 or 1");
262636db0b34SBarry Smith   }
262736db0b34SBarry Smith   aij->i = i;
262836db0b34SBarry Smith   aij->j = j;
262936db0b34SBarry Smith   aij->a = a;
263036db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
263136db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
263236db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
263336db0b34SBarry Smith 
263436db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
263536db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
26369860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
263729bbc08cSBarry 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]);
263836db0b34SBarry Smith #endif
263936db0b34SBarry Smith   }
26409860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
264136db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
264229bbc08cSBarry Smith     if (j[ii] < -aij->indexshift) SETERRQ2(1,"Negative column index at location = %d index = %d",ii,j[ii]);
264329bbc08cSBarry Smith     if (j[ii] > n - 1 -aij->indexshift) SETERRQ2(1,"Column index to large at location = %d index = %d",ii,j[ii]);
264436db0b34SBarry Smith   }
264536db0b34SBarry Smith #endif
264636db0b34SBarry Smith 
2647b65db4caSBarry Smith   /* changes indices to start at 0 */
2648b65db4caSBarry Smith   if (i[0]) {
2649b65db4caSBarry Smith     aij->indexshift = 0;
2650b65db4caSBarry Smith     for (ii=0; ii<m; ii++) {
2651b65db4caSBarry Smith       i[ii]--;
2652b65db4caSBarry Smith     }
2653b65db4caSBarry Smith     for (ii=0; ii<i[m]; ii++) {
2654b65db4caSBarry Smith       j[ii]--;
2655b65db4caSBarry Smith     }
2656b65db4caSBarry Smith   }
2657b65db4caSBarry Smith 
2658b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2659b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
266036db0b34SBarry Smith   PetscFunctionReturn(0);
266136db0b34SBarry Smith }
266236db0b34SBarry Smith 
266336db0b34SBarry Smith 
266436db0b34SBarry Smith 
2665