xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 54d96fa383ba80e0a45dd9f0fcec1909a0522f96)
1*54d96fa3SBarry Smith /*$Id: aij.c,v 1.350 2000/05/11 04:51:42 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) {
2931625ec5SSatish Balay     ierr = MatToSymmetricIJ_SeqAIJ(a->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
306945ee14SBarry Smith   } else if (oshift == 0 && ishift == -1) {
3131625ec5SSatish Balay     int nz = a->i[a->m];
323b2fbd54SBarry Smith     /* malloc space and  subtract 1 from i and j indices */
3331625ec5SSatish Balay     *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;
3631625ec5SSatish Balay     for (i=0; i<a->m+1; i++) (*ia)[i] = a->i[i] - 1;
376945ee14SBarry Smith   } else if (oshift == 1 && ishift == 0) {
3831625ec5SSatish Balay     int nz = a->i[a->m] + 1;
393b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
4031625ec5SSatish Balay     *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;
4331625ec5SSatish Balay     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) {
78179192dfSSatish Balay     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;
1304b0e389bSBarry Smith   int        *imax = a->imax,*ai = a->i,*ailen = a->ilen,roworiented = a->roworiented;
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);
13417ab2063SBarry Smith 
1353a40ed3dSBarry Smith   PetscFunctionBegin;
13617ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
137416022c9SBarry Smith     row  = im[k];
1385ef9f2a5SBarry Smith     if (row < 0) continue;
139aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
14032e87ba3SBarry Smith     if (row >= a->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large: row %d max %d",row,a->m);
1413b2fbd54SBarry Smith #endif
14217ab2063SBarry Smith     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
14317ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
144416022c9SBarry Smith     low = 0;
14517ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1465ef9f2a5SBarry Smith       if (in[l] < 0) continue;
147aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
14832e87ba3SBarry Smith       if (in[l] >= a->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large: col %d max %d",in[l],a->n);
1493b2fbd54SBarry Smith #endif
1504b0e389bSBarry Smith       col = in[l] - shift;
1514b0e389bSBarry Smith       if (roworiented) {
1525ef9f2a5SBarry Smith         value = v[l + k*n];
153bef8e0ddSBarry Smith       } else {
1544b0e389bSBarry Smith         value = v[k + l*m];
1554b0e389bSBarry Smith       }
15636db0b34SBarry Smith       if (value == 0.0 && ignorezeroentries) continue;
15736db0b34SBarry Smith 
158416022c9SBarry Smith       if (!sorted) low = 0; high = nrow;
159416022c9SBarry Smith       while (high-low > 5) {
160416022c9SBarry Smith         t = (low+high)/2;
161416022c9SBarry Smith         if (rp[t] > col) high = t;
162416022c9SBarry Smith         else             low  = t;
16317ab2063SBarry Smith       }
164416022c9SBarry Smith       for (i=low; i<high; i++) {
16517ab2063SBarry Smith         if (rp[i] > col) break;
16617ab2063SBarry Smith         if (rp[i] == col) {
167416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
16817ab2063SBarry Smith           else                  ap[i] = value;
16917ab2063SBarry Smith           goto noinsert;
17017ab2063SBarry Smith         }
17117ab2063SBarry Smith       }
172c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
173ca44d042SBarry Smith       else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero at (%d,%d) in the matrix",row,col);
17417ab2063SBarry Smith       if (nrow >= rmax) {
17517ab2063SBarry Smith         /* there is no extra room in row, therefore enlarge */
176416022c9SBarry Smith         int    new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j;
17717ab2063SBarry Smith         Scalar *new_a;
17817ab2063SBarry Smith 
179ca44d042SBarry Smith         if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero at (%d,%d) in the matrix requiring new malloc()",row,col);
18096854ed6SLois Curfman McInnes 
18117ab2063SBarry Smith         /* malloc new storage space */
182416022c9SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int);
1830452661fSBarry Smith         new_a   = (Scalar*)PetscMalloc(len);CHKPTRQ(new_a);
18417ab2063SBarry Smith         new_j   = (int*)(new_a + new_nz);
18517ab2063SBarry Smith         new_i   = new_j + new_nz;
18617ab2063SBarry Smith 
18717ab2063SBarry Smith         /* copy over old data into new slots */
18817ab2063SBarry Smith         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];}
189416022c9SBarry Smith         for (ii=row+1; ii<a->m+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;}
190549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr);
191416022c9SBarry Smith         len  = (new_nz - CHUNKSIZE - ai[row] - nrow - shift);
192549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow,len*sizeof(int));CHKERRQ(ierr);
193549d3d68SSatish Balay         ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar));CHKERRQ(ierr);
194549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow,len*sizeof(Scalar));CHKERRQ(ierr);
19517ab2063SBarry Smith         /* free up old matrix storage */
196606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);
197606d414cSSatish Balay         if (!a->singlemalloc) {
198606d414cSSatish Balay           ierr = PetscFree(a->i);CHKERRQ(ierr);
199606d414cSSatish Balay           ierr = PetscFree(a->j);CHKERRQ(ierr);
200606d414cSSatish Balay         }
201416022c9SBarry Smith         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;
202f1e2ffcdSBarry Smith         a->singlemalloc = PETSC_TRUE;
20317ab2063SBarry Smith 
20417ab2063SBarry Smith         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
205416022c9SBarry Smith         rmax = imax[row] = imax[row] + CHUNKSIZE;
206416022c9SBarry Smith         PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar)));
207416022c9SBarry Smith         a->maxnz += CHUNKSIZE;
208b810aeb4SBarry Smith         a->reallocs++;
20917ab2063SBarry Smith       }
210416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
211416022c9SBarry Smith       /* shift up all the later entries in this row */
212416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
21317ab2063SBarry Smith         rp[ii+1] = rp[ii];
21417ab2063SBarry Smith         ap[ii+1] = ap[ii];
21517ab2063SBarry Smith       }
21617ab2063SBarry Smith       rp[i] = col;
21717ab2063SBarry Smith       ap[i] = value;
21817ab2063SBarry Smith       noinsert:;
219416022c9SBarry Smith       low = i + 1;
22017ab2063SBarry Smith     }
22117ab2063SBarry Smith     ailen[row] = nrow;
22217ab2063SBarry Smith   }
2233a40ed3dSBarry Smith   PetscFunctionReturn(0);
22417ab2063SBarry Smith }
22517ab2063SBarry Smith 
2265615d1e5SSatish Balay #undef __FUNC__
227ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetValues_SeqAIJ"></a>*/"MatGetValues_SeqAIJ"
2288f6be9afSLois Curfman McInnes int MatGetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v)
2297eb43aa7SLois Curfman McInnes {
2307eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
231b49de8d1SLois Curfman McInnes   int        *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
2327eb43aa7SLois Curfman McInnes   int        *ai = a->i,*ailen = a->ilen,shift = a->indexshift;
2337eb43aa7SLois Curfman McInnes   Scalar     *ap,*aa = a->a,zero = 0.0;
2347eb43aa7SLois Curfman McInnes 
2353a40ed3dSBarry Smith   PetscFunctionBegin;
2367eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2377eb43aa7SLois Curfman McInnes     row  = im[k];
238ca44d042SBarry Smith     if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row: %d",row);
239ca44d042SBarry Smith     if (row >= a->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large: %d",row);
2407eb43aa7SLois Curfman McInnes     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
2417eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2427eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
243ca44d042SBarry Smith       if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column: %d",in[l]);
244ca44d042SBarry Smith       if (in[l] >= a->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large: %d",in[l]);
2457eb43aa7SLois Curfman McInnes       col = in[l] - shift;
2467eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2477eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2487eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2497eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2507eb43aa7SLois Curfman McInnes         else             low  = t;
2517eb43aa7SLois Curfman McInnes       }
2527eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2537eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2547eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
255b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2567eb43aa7SLois Curfman McInnes           goto finished;
2577eb43aa7SLois Curfman McInnes         }
2587eb43aa7SLois Curfman McInnes       }
259b49de8d1SLois Curfman McInnes       *v++ = zero;
2607eb43aa7SLois Curfman McInnes       finished:;
2617eb43aa7SLois Curfman McInnes     }
2627eb43aa7SLois Curfman McInnes   }
2633a40ed3dSBarry Smith   PetscFunctionReturn(0);
2647eb43aa7SLois Curfman McInnes }
2657eb43aa7SLois Curfman McInnes 
26617ab2063SBarry Smith 
2675615d1e5SSatish Balay #undef __FUNC__
268ca44d042SBarry Smith #define __FUNC__ /*<a name="MatView_SeqAIJ_Binary"></a>*/"MatView_SeqAIJ_Binary"
269480ef9eaSBarry Smith int MatView_SeqAIJ_Binary(Mat A,Viewer viewer)
27017ab2063SBarry Smith {
271416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
272416022c9SBarry Smith   int        i,fd,*col_lens,ierr;
27317ab2063SBarry Smith 
2743a40ed3dSBarry Smith   PetscFunctionBegin;
27590ace30eSBarry Smith   ierr = ViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
2760452661fSBarry Smith   col_lens = (int*)PetscMalloc((4+a->m)*sizeof(int));CHKPTRQ(col_lens);
277416022c9SBarry Smith   col_lens[0] = MAT_COOKIE;
278416022c9SBarry Smith   col_lens[1] = a->m;
279416022c9SBarry Smith   col_lens[2] = a->n;
280416022c9SBarry Smith   col_lens[3] = a->nz;
281416022c9SBarry Smith 
282416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
283416022c9SBarry Smith   for (i=0; i<a->m; i++) {
284416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
28517ab2063SBarry Smith   }
2860752156aSBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+a->m,PETSC_INT,1);CHKERRQ(ierr);
287606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
288416022c9SBarry Smith 
289416022c9SBarry Smith   /* store column indices (zero start index) */
290416022c9SBarry Smith   if (a->indexshift) {
291416022c9SBarry Smith     for (i=0; i<a->nz; i++) a->j[i]--;
29217ab2063SBarry Smith   }
2930752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,0);CHKERRQ(ierr);
294416022c9SBarry Smith   if (a->indexshift) {
295416022c9SBarry Smith     for (i=0; i<a->nz; i++) a->j[i]++;
29617ab2063SBarry Smith   }
297416022c9SBarry Smith 
298416022c9SBarry Smith   /* store nonzero values */
2990752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,0);CHKERRQ(ierr);
3003a40ed3dSBarry Smith   PetscFunctionReturn(0);
30117ab2063SBarry Smith }
302416022c9SBarry Smith 
3035615d1e5SSatish Balay #undef __FUNC__
304ca44d042SBarry Smith #define __FUNC__ /*<a name="MatView_SeqAIJ_ASCII"></a>*/"MatView_SeqAIJ_ASCII"
305480ef9eaSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,Viewer viewer)
306416022c9SBarry Smith {
307416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
3086831982aSBarry Smith   int         ierr,i,j,m = a->m,shift = a->indexshift,format;
30917ab2063SBarry Smith   char        *outputname;
31017ab2063SBarry Smith 
3113a40ed3dSBarry Smith   PetscFunctionBegin;
312fb4b0f7fSBarry Smith   ierr = ViewerGetOutputname(viewer,&outputname);CHKERRQ(ierr);
313888f2ed8SSatish Balay   ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr);
3146831982aSBarry Smith   if (format == VIEWER_FORMAT_ASCII_INFO_LONG || format == VIEWER_FORMAT_ASCII_INFO) {
3156831982aSBarry Smith     if (a->inode.size) {
3166831982aSBarry 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);
3176831982aSBarry Smith     } else {
3186831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"not using I-node routines\n");CHKERRQ(ierr);
3196831982aSBarry Smith     }
3203a40ed3dSBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_MATLAB) {
321d00d2cf4SBarry Smith     int nofinalvalue = 0;
322d00d2cf4SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != a->n-!shift)) {
323d00d2cf4SBarry Smith       nofinalvalue = 1;
324d00d2cf4SBarry Smith     }
3256831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
3266831982aSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"%% Size = %d %d \n",m,a->n);CHKERRQ(ierr);
3276831982aSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"%% Nonzeros = %d \n",a->nz);CHKERRQ(ierr);
3286831982aSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
3296831982aSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
33017ab2063SBarry Smith 
33117ab2063SBarry Smith     for (i=0; i<m; i++) {
332416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
333aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
33436db0b34SBarry 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);
33517ab2063SBarry Smith #else
3366831982aSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
33717ab2063SBarry Smith #endif
33817ab2063SBarry Smith       }
33917ab2063SBarry Smith     }
340d00d2cf4SBarry Smith     if (nofinalvalue) {
3416831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",m,a->n,0.0);CHKERRQ(ierr);
342d00d2cf4SBarry Smith     }
3436831982aSBarry Smith     if (outputname) {ierr = ViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",outputname);CHKERRQ(ierr);}
3446831982aSBarry Smith     else            {ierr = ViewerASCIIPrintf(viewer,"];\n M = spconvert(zzz);\n");CHKERRQ(ierr);}
3456831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
3463a40ed3dSBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_COMMON) {
3476831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
34844cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
3496831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
35044cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
351aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
35236db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
35336db0b34SBarry Smith           ierr = ViewerASCIIPrintf(viewer," %d %g + %g i",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
35436db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
35536db0b34SBarry Smith           ierr = ViewerASCIIPrintf(viewer," %d %g - %g i",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
35636db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
35736db0b34SBarry Smith           ierr = ViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3586831982aSBarry Smith         }
35944cd7ae7SLois Curfman McInnes #else
3606831982aSBarry Smith         if (a->a[j] != 0.0) {ierr = ViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
36144cd7ae7SLois Curfman McInnes #endif
36244cd7ae7SLois Curfman McInnes       }
3636831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
36444cd7ae7SLois Curfman McInnes     }
3656831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
36602594712SBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_SYMMODU) {
367496be53dSLois Curfman McInnes     int nzd=0,fshift=1,*sptr;
3686831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
3692e44a96cSLois Curfman McInnes     sptr = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(sptr);
370496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
371496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
372496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
373496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
374aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
37536db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
376496be53dSLois Curfman McInnes #else
377496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
378496be53dSLois Curfman McInnes #endif
379496be53dSLois Curfman McInnes         }
380496be53dSLois Curfman McInnes       }
381496be53dSLois Curfman McInnes     }
3822e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
3836831982aSBarry Smith     ierr = ViewerASCIIPrintf(viewer," %d %d\n\n",m,nzd);CHKERRQ(ierr);
3842e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
3856831982aSBarry 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);}
3866831982aSBarry 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);}
3876831982aSBarry 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);}
3886831982aSBarry Smith       else if (i+1<m) {ierr = ViewerASCIIPrintf(viewer," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
3896831982aSBarry Smith       else if (i<m)   {ierr = ViewerASCIIPrintf(viewer," %d %d\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
3906831982aSBarry Smith       else            {ierr = ViewerASCIIPrintf(viewer," %d\n",sptr[i]);CHKERRQ(ierr);}
391496be53dSLois Curfman McInnes     }
3926831982aSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
393606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
394496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
395496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
3966831982aSBarry Smith         if (a->j[j] >= i) {ierr = ViewerASCIIPrintf(viewer," %d ",a->j[j]+fshift);CHKERRQ(ierr);}
397496be53dSLois Curfman McInnes       }
3986831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
399496be53dSLois Curfman McInnes     }
4006831982aSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
401496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
402496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
403496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
404aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
40536db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
40636db0b34SBarry Smith             ierr = ViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4076831982aSBarry Smith           }
408496be53dSLois Curfman McInnes #else
4096831982aSBarry Smith           if (a->a[j] != 0.0) {ierr = ViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
410496be53dSLois Curfman McInnes #endif
411496be53dSLois Curfman McInnes         }
412496be53dSLois Curfman McInnes       }
4136831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
414496be53dSLois Curfman McInnes     }
4156831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
41602594712SBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_DENSE) {
41702594712SBarry Smith     int    cnt = 0,jcnt;
41802594712SBarry Smith     Scalar value;
41902594712SBarry Smith 
4206831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
42102594712SBarry Smith     for (i=0; i<m; i++) {
42202594712SBarry Smith       jcnt = 0;
42302594712SBarry Smith       for (j=0; j<a->n; j++) {
424e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
42502594712SBarry Smith           value = a->a[cnt++];
426e24b481bSBarry Smith           jcnt++;
42702594712SBarry Smith         } else {
42802594712SBarry Smith           value = 0.0;
42902594712SBarry Smith         }
430aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
43136db0b34SBarry Smith         ierr = ViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
43202594712SBarry Smith #else
4336831982aSBarry Smith         ierr = ViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
43402594712SBarry Smith #endif
43502594712SBarry Smith       }
4366831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
43702594712SBarry Smith     }
4386831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4393a40ed3dSBarry Smith   } else {
4406831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
44117ab2063SBarry Smith     for (i=0; i<m; i++) {
4426831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
443416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
444aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
44536db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
44636db0b34SBarry Smith           ierr = ViewerASCIIPrintf(viewer," %d %g + %g i",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
44736db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
44836db0b34SBarry Smith           ierr = ViewerASCIIPrintf(viewer," %d %g - %g i",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4493a40ed3dSBarry Smith         } else {
45036db0b34SBarry Smith           ierr = ViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
45117ab2063SBarry Smith         }
45217ab2063SBarry Smith #else
4536831982aSBarry Smith         ierr = ViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
45417ab2063SBarry Smith #endif
45517ab2063SBarry Smith       }
4566831982aSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45717ab2063SBarry Smith     }
4586831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
45917ab2063SBarry Smith   }
4606831982aSBarry Smith   ierr = ViewerFlush(viewer);CHKERRQ(ierr);
4613a40ed3dSBarry Smith   PetscFunctionReturn(0);
462416022c9SBarry Smith }
463416022c9SBarry Smith 
4645615d1e5SSatish Balay #undef __FUNC__
465ca44d042SBarry Smith #define __FUNC__ /*<a name="MatView_SeqAIJ_Draw_Zoom"></a>*/"MatView_SeqAIJ_Draw_Zoom"
466480ef9eaSBarry Smith int MatView_SeqAIJ_Draw_Zoom(Draw draw,void *Aa)
467416022c9SBarry Smith {
468480ef9eaSBarry Smith   Mat         A = (Mat) Aa;
469416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
470*54d96fa3SBarry Smith   int         ierr,i,j,m = a->m,shift = a->indexshift,color;
4710513a670SBarry Smith   int         format;
47236db0b34SBarry Smith   PetscReal   xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
473480ef9eaSBarry Smith   Viewer      viewer;
474cddf8d76SBarry Smith 
4753a40ed3dSBarry Smith   PetscFunctionBegin;
476480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
4770513a670SBarry Smith   ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr);
47819bcc07fSBarry Smith 
479480ef9eaSBarry Smith   ierr = DrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
480416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4810513a670SBarry Smith 
4820513a670SBarry Smith   if (format != VIEWER_FORMAT_DRAW_CONTOUR) {
4830513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
484cddf8d76SBarry Smith     color = DRAW_BLUE;
485416022c9SBarry Smith     for (i=0; i<m; i++) {
486cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
487416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
488cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
489aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
49036db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
491cddf8d76SBarry Smith #else
492cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
493cddf8d76SBarry Smith #endif
494480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
495cddf8d76SBarry Smith       }
496cddf8d76SBarry Smith     }
497cddf8d76SBarry Smith     color = DRAW_CYAN;
498cddf8d76SBarry Smith     for (i=0; i<m; i++) {
499cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
500cddf8d76SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
501cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
502cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
503480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
504cddf8d76SBarry Smith       }
505cddf8d76SBarry Smith     }
506cddf8d76SBarry Smith     color = DRAW_RED;
507cddf8d76SBarry Smith     for (i=0; i<m; i++) {
508cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
509cddf8d76SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
510cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
511aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
51236db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
513cddf8d76SBarry Smith #else
514cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
515cddf8d76SBarry Smith #endif
516480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
517416022c9SBarry Smith       }
518416022c9SBarry Smith     }
5190513a670SBarry Smith   } else {
5200513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5210513a670SBarry Smith     /* first determine max of all nonzero values */
5220513a670SBarry Smith     int    nz = a->nz,count;
5230513a670SBarry Smith     Draw   popup;
52436db0b34SBarry Smith     PetscReal scale;
5250513a670SBarry Smith 
5260513a670SBarry Smith     for (i=0; i<nz; i++) {
5270513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5280513a670SBarry Smith     }
529480ef9eaSBarry Smith     scale = (245.0 - DRAW_BASIC_COLORS)/maxv;
530522c5e43SBarry Smith     ierr  = DrawGetPopup(draw,&popup);CHKERRQ(ierr);
531f1e2ffcdSBarry Smith     if (popup) {ierr  = DrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5320513a670SBarry Smith     count = 0;
5330513a670SBarry Smith     for (i=0; i<m; i++) {
5340513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
5350513a670SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5360513a670SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
5376d6b6c30SSatish Balay         color = DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count]));
538480ef9eaSBarry Smith         ierr  = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5390513a670SBarry Smith         count++;
5400513a670SBarry Smith       }
5410513a670SBarry Smith     }
5420513a670SBarry Smith   }
543480ef9eaSBarry Smith   PetscFunctionReturn(0);
544480ef9eaSBarry Smith }
545cddf8d76SBarry Smith 
546480ef9eaSBarry Smith #undef __FUNC__
547ca44d042SBarry Smith #define __FUNC__ /*<a name="MatView_SeqAIJ_Draw"></a>*/"MatView_SeqAIJ_Draw"
548480ef9eaSBarry Smith int MatView_SeqAIJ_Draw(Mat A,Viewer viewer)
549480ef9eaSBarry Smith {
550480ef9eaSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
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);
562480ef9eaSBarry 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) {
58501820c62SBarry Smith       SETERRQ(1,1,"Can only socket send sparse matrix with 0 based indexing");
58601820c62SBarry Smith     }
5877b2a1423SBarry 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 {
5950f5bd95cSBarry Smith     SETERRQ1(1,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;
60743ee02c3SBarry 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   }
645c38d4ed2SBarry 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;
665549d3d68SSatish Balay   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;
67794d884c6SBarry Smith 
67894d884c6SBarry Smith   if (A->mapping) {
67994d884c6SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(A->mapping);CHKERRQ(ierr);
68094d884c6SBarry Smith   }
68194d884c6SBarry Smith   if (A->bmapping) {
68294d884c6SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(A->bmapping);CHKERRQ(ierr);
68394d884c6SBarry Smith   }
68461b13de0SBarry Smith   if (A->rmap) {
68561b13de0SBarry Smith     ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
68661b13de0SBarry Smith   }
68761b13de0SBarry Smith   if (A->cmap) {
68861b13de0SBarry Smith     ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
68961b13de0SBarry Smith   }
690606d414cSSatish Balay   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
691aa482453SBarry Smith #if defined(PETSC_USE_LOG)
692e1311b90SBarry Smith   PLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",a->m,a->n,a->nz);
69317ab2063SBarry Smith #endif
69436db0b34SBarry Smith   if (a->freedata) {
695606d414cSSatish Balay     ierr = PetscFree(a->a);CHKERRQ(ierr);
696606d414cSSatish Balay     if (!a->singlemalloc) {
697606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
698606d414cSSatish Balay       ierr = PetscFree(a->j);CHKERRQ(ierr);
699606d414cSSatish Balay     }
70036db0b34SBarry Smith   }
701c38d4ed2SBarry Smith   if (a->row) {
702c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
703c38d4ed2SBarry Smith   }
704c38d4ed2SBarry Smith   if (a->col) {
705c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
706c38d4ed2SBarry Smith   }
707606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
708606d414cSSatish Balay   if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);}
709606d414cSSatish Balay   if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);}
710606d414cSSatish Balay   if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
711606d414cSSatish Balay   if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
71282bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
713606d414cSSatish Balay   if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);}
714606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
715eed86810SBarry Smith 
716f2655603SLois Curfman McInnes   PLogObjectDestroy(A);
717f2655603SLois Curfman McInnes   PetscHeaderDestroy(A);
7183a40ed3dSBarry Smith   PetscFunctionReturn(0);
71917ab2063SBarry Smith }
72017ab2063SBarry Smith 
7215615d1e5SSatish Balay #undef __FUNC__
722ca44d042SBarry Smith #define __FUNC__ /*<a name="MatCompress_SeqAIJ"></a>*/"MatCompress_SeqAIJ"
7238f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
72417ab2063SBarry Smith {
7253a40ed3dSBarry Smith   PetscFunctionBegin;
7263a40ed3dSBarry Smith   PetscFunctionReturn(0);
72717ab2063SBarry Smith }
72817ab2063SBarry Smith 
7295615d1e5SSatish Balay #undef __FUNC__
730ca44d042SBarry Smith #define __FUNC__ /*<a name="MatSetOption_SeqAIJ"></a>*/"MatSetOption_SeqAIJ"
7318f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
73217ab2063SBarry Smith {
733416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7343a40ed3dSBarry Smith 
7353a40ed3dSBarry Smith   PetscFunctionBegin;
736f1e2ffcdSBarry Smith   if      (op == MAT_ROW_ORIENTED)                 a->roworiented       = PETSC_TRUE;
737f1e2ffcdSBarry Smith   else if (op == MAT_KEEP_ZEROED_ROWS)             a->keepzeroedrows    = PETSC_TRUE;
738f1e2ffcdSBarry Smith   else if (op == MAT_COLUMN_ORIENTED)              a->roworiented       = PETSC_FALSE;
739f1e2ffcdSBarry Smith   else if (op == MAT_COLUMNS_SORTED)               a->sorted            = PETSC_TRUE;
740f1e2ffcdSBarry Smith   else if (op == MAT_COLUMNS_UNSORTED)             a->sorted            = PETSC_FALSE;
7416d4a8577SBarry Smith   else if (op == MAT_NO_NEW_NONZERO_LOCATIONS)     a->nonew             = 1;
7424787f768SSatish Balay   else if (op == MAT_NEW_NONZERO_LOCATION_ERR)     a->nonew             = -1;
7434787f768SSatish Balay   else if (op == MAT_NEW_NONZERO_ALLOCATION_ERR)   a->nonew             = -2;
7446d4a8577SBarry Smith   else if (op == MAT_YES_NEW_NONZERO_LOCATIONS)    a->nonew             = 0;
74536db0b34SBarry Smith   else if (op == MAT_IGNORE_ZERO_ENTRIES)          a->ignorezeroentries = PETSC_TRUE;
746b65db4caSBarry Smith   else if (op == MAT_USE_INODES)                   a->inode.use         = PETSC_TRUE;
747b65db4caSBarry Smith   else if (op == MAT_DO_NOT_USE_INODES)            a->inode.use         = PETSC_FALSE;
7486d4a8577SBarry Smith   else if (op == MAT_ROWS_SORTED ||
749219d9a1aSLois Curfman McInnes            op == MAT_ROWS_UNSORTED ||
7506d4a8577SBarry Smith            op == MAT_SYMMETRIC ||
7516d4a8577SBarry Smith            op == MAT_STRUCTURALLY_SYMMETRIC ||
75290f02eecSBarry Smith            op == MAT_YES_NEW_DIAGONALS ||
753b51ba29fSSatish Balay            op == MAT_IGNORE_OFF_PROC_ENTRIES||
754b51ba29fSSatish Balay            op == MAT_USE_HASH_TABLE)
755981c4779SBarry Smith     PLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
7563a40ed3dSBarry Smith   else if (op == MAT_NO_NEW_DIAGONALS) {
7573a40ed3dSBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS");
7583a40ed3dSBarry Smith   } else if (op == MAT_INODE_LIMIT_1)          a->inode.limit  = 1;
7596d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_2)            a->inode.limit  = 2;
7606d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_3)            a->inode.limit  = 3;
7616d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_4)            a->inode.limit  = 4;
7626d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_5)            a->inode.limit  = 5;
7633a40ed3dSBarry Smith   else SETERRQ(PETSC_ERR_SUP,0,"unknown option");
7643a40ed3dSBarry Smith   PetscFunctionReturn(0);
76517ab2063SBarry Smith }
76617ab2063SBarry Smith 
7675615d1e5SSatish Balay #undef __FUNC__
768ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetDiagonal_SeqAIJ"></a>*/"MatGetDiagonal_SeqAIJ"
7698f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
77017ab2063SBarry Smith {
771416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7723a40ed3dSBarry Smith   int        i,j,n,shift = a->indexshift,ierr;
77317ab2063SBarry Smith   Scalar     *x,zero = 0.0;
77417ab2063SBarry Smith 
7753a40ed3dSBarry Smith   PetscFunctionBegin;
7763a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
77736db0b34SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
77836db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
779a8c6a408SBarry Smith   if (n != a->m) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Nonconforming matrix and vector");
780416022c9SBarry Smith   for (i=0; i<a->m; i++) {
781416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
782416022c9SBarry Smith       if (a->j[j]+shift == i) {
783416022c9SBarry Smith         x[i] = a->a[j];
78417ab2063SBarry Smith         break;
78517ab2063SBarry Smith       }
78617ab2063SBarry Smith     }
78717ab2063SBarry Smith   }
78836db0b34SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
7893a40ed3dSBarry Smith   PetscFunctionReturn(0);
79017ab2063SBarry Smith }
79117ab2063SBarry Smith 
7925615d1e5SSatish Balay #undef __FUNC__
793ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMultTranspose_SeqAIJ"></a>*/"MatMultTranspose_SeqAIJ"
7947c922b88SBarry Smith int MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
79517ab2063SBarry Smith {
796416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
797f1af5d2fSBarry Smith   Scalar     *x,*y,*v,alpha,zero = 0.0;
798e1311b90SBarry Smith   int        ierr,m = a->m,n,i,*idx,shift = a->indexshift;
79917ab2063SBarry Smith 
8003a40ed3dSBarry Smith   PetscFunctionBegin;
801f1af5d2fSBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
802e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
803e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
80417ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
80517ab2063SBarry Smith   for (i=0; i<m; i++) {
806416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
807416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
808416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
80917ab2063SBarry Smith     alpha = x[i];
81017ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
81117ab2063SBarry Smith   }
812416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
813e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
814e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8153a40ed3dSBarry Smith   PetscFunctionReturn(0);
81617ab2063SBarry Smith }
817d5d45c9bSBarry Smith 
8185615d1e5SSatish Balay #undef __FUNC__
819ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMultTransposeAdd_SeqAIJ"></a>*/"MatMultTransposeAdd_SeqAIJ"
8207c922b88SBarry Smith int MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
82117ab2063SBarry Smith {
822416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
82317ab2063SBarry Smith   Scalar     *x,*y,*v,alpha;
824e1311b90SBarry Smith   int        ierr,m = a->m,n,i,*idx,shift = a->indexshift;
82517ab2063SBarry Smith 
8263a40ed3dSBarry Smith   PetscFunctionBegin;
8272e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
828e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
829e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
83017ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
83117ab2063SBarry Smith   for (i=0; i<m; i++) {
832416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
833416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
834416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
83517ab2063SBarry Smith     alpha = x[i];
83617ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
83717ab2063SBarry Smith   }
83890f02eecSBarry Smith   PLogFlops(2*a->nz);
839e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
840e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8413a40ed3dSBarry Smith   PetscFunctionReturn(0);
84217ab2063SBarry Smith }
84317ab2063SBarry Smith 
8445615d1e5SSatish Balay #undef __FUNC__
845ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMult_SeqAIJ"></a>*/"MatMult_SeqAIJ"
84644cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
84717ab2063SBarry Smith {
848416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
84917ab2063SBarry Smith   Scalar     *x,*y,*v,sum;
850e1311b90SBarry Smith   int        ierr,m = a->m,*idx,shift = a->indexshift,*ii;
851aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
852e36a17ebSSatish Balay   int        n,i,jrow,j;
853e36a17ebSSatish Balay #endif
85417ab2063SBarry Smith 
855aa482453SBarry Smith #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
856fee21e36SBarry Smith #pragma disjoint(*x,*y,*v)
857fee21e36SBarry Smith #endif
858fee21e36SBarry Smith 
8593a40ed3dSBarry Smith   PetscFunctionBegin;
860e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
861e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
86217ab2063SBarry Smith   x    = x + shift;    /* shift for Fortran start by 1 indexing */
863416022c9SBarry Smith   idx  = a->j;
864d64ed03dSBarry Smith   v    = a->a;
865416022c9SBarry Smith   ii   = a->i;
866aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
86729b5ca8aSSatish Balay   fortranmultaij_(&m,x,ii,idx+shift,v+shift,y);
8688d195f9aSBarry Smith #else
869d64ed03dSBarry Smith   v    += shift; /* shift for Fortran start by 1 indexing */
870d64ed03dSBarry Smith   idx  += shift;
87117ab2063SBarry Smith   for (i=0; i<m; i++) {
8729ea0dfa2SSatish Balay     jrow = ii[i];
8739ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
87417ab2063SBarry Smith     sum  = 0.0;
8759ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
8769ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8779ea0dfa2SSatish Balay      }
87817ab2063SBarry Smith     y[i] = sum;
87917ab2063SBarry Smith   }
8808d195f9aSBarry Smith #endif
881416022c9SBarry Smith   PLogFlops(2*a->nz - m);
882e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
883e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8843a40ed3dSBarry Smith   PetscFunctionReturn(0);
88517ab2063SBarry Smith }
88617ab2063SBarry Smith 
8875615d1e5SSatish Balay #undef __FUNC__
888ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMultAdd_SeqAIJ"></a>*/"MatMultAdd_SeqAIJ"
88944cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
89017ab2063SBarry Smith {
891416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
89217ab2063SBarry Smith   Scalar     *x,*y,*z,*v,sum;
893e1311b90SBarry Smith   int        ierr,m = a->m,*idx,shift = a->indexshift,*ii;
894aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
895e36a17ebSSatish Balay   int        n,i,jrow,j;
896e36a17ebSSatish Balay #endif
8979ea0dfa2SSatish Balay 
8983a40ed3dSBarry Smith   PetscFunctionBegin;
899e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
900e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9012e8a6d31SBarry Smith   if (zz != yy) {
902e1311b90SBarry Smith     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9032e8a6d31SBarry Smith   } else {
9042e8a6d31SBarry Smith     z = y;
9052e8a6d31SBarry Smith   }
90617ab2063SBarry Smith   x    = x + shift; /* shift for Fortran start by 1 indexing */
907cddf8d76SBarry Smith   idx  = a->j;
908d64ed03dSBarry Smith   v    = a->a;
909cddf8d76SBarry Smith   ii   = a->i;
910aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
91129b5ca8aSSatish Balay   fortranmultaddaij_(&m,x,ii,idx+shift,v+shift,y,z);
91202ab625aSSatish Balay #else
913d64ed03dSBarry Smith   v   += shift; /* shift for Fortran start by 1 indexing */
914d64ed03dSBarry Smith   idx += shift;
91517ab2063SBarry Smith   for (i=0; i<m; i++) {
9169ea0dfa2SSatish Balay     jrow = ii[i];
9179ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
91817ab2063SBarry Smith     sum  = y[i];
9199ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
9209ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9219ea0dfa2SSatish Balay      }
92217ab2063SBarry Smith     z[i] = sum;
92317ab2063SBarry Smith   }
92402ab625aSSatish Balay #endif
925416022c9SBarry Smith   PLogFlops(2*a->nz);
926e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
927e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9282e8a6d31SBarry Smith   if (zz != yy) {
929e1311b90SBarry Smith     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
9302e8a6d31SBarry Smith   }
9313a40ed3dSBarry Smith   PetscFunctionReturn(0);
93217ab2063SBarry Smith }
93317ab2063SBarry Smith 
93417ab2063SBarry Smith /*
93517ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
93617ab2063SBarry Smith */
9375615d1e5SSatish Balay #undef __FUNC__
938ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMarkDiagonal_SeqAIJ"></a>*/"MatMarkDiagonal_SeqAIJ"
939f1e2ffcdSBarry Smith int MatMarkDiagonal_SeqAIJ(Mat A)
94017ab2063SBarry Smith {
941416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
942416022c9SBarry Smith   int        i,j,*diag,m = a->m,shift = a->indexshift;
94317ab2063SBarry Smith 
9443a40ed3dSBarry Smith   PetscFunctionBegin;
945f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
946f1e2ffcdSBarry Smith 
9470452661fSBarry Smith   diag = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(diag);
948416022c9SBarry Smith   PLogObjectMemory(A,(m+1)*sizeof(int));
949416022c9SBarry Smith   for (i=0; i<a->m; i++) {
95035b0346bSBarry Smith     diag[i] = a->i[i+1];
951416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
952416022c9SBarry Smith       if (a->j[j]+shift == i) {
95317ab2063SBarry Smith         diag[i] = j - shift;
95417ab2063SBarry Smith         break;
95517ab2063SBarry Smith       }
95617ab2063SBarry Smith     }
95717ab2063SBarry Smith   }
958416022c9SBarry Smith   a->diag = diag;
9593a40ed3dSBarry Smith   PetscFunctionReturn(0);
96017ab2063SBarry Smith }
96117ab2063SBarry Smith 
962be5855fcSBarry Smith /*
963be5855fcSBarry Smith      Checks for missing diagonals
964be5855fcSBarry Smith */
965be5855fcSBarry Smith #undef __FUNC__
966ca44d042SBarry Smith #define __FUNC__ /*<a name="MatMissingDiagonal_SeqAIJ"></a>*/"MatMissingDiagonal_SeqAIJ"
967f1e2ffcdSBarry Smith int MatMissingDiagonal_SeqAIJ(Mat A)
968be5855fcSBarry Smith {
969be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
970f1e2ffcdSBarry Smith   int        *diag,*jj = a->j,i,shift = a->indexshift,ierr;
971be5855fcSBarry Smith 
972be5855fcSBarry Smith   PetscFunctionBegin;
973f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
974f1e2ffcdSBarry Smith   diag = a->diag;
975be5855fcSBarry Smith   for (i=0; i<a->m; i++) {
976be5855fcSBarry Smith     if (jj[diag[i]+shift] != i-shift) {
977be5855fcSBarry Smith       SETERRQ1(1,1,"Matrix is missing diagonal number %d",i);
978be5855fcSBarry Smith     }
979be5855fcSBarry Smith   }
980be5855fcSBarry Smith   PetscFunctionReturn(0);
981be5855fcSBarry Smith }
982be5855fcSBarry Smith 
9835615d1e5SSatish Balay #undef __FUNC__
984ca44d042SBarry Smith #define __FUNC__ /*<a name="MatRelax_SeqAIJ"></a>*/"MatRelax_SeqAIJ"
98536db0b34SBarry Smith int MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,Vec xx)
98617ab2063SBarry Smith {
987416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
988d6ed3216SSatish Balay   Scalar     *x,*b,*bs, d,*xs,sum,*v = a->a,*t=0,scale,*ts,*xb,*idiag=0;
989d5d45c9bSBarry Smith   int        ierr,*idx,*diag,n = a->n,m = a->m,i,shift = a->indexshift;
99017ab2063SBarry Smith 
9913a40ed3dSBarry Smith   PetscFunctionBegin;
992e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
993fb2e594dSBarry Smith   if (xx != bb) {
994e1311b90SBarry Smith     ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
995fb2e594dSBarry Smith   } else {
996fb2e594dSBarry Smith     b = x;
997fb2e594dSBarry Smith   }
998fb2e594dSBarry Smith 
999f1e2ffcdSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1000416022c9SBarry Smith   diag = a->diag;
1001416022c9SBarry Smith   xs   = x + shift; /* shifted by one for index start of a or a->j*/
100217ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
100317ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
100417ab2063SBarry Smith     bs = b + shift;
100517ab2063SBarry Smith     for (i=0; i<m; i++) {
1006416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1007416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1008488ecbafSBarry Smith 	PLogFlops(2*n-1);
1009416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1010416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
101117ab2063SBarry Smith         sum  = b[i]*d/omega;
101217ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
101317ab2063SBarry Smith         x[i] = sum;
101417ab2063SBarry Smith     }
1015cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1016fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
10173a40ed3dSBarry Smith     PetscFunctionReturn(0);
101817ab2063SBarry Smith   }
1019c783ea89SBarry Smith 
1020c783ea89SBarry Smith   /* setup workspace for Eisenstat */
1021c783ea89SBarry Smith   if (flag & SOR_EISENSTAT) {
1022c783ea89SBarry Smith     if (!a->idiag) {
1023c783ea89SBarry Smith       a->idiag = (Scalar*)PetscMalloc(2*m*sizeof(Scalar));CHKPTRQ(a->idiag);
1024c783ea89SBarry Smith       a->ssor  = a->idiag + m;
1025c783ea89SBarry Smith       v        = a->a;
1026c783ea89SBarry Smith       for (i=0; i<m; i++) { a->idiag[i] = 1.0/v[diag[i]];}
1027c783ea89SBarry Smith     }
1028c783ea89SBarry Smith     t     = a->ssor;
1029c783ea89SBarry Smith     idiag = a->idiag;
1030c783ea89SBarry Smith   }
1031fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1032fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1033fc3d8934SBarry Smith 
1034fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1035fc3d8934SBarry Smith 
1036fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1037fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
103848af12d7SBarry Smith     is the relaxation factor.
1039fc3d8934SBarry Smith     */
1040fc3d8934SBarry Smith 
104148af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1042ca44d042SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"SOR_APPLY_LOWER is not implemented");
104348af12d7SBarry Smith   } else if ((flag & SOR_EISENSTAT) && omega == 1.0 && shift == 0 && fshift == 0.0) {
104448af12d7SBarry Smith     /* special case for omega = 1.0 saves flops and some integer ops */
1045733d66baSBarry Smith     Scalar *v2;
104648af12d7SBarry Smith 
1047733d66baSBarry Smith     v2    = a->a;
1048fc3d8934SBarry Smith     /*  x = (E + U)^{-1} b */
1049fc3d8934SBarry Smith     for (i=m-1; i>=0; i--) {
1050fc3d8934SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1051fc3d8934SBarry Smith       idx  = a->j + diag[i] + 1;
1052fc3d8934SBarry Smith       v    = a->a + diag[i] + 1;
1053fc3d8934SBarry Smith       sum  = b[i];
1054fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1055b4632166SBarry Smith       x[i] = sum*idiag[i];
1056fc3d8934SBarry Smith 
1057fc3d8934SBarry Smith       /*  t = b - (2*E - D)x */
1058733d66baSBarry Smith       t[i] = b[i] - (v2[diag[i]])*x[i];
1059733d66baSBarry Smith     }
1060fc3d8934SBarry Smith 
1061fc3d8934SBarry Smith     /*  t = (E + L)^{-1}t */
1062fc3d8934SBarry Smith     diag = a->diag;
1063fc3d8934SBarry Smith     for (i=0; i<m; i++) {
1064fc3d8934SBarry Smith       n    = diag[i] - a->i[i];
1065fc3d8934SBarry Smith       idx  = a->j + a->i[i];
1066fc3d8934SBarry Smith       v    = a->a + a->i[i];
1067fc3d8934SBarry Smith       sum  = t[i];
1068fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,t,v,idx,n);
1069b4632166SBarry Smith       t[i]  = sum*idiag[i];
1070fc3d8934SBarry Smith 
1071fc3d8934SBarry Smith       /*  x = x + t */
1072733d66baSBarry Smith       x[i] += t[i];
1073733d66baSBarry Smith     }
1074733d66baSBarry Smith 
1075a4d9cbf6SBarry Smith     PLogFlops(3*m-1 + 2*a->nz);
1076fc3d8934SBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1077fc3d8934SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
1078fc3d8934SBarry Smith     PetscFunctionReturn(0);
10793a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
108017ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
108117ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
108217ab2063SBarry Smith 
108317ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
108417ab2063SBarry Smith 
108517ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
108617ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
108717ab2063SBarry Smith     is the relaxation factor.
108817ab2063SBarry Smith     */
108917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
109017ab2063SBarry Smith 
109117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
109217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1093416022c9SBarry Smith       d    = fshift + a->a[diag[i] + shift];
1094416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1095416022c9SBarry Smith       idx  = a->j + diag[i] + (!shift);
1096416022c9SBarry Smith       v    = a->a + diag[i] + (!shift);
109717ab2063SBarry Smith       sum  = b[i];
109817ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
109917ab2063SBarry Smith       x[i] = omega*(sum/d);
110017ab2063SBarry Smith     }
110117ab2063SBarry Smith 
110217ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1103416022c9SBarry Smith     v = a->a;
110417ab2063SBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; }
110517ab2063SBarry Smith 
110617ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1107416022c9SBarry Smith     ts = t + shift; /* shifted by one for index start of a or a->j*/
1108416022c9SBarry Smith     diag = a->diag;
110917ab2063SBarry Smith     for (i=0; i<m; i++) {
1110416022c9SBarry Smith       d    = fshift + a->a[diag[i]+shift];
1111416022c9SBarry Smith       n    = diag[i] - a->i[i];
1112416022c9SBarry Smith       idx  = a->j + a->i[i] + shift;
1113416022c9SBarry Smith       v    = a->a + a->i[i] + shift;
111417ab2063SBarry Smith       sum  = t[i];
111517ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
111617ab2063SBarry Smith       t[i] = omega*(sum/d);
1117733d66baSBarry Smith       /*  x = x + t */
1118733d66baSBarry Smith       x[i] += t[i];
111917ab2063SBarry Smith     }
112017ab2063SBarry Smith 
1121c783ea89SBarry Smith     PLogFlops(6*m-1 + 2*a->nz);
1122cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1123fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
11243a40ed3dSBarry Smith     PetscFunctionReturn(0);
112517ab2063SBarry Smith   }
112617ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
112717ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
112817ab2063SBarry Smith       for (i=0; i<m; i++) {
1129416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1130416022c9SBarry Smith         n    = diag[i] - a->i[i];
1131488ecbafSBarry Smith 	PLogFlops(2*n-1);
1132416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1133416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
113417ab2063SBarry Smith         sum  = b[i];
113517ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
113617ab2063SBarry Smith         x[i] = omega*(sum/d);
113717ab2063SBarry Smith       }
113817ab2063SBarry Smith       xb = x;
11393a40ed3dSBarry Smith     } else xb = b;
114017ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
114117ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
114217ab2063SBarry Smith       for (i=0; i<m; i++) {
1143416022c9SBarry Smith         x[i] *= a->a[diag[i]+shift];
114417ab2063SBarry Smith       }
1145488ecbafSBarry Smith       PLogFlops(m);
114617ab2063SBarry Smith     }
114717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
114817ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1149416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1150416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1151488ecbafSBarry Smith 	PLogFlops(2*n-1);
1152416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1153416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
115417ab2063SBarry Smith         sum  = xb[i];
115517ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
115617ab2063SBarry Smith         x[i] = omega*(sum/d);
115717ab2063SBarry Smith       }
115817ab2063SBarry Smith     }
115917ab2063SBarry Smith     its--;
116017ab2063SBarry Smith   }
116117ab2063SBarry Smith   while (its--) {
116217ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
116317ab2063SBarry Smith       for (i=0; i<m; i++) {
1164416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1165416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1166488ecbafSBarry Smith 	PLogFlops(2*n-1);
1167416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1168416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
116917ab2063SBarry Smith         sum  = b[i];
117017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
11717e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
117217ab2063SBarry Smith       }
117317ab2063SBarry Smith     }
117417ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
117517ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1176416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1177416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1178488ecbafSBarry Smith 	PLogFlops(2*n-1);
1179416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1180416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
118117ab2063SBarry Smith         sum  = b[i];
118217ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
11837e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
118417ab2063SBarry Smith       }
118517ab2063SBarry Smith     }
118617ab2063SBarry Smith   }
1187cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1188fb2e594dSBarry Smith   if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
11893a40ed3dSBarry Smith   PetscFunctionReturn(0);
119017ab2063SBarry Smith }
119117ab2063SBarry Smith 
11925615d1e5SSatish Balay #undef __FUNC__
1193ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetInfo_SeqAIJ"></a>*/"MatGetInfo_SeqAIJ"
11948f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
119517ab2063SBarry Smith {
1196416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
11974e220ebcSLois Curfman McInnes 
11983a40ed3dSBarry Smith   PetscFunctionBegin;
11994e220ebcSLois Curfman McInnes   info->rows_global    = (double)a->m;
12004e220ebcSLois Curfman McInnes   info->columns_global = (double)a->n;
12014e220ebcSLois Curfman McInnes   info->rows_local     = (double)a->m;
12024e220ebcSLois Curfman McInnes   info->columns_local  = (double)a->n;
12034e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12044e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12054e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12064e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12074e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12084e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12094e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12104e220ebcSLois Curfman McInnes   if (A->factor) {
12114e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12124e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12134e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12144e220ebcSLois Curfman McInnes   } else {
12154e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12164e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12174e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12184e220ebcSLois Curfman McInnes   }
12193a40ed3dSBarry Smith   PetscFunctionReturn(0);
122017ab2063SBarry Smith }
122117ab2063SBarry Smith 
1222ca44d042SBarry Smith EXTERN int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,PetscReal,Mat*);
1223ca44d042SBarry Smith EXTERN int MatLUFactorNumeric_SeqAIJ(Mat,Mat*);
1224ca44d042SBarry Smith EXTERN int MatLUFactor_SeqAIJ(Mat,IS,IS,PetscReal);
1225ca44d042SBarry Smith EXTERN int MatSolve_SeqAIJ(Mat,Vec,Vec);
1226ca44d042SBarry Smith EXTERN int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
1227ca44d042SBarry Smith EXTERN int MatSolveTranspose_SeqAIJ(Mat,Vec,Vec);
1228ca44d042SBarry Smith EXTERN int MatSolveTransposeAdd_SeqAIJ(Mat,Vec,Vec,Vec);
122917ab2063SBarry Smith 
12305615d1e5SSatish Balay #undef __FUNC__
1231e7592fafSBarry Smith #define __FUNC__ /*<a name="MatZeroRows_SeqAIJ"></a>*/"MatZeroRows_SeqAIJ"
12328f6be9afSLois Curfman McInnes int MatZeroRows_SeqAIJ(Mat A,IS is,Scalar *diag)
123317ab2063SBarry Smith {
1234416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1235416022c9SBarry Smith   int         i,ierr,N,*rows,m = a->m - 1,shift = a->indexshift;
123617ab2063SBarry Smith 
12373a40ed3dSBarry Smith   PetscFunctionBegin;
123877c4ece6SBarry Smith   ierr = ISGetSize(is,&N);CHKERRQ(ierr);
123917ab2063SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
1240f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1241f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1242f1e2ffcdSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range");
1243f1e2ffcdSBarry Smith       ierr = PetscMemzero(&a->a[a->i[rows[i]]+shift],a->ilen[rows[i]]*sizeof(Scalar));CHKERRQ(ierr);
1244f1e2ffcdSBarry Smith     }
1245f1e2ffcdSBarry Smith     if (diag) {
1246f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1247f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1248f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1249f1e2ffcdSBarry Smith         a->a[a->diag[rows[i]]] = *diag;
1250f1e2ffcdSBarry Smith       }
1251f1e2ffcdSBarry Smith     }
1252f1e2ffcdSBarry Smith   } else {
125317ab2063SBarry Smith     if (diag) {
125417ab2063SBarry Smith       for (i=0; i<N; i++) {
1255a8c6a408SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range");
12567ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1257416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1258416022c9SBarry Smith           a->a[a->i[rows[i]]+shift] = *diag;
1259416022c9SBarry Smith           a->j[a->i[rows[i]]+shift] = rows[i]+shift;
12607ae801bdSBarry Smith         } else { /* in case row was completely empty */
1261d64ed03dSBarry Smith           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
126217ab2063SBarry Smith         }
126317ab2063SBarry Smith       }
12643a40ed3dSBarry Smith     } else {
126517ab2063SBarry Smith       for (i=0; i<N; i++) {
1266a8c6a408SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range");
1267416022c9SBarry Smith         a->ilen[rows[i]] = 0;
126817ab2063SBarry Smith       }
126917ab2063SBarry Smith     }
1270f1e2ffcdSBarry Smith   }
12717ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
127243a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12733a40ed3dSBarry Smith   PetscFunctionReturn(0);
127417ab2063SBarry Smith }
127517ab2063SBarry Smith 
12765615d1e5SSatish Balay #undef __FUNC__
1277ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetSize_SeqAIJ"></a>*/"MatGetSize_SeqAIJ"
12788f6be9afSLois Curfman McInnes int MatGetSize_SeqAIJ(Mat A,int *m,int *n)
127917ab2063SBarry Smith {
1280416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12813a40ed3dSBarry Smith 
12823a40ed3dSBarry Smith   PetscFunctionBegin;
12830752156aSBarry Smith   if (m) *m = a->m;
12840752156aSBarry Smith   if (n) *n = a->n;
12853a40ed3dSBarry Smith   PetscFunctionReturn(0);
128617ab2063SBarry Smith }
128717ab2063SBarry Smith 
12885615d1e5SSatish Balay #undef __FUNC__
1289ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetOwnershipRange_SeqAIJ"></a>*/"MatGetOwnershipRange_SeqAIJ"
12908f6be9afSLois Curfman McInnes int MatGetOwnershipRange_SeqAIJ(Mat A,int *m,int *n)
129117ab2063SBarry Smith {
1292416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12933a40ed3dSBarry Smith 
12943a40ed3dSBarry Smith   PetscFunctionBegin;
12954c49b128SBarry Smith   if (m) *m = 0;
12964c49b128SBarry Smith   if (n) *n = a->m;
12973a40ed3dSBarry Smith   PetscFunctionReturn(0);
129817ab2063SBarry Smith }
1299026e39d0SSatish Balay 
13005615d1e5SSatish Balay #undef __FUNC__
1301ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetRow_SeqAIJ"></a>*/"MatGetRow_SeqAIJ"
13024e093b46SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
130317ab2063SBarry Smith {
1304416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1305c456f294SBarry Smith   int        *itmp,i,shift = a->indexshift;
130617ab2063SBarry Smith 
13073a40ed3dSBarry Smith   PetscFunctionBegin;
1308ca44d042SBarry Smith   if (row < 0 || row >= a->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Row %d out of range",row);
130917ab2063SBarry Smith 
1310416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1311416022c9SBarry Smith   if (v) *v = a->a + a->i[row] + shift;
131217ab2063SBarry Smith   if (idx) {
1313416022c9SBarry Smith     itmp = a->j + a->i[row] + shift;
13144e093b46SBarry Smith     if (*nz && shift) {
13150452661fSBarry Smith       *idx = (int*)PetscMalloc((*nz)*sizeof(int));CHKPTRQ(*idx);
131617ab2063SBarry Smith       for (i=0; i<(*nz); i++) {(*idx)[i] = itmp[i] + shift;}
13174e093b46SBarry Smith     } else if (*nz) {
13184e093b46SBarry Smith       *idx = itmp;
131917ab2063SBarry Smith     }
132017ab2063SBarry Smith     else *idx = 0;
132117ab2063SBarry Smith   }
13223a40ed3dSBarry Smith   PetscFunctionReturn(0);
132317ab2063SBarry Smith }
132417ab2063SBarry Smith 
13255615d1e5SSatish Balay #undef __FUNC__
1326ca44d042SBarry Smith #define __FUNC__ /*<a name="MatRestoreRow_SeqAIJ"></a>*/"MatRestoreRow_SeqAIJ"
13274e093b46SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
132817ab2063SBarry Smith {
13294e093b46SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1330606d414cSSatish Balay   int ierr;
13313a40ed3dSBarry Smith 
13323a40ed3dSBarry Smith   PetscFunctionBegin;
1333606d414cSSatish Balay   if (idx) {if (*idx && a->indexshift) {ierr = PetscFree(*idx);CHKERRQ(ierr);}}
13343a40ed3dSBarry Smith   PetscFunctionReturn(0);
133517ab2063SBarry Smith }
133617ab2063SBarry Smith 
13375615d1e5SSatish Balay #undef __FUNC__
1338ca44d042SBarry Smith #define __FUNC__ /*<a name="MatNorm_SeqAIJ"></a>*/"MatNorm_SeqAIJ"
133936db0b34SBarry Smith int MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *norm)
134017ab2063SBarry Smith {
1341416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1342416022c9SBarry Smith   Scalar     *v = a->a;
134336db0b34SBarry Smith   PetscReal  sum = 0.0;
1344549d3d68SSatish Balay   int        i,j,shift = a->indexshift,ierr;
134517ab2063SBarry Smith 
13463a40ed3dSBarry Smith   PetscFunctionBegin;
134717ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1348416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1349aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
135036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
135117ab2063SBarry Smith #else
135217ab2063SBarry Smith       sum += (*v)*(*v); v++;
135317ab2063SBarry Smith #endif
135417ab2063SBarry Smith     }
135517ab2063SBarry Smith     *norm = sqrt(sum);
13563a40ed3dSBarry Smith   } else if (type == NORM_1) {
135736db0b34SBarry Smith     PetscReal *tmp;
1358416022c9SBarry Smith     int    *jj = a->j;
135936db0b34SBarry Smith     tmp = (PetscReal*)PetscMalloc((a->n+1)*sizeof(PetscReal));CHKPTRQ(tmp);
136036db0b34SBarry Smith     ierr = PetscMemzero(tmp,a->n*sizeof(PetscReal));CHKERRQ(ierr);
136117ab2063SBarry Smith     *norm = 0.0;
1362416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1363a2744918SBarry Smith         tmp[*jj++ + shift] += PetscAbsScalar(*v);  v++;
136417ab2063SBarry Smith     }
1365416022c9SBarry Smith     for (j=0; j<a->n; j++) {
136617ab2063SBarry Smith       if (tmp[j] > *norm) *norm = tmp[j];
136717ab2063SBarry Smith     }
1368606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13693a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
137017ab2063SBarry Smith     *norm = 0.0;
1371416022c9SBarry Smith     for (j=0; j<a->m; j++) {
1372416022c9SBarry Smith       v = a->a + a->i[j] + shift;
137317ab2063SBarry Smith       sum = 0.0;
1374416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1375cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
137617ab2063SBarry Smith       }
137717ab2063SBarry Smith       if (sum > *norm) *norm = sum;
137817ab2063SBarry Smith     }
13793a40ed3dSBarry Smith   } else {
1380a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"No support for two norm");
138117ab2063SBarry Smith   }
13823a40ed3dSBarry Smith   PetscFunctionReturn(0);
138317ab2063SBarry Smith }
138417ab2063SBarry Smith 
13855615d1e5SSatish Balay #undef __FUNC__
1386ca44d042SBarry Smith #define __FUNC__ /*<a name="MatTranspose_SeqAIJ"></a>*/"MatTranspose_SeqAIJ"
13878f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
138817ab2063SBarry Smith {
1389416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1390416022c9SBarry Smith   Mat        C;
1391416022c9SBarry Smith   int        i,ierr,*aj = a->j,*ai = a->i,m = a->m,len,*col;
139236db0b34SBarry Smith   int        shift = a->indexshift,refct;
1393d5d45c9bSBarry Smith   Scalar     *array = a->a;
139417ab2063SBarry Smith 
13953a40ed3dSBarry Smith   PetscFunctionBegin;
1396f1e2ffcdSBarry Smith   if (!B && m != a->n) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Square matrix only for in-place");
13970452661fSBarry Smith   col  = (int*)PetscMalloc((1+a->n)*sizeof(int));CHKPTRQ(col);
1398549d3d68SSatish Balay   ierr = PetscMemzero(col,(1+a->n)*sizeof(int));CHKERRQ(ierr);
139917ab2063SBarry Smith   if (shift) {
140017ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] -= 1;
140117ab2063SBarry Smith   }
140217ab2063SBarry Smith   for (i=0; i<ai[m]+shift; i++) col[aj[i]] += 1;
1403416022c9SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,a->n,m,0,col,&C);CHKERRQ(ierr);
1404606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
140517ab2063SBarry Smith   for (i=0; i<m; i++) {
140617ab2063SBarry Smith     len = ai[i+1]-ai[i];
1407416022c9SBarry Smith     ierr = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
140817ab2063SBarry Smith     array += len; aj += len;
140917ab2063SBarry Smith   }
141017ab2063SBarry Smith   if (shift) {
141117ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] += 1;
141217ab2063SBarry Smith   }
141317ab2063SBarry Smith 
14146d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14156d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
141617ab2063SBarry Smith 
1417f1e2ffcdSBarry Smith   if (B) {
1418416022c9SBarry Smith     *B = C;
141917ab2063SBarry Smith   } else {
1420f830108cSBarry Smith     PetscOps *Abops;
14210a6ffc59SBarry Smith     MatOps   Aops;
1422f830108cSBarry Smith 
1423416022c9SBarry Smith     /* This isn't really an in-place transpose */
1424606d414cSSatish Balay     ierr = PetscFree(a->a);CHKERRQ(ierr);
1425606d414cSSatish Balay     if (!a->singlemalloc) {
1426606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
1427606d414cSSatish Balay       ierr = PetscFree(a->j);
1428606d414cSSatish Balay     }
1429606d414cSSatish Balay     if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
1430606d414cSSatish Balay     if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);}
1431606d414cSSatish Balay     if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);}
1432606d414cSSatish Balay     if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
1433606d414cSSatish Balay     if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
1434606d414cSSatish Balay     ierr = PetscFree(a);CHKERRQ(ierr);
1435f830108cSBarry Smith 
1436488ecbafSBarry Smith 
1437488ecbafSBarry Smith     ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
1438488ecbafSBarry Smith     ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
1439488ecbafSBarry Smith 
1440f830108cSBarry Smith     /*
1441f830108cSBarry Smith       This is horrible, horrible code. We need to keep the
14428f0f457cSSatish Balay       the bops and ops Structures, copy everything from C
14438f0f457cSSatish Balay       including the function pointers..
1444f830108cSBarry Smith     */
1445549d3d68SSatish Balay     ierr     = PetscMemcpy(A->ops,C->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
1446549d3d68SSatish Balay     ierr     = PetscMemcpy(A->bops,C->bops,sizeof(PetscOps));CHKERRQ(ierr);
1447f830108cSBarry Smith     Abops    = A->bops;
1448f830108cSBarry Smith     Aops     = A->ops;
144936db0b34SBarry Smith     refct    = A->refct;
1450549d3d68SSatish Balay     ierr     = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
1451f830108cSBarry Smith     A->bops  = Abops;
1452f830108cSBarry Smith     A->ops   = Aops;
145327a8da17SBarry Smith     A->qlist = 0;
145436db0b34SBarry Smith     A->refct = refct;
145536db0b34SBarry Smith     /* copy over the type_name and name */
145636db0b34SBarry Smith     ierr     = PetscStrallocpy(C->type_name,&A->type_name);CHKERRQ(ierr);
145736db0b34SBarry Smith     ierr     = PetscStrallocpy(C->name,&A->name);CHKERRQ(ierr);
1458f830108cSBarry Smith 
14590452661fSBarry Smith     PetscHeaderDestroy(C);
146017ab2063SBarry Smith   }
14613a40ed3dSBarry Smith   PetscFunctionReturn(0);
146217ab2063SBarry Smith }
146317ab2063SBarry Smith 
14645615d1e5SSatish Balay #undef __FUNC__
1465ca44d042SBarry Smith #define __FUNC__ /*<a name="MatDiagonalScale_SeqAIJ"></a>*/"MatDiagonalScale_SeqAIJ"
14668f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
146717ab2063SBarry Smith {
1468416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
146917ab2063SBarry Smith   Scalar     *l,*r,x,*v;
1470e1311b90SBarry Smith   int        ierr,i,j,m = a->m,n = a->n,M,nz = a->nz,*jj,shift = a->indexshift;
147117ab2063SBarry Smith 
14723a40ed3dSBarry Smith   PetscFunctionBegin;
147317ab2063SBarry Smith   if (ll) {
14743ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14753ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1476e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1477a8c6a408SBarry Smith     if (m != a->m) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Left scaling vector wrong length");
1478e1311b90SBarry Smith     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1479416022c9SBarry Smith     v = a->a;
148017ab2063SBarry Smith     for (i=0; i<m; i++) {
148117ab2063SBarry Smith       x = l[i];
1482416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
148317ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
148417ab2063SBarry Smith     }
1485e1311b90SBarry Smith     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
148644cd7ae7SLois Curfman McInnes     PLogFlops(nz);
148717ab2063SBarry Smith   }
148817ab2063SBarry Smith   if (rr) {
1489e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1490a8c6a408SBarry Smith     if (n != a->n) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Right scaling vector wrong length");
1491e1311b90SBarry Smith     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1492416022c9SBarry Smith     v = a->a; jj = a->j;
149317ab2063SBarry Smith     for (i=0; i<nz; i++) {
149417ab2063SBarry Smith       (*v++) *= r[*jj++ + shift];
149517ab2063SBarry Smith     }
1496e1311b90SBarry Smith     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
149744cd7ae7SLois Curfman McInnes     PLogFlops(nz);
149817ab2063SBarry Smith   }
14993a40ed3dSBarry Smith   PetscFunctionReturn(0);
150017ab2063SBarry Smith }
150117ab2063SBarry Smith 
15025615d1e5SSatish Balay #undef __FUNC__
1503ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetSubMatrix_SeqAIJ"></a>*/"MatGetSubMatrix_SeqAIJ"
15047b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B)
150517ab2063SBarry Smith {
1506db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data,*c;
1507d5db1b72SBarry Smith   int          *smap,i,k,kstart,kend,ierr,oldcols = a->n,*lens;
150836db0b34SBarry Smith   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
150999141d43SSatish Balay   int          *irow,*icol,nrows,ncols,shift = a->indexshift,*ssmap;
151099141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
151199141d43SSatish Balay   Scalar       *a_new,*mat_a;
1512416022c9SBarry Smith   Mat          C;
1513fee21e36SBarry Smith   PetscTruth   stride;
151417ab2063SBarry Smith 
15153a40ed3dSBarry Smith   PetscFunctionBegin;
1516d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
1517a8c6a408SBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"ISrow is not sorted");
1518d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
1519a8c6a408SBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"IScol is not sorted");
152099141d43SSatish Balay 
152117ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
152217ab2063SBarry Smith   ierr = ISGetSize(isrow,&nrows);CHKERRQ(ierr);
152317ab2063SBarry Smith   ierr = ISGetSize(iscol,&ncols);CHKERRQ(ierr);
152417ab2063SBarry Smith 
1525fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1526fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1527fee21e36SBarry Smith   if (stride && step == 1) {
152802834360SBarry Smith     /* special case of contiguous rows */
152957faeb66SBarry Smith     lens   = (int*)PetscMalloc((ncols+nrows+1)*sizeof(int));CHKPTRQ(lens);
153002834360SBarry Smith     starts = lens + ncols;
153102834360SBarry Smith     /* loop over new rows determining lens and starting points */
153202834360SBarry Smith     for (i=0; i<nrows; i++) {
1533a2744918SBarry Smith       kstart  = ai[irow[i]]+shift;
1534a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
153502834360SBarry Smith       for (k=kstart; k<kend; k++) {
1536d8ced48eSBarry Smith         if (aj[k]+shift >= first) {
153702834360SBarry Smith           starts[i] = k;
153802834360SBarry Smith           break;
153902834360SBarry Smith 	}
154002834360SBarry Smith       }
1541a2744918SBarry Smith       sum = 0;
154202834360SBarry Smith       while (k < kend) {
1543d8ced48eSBarry Smith         if (aj[k++]+shift >= first+ncols) break;
1544a2744918SBarry Smith         sum++;
154502834360SBarry Smith       }
1546a2744918SBarry Smith       lens[i] = sum;
154702834360SBarry Smith     }
154802834360SBarry Smith     /* create submatrix */
1549cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
155008480c60SBarry Smith       int n_cols,n_rows;
155108480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
1552a8c6a408SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Reused submatrix wrong size");
1553d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
155408480c60SBarry Smith       C = *B;
15553a40ed3dSBarry Smith     } else {
155602834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
155708480c60SBarry Smith     }
1558db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1559db02288aSLois Curfman McInnes 
156002834360SBarry Smith     /* loop over rows inserting into submatrix */
1561db02288aSLois Curfman McInnes     a_new    = c->a;
1562db02288aSLois Curfman McInnes     j_new    = c->j;
1563db02288aSLois Curfman McInnes     i_new    = c->i;
1564db02288aSLois Curfman McInnes     i_new[0] = -shift;
156502834360SBarry Smith     for (i=0; i<nrows; i++) {
1566a2744918SBarry Smith       ii    = starts[i];
1567a2744918SBarry Smith       lensi = lens[i];
1568a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1569a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
157002834360SBarry Smith       }
1571549d3d68SSatish Balay       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(Scalar));CHKERRQ(ierr);
1572a2744918SBarry Smith       a_new      += lensi;
1573a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1574a2744918SBarry Smith       c->ilen[i]  = lensi;
157502834360SBarry Smith     }
1576606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15773a40ed3dSBarry Smith   } else {
157802834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
15790452661fSBarry Smith     smap  = (int*)PetscMalloc((1+oldcols)*sizeof(int));CHKPTRQ(smap);
158002834360SBarry Smith     ssmap = smap + shift;
158199141d43SSatish Balay     lens  = (int*)PetscMalloc((1+nrows)*sizeof(int));CHKPTRQ(lens);
1582549d3d68SSatish Balay     ierr  = PetscMemzero(smap,oldcols*sizeof(int));CHKERRQ(ierr);
158317ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
158402834360SBarry Smith     /* determine lens of each row */
158502834360SBarry Smith     for (i=0; i<nrows; i++) {
1586d8ced48eSBarry Smith       kstart  = ai[irow[i]]+shift;
158702834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
158802834360SBarry Smith       lens[i] = 0;
158902834360SBarry Smith       for (k=kstart; k<kend; k++) {
1590d8ced48eSBarry Smith         if (ssmap[aj[k]]) {
159102834360SBarry Smith           lens[i]++;
159202834360SBarry Smith         }
159302834360SBarry Smith       }
159402834360SBarry Smith     }
159517ab2063SBarry Smith     /* Create and fill new matrix */
1596a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
15970f5bd95cSBarry Smith       PetscTruth equal;
15980f5bd95cSBarry Smith 
159999141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1600a8c6a408SBarry Smith       if (c->m  != nrows || c->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong size");
16010f5bd95cSBarry Smith       ierr = PetscMemcmp(c->ilen,lens,c->m*sizeof(int),&equal);CHKERRQ(ierr);
16020f5bd95cSBarry Smith       if (!equal) {
1603a8c6a408SBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong no of nonzeros");
160499141d43SSatish Balay       }
1605549d3d68SSatish Balay       ierr = PetscMemzero(c->ilen,c->m*sizeof(int));CHKERRQ(ierr);
160608480c60SBarry Smith       C = *B;
16073a40ed3dSBarry Smith     } else {
160802834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
160908480c60SBarry Smith     }
161099141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
161117ab2063SBarry Smith     for (i=0; i<nrows; i++) {
161299141d43SSatish Balay       row    = irow[i];
161399141d43SSatish Balay       kstart = ai[row]+shift;
161499141d43SSatish Balay       kend   = kstart + a->ilen[row];
161599141d43SSatish Balay       mat_i  = c->i[i]+shift;
161699141d43SSatish Balay       mat_j  = c->j + mat_i;
161799141d43SSatish Balay       mat_a  = c->a + mat_i;
161899141d43SSatish Balay       mat_ilen = c->ilen + i;
161917ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
162099141d43SSatish Balay         if ((tcol=ssmap[a->j[k]])) {
162199141d43SSatish Balay           *mat_j++ = tcol - (!shift);
162299141d43SSatish Balay           *mat_a++ = a->a[k];
162399141d43SSatish Balay           (*mat_ilen)++;
162499141d43SSatish Balay 
162517ab2063SBarry Smith         }
162617ab2063SBarry Smith       }
162717ab2063SBarry Smith     }
162802834360SBarry Smith     /* Free work space */
162902834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1630606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1631606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
163202834360SBarry Smith   }
16336d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16346d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
163517ab2063SBarry Smith 
163617ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1637416022c9SBarry Smith   *B = C;
16383a40ed3dSBarry Smith   PetscFunctionReturn(0);
163917ab2063SBarry Smith }
164017ab2063SBarry Smith 
1641a871dcd8SBarry Smith /*
1642a871dcd8SBarry Smith */
16435615d1e5SSatish Balay #undef __FUNC__
1644ca44d042SBarry Smith #define __FUNC__ /*<a name="MatILUFactor_SeqAIJ"></a>*/"MatILUFactor_SeqAIJ"
16455ef9f2a5SBarry Smith int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatILUInfo *info)
1646a871dcd8SBarry Smith {
164763b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
164808480c60SBarry Smith   int        ierr;
164963b91edcSBarry Smith   Mat        outA;
1650b8a78c4aSBarry Smith   PetscTruth row_identity,col_identity;
165163b91edcSBarry Smith 
16523a40ed3dSBarry Smith   PetscFunctionBegin;
1653b8a78c4aSBarry Smith   if (info && info->levels != 0) SETERRQ(PETSC_ERR_SUP,0,"Only levels=0 supported for in-place ilu");
1654b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1655b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1656b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
1657b8a78c4aSBarry Smith     SETERRQ(1,1,"Row and column permutations must be identity for in-place ILU");
1658b8a78c4aSBarry Smith   }
1659a871dcd8SBarry Smith 
166063b91edcSBarry Smith   outA          = inA;
166163b91edcSBarry Smith   inA->factor   = FACTOR_LU;
166263b91edcSBarry Smith   a->row        = row;
166363b91edcSBarry Smith   a->col        = col;
1664c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1665c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
166663b91edcSBarry Smith 
166736db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1668c38d4ed2SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* if this came from a previous factored; need to remove old one */
16694c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
16701e4dd532SSatish Balay   PLogObjectParent(inA,a->icol);
1671f0ec6fceSSatish Balay 
167294a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
16730452661fSBarry Smith     a->solve_work = (Scalar*)PetscMalloc((a->m+1)*sizeof(Scalar));CHKPTRQ(a->solve_work);
167494a9d846SBarry Smith   }
167563b91edcSBarry Smith 
167608480c60SBarry Smith   if (!a->diag) {
1677f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
167863b91edcSBarry Smith   }
167963b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA);CHKERRQ(ierr);
16803a40ed3dSBarry Smith   PetscFunctionReturn(0);
1681a871dcd8SBarry Smith }
1682a871dcd8SBarry Smith 
168374cdf0dfSBarry Smith #include "pinclude/blaslapack.h"
16845615d1e5SSatish Balay #undef __FUNC__
1685ca44d042SBarry Smith #define __FUNC__ /*<a name="MatScale_SeqAIJ"></a>*/"MatScale_SeqAIJ"
16868f6be9afSLois Curfman McInnes int MatScale_SeqAIJ(Scalar *alpha,Mat inA)
1687f0b747eeSBarry Smith {
1688f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
1689f0b747eeSBarry Smith   int        one = 1;
16903a40ed3dSBarry Smith 
16913a40ed3dSBarry Smith   PetscFunctionBegin;
1692f0b747eeSBarry Smith   BLscal_(&a->nz,alpha,a->a,&one);
1693f0b747eeSBarry Smith   PLogFlops(a->nz);
16943a40ed3dSBarry Smith   PetscFunctionReturn(0);
1695f0b747eeSBarry Smith }
1696f0b747eeSBarry Smith 
16975615d1e5SSatish Balay #undef __FUNC__
1698ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetSubMatrices_SeqAIJ"></a>*/"MatGetSubMatrices_SeqAIJ"
16997b2a1423SBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n,IS *irow,IS *icol,MatReuse scall,Mat **B)
1700cddf8d76SBarry Smith {
1701cddf8d76SBarry Smith   int ierr,i;
1702cddf8d76SBarry Smith 
17033a40ed3dSBarry Smith   PetscFunctionBegin;
1704cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
17050452661fSBarry Smith     *B = (Mat*)PetscMalloc((n+1)*sizeof(Mat));CHKPTRQ(*B);
1706cddf8d76SBarry Smith   }
1707cddf8d76SBarry Smith 
1708cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17096a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1710cddf8d76SBarry Smith   }
17113a40ed3dSBarry Smith   PetscFunctionReturn(0);
1712cddf8d76SBarry Smith }
1713cddf8d76SBarry Smith 
17145615d1e5SSatish Balay #undef __FUNC__
1715ca44d042SBarry Smith #define __FUNC__ /*<a name="MatGetBlockSize_SeqAIJ"></a>*/"MatGetBlockSize_SeqAIJ"
17168f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A,int *bs)
17175a838052SSatish Balay {
1718f830108cSBarry Smith   PetscFunctionBegin;
17195a838052SSatish Balay   *bs = 1;
17203a40ed3dSBarry Smith   PetscFunctionReturn(0);
17215a838052SSatish Balay }
17225a838052SSatish Balay 
17235615d1e5SSatish Balay #undef __FUNC__
1724ca44d042SBarry Smith #define __FUNC__ /*<a name="MatIncreaseOverlap_SeqAIJ"></a>*/"MatIncreaseOverlap_SeqAIJ"
17258f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A,int is_max,IS *is,int ov)
17264dcbc457SBarry Smith {
1727e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
172806763907SSatish Balay   int        shift,row,i,j,k,l,m,n,*idx,ierr,*nidx,isz,val;
17298a047759SSatish Balay   int        start,end,*ai,*aj;
1730f1af5d2fSBarry Smith   PetscBT    table;
1731bbd702dbSSatish Balay 
17323a40ed3dSBarry Smith   PetscFunctionBegin;
17338a047759SSatish Balay   shift = a->indexshift;
1734e4d965acSSatish Balay   m     = a->m;
1735e4d965acSSatish Balay   ai    = a->i;
17368a047759SSatish Balay   aj    = a->j+shift;
17378a047759SSatish Balay 
1738a8c6a408SBarry Smith   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"illegal overlap value used");
173906763907SSatish Balay 
174006763907SSatish Balay   nidx  = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(nidx);
17416831982aSBarry Smith   ierr  = PetscBTCreate(m,table);CHKERRQ(ierr);
174206763907SSatish Balay 
1743e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1744b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1745e4d965acSSatish Balay     isz  = 0;
17466831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1747e4d965acSSatish Balay 
1748e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17494dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
175077c4ece6SBarry Smith     ierr = ISGetSize(is[i],&n);CHKERRQ(ierr);
1751e4d965acSSatish Balay 
1752dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1753e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1754f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
17554dcbc457SBarry Smith     }
175606763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
175706763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1758e4d965acSSatish Balay 
175904a348a9SBarry Smith     k = 0;
176004a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
176104a348a9SBarry Smith       n = isz;
176206763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1763e4d965acSSatish Balay         row   = nidx[k];
1764e4d965acSSatish Balay         start = ai[row];
1765e4d965acSSatish Balay         end   = ai[row+1];
176604a348a9SBarry Smith         for (l = start; l<end ; l++){
17678a047759SSatish Balay           val = aj[l] + shift;
1768f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1769e4d965acSSatish Balay         }
1770e4d965acSSatish Balay       }
1771e4d965acSSatish Balay     }
1772029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1773e4d965acSSatish Balay   }
17746831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1775606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17763a40ed3dSBarry Smith   PetscFunctionReturn(0);
17774dcbc457SBarry Smith }
177817ab2063SBarry Smith 
17790513a670SBarry Smith /* -------------------------------------------------------------- */
17800513a670SBarry Smith #undef __FUNC__
1781ca44d042SBarry Smith #define __FUNC__ /*<a name="MatPermute_SeqAIJ"></a>*/"MatPermute_SeqAIJ"
17820513a670SBarry Smith int MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
17830513a670SBarry Smith {
17840513a670SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
17850513a670SBarry Smith   Scalar     *vwork;
17860513a670SBarry Smith   int        i,ierr,nz,m = a->m,n = a->n,*cwork;
17870513a670SBarry Smith   int        *row,*col,*cnew,j,*lens;
178856cd22aeSBarry Smith   IS         icolp,irowp;
17890513a670SBarry Smith 
17903a40ed3dSBarry Smith   PetscFunctionBegin;
17914c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
179256cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
17934c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
179456cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
17950513a670SBarry Smith 
17960513a670SBarry Smith   /* determine lengths of permuted rows */
17970513a670SBarry Smith   lens = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(lens);
17980513a670SBarry Smith   for (i=0; i<m; i++) {
17990513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
18000513a670SBarry Smith   }
18010513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
1802606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
18030513a670SBarry Smith 
18040513a670SBarry Smith   cnew = (int*)PetscMalloc(n*sizeof(int));CHKPTRQ(cnew);
18050513a670SBarry Smith   for (i=0; i<m; i++) {
18060513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18070513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
18080513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
18090513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18100513a670SBarry Smith   }
1811606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18120513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18130513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
181456cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
181556cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
181656cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
181756cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18183a40ed3dSBarry Smith   PetscFunctionReturn(0);
18190513a670SBarry Smith }
18200513a670SBarry Smith 
18215615d1e5SSatish Balay #undef __FUNC__
1822ca44d042SBarry Smith #define __FUNC__ /*<a name="MatPrintHelp_SeqAIJ"></a>*/"MatPrintHelp_SeqAIJ"
1823682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1824682d7d0cSBarry Smith {
1825c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1826682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1827d132466eSBarry Smith   int               ierr;
1828682d7d0cSBarry Smith 
18293a40ed3dSBarry Smith   PetscFunctionBegin;
1830c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1831d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1832d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1833d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
1834d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr);
1835d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr);
1836aa482453SBarry Smith #if defined(PETSC_HAVE_ESSL)
1837d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");CHKERRQ(ierr);
1838682d7d0cSBarry Smith #endif
1839ca44d042SBarry Smith #if defined(PETSC_HAVE_MATLAB)
1840ca44d042SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_matlab: Use Matlab engine sparse LU factorization and solve.\n");CHKERRQ(ierr);
1841ca44d042SBarry Smith #endif
18423a40ed3dSBarry Smith   PetscFunctionReturn(0);
1843682d7d0cSBarry Smith }
1844ca44d042SBarry Smith EXTERN int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg);
1845ca44d042SBarry Smith EXTERN int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
1846ca44d042SBarry Smith EXTERN int MatColoringPatch_SeqAIJ(Mat,int,int *,ISColoring *);
1847ca44d042SBarry Smith EXTERN int MatILUDTFactor_SeqAIJ(Mat,MatILUInfo*,IS,IS,Mat*);
1848cb5b572fSBarry Smith #undef __FUNC__
1849ca44d042SBarry Smith #define __FUNC__ /*<a name="MatCopy_SeqAIJ"></a>*/"MatCopy_SeqAIJ"
1850cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1851cb5b572fSBarry Smith {
1852be6bf707SBarry Smith   int    ierr;
1853cb5b572fSBarry Smith 
1854cb5b572fSBarry Smith   PetscFunctionBegin;
1855be6bf707SBarry Smith   if (str == SAME_NONZERO_PATTERN && B->type == MATSEQAIJ) {
1856be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1857be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1858be6bf707SBarry Smith 
1859be6bf707SBarry Smith     if (a->i[a->m]+a->indexshift != b->i[b->m]+a->indexshift) {
1860be6bf707SBarry Smith       SETERRQ(1,1,"Number of nonzeros in two matrices are different");
1861cb5b572fSBarry Smith     }
1862549d3d68SSatish Balay     ierr = PetscMemcpy(b->a,a->a,(a->i[a->m]+a->indexshift)*sizeof(Scalar));CHKERRQ(ierr);
1863cb5b572fSBarry Smith   } else {
1864cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1865cb5b572fSBarry Smith   }
1866cb5b572fSBarry Smith   PetscFunctionReturn(0);
1867cb5b572fSBarry Smith }
1868cb5b572fSBarry Smith 
1869682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
18700a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
1871cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
1872cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
1873cb5b572fSBarry Smith        MatMult_SeqAIJ,
1874cb5b572fSBarry Smith        MatMultAdd_SeqAIJ,
18757c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
18767c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
1877cb5b572fSBarry Smith        MatSolve_SeqAIJ,
1878cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
18797c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
18807c922b88SBarry Smith        MatSolveTransposeAdd_SeqAIJ,
1881cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
1882cb5b572fSBarry Smith        0,
188317ab2063SBarry Smith        MatRelax_SeqAIJ,
188417ab2063SBarry Smith        MatTranspose_SeqAIJ,
1885cb5b572fSBarry Smith        MatGetInfo_SeqAIJ,
1886cb5b572fSBarry Smith        MatEqual_SeqAIJ,
1887cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
1888cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
1889cb5b572fSBarry Smith        MatNorm_SeqAIJ,
1890cb5b572fSBarry Smith        0,
1891cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
189217ab2063SBarry Smith        MatCompress_SeqAIJ,
1893cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
1894cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
1895cb5b572fSBarry Smith        MatZeroRows_SeqAIJ,
1896cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
1897cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
1898cb5b572fSBarry Smith        0,
1899cb5b572fSBarry Smith        0,
1900cb5b572fSBarry Smith        MatGetSize_SeqAIJ,
1901cb5b572fSBarry Smith        MatGetSize_SeqAIJ,
1902cb5b572fSBarry Smith        MatGetOwnershipRange_SeqAIJ,
1903cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
1904cb5b572fSBarry Smith        0,
1905cb5b572fSBarry Smith        0,
1906cb5b572fSBarry Smith        0,
1907be6bf707SBarry Smith        MatDuplicate_SeqAIJ,
1908cb5b572fSBarry Smith        0,
1909cb5b572fSBarry Smith        0,
1910cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
1911cb5b572fSBarry Smith        0,
1912cb5b572fSBarry Smith        0,
1913cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
1914cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
1915cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
1916cb5b572fSBarry Smith        MatCopy_SeqAIJ,
1917f0b747eeSBarry Smith        MatPrintHelp_SeqAIJ,
1918cb5b572fSBarry Smith        MatScale_SeqAIJ,
1919cb5b572fSBarry Smith        0,
1920cb5b572fSBarry Smith        0,
19216945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
19226945ee14SBarry Smith        MatGetBlockSize_SeqAIJ,
19233b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
19243b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
19253b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
1926a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
1927a93ec695SBarry Smith        MatFDColoringCreate_SeqAIJ,
19280513a670SBarry Smith        MatColoringPatch_SeqAIJ,
19290513a670SBarry Smith        0,
1930cda55fadSBarry Smith        MatPermute_SeqAIJ,
1931cda55fadSBarry Smith        0,
1932cda55fadSBarry Smith        0,
1933cda55fadSBarry Smith        0,
1934cda55fadSBarry Smith        0,
1935cda55fadSBarry Smith        MatGetMaps_Petsc};
193617ab2063SBarry Smith 
1937ca44d042SBarry Smith EXTERN int MatUseSuperLU_SeqAIJ(Mat);
1938ca44d042SBarry Smith EXTERN int MatUseEssl_SeqAIJ(Mat);
1939ca44d042SBarry Smith EXTERN int MatUseMatlab_SeqAIJ(Mat);
1940ca44d042SBarry Smith EXTERN int MatUseDXML_SeqAIJ(Mat);
194117ab2063SBarry Smith 
1942fb2e594dSBarry Smith EXTERN_C_BEGIN
19435615d1e5SSatish Balay #undef __FUNC__
1944ca44d042SBarry Smith #define __FUNC__ /*<a name="MatSeqAIJSetColumnIndices_SeqAIJ"></a>*/"MatSeqAIJSetColumnIndices_SeqAIJ"
194515091d37SBarry Smith 
1946bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices)
1947bef8e0ddSBarry Smith {
1948bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
1949bef8e0ddSBarry Smith   int        i,nz,n;
1950bef8e0ddSBarry Smith 
1951bef8e0ddSBarry Smith   PetscFunctionBegin;
1952bef8e0ddSBarry Smith   if (aij->indexshift) SETERRQ(1,1,"No support with 1 based indexing");
1953bef8e0ddSBarry Smith 
1954bef8e0ddSBarry Smith   nz = aij->maxnz;
1955bef8e0ddSBarry Smith   n  = aij->n;
1956bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
1957bef8e0ddSBarry Smith     aij->j[i] = indices[i];
1958bef8e0ddSBarry Smith   }
1959bef8e0ddSBarry Smith   aij->nz = nz;
1960bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
1961bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
1962bef8e0ddSBarry Smith   }
1963bef8e0ddSBarry Smith 
1964bef8e0ddSBarry Smith   PetscFunctionReturn(0);
1965bef8e0ddSBarry Smith }
1966fb2e594dSBarry Smith EXTERN_C_END
1967bef8e0ddSBarry Smith 
1968bef8e0ddSBarry Smith #undef __FUNC__
1969ca44d042SBarry Smith #define __FUNC__ /*<a name="MatSeqAIJSetColumnIndices"></a>*/"MatSeqAIJSetColumnIndices"
1970bef8e0ddSBarry Smith /*@
1971bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
1972bef8e0ddSBarry Smith        in the matrix.
1973bef8e0ddSBarry Smith 
1974bef8e0ddSBarry Smith   Input Parameters:
1975bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
1976bef8e0ddSBarry Smith -  indices - the column indices
1977bef8e0ddSBarry Smith 
197815091d37SBarry Smith   Level: advanced
197915091d37SBarry Smith 
1980bef8e0ddSBarry Smith   Notes:
1981bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
1982bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
1983bef8e0ddSBarry Smith   of the MatSetValues() operation.
1984bef8e0ddSBarry Smith 
1985bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
1986bef8e0ddSBarry Smith   MatCreateSeqAIJ().
1987bef8e0ddSBarry Smith 
1988bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
1989bef8e0ddSBarry Smith 
1990bef8e0ddSBarry Smith @*/
1991bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices)
1992bef8e0ddSBarry Smith {
1993bef8e0ddSBarry Smith   int ierr,(*f)(Mat,int *);
1994bef8e0ddSBarry Smith 
1995bef8e0ddSBarry Smith   PetscFunctionBegin;
1996bef8e0ddSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1997549d3d68SSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void **)&f);CHKERRQ(ierr);
1998bef8e0ddSBarry Smith   if (f) {
1999bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2000bef8e0ddSBarry Smith   } else {
2001bef8e0ddSBarry Smith     SETERRQ(1,1,"Wrong type of matrix to set column indices");
2002bef8e0ddSBarry Smith   }
2003bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2004bef8e0ddSBarry Smith }
2005bef8e0ddSBarry Smith 
2006be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2007be6bf707SBarry Smith 
2008fb2e594dSBarry Smith EXTERN_C_BEGIN
2009be6bf707SBarry Smith #undef __FUNC__
2010ca44d042SBarry Smith #define __FUNC__ /*<a name="MatStoreValues_SeqAIJ"></a>*/"MatStoreValues_SeqAIJ"
2011be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat)
2012be6bf707SBarry Smith {
2013be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2014549d3d68SSatish Balay   int        nz = aij->i[aij->m]+aij->indexshift,ierr;
2015be6bf707SBarry Smith 
2016be6bf707SBarry Smith   PetscFunctionBegin;
2017be6bf707SBarry Smith   if (aij->nonew != 1) {
2018be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2019be6bf707SBarry Smith   }
2020be6bf707SBarry Smith 
2021be6bf707SBarry Smith   /* allocate space for values if not already there */
2022be6bf707SBarry Smith   if (!aij->saved_values) {
2023be6bf707SBarry Smith     aij->saved_values = (Scalar*)PetscMalloc(nz*sizeof(Scalar));CHKPTRQ(aij->saved_values);
2024be6bf707SBarry Smith   }
2025be6bf707SBarry Smith 
2026be6bf707SBarry Smith   /* copy values over */
2027549d3d68SSatish Balay   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(Scalar));CHKERRQ(ierr);
2028be6bf707SBarry Smith   PetscFunctionReturn(0);
2029be6bf707SBarry Smith }
2030fb2e594dSBarry Smith EXTERN_C_END
2031be6bf707SBarry Smith 
2032be6bf707SBarry Smith #undef __FUNC__
2033ca44d042SBarry Smith #define __FUNC__ /*<a name="MatStoreValues""></a>*/"MatStoreValues"
2034be6bf707SBarry Smith /*@
2035be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2036be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2037be6bf707SBarry Smith        nonlinear portion.
2038be6bf707SBarry Smith 
2039be6bf707SBarry Smith    Collect on Mat
2040be6bf707SBarry Smith 
2041be6bf707SBarry Smith   Input Parameters:
2042be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2043be6bf707SBarry Smith 
204415091d37SBarry Smith   Level: advanced
204515091d37SBarry Smith 
2046be6bf707SBarry Smith   Common Usage, with SNESSolve():
2047be6bf707SBarry Smith $    Create Jacobian matrix
2048be6bf707SBarry Smith $    Set linear terms into matrix
2049be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2050be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2051be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2052be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2053be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2054be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2055be6bf707SBarry Smith $    In your Jacobian routine
2056be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2057be6bf707SBarry Smith $      Set nonlinear terms in matrix
2058be6bf707SBarry Smith 
2059be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2060be6bf707SBarry Smith $    // build linear portion of Jacobian
2061be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2062be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2063be6bf707SBarry Smith $    loop over nonlinear iterations
2064be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2065be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2066be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2067be6bf707SBarry Smith $       Solve linear system with Jacobian
2068be6bf707SBarry Smith $    endloop
2069be6bf707SBarry Smith 
2070be6bf707SBarry Smith   Notes:
2071be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2072be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2073be6bf707SBarry Smith     calling this routine.
2074be6bf707SBarry Smith 
2075be6bf707SBarry Smith .seealso: MatRetrieveValues()
2076be6bf707SBarry Smith 
2077be6bf707SBarry Smith @*/
2078be6bf707SBarry Smith int MatStoreValues(Mat mat)
2079be6bf707SBarry Smith {
2080be6bf707SBarry Smith   int ierr,(*f)(Mat);
2081be6bf707SBarry Smith 
2082be6bf707SBarry Smith   PetscFunctionBegin;
2083be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2084be6bf707SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2085be6bf707SBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2086be6bf707SBarry Smith 
2087c5d6004cSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void **)&f);CHKERRQ(ierr);
2088be6bf707SBarry Smith   if (f) {
2089be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2090be6bf707SBarry Smith   } else {
2091be6bf707SBarry Smith     SETERRQ(1,1,"Wrong type of matrix to store values");
2092be6bf707SBarry Smith   }
2093be6bf707SBarry Smith   PetscFunctionReturn(0);
2094be6bf707SBarry Smith }
2095be6bf707SBarry Smith 
2096fb2e594dSBarry Smith EXTERN_C_BEGIN
2097be6bf707SBarry Smith #undef __FUNC__
2098ca44d042SBarry Smith #define __FUNC__ /*<a name="MatRetrieveValues_SeqAIJ"></a>*/"MatRetrieveValues_SeqAIJ"
2099be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat)
2100be6bf707SBarry Smith {
2101be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2102549d3d68SSatish Balay   int        nz = aij->i[aij->m]+aij->indexshift,ierr;
2103be6bf707SBarry Smith 
2104be6bf707SBarry Smith   PetscFunctionBegin;
2105be6bf707SBarry Smith   if (aij->nonew != 1) {
2106be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2107be6bf707SBarry Smith   }
2108be6bf707SBarry Smith   if (!aij->saved_values) {
2109be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatStoreValues(A);first");
2110be6bf707SBarry Smith   }
2111be6bf707SBarry Smith 
2112be6bf707SBarry Smith   /* copy values over */
2113549d3d68SSatish Balay   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(Scalar));CHKERRQ(ierr);
2114be6bf707SBarry Smith   PetscFunctionReturn(0);
2115be6bf707SBarry Smith }
2116fb2e594dSBarry Smith EXTERN_C_END
2117be6bf707SBarry Smith 
2118be6bf707SBarry Smith #undef __FUNC__
2119ca44d042SBarry Smith #define __FUNC__ /*<a name="MatRetrieveValues"></a>*/"MatRetrieveValues"
2120be6bf707SBarry Smith /*@
2121be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2122be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2123be6bf707SBarry Smith        nonlinear portion.
2124be6bf707SBarry Smith 
2125be6bf707SBarry Smith    Collect on Mat
2126be6bf707SBarry Smith 
2127be6bf707SBarry Smith   Input Parameters:
2128be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2129be6bf707SBarry Smith 
213015091d37SBarry Smith   Level: advanced
213115091d37SBarry Smith 
2132be6bf707SBarry Smith .seealso: MatStoreValues()
2133be6bf707SBarry Smith 
2134be6bf707SBarry Smith @*/
2135be6bf707SBarry Smith int MatRetrieveValues(Mat mat)
2136be6bf707SBarry Smith {
2137be6bf707SBarry Smith   int ierr,(*f)(Mat);
2138be6bf707SBarry Smith 
2139be6bf707SBarry Smith   PetscFunctionBegin;
2140be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2141be6bf707SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2142be6bf707SBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2143be6bf707SBarry Smith 
2144c5d6004cSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void **)&f);CHKERRQ(ierr);
2145be6bf707SBarry Smith   if (f) {
2146be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2147be6bf707SBarry Smith   } else {
2148be6bf707SBarry Smith     SETERRQ(1,1,"Wrong type of matrix to retrieve values");
2149be6bf707SBarry Smith   }
2150be6bf707SBarry Smith   PetscFunctionReturn(0);
2151be6bf707SBarry Smith }
2152be6bf707SBarry Smith 
2153f83d6046SBarry Smith /*
2154f83d6046SBarry Smith    This allows SeqAIJ matrices to be passed to the matlab engine
2155f83d6046SBarry Smith */
2156f83d6046SBarry Smith #if defined(PETSC_HAVE_MATLAB)
2157f83d6046SBarry Smith #include "engine.h"   /* Matlab include file */
2158f83d6046SBarry Smith #include "mex.h"      /* Matlab include file */
2159f83d6046SBarry Smith EXTERN_C_BEGIN
2160f83d6046SBarry Smith #undef __FUNC__
2161f83d6046SBarry Smith #define __FUNC__ /*<a name="MatMatlabEnginePut_SeqAIJ"></a>*/"MatMatlabEnginePut_SeqAIJ"
2162f83d6046SBarry Smith int MatMatlabEnginePut_SeqAIJ(PetscObject obj,void *engine)
2163f83d6046SBarry Smith {
2164d79a51d8SBarry Smith   int        ierr,i,*aj,*ai;
2165f83d6046SBarry Smith   Mat        B = (Mat)obj;
2166f83d6046SBarry Smith   Scalar     *array;
2167f83d6046SBarry Smith   mxArray    *mat;
2168d79a51d8SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)B->data;
2169d79a51d8SBarry Smith 
2170f83d6046SBarry Smith 
2171f83d6046SBarry Smith   PetscFunctionBegin;
217201820c62SBarry Smith   mat  = mxCreateSparse(B->n,B->m,aij->nz,mxREAL);
2173d79a51d8SBarry Smith   ierr = PetscMemcpy(mxGetPr(mat),aij->a,aij->nz*sizeof(Scalar));CHKERRQ(ierr);
2174d79a51d8SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
2175d79a51d8SBarry Smith   ai   = mxGetJc(mat);
2176d79a51d8SBarry Smith   aj   = mxGetIr(mat);
2177d79a51d8SBarry Smith   ierr = PetscMemcpy(aj,aij->j,aij->nz*sizeof(int));CHKERRQ(ierr);
2178d79a51d8SBarry Smith   ierr = PetscMemcpy(ai,aij->i,(B->m+1)*sizeof(int));CHKERRQ(ierr);
2179f83d6046SBarry Smith 
218001820c62SBarry Smith   /* Matlab indices start at 0 for sparse (what a surprise) */
218101820c62SBarry Smith   if (aij->indexshift) {
218201820c62SBarry Smith     for (i=0; i<B->m+1; i++) {
218301820c62SBarry Smith       ai[i]--;
2184d79a51d8SBarry Smith     }
2185d79a51d8SBarry Smith     for (i=0; i<aij->nz; i++) {
218601820c62SBarry Smith       aj[i]--;
2187d79a51d8SBarry Smith     }
2188d79a51d8SBarry Smith   }
2189ca44d042SBarry Smith   ierr = PetscObjectName(obj);CHKERRQ(ierr);
2190f83d6046SBarry Smith   mxSetName(mat,obj->name);
2191f83d6046SBarry Smith   engPutArray((Engine *)engine,mat);
2192f83d6046SBarry Smith   PetscFunctionReturn(0);
2193f83d6046SBarry Smith }
2194f83d6046SBarry Smith EXTERN_C_END
2195f83d6046SBarry Smith #endif
2196f83d6046SBarry Smith 
2197be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
2198cb5b572fSBarry Smith 
2199bef8e0ddSBarry Smith #undef __FUNC__
2200ca44d042SBarry Smith #define __FUNC__ /*<a name="MatCreateSeqAIJ"></a>*/"MatCreateSeqAIJ"
220117ab2063SBarry Smith /*@C
2202682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
22030d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
22046e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
220551c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
22062bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
220717ab2063SBarry Smith 
2208db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2209db81eaa0SLois Curfman McInnes 
221017ab2063SBarry Smith    Input Parameters:
2211db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
221217ab2063SBarry Smith .  m - number of rows
221317ab2063SBarry Smith .  n - number of columns
221417ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
221551c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
22162bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
221717ab2063SBarry Smith 
221817ab2063SBarry Smith    Output Parameter:
2219416022c9SBarry Smith .  A - the matrix
222017ab2063SBarry Smith 
2221b259b22eSLois Curfman McInnes    Notes:
222217ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
222317ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
22240002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
222544cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
222617ab2063SBarry Smith 
222717ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2228a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
22293d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
22306da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
223117ab2063SBarry Smith 
2232682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
22334fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2234682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
22356c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
22366c7ebb05SLois Curfman McInnes 
22376c7ebb05SLois Curfman McInnes    Options Database Keys:
2238db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2239db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2240db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2241db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2242db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
224317ab2063SBarry Smith 
2244027ccd11SLois Curfman McInnes    Level: intermediate
2245027ccd11SLois Curfman McInnes 
224636db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
224736db0b34SBarry Smith 
224817ab2063SBarry Smith @*/
2249416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz,Mat *A)
225017ab2063SBarry Smith {
2251416022c9SBarry Smith   Mat        B;
2252416022c9SBarry Smith   Mat_SeqAIJ *b;
2253f1af5d2fSBarry Smith   int        i,len,ierr,size;
2254f1af5d2fSBarry Smith   PetscTruth flg;
22556945ee14SBarry Smith 
22563a40ed3dSBarry Smith   PetscFunctionBegin;
2257d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2258a8c6a408SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Comm must be of size 1");
2259d5d45c9bSBarry Smith 
2260b73539f3SBarry Smith   if (nz < -2) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"nz cannot be less than -2: value %d",nz);
2261b73539f3SBarry Smith   if (nnz) {
2262b73539f3SBarry Smith     for (i=0; i<m; i++) {
2263b73539f3SBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,0,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2264b73539f3SBarry Smith     }
2265b73539f3SBarry Smith   }
2266b73539f3SBarry Smith 
2267416022c9SBarry Smith   *A                  = 0;
22683f1db9ecSBarry Smith   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATSEQAIJ,"Mat",comm,MatDestroy,MatView);
2269416022c9SBarry Smith   PLogObjectCreate(B);
22700452661fSBarry Smith   B->data             = (void*)(b = PetscNew(Mat_SeqAIJ));CHKPTRQ(b);
2271549d3d68SSatish Balay   ierr = PetscMemzero(b,sizeof(Mat_SeqAIJ));CHKERRQ(ierr);
2272549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2273e1311b90SBarry Smith   B->ops->destroy          = MatDestroy_SeqAIJ;
2274e1311b90SBarry Smith   B->ops->view             = MatView_SeqAIJ;
2275416022c9SBarry Smith   B->factor           = 0;
2276416022c9SBarry Smith   B->lupivotthreshold = 1.0;
227790f02eecSBarry Smith   B->mapping          = 0;
2278f1af5d2fSBarry Smith   ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr);
2279f1af5d2fSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2280416022c9SBarry Smith   b->row              = 0;
2281416022c9SBarry Smith   b->col              = 0;
228282bf6240SBarry Smith   b->icol             = 0;
2283416022c9SBarry Smith   b->indexshift       = 0;
2284b810aeb4SBarry Smith   b->reallocs         = 0;
228569957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_oneindex",&flg);CHKERRQ(ierr);
228669957df2SSatish Balay   if (flg) b->indexshift = -1;
228717ab2063SBarry Smith 
228844cd7ae7SLois Curfman McInnes   b->m = m; B->m = m; B->M = m;
228944cd7ae7SLois Curfman McInnes   b->n = n; B->n = n; B->N = n;
2290a5ae1ecdSBarry Smith 
22914d197716SBarry Smith   ierr = MapCreateMPI(comm,m,m,&B->rmap);CHKERRQ(ierr);
22924d197716SBarry Smith   ierr = MapCreateMPI(comm,n,n,&B->cmap);CHKERRQ(ierr);
2293a5ae1ecdSBarry Smith 
22940452661fSBarry Smith   b->imax = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(b->imax);
2295f1e2ffcdSBarry Smith   if (!nnz) {
22967b8455f0SLois Curfman McInnes     if (nz == PETSC_DEFAULT) nz = 10;
22977b8455f0SLois Curfman McInnes     else if (nz <= 0)        nz = 1;
2298416022c9SBarry Smith     for (i=0; i<m; i++) b->imax[i] = nz;
229917ab2063SBarry Smith     nz = nz*m;
23003a40ed3dSBarry Smith   } else {
230117ab2063SBarry Smith     nz = 0;
2302416022c9SBarry Smith     for (i=0; i<m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
230317ab2063SBarry Smith   }
230417ab2063SBarry Smith 
230517ab2063SBarry Smith   /* allocate the matrix space */
2306416022c9SBarry Smith   len             = nz*(sizeof(int) + sizeof(Scalar)) + (b->m+1)*sizeof(int);
23070452661fSBarry Smith   b->a            = (Scalar*)PetscMalloc(len);CHKPTRQ(b->a);
2308416022c9SBarry Smith   b->j            = (int*)(b->a + nz);
2309549d3d68SSatish Balay   ierr            = PetscMemzero(b->j,nz*sizeof(int));CHKERRQ(ierr);
2310416022c9SBarry Smith   b->i            = b->j + nz;
2311f1e2ffcdSBarry Smith   b->singlemalloc = PETSC_TRUE;
231236db0b34SBarry Smith   b->freedata     = PETSC_TRUE;
231317ab2063SBarry Smith 
2314416022c9SBarry Smith   b->i[0] = -b->indexshift;
231517ab2063SBarry Smith   for (i=1; i<m+1; i++) {
2316416022c9SBarry Smith     b->i[i] = b->i[i-1] + b->imax[i-1];
231717ab2063SBarry Smith   }
231817ab2063SBarry Smith 
2319416022c9SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
23200452661fSBarry Smith   b->ilen = (int*)PetscMalloc((m+1)*sizeof(int));
2321f09e8eb9SSatish Balay   PLogObjectMemory(B,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2322416022c9SBarry Smith   for (i=0; i<b->m; i++) { b->ilen[i] = 0;}
232317ab2063SBarry Smith 
2324416022c9SBarry Smith   b->nz                = 0;
2325416022c9SBarry Smith   b->maxnz             = nz;
2326f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
232736db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2328f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2329416022c9SBarry Smith   b->nonew             = 0;
2330416022c9SBarry Smith   b->diag              = 0;
2331416022c9SBarry Smith   b->solve_work        = 0;
233271bd300dSLois Curfman McInnes   b->spptr             = 0;
2333b65db4caSBarry Smith   b->inode.use         = PETSC_TRUE;
2334754ec7b1SSatish Balay   b->inode.node_count  = 0;
2335754ec7b1SSatish Balay   b->inode.size        = 0;
23366c7ebb05SLois Curfman McInnes   b->inode.limit       = 5;
23376c7ebb05SLois Curfman McInnes   b->inode.max_limit   = 5;
2338be6bf707SBarry Smith   b->saved_values      = 0;
23394e220ebcSLois Curfman McInnes   B->info.nz_unneeded  = (double)b->maxnz;
2340d7f994e1SBarry Smith   b->idiag             = 0;
2341d7f994e1SBarry Smith   b->ssor              = 0;
2342f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
234317ab2063SBarry Smith 
2344416022c9SBarry Smith   *A = B;
23454e220ebcSLois Curfman McInnes 
23464b14c69eSBarry Smith   /*  SuperLU is not currently supported through PETSc */
2347aa482453SBarry Smith #if defined(PETSC_HAVE_SUPERLU)
234869957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_superlu",&flg);CHKERRQ(ierr);
234969957df2SSatish Balay   if (flg) { ierr = MatUseSuperLU_SeqAIJ(B);CHKERRQ(ierr); }
23504b14c69eSBarry Smith #endif
235169957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_essl",&flg);CHKERRQ(ierr);
235269957df2SSatish Balay   if (flg) { ierr = MatUseEssl_SeqAIJ(B);CHKERRQ(ierr); }
2353ca44d042SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_matlab",&flg);CHKERRQ(ierr);
2354ca44d042SBarry Smith   if (flg) {ierr = MatUseMatlab_SeqAIJ(B);CHKERRQ(ierr);}
235569957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_dxml",&flg);CHKERRQ(ierr);
235669957df2SSatish Balay   if (flg) {
2357a8c6a408SBarry Smith     if (!b->indexshift) SETERRQ(PETSC_ERR_LIB,0,"need -mat_aij_oneindex with -mat_aij_dxml");
2358416022c9SBarry Smith     ierr = MatUseDXML_SeqAIJ(B);CHKERRQ(ierr);
235917ab2063SBarry Smith   }
236069957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
236169957df2SSatish Balay   if (flg) {ierr = MatPrintHelp(B);CHKERRQ(ierr); }
2362bef8e0ddSBarry Smith 
2363f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2364bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2365bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2366f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2367be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2368bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2369f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2370be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2371bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2372f83d6046SBarry Smith #if defined(PETSC_HAVE_MATLAB)
2373f83d6046SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatMatlabEnginePut_SeqAIJ",MatMatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
2374f83d6046SBarry Smith #endif
23753a40ed3dSBarry Smith   PetscFunctionReturn(0);
237617ab2063SBarry Smith }
237717ab2063SBarry Smith 
23785615d1e5SSatish Balay #undef __FUNC__
2379ca44d042SBarry Smith #define __FUNC__ /*<a name="MatDuplicate_SeqAIJ"></a>*/"MatDuplicate_SeqAIJ"
2380be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
238117ab2063SBarry Smith {
2382416022c9SBarry Smith   Mat        C;
2383416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data;
2384bef8e0ddSBarry Smith   int        i,len,m = a->m,shift = a->indexshift,ierr;
238517ab2063SBarry Smith 
23863a40ed3dSBarry Smith   PetscFunctionBegin;
23874043dd9cSLois Curfman McInnes   *B = 0;
23883f1db9ecSBarry Smith   PetscHeaderCreate(C,_p_Mat,struct _MatOps,MAT_COOKIE,MATSEQAIJ,"Mat",A->comm,MatDestroy,MatView);
2389416022c9SBarry Smith   PLogObjectCreate(C);
23900452661fSBarry Smith   C->data           = (void*)(c = PetscNew(Mat_SeqAIJ));CHKPTRQ(c);
2391549d3d68SSatish Balay   ierr              = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
2392e1311b90SBarry Smith   C->ops->destroy   = MatDestroy_SeqAIJ;
2393e1311b90SBarry Smith   C->ops->view      = MatView_SeqAIJ;
2394416022c9SBarry Smith   C->factor         = A->factor;
2395416022c9SBarry Smith   c->row            = 0;
2396416022c9SBarry Smith   c->col            = 0;
239782bf6240SBarry Smith   c->icol           = 0;
2398416022c9SBarry Smith   c->indexshift     = shift;
2399f1e2ffcdSBarry Smith   c->keepzeroedrows = a->keepzeroedrows;
2400c456f294SBarry Smith   C->assembled      = PETSC_TRUE;
240117ab2063SBarry Smith 
240244cd7ae7SLois Curfman McInnes   c->m = C->m   = a->m;
240344cd7ae7SLois Curfman McInnes   c->n = C->n   = a->n;
240444cd7ae7SLois Curfman McInnes   C->M          = a->m;
240544cd7ae7SLois Curfman McInnes   C->N          = a->n;
240617ab2063SBarry Smith 
24070452661fSBarry Smith   c->imax       = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->imax);
24080452661fSBarry Smith   c->ilen       = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->ilen);
240917ab2063SBarry Smith   for (i=0; i<m; i++) {
2410416022c9SBarry Smith     c->imax[i] = a->imax[i];
2411416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
241217ab2063SBarry Smith   }
241317ab2063SBarry Smith 
241417ab2063SBarry Smith   /* allocate the matrix space */
2415f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
2416416022c9SBarry Smith   len     = (m+1)*sizeof(int)+(a->i[m])*(sizeof(Scalar)+sizeof(int));
24170452661fSBarry Smith   c->a  = (Scalar*)PetscMalloc(len);CHKPTRQ(c->a);
2418416022c9SBarry Smith   c->j  = (int*)(c->a + a->i[m] + shift);
2419416022c9SBarry Smith   c->i  = c->j + a->i[m] + shift;
2420549d3d68SSatish Balay   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));CHKERRQ(ierr);
242117ab2063SBarry Smith   if (m > 0) {
2422549d3d68SSatish Balay     ierr = PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int));CHKERRQ(ierr);
2423be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2424549d3d68SSatish Balay       ierr = PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(Scalar));CHKERRQ(ierr);
2425be6bf707SBarry Smith     } else {
2426549d3d68SSatish Balay       ierr = PetscMemzero(c->a,(a->i[m]+shift)*sizeof(Scalar));CHKERRQ(ierr);
242717ab2063SBarry Smith     }
242808480c60SBarry Smith   }
242917ab2063SBarry Smith 
2430f09e8eb9SSatish Balay   PLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2431416022c9SBarry Smith   c->sorted      = a->sorted;
2432416022c9SBarry Smith   c->roworiented = a->roworiented;
2433416022c9SBarry Smith   c->nonew       = a->nonew;
24347a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
2435be6bf707SBarry Smith   c->saved_values = 0;
2436d7f994e1SBarry Smith   c->idiag        = 0;
2437d7f994e1SBarry Smith   c->ssor         = 0;
243836db0b34SBarry Smith   c->ignorezeroentries = a->ignorezeroentries;
243936db0b34SBarry Smith   c->freedata     = PETSC_TRUE;
244017ab2063SBarry Smith 
2441416022c9SBarry Smith   if (a->diag) {
24420452661fSBarry Smith     c->diag = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->diag);
2443416022c9SBarry Smith     PLogObjectMemory(C,(m+1)*sizeof(int));
244417ab2063SBarry Smith     for (i=0; i<m; i++) {
2445416022c9SBarry Smith       c->diag[i] = a->diag[i];
244617ab2063SBarry Smith     }
24473a40ed3dSBarry Smith   } else c->diag        = 0;
2448b65db4caSBarry Smith   c->inode.use          = a->inode.use;
24496c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
24506c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
2451754ec7b1SSatish Balay   if (a->inode.size){
2452daed632aSSatish Balay     c->inode.size       = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->inode.size);
2453754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
2454549d3d68SSatish Balay     ierr = PetscMemcpy(c->inode.size,a->inode.size,(m+1)*sizeof(int));CHKERRQ(ierr);
2455754ec7b1SSatish Balay   } else {
2456754ec7b1SSatish Balay     c->inode.size       = 0;
2457754ec7b1SSatish Balay     c->inode.node_count = 0;
2458754ec7b1SSatish Balay   }
2459416022c9SBarry Smith   c->nz                 = a->nz;
2460416022c9SBarry Smith   c->maxnz              = a->maxnz;
2461416022c9SBarry Smith   c->solve_work         = 0;
246276dd722bSSatish Balay   c->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
2463754ec7b1SSatish Balay 
2464416022c9SBarry Smith   *B = C;
24657b2a1423SBarry Smith   ierr = FListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
24663a40ed3dSBarry Smith   PetscFunctionReturn(0);
246717ab2063SBarry Smith }
246817ab2063SBarry Smith 
24695615d1e5SSatish Balay #undef __FUNC__
2470ca44d042SBarry Smith #define __FUNC__ /*<a name="MatLoad_SeqAIJ"></a>*/"MatLoad_SeqAIJ"
247119bcc07fSBarry Smith int MatLoad_SeqAIJ(Viewer viewer,MatType type,Mat *A)
247217ab2063SBarry Smith {
2473416022c9SBarry Smith   Mat_SeqAIJ   *a;
2474416022c9SBarry Smith   Mat          B;
247517699dbbSLois Curfman McInnes   int          i,nz,ierr,fd,header[4],size,*rowlengths = 0,M,N,shift;
2476bcd2baecSBarry Smith   MPI_Comm     comm;
247717ab2063SBarry Smith 
24783a40ed3dSBarry Smith   PetscFunctionBegin;
2479e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2480d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2481a8c6a408SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,0,"view must have one processor");
248290ace30eSBarry Smith   ierr = ViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
24830752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2484a8c6a408SBarry Smith   if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"not matrix object in file");
248517ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
248617ab2063SBarry Smith 
2487d64ed03dSBarry Smith   if (nz < 0) {
2488a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,1,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2489d64ed03dSBarry Smith   }
2490d64ed03dSBarry Smith 
249117ab2063SBarry Smith   /* read in row lengths */
24920452661fSBarry Smith   rowlengths = (int*)PetscMalloc(M*sizeof(int));CHKPTRQ(rowlengths);
24930752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
249417ab2063SBarry Smith 
249517ab2063SBarry Smith   /* create our matrix */
2496416022c9SBarry Smith   ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A);CHKERRQ(ierr);
2497416022c9SBarry Smith   B = *A;
2498416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
2499416022c9SBarry Smith   shift = a->indexshift;
250017ab2063SBarry Smith 
250117ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
25020752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
250317ab2063SBarry Smith   if (shift) {
250417ab2063SBarry Smith     for (i=0; i<nz; i++) {
2505416022c9SBarry Smith       a->j[i] += 1;
250617ab2063SBarry Smith     }
250717ab2063SBarry Smith   }
250817ab2063SBarry Smith 
250917ab2063SBarry Smith   /* read in nonzero values */
25100752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
251117ab2063SBarry Smith 
251217ab2063SBarry Smith   /* set matrix "i" values */
2513416022c9SBarry Smith   a->i[0] = -shift;
251417ab2063SBarry Smith   for (i=1; i<= M; i++) {
2515416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2516416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
251717ab2063SBarry Smith   }
2518606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
251917ab2063SBarry Smith 
25206d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
25216d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
25223a40ed3dSBarry Smith   PetscFunctionReturn(0);
252317ab2063SBarry Smith }
252417ab2063SBarry Smith 
25255615d1e5SSatish Balay #undef __FUNC__
2526ca44d042SBarry Smith #define __FUNC__ /*<a name="MatEqual_SeqAIJ""></a>*/"MatEqual_SeqAIJ"
25278f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
25287264ac53SSatish Balay {
25297264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
25300f5bd95cSBarry Smith   int        ierr;
25317264ac53SSatish Balay 
25323a40ed3dSBarry Smith   PetscFunctionBegin;
2533a8c6a408SBarry Smith   if (B->type != MATSEQAIJ) SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type");
25347264ac53SSatish Balay 
25357264ac53SSatish Balay   /* If the  matrix dimensions are not equal,or no of nonzeros or shift */
2536ca44d042SBarry Smith   if ((a->m != b->m) || (a->n !=b->n) ||(a->nz != b->nz)|| (a->indexshift != b->indexshift)) {
2537ca44d042SBarry Smith     *flg = PETSC_FALSE;
2538ca44d042SBarry Smith     PetscFunctionReturn(0);
2539bcd2baecSBarry Smith   }
25407264ac53SSatish Balay 
25417264ac53SSatish Balay   /* if the a->i are the same */
25420f5bd95cSBarry Smith   ierr = PetscMemcmp(a->i,b->i,(a->m+1)*sizeof(int),flg);CHKERRQ(ierr);
25430f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
25447264ac53SSatish Balay 
25457264ac53SSatish Balay   /* if a->j are the same */
25460f5bd95cSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),flg);CHKERRQ(ierr);
25470f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
2548bcd2baecSBarry Smith 
2549bcd2baecSBarry Smith   /* if a->a are the same */
25500f5bd95cSBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(Scalar),flg);CHKERRQ(ierr);
25510f5bd95cSBarry Smith 
25523a40ed3dSBarry Smith   PetscFunctionReturn(0);
25537264ac53SSatish Balay 
25547264ac53SSatish Balay }
255536db0b34SBarry Smith 
255636db0b34SBarry Smith #undef __FUNC__
2557ca44d042SBarry Smith #define __FUNC__ /*<a name="MatCreateSeqAIJWithArrays"></a>*/"MatCreateSeqAIJWithArrays"
255836db0b34SBarry Smith /*@C
255936db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
256036db0b34SBarry Smith               provided by the user.
256136db0b34SBarry Smith 
256236db0b34SBarry Smith       Coolective on MPI_Comm
256336db0b34SBarry Smith 
256436db0b34SBarry Smith    Input Parameters:
256536db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
256636db0b34SBarry Smith .   m - number of rows
256736db0b34SBarry Smith .   n - number of columns
256836db0b34SBarry Smith .   i - row indices
256936db0b34SBarry Smith .   j - column indices
257036db0b34SBarry Smith -   a - matrix values
257136db0b34SBarry Smith 
257236db0b34SBarry Smith    Output Parameter:
257336db0b34SBarry Smith .   mat - the matrix
257436db0b34SBarry Smith 
257536db0b34SBarry Smith    Level: intermediate
257636db0b34SBarry Smith 
257736db0b34SBarry Smith    Notes:
25780551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
257936db0b34SBarry Smith     once the matrix is destroyed
258036db0b34SBarry Smith 
258136db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
258236db0b34SBarry Smith 
258336db0b34SBarry Smith        The i and j indices can be either 0- or 1 based
258436db0b34SBarry Smith 
258536db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
258636db0b34SBarry Smith 
258736db0b34SBarry Smith @*/
258836db0b34SBarry Smith int MatCreateSeqAIJWithArrays(MPI_Comm comm,int m,int n,int* i,int*j,Scalar *a,Mat *mat)
258936db0b34SBarry Smith {
259036db0b34SBarry Smith   int        ierr,ii;
259136db0b34SBarry Smith   Mat_SeqAIJ *aij;
259236db0b34SBarry Smith 
259336db0b34SBarry Smith   PetscFunctionBegin;
259436db0b34SBarry Smith   ierr = MatCreateSeqAIJ(comm,m,n,0,0,mat);CHKERRQ(ierr);
259536db0b34SBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
259636db0b34SBarry Smith   ierr = PetscFree(aij->a);CHKERRQ(ierr);
259736db0b34SBarry Smith 
259836db0b34SBarry Smith   if (i[0] == 1) {
259936db0b34SBarry Smith     aij->indexshift = -1;
260036db0b34SBarry Smith   } else if (i[0]) {
260136db0b34SBarry Smith     SETERRQ(1,1,"i (row indices) do not start with 0 or 1");
260236db0b34SBarry Smith   }
260336db0b34SBarry Smith   aij->i = i;
260436db0b34SBarry Smith   aij->j = j;
260536db0b34SBarry Smith   aij->a = a;
260636db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
260736db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
260836db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
260936db0b34SBarry Smith 
261036db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
261136db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
261236db0b34SBarry Smith #if defined(PETSC_BOPT_g)
261336db0b34SBarry Smith     if (i[ii+1] - i[i] < 0) SETERRQ2(1,1,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
261436db0b34SBarry Smith #endif
261536db0b34SBarry Smith   }
261636db0b34SBarry Smith #if defined(PETSC_BOPT_g)
261736db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
261836db0b34SBarry Smith     if (j[ii] < -aij->indexshift) SETERRQ2(1,1,"Negative column index at location = %d index = %d",ii,j[ii]);
261936db0b34SBarry Smith     if (j[ii] > n - 1 -aij->indexshift) SETERRQ2(1,1,"Column index to large at location = %d index = %d",ii,j[ii]);
262036db0b34SBarry Smith   }
262136db0b34SBarry Smith #endif
262236db0b34SBarry Smith 
2623b65db4caSBarry Smith   /* changes indices to start at 0 */
2624b65db4caSBarry Smith   if (i[0]) {
2625b65db4caSBarry Smith     aij->indexshift = 0;
2626b65db4caSBarry Smith     for (ii=0; ii<m; ii++) {
2627b65db4caSBarry Smith       i[ii]--;
2628b65db4caSBarry Smith     }
2629b65db4caSBarry Smith     for (ii=0; ii<i[m]; ii++) {
2630b65db4caSBarry Smith       j[ii]--;
2631b65db4caSBarry Smith     }
2632b65db4caSBarry Smith   }
2633b65db4caSBarry Smith 
2634b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2635b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
263636db0b34SBarry Smith   PetscFunctionReturn(0);
263736db0b34SBarry Smith }
263836db0b34SBarry Smith 
263936db0b34SBarry Smith 
264036db0b34SBarry Smith 
2641