xref: /petsc/src/mat/impls/aij/seq/aij.c (revision b65db4ca3424960b3ee21ed30a2ef6b8bdcb9ab8)
1*b65db4caSBarry Smith /*$Id: aij.c,v 1.336 2000/01/08 01:21:16 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 
73369ce9aSBarry Smith #include "sys.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"
12eeb667a2SSatish Balay #include "bitarray.h"
1317ab2063SBarry Smith 
14a2ce50c7SBarry Smith 
15bcd2baecSBarry Smith extern int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**);
1617ab2063SBarry Smith 
175615d1e5SSatish Balay #undef __FUNC__
185615d1e5SSatish Balay #define __FUNC__ "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__
515615d1e5SSatish Balay #define __FUNC__ "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__
675615d1e5SSatish Balay #define __FUNC__ "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__
1085615d1e5SSatish Balay #define __FUNC__ "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__
1255615d1e5SSatish Balay #define __FUNC__ "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;
173a8c6a408SBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix");
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 
179a8c6a408SBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix");
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__
2275615d1e5SSatish Balay #define __FUNC__ "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];
238a8c6a408SBarry Smith     if (row < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row");
239a8c6a408SBarry Smith     if (row >= a->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large");
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 */
243a8c6a408SBarry Smith       if (in[l] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column");
244a8c6a408SBarry Smith       if (in[l] >= a->n) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");
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__
2685615d1e5SSatish Balay #define __FUNC__ "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__
3045615d1e5SSatish Balay #define __FUNC__ "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.16e i \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__
465480ef9eaSBarry Smith #define __FUNC__ "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;
47077ed5343SBarry Smith   int         ierr,i,j,m = a->m,shift = a->indexshift,color,rank;
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;
47477ed5343SBarry Smith   MPI_Comm    comm;
475cddf8d76SBarry Smith 
4763a40ed3dSBarry Smith   PetscFunctionBegin;
47777ed5343SBarry Smith   /*
47877ed5343SBarry Smith       This is nasty. If this is called from an originally parallel matrix
47977ed5343SBarry Smith    then all processes call this,but only the first has the matrix so the
48077ed5343SBarry Smith    rest should return immediately.
48177ed5343SBarry Smith   */
48277ed5343SBarry Smith   ierr = PetscObjectGetComm((PetscObject)draw,&comm);CHKERRQ(ierr);
483d132466eSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
48477ed5343SBarry Smith   if (rank) PetscFunctionReturn(0);
48577ed5343SBarry Smith 
486480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
4870513a670SBarry Smith   ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr);
48819bcc07fSBarry Smith 
489480ef9eaSBarry Smith   ierr = DrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
490416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4910513a670SBarry Smith 
4920513a670SBarry Smith   if (format != VIEWER_FORMAT_DRAW_CONTOUR) {
4930513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
494cddf8d76SBarry Smith     color = DRAW_BLUE;
495416022c9SBarry Smith     for (i=0; i<m; i++) {
496cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
497416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
498cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
499aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
50036db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
501cddf8d76SBarry Smith #else
502cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
503cddf8d76SBarry Smith #endif
504480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
505cddf8d76SBarry Smith       }
506cddf8d76SBarry Smith     }
507cddf8d76SBarry Smith     color = DRAW_CYAN;
508cddf8d76SBarry Smith     for (i=0; i<m; i++) {
509cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
510cddf8d76SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
511cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
512cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
513480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
514cddf8d76SBarry Smith       }
515cddf8d76SBarry Smith     }
516cddf8d76SBarry Smith     color = DRAW_RED;
517cddf8d76SBarry Smith     for (i=0; i<m; i++) {
518cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
519cddf8d76SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
520cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
521aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52236db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
523cddf8d76SBarry Smith #else
524cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
525cddf8d76SBarry Smith #endif
526480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
527416022c9SBarry Smith       }
528416022c9SBarry Smith     }
5290513a670SBarry Smith   } else {
5300513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5310513a670SBarry Smith     /* first determine max of all nonzero values */
5320513a670SBarry Smith     int    nz = a->nz,count;
5330513a670SBarry Smith     Draw   popup;
53436db0b34SBarry Smith     PetscReal scale;
5350513a670SBarry Smith 
5360513a670SBarry Smith     for (i=0; i<nz; i++) {
5370513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5380513a670SBarry Smith     }
539480ef9eaSBarry Smith     scale = (245.0 - DRAW_BASIC_COLORS)/maxv;
540522c5e43SBarry Smith     ierr  = DrawGetPopup(draw,&popup);CHKERRQ(ierr);
541f1e2ffcdSBarry Smith     if (popup) {ierr  = DrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5420513a670SBarry Smith     count = 0;
5430513a670SBarry Smith     for (i=0; i<m; i++) {
5440513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
5450513a670SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5460513a670SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
5476d6b6c30SSatish Balay         color = DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count]));
548480ef9eaSBarry Smith         ierr  = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5490513a670SBarry Smith         count++;
5500513a670SBarry Smith       }
5510513a670SBarry Smith     }
5520513a670SBarry Smith   }
553480ef9eaSBarry Smith   PetscFunctionReturn(0);
554480ef9eaSBarry Smith }
555cddf8d76SBarry Smith 
556480ef9eaSBarry Smith #undef __FUNC__
557480ef9eaSBarry Smith #define __FUNC__ "MatView_SeqAIJ_Draw"
558480ef9eaSBarry Smith int MatView_SeqAIJ_Draw(Mat A,Viewer viewer)
559480ef9eaSBarry Smith {
560480ef9eaSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
561480ef9eaSBarry Smith   int        ierr;
562480ef9eaSBarry Smith   Draw       draw;
56336db0b34SBarry Smith   PetscReal  xr,yr,xl,yl,h,w;
564480ef9eaSBarry Smith   PetscTruth isnull;
565480ef9eaSBarry Smith 
566480ef9eaSBarry Smith   PetscFunctionBegin;
56777ed5343SBarry Smith   ierr = ViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
568480ef9eaSBarry Smith   ierr = DrawIsNull(draw,&isnull);CHKERRQ(ierr);
569480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
570480ef9eaSBarry Smith 
571480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
572480ef9eaSBarry Smith   xr  = a->n; yr = a->m; h = yr/10.0; w = xr/10.0;
573480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
574cddf8d76SBarry Smith   ierr = DrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
575480ef9eaSBarry Smith   ierr = DrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
576480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5773a40ed3dSBarry Smith   PetscFunctionReturn(0);
578416022c9SBarry Smith }
579416022c9SBarry Smith 
5805615d1e5SSatish Balay #undef __FUNC__
581d4bb536fSBarry Smith #define __FUNC__ "MatView_SeqAIJ"
582e1311b90SBarry Smith int MatView_SeqAIJ(Mat A,Viewer viewer)
583416022c9SBarry Smith {
584416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
585bcd2baecSBarry Smith   int         ierr;
5866831982aSBarry Smith   PetscTruth  issocket,isascii,isbinary,isdraw;
587416022c9SBarry Smith 
5883a40ed3dSBarry Smith   PetscFunctionBegin;
5896831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,SOCKET_VIEWER,&issocket);CHKERRQ(ierr);
5906831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr);
5916831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,BINARY_VIEWER,&isbinary);CHKERRQ(ierr);
5926831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,DRAW_VIEWER,&isdraw);CHKERRQ(ierr);
5930f5bd95cSBarry Smith   if (issocket) {
5947b2a1423SBarry Smith     ierr = ViewerSocketPutSparse_Private(viewer,a->m,a->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr);
5950f5bd95cSBarry Smith   } else if (isascii) {
5963a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5970f5bd95cSBarry Smith   } else if (isbinary) {
5983a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5990f5bd95cSBarry Smith   } else if (isdraw) {
6003a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6015cd90555SBarry Smith   } else {
6020f5bd95cSBarry Smith     SETERRQ1(1,1,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
60317ab2063SBarry Smith   }
6043a40ed3dSBarry Smith   PetscFunctionReturn(0);
60517ab2063SBarry Smith }
60619bcc07fSBarry Smith 
607c456f294SBarry Smith extern int Mat_AIJ_CheckInode(Mat);
6085615d1e5SSatish Balay #undef __FUNC__
6095615d1e5SSatish Balay #define __FUNC__ "MatAssemblyEnd_SeqAIJ"
6108f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
61117ab2063SBarry Smith {
612416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
61341c01911SSatish Balay   int        fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax,ierr;
61443ee02c3SBarry Smith   int        m = a->m,*ip,N,*ailen = a->ilen,shift = a->indexshift,rmax = 0;
615416022c9SBarry Smith   Scalar     *aa = a->a,*ap;
61617ab2063SBarry Smith 
6173a40ed3dSBarry Smith   PetscFunctionBegin;
6183a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
61917ab2063SBarry Smith 
62043ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
62117ab2063SBarry Smith   for (i=1; i<m; i++) {
622416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
62317ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
62494a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
62517ab2063SBarry Smith     if (fshift) {
6260f5bd95cSBarry Smith       ip = aj + ai[i] + shift;
6270f5bd95cSBarry Smith       ap = aa + ai[i] + shift;
62817ab2063SBarry Smith       N  = ailen[i];
62917ab2063SBarry Smith       for (j=0; j<N; j++) {
63017ab2063SBarry Smith         ip[j-fshift] = ip[j];
63117ab2063SBarry Smith         ap[j-fshift] = ap[j];
63217ab2063SBarry Smith       }
63317ab2063SBarry Smith     }
63417ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
63517ab2063SBarry Smith   }
63617ab2063SBarry Smith   if (m) {
63717ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
63817ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
63917ab2063SBarry Smith   }
64017ab2063SBarry Smith   /* reset ilen and imax for each row */
64117ab2063SBarry Smith   for (i=0; i<m; i++) {
64217ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
64317ab2063SBarry Smith   }
644416022c9SBarry Smith   a->nz = ai[m] + shift;
64517ab2063SBarry Smith 
64617ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
647416022c9SBarry Smith   if (fshift && a->diag) {
648606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
649416022c9SBarry Smith     PLogObjectMemory(A,-(m+1)*sizeof(int));
650416022c9SBarry Smith     a->diag = 0;
65117ab2063SBarry Smith   }
652c38d4ed2SBarry Smith   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded,%d used\n",m,a->n,fshift,a->nz);
653c38d4ed2SBarry Smith   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %d\n",a->reallocs);
65494a9d846SBarry Smith   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax);
655dd5f02e7SSatish Balay   a->reallocs          = 0;
6564e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
65736db0b34SBarry Smith   a->rmax              = rmax;
6584e220ebcSLois Curfman McInnes 
65976dd722bSSatish Balay   /* check out for identical nodes. If found, use inode functions */
66041c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(A);CHKERRQ(ierr);
6613a40ed3dSBarry Smith   PetscFunctionReturn(0);
66217ab2063SBarry Smith }
66317ab2063SBarry Smith 
6645615d1e5SSatish Balay #undef __FUNC__
6655615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_SeqAIJ"
6668f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A)
66717ab2063SBarry Smith {
668416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
669549d3d68SSatish Balay   int        ierr;
6703a40ed3dSBarry Smith 
6713a40ed3dSBarry Smith   PetscFunctionBegin;
672549d3d68SSatish Balay   ierr = PetscMemzero(a->a,(a->i[a->m]+a->indexshift)*sizeof(Scalar));CHKERRQ(ierr);
6733a40ed3dSBarry Smith   PetscFunctionReturn(0);
67417ab2063SBarry Smith }
675416022c9SBarry Smith 
6765615d1e5SSatish Balay #undef __FUNC__
6775615d1e5SSatish Balay #define __FUNC__ "MatDestroy_SeqAIJ"
678e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A)
67917ab2063SBarry Smith {
680416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
68182bf6240SBarry Smith   int        ierr;
682d5d45c9bSBarry Smith 
6833a40ed3dSBarry Smith   PetscFunctionBegin;
68494d884c6SBarry Smith 
68594d884c6SBarry Smith   if (A->mapping) {
68694d884c6SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(A->mapping);CHKERRQ(ierr);
68794d884c6SBarry Smith   }
68894d884c6SBarry Smith   if (A->bmapping) {
68994d884c6SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(A->bmapping);CHKERRQ(ierr);
69094d884c6SBarry Smith   }
69161b13de0SBarry Smith   if (A->rmap) {
69261b13de0SBarry Smith     ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
69361b13de0SBarry Smith   }
69461b13de0SBarry Smith   if (A->cmap) {
69561b13de0SBarry Smith     ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
69661b13de0SBarry Smith   }
697606d414cSSatish Balay   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
698aa482453SBarry Smith #if defined(PETSC_USE_LOG)
699e1311b90SBarry Smith   PLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",a->m,a->n,a->nz);
70017ab2063SBarry Smith #endif
70136db0b34SBarry Smith   if (a->freedata) {
702606d414cSSatish Balay     ierr = PetscFree(a->a);CHKERRQ(ierr);
703606d414cSSatish Balay     if (!a->singlemalloc) {
704606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
705606d414cSSatish Balay       ierr = PetscFree(a->j);CHKERRQ(ierr);
706606d414cSSatish Balay     }
70736db0b34SBarry Smith   }
708c38d4ed2SBarry Smith   if (a->row) {
709c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
710c38d4ed2SBarry Smith   }
711c38d4ed2SBarry Smith   if (a->col) {
712c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
713c38d4ed2SBarry Smith   }
714606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
715606d414cSSatish Balay   if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);}
716606d414cSSatish Balay   if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);}
717606d414cSSatish Balay   if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
718606d414cSSatish Balay   if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
71982bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
720606d414cSSatish Balay   if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);}
721606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
722eed86810SBarry Smith 
723f2655603SLois Curfman McInnes   PLogObjectDestroy(A);
724f2655603SLois Curfman McInnes   PetscHeaderDestroy(A);
7253a40ed3dSBarry Smith   PetscFunctionReturn(0);
72617ab2063SBarry Smith }
72717ab2063SBarry Smith 
7285615d1e5SSatish Balay #undef __FUNC__
7295615d1e5SSatish Balay #define __FUNC__ "MatCompress_SeqAIJ"
7308f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
73117ab2063SBarry Smith {
7323a40ed3dSBarry Smith   PetscFunctionBegin;
7333a40ed3dSBarry Smith   PetscFunctionReturn(0);
73417ab2063SBarry Smith }
73517ab2063SBarry Smith 
7365615d1e5SSatish Balay #undef __FUNC__
7375615d1e5SSatish Balay #define __FUNC__ "MatSetOption_SeqAIJ"
7388f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
73917ab2063SBarry Smith {
740416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7413a40ed3dSBarry Smith 
7423a40ed3dSBarry Smith   PetscFunctionBegin;
743f1e2ffcdSBarry Smith   if      (op == MAT_ROW_ORIENTED)                 a->roworiented       = PETSC_TRUE;
744f1e2ffcdSBarry Smith   else if (op == MAT_KEEP_ZEROED_ROWS)             a->keepzeroedrows    = PETSC_TRUE;
745f1e2ffcdSBarry Smith   else if (op == MAT_COLUMN_ORIENTED)              a->roworiented       = PETSC_FALSE;
746f1e2ffcdSBarry Smith   else if (op == MAT_COLUMNS_SORTED)               a->sorted            = PETSC_TRUE;
747f1e2ffcdSBarry Smith   else if (op == MAT_COLUMNS_UNSORTED)             a->sorted            = PETSC_FALSE;
7486d4a8577SBarry Smith   else if (op == MAT_NO_NEW_NONZERO_LOCATIONS)     a->nonew             = 1;
7494787f768SSatish Balay   else if (op == MAT_NEW_NONZERO_LOCATION_ERR)     a->nonew             = -1;
7504787f768SSatish Balay   else if (op == MAT_NEW_NONZERO_ALLOCATION_ERR)   a->nonew             = -2;
7516d4a8577SBarry Smith   else if (op == MAT_YES_NEW_NONZERO_LOCATIONS)    a->nonew             = 0;
75236db0b34SBarry Smith   else if (op == MAT_IGNORE_ZERO_ENTRIES)          a->ignorezeroentries = PETSC_TRUE;
753*b65db4caSBarry Smith   else if (op == MAT_USE_INODES)                   a->inode.use         = PETSC_TRUE;
754*b65db4caSBarry Smith   else if (op == MAT_DO_NOT_USE_INODES)            a->inode.use         = PETSC_FALSE;
7556d4a8577SBarry Smith   else if (op == MAT_ROWS_SORTED ||
756219d9a1aSLois Curfman McInnes            op == MAT_ROWS_UNSORTED ||
7576d4a8577SBarry Smith            op == MAT_SYMMETRIC ||
7586d4a8577SBarry Smith            op == MAT_STRUCTURALLY_SYMMETRIC ||
75990f02eecSBarry Smith            op == MAT_YES_NEW_DIAGONALS ||
760b51ba29fSSatish Balay            op == MAT_IGNORE_OFF_PROC_ENTRIES||
761b51ba29fSSatish Balay            op == MAT_USE_HASH_TABLE)
762981c4779SBarry Smith     PLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
7633a40ed3dSBarry Smith   else if (op == MAT_NO_NEW_DIAGONALS) {
7643a40ed3dSBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS");
7653a40ed3dSBarry Smith   } else if (op == MAT_INODE_LIMIT_1)          a->inode.limit  = 1;
7666d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_2)            a->inode.limit  = 2;
7676d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_3)            a->inode.limit  = 3;
7686d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_4)            a->inode.limit  = 4;
7696d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_5)            a->inode.limit  = 5;
7703a40ed3dSBarry Smith   else SETERRQ(PETSC_ERR_SUP,0,"unknown option");
7713a40ed3dSBarry Smith   PetscFunctionReturn(0);
77217ab2063SBarry Smith }
77317ab2063SBarry Smith 
7745615d1e5SSatish Balay #undef __FUNC__
7755615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_SeqAIJ"
7768f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
77717ab2063SBarry Smith {
778416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7793a40ed3dSBarry Smith   int        i,j,n,shift = a->indexshift,ierr;
78017ab2063SBarry Smith   Scalar     *x,zero = 0.0;
78117ab2063SBarry Smith 
7823a40ed3dSBarry Smith   PetscFunctionBegin;
7833a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
78436db0b34SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
78536db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
786a8c6a408SBarry Smith   if (n != a->m) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Nonconforming matrix and vector");
787416022c9SBarry Smith   for (i=0; i<a->m; i++) {
788416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
789416022c9SBarry Smith       if (a->j[j]+shift == i) {
790416022c9SBarry Smith         x[i] = a->a[j];
79117ab2063SBarry Smith         break;
79217ab2063SBarry Smith       }
79317ab2063SBarry Smith     }
79417ab2063SBarry Smith   }
79536db0b34SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
7963a40ed3dSBarry Smith   PetscFunctionReturn(0);
79717ab2063SBarry Smith }
79817ab2063SBarry Smith 
79917ab2063SBarry Smith /* -------------------------------------------------------*/
80017ab2063SBarry Smith /* Should check that shapes of vectors and matrices match */
80117ab2063SBarry Smith /* -------------------------------------------------------*/
8025615d1e5SSatish Balay #undef __FUNC__
8037c922b88SBarry Smith #define __FUNC__ "MatMultTranspose_SeqAIJ"
8047c922b88SBarry Smith int MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
80517ab2063SBarry Smith {
806416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
807f1af5d2fSBarry Smith   Scalar     *x,*y,*v,alpha,zero = 0.0;
808e1311b90SBarry Smith   int        ierr,m = a->m,n,i,*idx,shift = a->indexshift;
80917ab2063SBarry Smith 
8103a40ed3dSBarry Smith   PetscFunctionBegin;
811f1af5d2fSBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
812e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
813e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
81417ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
81517ab2063SBarry Smith   for (i=0; i<m; i++) {
816416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
817416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
818416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
81917ab2063SBarry Smith     alpha = x[i];
82017ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
82117ab2063SBarry Smith   }
822416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
823e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
824e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8253a40ed3dSBarry Smith   PetscFunctionReturn(0);
82617ab2063SBarry Smith }
827d5d45c9bSBarry Smith 
8285615d1e5SSatish Balay #undef __FUNC__
8297c922b88SBarry Smith #define __FUNC__ "MatMultTransposeAdd_SeqAIJ"
8307c922b88SBarry Smith int MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
83117ab2063SBarry Smith {
832416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
83317ab2063SBarry Smith   Scalar     *x,*y,*v,alpha;
834e1311b90SBarry Smith   int        ierr,m = a->m,n,i,*idx,shift = a->indexshift;
83517ab2063SBarry Smith 
8363a40ed3dSBarry Smith   PetscFunctionBegin;
8372e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
838e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
839e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
84017ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
84117ab2063SBarry Smith   for (i=0; i<m; i++) {
842416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
843416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
844416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
84517ab2063SBarry Smith     alpha = x[i];
84617ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
84717ab2063SBarry Smith   }
84890f02eecSBarry Smith   PLogFlops(2*a->nz);
849e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
850e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8513a40ed3dSBarry Smith   PetscFunctionReturn(0);
85217ab2063SBarry Smith }
85317ab2063SBarry Smith 
8545615d1e5SSatish Balay #undef __FUNC__
8555615d1e5SSatish Balay #define __FUNC__ "MatMult_SeqAIJ"
85644cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
85717ab2063SBarry Smith {
858416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
85917ab2063SBarry Smith   Scalar     *x,*y,*v,sum;
860e1311b90SBarry Smith   int        ierr,m = a->m,*idx,shift = a->indexshift,*ii;
861aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
862e36a17ebSSatish Balay   int        n,i,jrow,j;
863e36a17ebSSatish Balay #endif
86417ab2063SBarry Smith 
865aa482453SBarry Smith #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
866fee21e36SBarry Smith #pragma disjoint(*x,*y,*v)
867fee21e36SBarry Smith #endif
868fee21e36SBarry Smith 
8693a40ed3dSBarry Smith   PetscFunctionBegin;
870e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
871e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
87217ab2063SBarry Smith   x    = x + shift;    /* shift for Fortran start by 1 indexing */
873416022c9SBarry Smith   idx  = a->j;
874d64ed03dSBarry Smith   v    = a->a;
875416022c9SBarry Smith   ii   = a->i;
876aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
87729b5ca8aSSatish Balay   fortranmultaij_(&m,x,ii,idx+shift,v+shift,y);
8788d195f9aSBarry Smith #else
879d64ed03dSBarry Smith   v    += shift; /* shift for Fortran start by 1 indexing */
880d64ed03dSBarry Smith   idx  += shift;
88117ab2063SBarry Smith   for (i=0; i<m; i++) {
8829ea0dfa2SSatish Balay     jrow = ii[i];
8839ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
88417ab2063SBarry Smith     sum  = 0.0;
8859ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
8869ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8879ea0dfa2SSatish Balay      }
88817ab2063SBarry Smith     y[i] = sum;
88917ab2063SBarry Smith   }
8908d195f9aSBarry Smith #endif
891416022c9SBarry Smith   PLogFlops(2*a->nz - m);
892e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
893e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8943a40ed3dSBarry Smith   PetscFunctionReturn(0);
89517ab2063SBarry Smith }
89617ab2063SBarry Smith 
8975615d1e5SSatish Balay #undef __FUNC__
8985615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_SeqAIJ"
89944cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
90017ab2063SBarry Smith {
901416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
90217ab2063SBarry Smith   Scalar     *x,*y,*z,*v,sum;
903e1311b90SBarry Smith   int        ierr,m = a->m,*idx,shift = a->indexshift,*ii;
904aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
905e36a17ebSSatish Balay   int        n,i,jrow,j;
906e36a17ebSSatish Balay #endif
9079ea0dfa2SSatish Balay 
9083a40ed3dSBarry Smith   PetscFunctionBegin;
909e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
910e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9112e8a6d31SBarry Smith   if (zz != yy) {
912e1311b90SBarry Smith     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9132e8a6d31SBarry Smith   } else {
9142e8a6d31SBarry Smith     z = y;
9152e8a6d31SBarry Smith   }
91617ab2063SBarry Smith   x    = x + shift; /* shift for Fortran start by 1 indexing */
917cddf8d76SBarry Smith   idx  = a->j;
918d64ed03dSBarry Smith   v    = a->a;
919cddf8d76SBarry Smith   ii   = a->i;
920aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
92129b5ca8aSSatish Balay   fortranmultaddaij_(&m,x,ii,idx+shift,v+shift,y,z);
92202ab625aSSatish Balay #else
923d64ed03dSBarry Smith   v   += shift; /* shift for Fortran start by 1 indexing */
924d64ed03dSBarry Smith   idx += shift;
92517ab2063SBarry Smith   for (i=0; i<m; i++) {
9269ea0dfa2SSatish Balay     jrow = ii[i];
9279ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
92817ab2063SBarry Smith     sum  = y[i];
9299ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
9309ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9319ea0dfa2SSatish Balay      }
93217ab2063SBarry Smith     z[i] = sum;
93317ab2063SBarry Smith   }
93402ab625aSSatish Balay #endif
935416022c9SBarry Smith   PLogFlops(2*a->nz);
936e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
937e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9382e8a6d31SBarry Smith   if (zz != yy) {
939e1311b90SBarry Smith     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
9402e8a6d31SBarry Smith   }
9413a40ed3dSBarry Smith   PetscFunctionReturn(0);
94217ab2063SBarry Smith }
94317ab2063SBarry Smith 
94417ab2063SBarry Smith /*
94517ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
94617ab2063SBarry Smith */
9475615d1e5SSatish Balay #undef __FUNC__
948f1e2ffcdSBarry Smith #define __FUNC__ "MatMarkDiagonal_SeqAIJ"
949f1e2ffcdSBarry Smith int MatMarkDiagonal_SeqAIJ(Mat A)
95017ab2063SBarry Smith {
951416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
952416022c9SBarry Smith   int        i,j,*diag,m = a->m,shift = a->indexshift;
95317ab2063SBarry Smith 
9543a40ed3dSBarry Smith   PetscFunctionBegin;
955f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
956f1e2ffcdSBarry Smith 
9570452661fSBarry Smith   diag = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(diag);
958416022c9SBarry Smith   PLogObjectMemory(A,(m+1)*sizeof(int));
959416022c9SBarry Smith   for (i=0; i<a->m; i++) {
96035b0346bSBarry Smith     diag[i] = a->i[i+1];
961416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
962416022c9SBarry Smith       if (a->j[j]+shift == i) {
96317ab2063SBarry Smith         diag[i] = j - shift;
96417ab2063SBarry Smith         break;
96517ab2063SBarry Smith       }
96617ab2063SBarry Smith     }
96717ab2063SBarry Smith   }
968416022c9SBarry Smith   a->diag = diag;
9693a40ed3dSBarry Smith   PetscFunctionReturn(0);
97017ab2063SBarry Smith }
97117ab2063SBarry Smith 
972be5855fcSBarry Smith /*
973be5855fcSBarry Smith      Checks for missing diagonals
974be5855fcSBarry Smith */
975be5855fcSBarry Smith #undef __FUNC__
976f1e2ffcdSBarry Smith #define __FUNC__ "MatMissingDiagonal_SeqAIJ"
977f1e2ffcdSBarry Smith int MatMissingDiagonal_SeqAIJ(Mat A)
978be5855fcSBarry Smith {
979be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
980f1e2ffcdSBarry Smith   int        *diag,*jj = a->j,i,shift = a->indexshift,ierr;
981be5855fcSBarry Smith 
982be5855fcSBarry Smith   PetscFunctionBegin;
983f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
984f1e2ffcdSBarry Smith   diag = a->diag;
985be5855fcSBarry Smith   for (i=0; i<a->m; i++) {
986be5855fcSBarry Smith     if (jj[diag[i]+shift] != i-shift) {
987be5855fcSBarry Smith       SETERRQ1(1,1,"Matrix is missing diagonal number %d",i);
988be5855fcSBarry Smith     }
989be5855fcSBarry Smith   }
990be5855fcSBarry Smith   PetscFunctionReturn(0);
991be5855fcSBarry Smith }
992be5855fcSBarry Smith 
9935615d1e5SSatish Balay #undef __FUNC__
9945615d1e5SSatish Balay #define __FUNC__ "MatRelax_SeqAIJ"
99536db0b34SBarry Smith int MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,Vec xx)
99617ab2063SBarry Smith {
997416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
998d6ed3216SSatish Balay   Scalar     *x,*b,*bs, d,*xs,sum,*v = a->a,*t=0,scale,*ts,*xb,*idiag=0;
999d5d45c9bSBarry Smith   int        ierr,*idx,*diag,n = a->n,m = a->m,i,shift = a->indexshift;
100017ab2063SBarry Smith 
10013a40ed3dSBarry Smith   PetscFunctionBegin;
1002e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1003fb2e594dSBarry Smith   if (xx != bb) {
1004e1311b90SBarry Smith     ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
1005fb2e594dSBarry Smith   } else {
1006fb2e594dSBarry Smith     b = x;
1007fb2e594dSBarry Smith   }
1008fb2e594dSBarry Smith 
1009f1e2ffcdSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1010416022c9SBarry Smith   diag = a->diag;
1011416022c9SBarry Smith   xs   = x + shift; /* shifted by one for index start of a or a->j*/
101217ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
101317ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
101417ab2063SBarry Smith     bs = b + shift;
101517ab2063SBarry Smith     for (i=0; i<m; i++) {
1016416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1017416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1018488ecbafSBarry Smith 	PLogFlops(2*n-1);
1019416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1020416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
102117ab2063SBarry Smith         sum  = b[i]*d/omega;
102217ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
102317ab2063SBarry Smith         x[i] = sum;
102417ab2063SBarry Smith     }
1025cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1026fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
10273a40ed3dSBarry Smith     PetscFunctionReturn(0);
102817ab2063SBarry Smith   }
1029c783ea89SBarry Smith 
1030c783ea89SBarry Smith   /* setup workspace for Eisenstat */
1031c783ea89SBarry Smith   if (flag & SOR_EISENSTAT) {
1032c783ea89SBarry Smith     if (!a->idiag) {
1033c783ea89SBarry Smith       a->idiag = (Scalar*)PetscMalloc(2*m*sizeof(Scalar));CHKPTRQ(a->idiag);
1034c783ea89SBarry Smith       a->ssor  = a->idiag + m;
1035c783ea89SBarry Smith       v        = a->a;
1036c783ea89SBarry Smith       for (i=0; i<m; i++) { a->idiag[i] = 1.0/v[diag[i]];}
1037c783ea89SBarry Smith     }
1038c783ea89SBarry Smith     t     = a->ssor;
1039c783ea89SBarry Smith     idiag = a->idiag;
1040c783ea89SBarry Smith   }
1041fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1042fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1043fc3d8934SBarry Smith 
1044fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1045fc3d8934SBarry Smith 
1046fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1047fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
104848af12d7SBarry Smith     is the relaxation factor.
1049fc3d8934SBarry Smith     */
1050fc3d8934SBarry Smith 
105148af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
105248af12d7SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"SOR_APPLY_LOWER is not done");
105348af12d7SBarry Smith   } else if ((flag & SOR_EISENSTAT) && omega == 1.0 && shift == 0 && fshift == 0.0) {
105448af12d7SBarry Smith     /* special case for omega = 1.0 saves flops and some integer ops */
1055733d66baSBarry Smith     Scalar *v2;
105648af12d7SBarry Smith 
1057733d66baSBarry Smith     v2    = a->a;
1058fc3d8934SBarry Smith     /*  x = (E + U)^{-1} b */
1059fc3d8934SBarry Smith     for (i=m-1; i>=0; i--) {
1060fc3d8934SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1061fc3d8934SBarry Smith       idx  = a->j + diag[i] + 1;
1062fc3d8934SBarry Smith       v    = a->a + diag[i] + 1;
1063fc3d8934SBarry Smith       sum  = b[i];
1064fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1065b4632166SBarry Smith       x[i] = sum*idiag[i];
1066fc3d8934SBarry Smith 
1067fc3d8934SBarry Smith       /*  t = b - (2*E - D)x */
1068733d66baSBarry Smith       t[i] = b[i] - (v2[diag[i]])*x[i];
1069733d66baSBarry Smith     }
1070fc3d8934SBarry Smith 
1071fc3d8934SBarry Smith     /*  t = (E + L)^{-1}t */
1072fc3d8934SBarry Smith     diag = a->diag;
1073fc3d8934SBarry Smith     for (i=0; i<m; i++) {
1074fc3d8934SBarry Smith       n    = diag[i] - a->i[i];
1075fc3d8934SBarry Smith       idx  = a->j + a->i[i];
1076fc3d8934SBarry Smith       v    = a->a + a->i[i];
1077fc3d8934SBarry Smith       sum  = t[i];
1078fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,t,v,idx,n);
1079b4632166SBarry Smith       t[i]  = sum*idiag[i];
1080fc3d8934SBarry Smith 
1081fc3d8934SBarry Smith       /*  x = x + t */
1082733d66baSBarry Smith       x[i] += t[i];
1083733d66baSBarry Smith     }
1084733d66baSBarry Smith 
1085a4d9cbf6SBarry Smith     PLogFlops(3*m-1 + 2*a->nz);
1086fc3d8934SBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1087fc3d8934SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
1088fc3d8934SBarry Smith     PetscFunctionReturn(0);
10893a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
109017ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
109117ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
109217ab2063SBarry Smith 
109317ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
109417ab2063SBarry Smith 
109517ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
109617ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
109717ab2063SBarry Smith     is the relaxation factor.
109817ab2063SBarry Smith     */
109917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
110017ab2063SBarry Smith 
110117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
110217ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1103416022c9SBarry Smith       d    = fshift + a->a[diag[i] + shift];
1104416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1105416022c9SBarry Smith       idx  = a->j + diag[i] + (!shift);
1106416022c9SBarry Smith       v    = a->a + diag[i] + (!shift);
110717ab2063SBarry Smith       sum  = b[i];
110817ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
110917ab2063SBarry Smith       x[i] = omega*(sum/d);
111017ab2063SBarry Smith     }
111117ab2063SBarry Smith 
111217ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1113416022c9SBarry Smith     v = a->a;
111417ab2063SBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; }
111517ab2063SBarry Smith 
111617ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1117416022c9SBarry Smith     ts = t + shift; /* shifted by one for index start of a or a->j*/
1118416022c9SBarry Smith     diag = a->diag;
111917ab2063SBarry Smith     for (i=0; i<m; i++) {
1120416022c9SBarry Smith       d    = fshift + a->a[diag[i]+shift];
1121416022c9SBarry Smith       n    = diag[i] - a->i[i];
1122416022c9SBarry Smith       idx  = a->j + a->i[i] + shift;
1123416022c9SBarry Smith       v    = a->a + a->i[i] + shift;
112417ab2063SBarry Smith       sum  = t[i];
112517ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
112617ab2063SBarry Smith       t[i] = omega*(sum/d);
1127733d66baSBarry Smith       /*  x = x + t */
1128733d66baSBarry Smith       x[i] += t[i];
112917ab2063SBarry Smith     }
113017ab2063SBarry Smith 
1131c783ea89SBarry Smith     PLogFlops(6*m-1 + 2*a->nz);
1132cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1133fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
11343a40ed3dSBarry Smith     PetscFunctionReturn(0);
113517ab2063SBarry Smith   }
113617ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
113717ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
113817ab2063SBarry Smith       for (i=0; i<m; i++) {
1139416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1140416022c9SBarry Smith         n    = diag[i] - a->i[i];
1141488ecbafSBarry Smith 	PLogFlops(2*n-1);
1142416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1143416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
114417ab2063SBarry Smith         sum  = b[i];
114517ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
114617ab2063SBarry Smith         x[i] = omega*(sum/d);
114717ab2063SBarry Smith       }
114817ab2063SBarry Smith       xb = x;
11493a40ed3dSBarry Smith     } else xb = b;
115017ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
115117ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
115217ab2063SBarry Smith       for (i=0; i<m; i++) {
1153416022c9SBarry Smith         x[i] *= a->a[diag[i]+shift];
115417ab2063SBarry Smith       }
1155488ecbafSBarry Smith       PLogFlops(m);
115617ab2063SBarry Smith     }
115717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
115817ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1159416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1160416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1161488ecbafSBarry Smith 	PLogFlops(2*n-1);
1162416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1163416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
116417ab2063SBarry Smith         sum  = xb[i];
116517ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
116617ab2063SBarry Smith         x[i] = omega*(sum/d);
116717ab2063SBarry Smith       }
116817ab2063SBarry Smith     }
116917ab2063SBarry Smith     its--;
117017ab2063SBarry Smith   }
117117ab2063SBarry Smith   while (its--) {
117217ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
117317ab2063SBarry Smith       for (i=0; i<m; i++) {
1174416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1175416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1176488ecbafSBarry Smith 	PLogFlops(2*n-1);
1177416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1178416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
117917ab2063SBarry Smith         sum  = b[i];
118017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
11817e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
118217ab2063SBarry Smith       }
118317ab2063SBarry Smith     }
118417ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
118517ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1186416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1187416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1188488ecbafSBarry Smith 	PLogFlops(2*n-1);
1189416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1190416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
119117ab2063SBarry Smith         sum  = b[i];
119217ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
11937e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
119417ab2063SBarry Smith       }
119517ab2063SBarry Smith     }
119617ab2063SBarry Smith   }
1197cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1198fb2e594dSBarry Smith   if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
11993a40ed3dSBarry Smith   PetscFunctionReturn(0);
120017ab2063SBarry Smith }
120117ab2063SBarry Smith 
12025615d1e5SSatish Balay #undef __FUNC__
12035615d1e5SSatish Balay #define __FUNC__ "MatGetInfo_SeqAIJ"
12048f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
120517ab2063SBarry Smith {
1206416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12074e220ebcSLois Curfman McInnes 
12083a40ed3dSBarry Smith   PetscFunctionBegin;
12094e220ebcSLois Curfman McInnes   info->rows_global    = (double)a->m;
12104e220ebcSLois Curfman McInnes   info->columns_global = (double)a->n;
12114e220ebcSLois Curfman McInnes   info->rows_local     = (double)a->m;
12124e220ebcSLois Curfman McInnes   info->columns_local  = (double)a->n;
12134e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12144e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12154e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12164e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12174e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12184e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12194e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12204e220ebcSLois Curfman McInnes   if (A->factor) {
12214e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12224e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12234e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12244e220ebcSLois Curfman McInnes   } else {
12254e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12264e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12274e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12284e220ebcSLois Curfman McInnes   }
12293a40ed3dSBarry Smith   PetscFunctionReturn(0);
123017ab2063SBarry Smith }
123117ab2063SBarry Smith 
123236db0b34SBarry Smith extern int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,PetscReal,Mat*);
123317ab2063SBarry Smith extern int MatLUFactorNumeric_SeqAIJ(Mat,Mat*);
123436db0b34SBarry Smith extern int MatLUFactor_SeqAIJ(Mat,IS,IS,PetscReal);
123517ab2063SBarry Smith extern int MatSolve_SeqAIJ(Mat,Vec,Vec);
123617ab2063SBarry Smith extern int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
12377c922b88SBarry Smith extern int MatSolveTranspose_SeqAIJ(Mat,Vec,Vec);
12387c922b88SBarry Smith extern int MatSolveTransposeAdd_SeqAIJ(Mat,Vec,Vec,Vec);
123917ab2063SBarry Smith 
12405615d1e5SSatish Balay #undef __FUNC__
12415615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_SeqAIJ"
12428f6be9afSLois Curfman McInnes int MatZeroRows_SeqAIJ(Mat A,IS is,Scalar *diag)
124317ab2063SBarry Smith {
1244416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1245416022c9SBarry Smith   int         i,ierr,N,*rows,m = a->m - 1,shift = a->indexshift;
124617ab2063SBarry Smith 
12473a40ed3dSBarry Smith   PetscFunctionBegin;
124877c4ece6SBarry Smith   ierr = ISGetSize(is,&N);CHKERRQ(ierr);
124917ab2063SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
1250f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1251f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1252f1e2ffcdSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range");
1253f1e2ffcdSBarry Smith       ierr = PetscMemzero(&a->a[a->i[rows[i]]+shift],a->ilen[rows[i]]*sizeof(Scalar));CHKERRQ(ierr);
1254f1e2ffcdSBarry Smith     }
1255f1e2ffcdSBarry Smith     if (diag) {
1256f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1257f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1258f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1259f1e2ffcdSBarry Smith         a->a[a->diag[rows[i]]] = *diag;
1260f1e2ffcdSBarry Smith       }
1261f1e2ffcdSBarry Smith     }
1262f1e2ffcdSBarry Smith   } else {
126317ab2063SBarry Smith     if (diag) {
126417ab2063SBarry Smith       for (i=0; i<N; i++) {
1265a8c6a408SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range");
12667ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1267416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1268416022c9SBarry Smith           a->a[a->i[rows[i]]+shift] = *diag;
1269416022c9SBarry Smith           a->j[a->i[rows[i]]+shift] = rows[i]+shift;
12707ae801bdSBarry Smith         } else { /* in case row was completely empty */
1271d64ed03dSBarry Smith           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
127217ab2063SBarry Smith         }
127317ab2063SBarry Smith       }
12743a40ed3dSBarry Smith     } else {
127517ab2063SBarry Smith       for (i=0; i<N; i++) {
1276a8c6a408SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range");
1277416022c9SBarry Smith         a->ilen[rows[i]] = 0;
127817ab2063SBarry Smith       }
127917ab2063SBarry Smith     }
1280f1e2ffcdSBarry Smith   }
12817ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
128243a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12833a40ed3dSBarry Smith   PetscFunctionReturn(0);
128417ab2063SBarry Smith }
128517ab2063SBarry Smith 
12865615d1e5SSatish Balay #undef __FUNC__
12875615d1e5SSatish Balay #define __FUNC__ "MatGetSize_SeqAIJ"
12888f6be9afSLois Curfman McInnes int MatGetSize_SeqAIJ(Mat A,int *m,int *n)
128917ab2063SBarry Smith {
1290416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12913a40ed3dSBarry Smith 
12923a40ed3dSBarry Smith   PetscFunctionBegin;
12930752156aSBarry Smith   if (m) *m = a->m;
12940752156aSBarry Smith   if (n) *n = a->n;
12953a40ed3dSBarry Smith   PetscFunctionReturn(0);
129617ab2063SBarry Smith }
129717ab2063SBarry Smith 
12985615d1e5SSatish Balay #undef __FUNC__
12995615d1e5SSatish Balay #define __FUNC__ "MatGetOwnershipRange_SeqAIJ"
13008f6be9afSLois Curfman McInnes int MatGetOwnershipRange_SeqAIJ(Mat A,int *m,int *n)
130117ab2063SBarry Smith {
1302416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13033a40ed3dSBarry Smith 
13043a40ed3dSBarry Smith   PetscFunctionBegin;
1305416022c9SBarry Smith   *m = 0; *n = a->m;
13063a40ed3dSBarry Smith   PetscFunctionReturn(0);
130717ab2063SBarry Smith }
1308026e39d0SSatish Balay 
13095615d1e5SSatish Balay #undef __FUNC__
13105615d1e5SSatish Balay #define __FUNC__ "MatGetRow_SeqAIJ"
13114e093b46SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
131217ab2063SBarry Smith {
1313416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1314c456f294SBarry Smith   int        *itmp,i,shift = a->indexshift;
131517ab2063SBarry Smith 
13163a40ed3dSBarry Smith   PetscFunctionBegin;
1317a8c6a408SBarry Smith   if (row < 0 || row >= a->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row out of range");
131817ab2063SBarry Smith 
1319416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1320416022c9SBarry Smith   if (v) *v = a->a + a->i[row] + shift;
132117ab2063SBarry Smith   if (idx) {
1322416022c9SBarry Smith     itmp = a->j + a->i[row] + shift;
13234e093b46SBarry Smith     if (*nz && shift) {
13240452661fSBarry Smith       *idx = (int*)PetscMalloc((*nz)*sizeof(int));CHKPTRQ(*idx);
132517ab2063SBarry Smith       for (i=0; i<(*nz); i++) {(*idx)[i] = itmp[i] + shift;}
13264e093b46SBarry Smith     } else if (*nz) {
13274e093b46SBarry Smith       *idx = itmp;
132817ab2063SBarry Smith     }
132917ab2063SBarry Smith     else *idx = 0;
133017ab2063SBarry Smith   }
13313a40ed3dSBarry Smith   PetscFunctionReturn(0);
133217ab2063SBarry Smith }
133317ab2063SBarry Smith 
13345615d1e5SSatish Balay #undef __FUNC__
13355615d1e5SSatish Balay #define __FUNC__ "MatRestoreRow_SeqAIJ"
13364e093b46SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
133717ab2063SBarry Smith {
13384e093b46SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1339606d414cSSatish Balay   int ierr;
13403a40ed3dSBarry Smith 
13413a40ed3dSBarry Smith   PetscFunctionBegin;
1342606d414cSSatish Balay   if (idx) {if (*idx && a->indexshift) {ierr = PetscFree(*idx);CHKERRQ(ierr);}}
13433a40ed3dSBarry Smith   PetscFunctionReturn(0);
134417ab2063SBarry Smith }
134517ab2063SBarry Smith 
13465615d1e5SSatish Balay #undef __FUNC__
13475615d1e5SSatish Balay #define __FUNC__ "MatNorm_SeqAIJ"
134836db0b34SBarry Smith int MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *norm)
134917ab2063SBarry Smith {
1350416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1351416022c9SBarry Smith   Scalar     *v = a->a;
135236db0b34SBarry Smith   PetscReal  sum = 0.0;
1353549d3d68SSatish Balay   int        i,j,shift = a->indexshift,ierr;
135417ab2063SBarry Smith 
13553a40ed3dSBarry Smith   PetscFunctionBegin;
135617ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1357416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1358aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
135936db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
136017ab2063SBarry Smith #else
136117ab2063SBarry Smith       sum += (*v)*(*v); v++;
136217ab2063SBarry Smith #endif
136317ab2063SBarry Smith     }
136417ab2063SBarry Smith     *norm = sqrt(sum);
13653a40ed3dSBarry Smith   } else if (type == NORM_1) {
136636db0b34SBarry Smith     PetscReal *tmp;
1367416022c9SBarry Smith     int    *jj = a->j;
136836db0b34SBarry Smith     tmp = (PetscReal*)PetscMalloc((a->n+1)*sizeof(PetscReal));CHKPTRQ(tmp);
136936db0b34SBarry Smith     ierr = PetscMemzero(tmp,a->n*sizeof(PetscReal));CHKERRQ(ierr);
137017ab2063SBarry Smith     *norm = 0.0;
1371416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1372a2744918SBarry Smith         tmp[*jj++ + shift] += PetscAbsScalar(*v);  v++;
137317ab2063SBarry Smith     }
1374416022c9SBarry Smith     for (j=0; j<a->n; j++) {
137517ab2063SBarry Smith       if (tmp[j] > *norm) *norm = tmp[j];
137617ab2063SBarry Smith     }
1377606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13783a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
137917ab2063SBarry Smith     *norm = 0.0;
1380416022c9SBarry Smith     for (j=0; j<a->m; j++) {
1381416022c9SBarry Smith       v = a->a + a->i[j] + shift;
138217ab2063SBarry Smith       sum = 0.0;
1383416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1384cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
138517ab2063SBarry Smith       }
138617ab2063SBarry Smith       if (sum > *norm) *norm = sum;
138717ab2063SBarry Smith     }
13883a40ed3dSBarry Smith   } else {
1389a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"No support for two norm");
139017ab2063SBarry Smith   }
13913a40ed3dSBarry Smith   PetscFunctionReturn(0);
139217ab2063SBarry Smith }
139317ab2063SBarry Smith 
13945615d1e5SSatish Balay #undef __FUNC__
13955615d1e5SSatish Balay #define __FUNC__ "MatTranspose_SeqAIJ"
13968f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
139717ab2063SBarry Smith {
1398416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1399416022c9SBarry Smith   Mat        C;
1400416022c9SBarry Smith   int        i,ierr,*aj = a->j,*ai = a->i,m = a->m,len,*col;
140136db0b34SBarry Smith   int        shift = a->indexshift,refct;
1402d5d45c9bSBarry Smith   Scalar     *array = a->a;
140317ab2063SBarry Smith 
14043a40ed3dSBarry Smith   PetscFunctionBegin;
1405f1e2ffcdSBarry Smith   if (!B && m != a->n) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Square matrix only for in-place");
14060452661fSBarry Smith   col  = (int*)PetscMalloc((1+a->n)*sizeof(int));CHKPTRQ(col);
1407549d3d68SSatish Balay   ierr = PetscMemzero(col,(1+a->n)*sizeof(int));CHKERRQ(ierr);
140817ab2063SBarry Smith   if (shift) {
140917ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] -= 1;
141017ab2063SBarry Smith   }
141117ab2063SBarry Smith   for (i=0; i<ai[m]+shift; i++) col[aj[i]] += 1;
1412416022c9SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,a->n,m,0,col,&C);CHKERRQ(ierr);
1413606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
141417ab2063SBarry Smith   for (i=0; i<m; i++) {
141517ab2063SBarry Smith     len = ai[i+1]-ai[i];
1416416022c9SBarry Smith     ierr = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
141717ab2063SBarry Smith     array += len; aj += len;
141817ab2063SBarry Smith   }
141917ab2063SBarry Smith   if (shift) {
142017ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] += 1;
142117ab2063SBarry Smith   }
142217ab2063SBarry Smith 
14236d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14246d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
142517ab2063SBarry Smith 
1426f1e2ffcdSBarry Smith   if (B) {
1427416022c9SBarry Smith     *B = C;
142817ab2063SBarry Smith   } else {
1429f830108cSBarry Smith     PetscOps *Abops;
14300a6ffc59SBarry Smith     MatOps   Aops;
1431f830108cSBarry Smith 
1432416022c9SBarry Smith     /* This isn't really an in-place transpose */
1433606d414cSSatish Balay     ierr = PetscFree(a->a);CHKERRQ(ierr);
1434606d414cSSatish Balay     if (!a->singlemalloc) {
1435606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
1436606d414cSSatish Balay       ierr = PetscFree(a->j);
1437606d414cSSatish Balay     }
1438606d414cSSatish Balay     if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
1439606d414cSSatish Balay     if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);}
1440606d414cSSatish Balay     if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);}
1441606d414cSSatish Balay     if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
1442606d414cSSatish Balay     if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
1443606d414cSSatish Balay     ierr = PetscFree(a);CHKERRQ(ierr);
1444f830108cSBarry Smith 
1445488ecbafSBarry Smith 
1446488ecbafSBarry Smith     ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
1447488ecbafSBarry Smith     ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
1448488ecbafSBarry Smith 
1449f830108cSBarry Smith     /*
1450f830108cSBarry Smith       This is horrible, horrible code. We need to keep the
14518f0f457cSSatish Balay       the bops and ops Structures, copy everything from C
14528f0f457cSSatish Balay       including the function pointers..
1453f830108cSBarry Smith     */
1454549d3d68SSatish Balay     ierr     = PetscMemcpy(A->ops,C->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
1455549d3d68SSatish Balay     ierr     = PetscMemcpy(A->bops,C->bops,sizeof(PetscOps));CHKERRQ(ierr);
1456f830108cSBarry Smith     Abops    = A->bops;
1457f830108cSBarry Smith     Aops     = A->ops;
145836db0b34SBarry Smith     refct    = A->refct;
1459549d3d68SSatish Balay     ierr     = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr);
1460f830108cSBarry Smith     A->bops  = Abops;
1461f830108cSBarry Smith     A->ops   = Aops;
146227a8da17SBarry Smith     A->qlist = 0;
146336db0b34SBarry Smith     A->refct = refct;
146436db0b34SBarry Smith     /* copy over the type_name and name */
146536db0b34SBarry Smith     ierr     = PetscStrallocpy(C->type_name,&A->type_name);CHKERRQ(ierr);
146636db0b34SBarry Smith     ierr     = PetscStrallocpy(C->name,&A->name);CHKERRQ(ierr);
1467f830108cSBarry Smith 
14680452661fSBarry Smith     PetscHeaderDestroy(C);
146917ab2063SBarry Smith   }
14703a40ed3dSBarry Smith   PetscFunctionReturn(0);
147117ab2063SBarry Smith }
147217ab2063SBarry Smith 
14735615d1e5SSatish Balay #undef __FUNC__
14745615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_SeqAIJ"
14758f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
147617ab2063SBarry Smith {
1477416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
147817ab2063SBarry Smith   Scalar     *l,*r,x,*v;
1479e1311b90SBarry Smith   int        ierr,i,j,m = a->m,n = a->n,M,nz = a->nz,*jj,shift = a->indexshift;
148017ab2063SBarry Smith 
14813a40ed3dSBarry Smith   PetscFunctionBegin;
148217ab2063SBarry Smith   if (ll) {
14833ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14843ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1485e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1486a8c6a408SBarry Smith     if (m != a->m) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Left scaling vector wrong length");
1487e1311b90SBarry Smith     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1488416022c9SBarry Smith     v = a->a;
148917ab2063SBarry Smith     for (i=0; i<m; i++) {
149017ab2063SBarry Smith       x = l[i];
1491416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
149217ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
149317ab2063SBarry Smith     }
1494e1311b90SBarry Smith     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
149544cd7ae7SLois Curfman McInnes     PLogFlops(nz);
149617ab2063SBarry Smith   }
149717ab2063SBarry Smith   if (rr) {
1498e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1499a8c6a408SBarry Smith     if (n != a->n) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Right scaling vector wrong length");
1500e1311b90SBarry Smith     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1501416022c9SBarry Smith     v = a->a; jj = a->j;
150217ab2063SBarry Smith     for (i=0; i<nz; i++) {
150317ab2063SBarry Smith       (*v++) *= r[*jj++ + shift];
150417ab2063SBarry Smith     }
1505e1311b90SBarry Smith     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
150644cd7ae7SLois Curfman McInnes     PLogFlops(nz);
150717ab2063SBarry Smith   }
15083a40ed3dSBarry Smith   PetscFunctionReturn(0);
150917ab2063SBarry Smith }
151017ab2063SBarry Smith 
15115615d1e5SSatish Balay #undef __FUNC__
15125615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrix_SeqAIJ"
15137b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B)
151417ab2063SBarry Smith {
1515db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data,*c;
1516d5db1b72SBarry Smith   int          *smap,i,k,kstart,kend,ierr,oldcols = a->n,*lens;
151736db0b34SBarry Smith   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
151899141d43SSatish Balay   int          *irow,*icol,nrows,ncols,shift = a->indexshift,*ssmap;
151999141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
152099141d43SSatish Balay   Scalar       *a_new,*mat_a;
1521416022c9SBarry Smith   Mat          C;
1522fee21e36SBarry Smith   PetscTruth   stride;
152317ab2063SBarry Smith 
15243a40ed3dSBarry Smith   PetscFunctionBegin;
1525d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
1526a8c6a408SBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"ISrow is not sorted");
1527d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
1528a8c6a408SBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"IScol is not sorted");
152999141d43SSatish Balay 
153017ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
153117ab2063SBarry Smith   ierr = ISGetSize(isrow,&nrows);CHKERRQ(ierr);
153217ab2063SBarry Smith   ierr = ISGetSize(iscol,&ncols);CHKERRQ(ierr);
153317ab2063SBarry Smith 
1534fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1535fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1536fee21e36SBarry Smith   if (stride && step == 1) {
153702834360SBarry Smith     /* special case of contiguous rows */
153857faeb66SBarry Smith     lens   = (int*)PetscMalloc((ncols+nrows+1)*sizeof(int));CHKPTRQ(lens);
153902834360SBarry Smith     starts = lens + ncols;
154002834360SBarry Smith     /* loop over new rows determining lens and starting points */
154102834360SBarry Smith     for (i=0; i<nrows; i++) {
1542a2744918SBarry Smith       kstart  = ai[irow[i]]+shift;
1543a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
154402834360SBarry Smith       for (k=kstart; k<kend; k++) {
1545d8ced48eSBarry Smith         if (aj[k]+shift >= first) {
154602834360SBarry Smith           starts[i] = k;
154702834360SBarry Smith           break;
154802834360SBarry Smith 	}
154902834360SBarry Smith       }
1550a2744918SBarry Smith       sum = 0;
155102834360SBarry Smith       while (k < kend) {
1552d8ced48eSBarry Smith         if (aj[k++]+shift >= first+ncols) break;
1553a2744918SBarry Smith         sum++;
155402834360SBarry Smith       }
1555a2744918SBarry Smith       lens[i] = sum;
155602834360SBarry Smith     }
155702834360SBarry Smith     /* create submatrix */
1558cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
155908480c60SBarry Smith       int n_cols,n_rows;
156008480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
1561a8c6a408SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Reused submatrix wrong size");
1562d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
156308480c60SBarry Smith       C = *B;
15643a40ed3dSBarry Smith     } else {
156502834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
156608480c60SBarry Smith     }
1567db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1568db02288aSLois Curfman McInnes 
156902834360SBarry Smith     /* loop over rows inserting into submatrix */
1570db02288aSLois Curfman McInnes     a_new    = c->a;
1571db02288aSLois Curfman McInnes     j_new    = c->j;
1572db02288aSLois Curfman McInnes     i_new    = c->i;
1573db02288aSLois Curfman McInnes     i_new[0] = -shift;
157402834360SBarry Smith     for (i=0; i<nrows; i++) {
1575a2744918SBarry Smith       ii    = starts[i];
1576a2744918SBarry Smith       lensi = lens[i];
1577a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1578a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
157902834360SBarry Smith       }
1580549d3d68SSatish Balay       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(Scalar));CHKERRQ(ierr);
1581a2744918SBarry Smith       a_new      += lensi;
1582a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1583a2744918SBarry Smith       c->ilen[i]  = lensi;
158402834360SBarry Smith     }
1585606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15863a40ed3dSBarry Smith   } else {
158702834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
15880452661fSBarry Smith     smap  = (int*)PetscMalloc((1+oldcols)*sizeof(int));CHKPTRQ(smap);
158902834360SBarry Smith     ssmap = smap + shift;
159099141d43SSatish Balay     lens  = (int*)PetscMalloc((1+nrows)*sizeof(int));CHKPTRQ(lens);
1591549d3d68SSatish Balay     ierr  = PetscMemzero(smap,oldcols*sizeof(int));CHKERRQ(ierr);
159217ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
159302834360SBarry Smith     /* determine lens of each row */
159402834360SBarry Smith     for (i=0; i<nrows; i++) {
1595d8ced48eSBarry Smith       kstart  = ai[irow[i]]+shift;
159602834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
159702834360SBarry Smith       lens[i] = 0;
159802834360SBarry Smith       for (k=kstart; k<kend; k++) {
1599d8ced48eSBarry Smith         if (ssmap[aj[k]]) {
160002834360SBarry Smith           lens[i]++;
160102834360SBarry Smith         }
160202834360SBarry Smith       }
160302834360SBarry Smith     }
160417ab2063SBarry Smith     /* Create and fill new matrix */
1605a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
16060f5bd95cSBarry Smith       PetscTruth equal;
16070f5bd95cSBarry Smith 
160899141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1609a8c6a408SBarry Smith       if (c->m  != nrows || c->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong size");
16100f5bd95cSBarry Smith       ierr = PetscMemcmp(c->ilen,lens,c->m*sizeof(int),&equal);CHKERRQ(ierr);
16110f5bd95cSBarry Smith       if (!equal) {
1612a8c6a408SBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong no of nonzeros");
161399141d43SSatish Balay       }
1614549d3d68SSatish Balay       ierr = PetscMemzero(c->ilen,c->m*sizeof(int));CHKERRQ(ierr);
161508480c60SBarry Smith       C = *B;
16163a40ed3dSBarry Smith     } else {
161702834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
161808480c60SBarry Smith     }
161999141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
162017ab2063SBarry Smith     for (i=0; i<nrows; i++) {
162199141d43SSatish Balay       row    = irow[i];
162299141d43SSatish Balay       kstart = ai[row]+shift;
162399141d43SSatish Balay       kend   = kstart + a->ilen[row];
162499141d43SSatish Balay       mat_i  = c->i[i]+shift;
162599141d43SSatish Balay       mat_j  = c->j + mat_i;
162699141d43SSatish Balay       mat_a  = c->a + mat_i;
162799141d43SSatish Balay       mat_ilen = c->ilen + i;
162817ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
162999141d43SSatish Balay         if ((tcol=ssmap[a->j[k]])) {
163099141d43SSatish Balay           *mat_j++ = tcol - (!shift);
163199141d43SSatish Balay           *mat_a++ = a->a[k];
163299141d43SSatish Balay           (*mat_ilen)++;
163399141d43SSatish Balay 
163417ab2063SBarry Smith         }
163517ab2063SBarry Smith       }
163617ab2063SBarry Smith     }
163702834360SBarry Smith     /* Free work space */
163802834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1639606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1640606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
164102834360SBarry Smith   }
16426d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16436d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
164417ab2063SBarry Smith 
164517ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1646416022c9SBarry Smith   *B = C;
16473a40ed3dSBarry Smith   PetscFunctionReturn(0);
164817ab2063SBarry Smith }
164917ab2063SBarry Smith 
1650a871dcd8SBarry Smith /*
1651a871dcd8SBarry Smith */
16525615d1e5SSatish Balay #undef __FUNC__
16535615d1e5SSatish Balay #define __FUNC__ "MatILUFactor_SeqAIJ"
16545ef9f2a5SBarry Smith int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatILUInfo *info)
1655a871dcd8SBarry Smith {
165663b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
165708480c60SBarry Smith   int        ierr;
165863b91edcSBarry Smith   Mat        outA;
1659b8a78c4aSBarry Smith   PetscTruth row_identity,col_identity;
166063b91edcSBarry Smith 
16613a40ed3dSBarry Smith   PetscFunctionBegin;
1662b8a78c4aSBarry Smith   if (info && info->levels != 0) SETERRQ(PETSC_ERR_SUP,0,"Only levels=0 supported for in-place ilu");
1663b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1664b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1665b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
1666b8a78c4aSBarry Smith     SETERRQ(1,1,"Row and column permutations must be identity for in-place ILU");
1667b8a78c4aSBarry Smith   }
1668a871dcd8SBarry Smith 
166963b91edcSBarry Smith   outA          = inA;
167063b91edcSBarry Smith   inA->factor   = FACTOR_LU;
167163b91edcSBarry Smith   a->row        = row;
167263b91edcSBarry Smith   a->col        = col;
1673c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1674c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
167563b91edcSBarry Smith 
167636db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1677c38d4ed2SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* if this came from a previous factored; need to remove old one */
1678f0ec6fceSSatish Balay   ierr = ISInvertPermutation(col,&(a->icol));CHKERRQ(ierr);
16791e4dd532SSatish Balay   PLogObjectParent(inA,a->icol);
1680f0ec6fceSSatish Balay 
168194a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
16820452661fSBarry Smith     a->solve_work = (Scalar*)PetscMalloc((a->m+1)*sizeof(Scalar));CHKPTRQ(a->solve_work);
168394a9d846SBarry Smith   }
168463b91edcSBarry Smith 
168508480c60SBarry Smith   if (!a->diag) {
1686f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
168763b91edcSBarry Smith   }
168863b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA);CHKERRQ(ierr);
16893a40ed3dSBarry Smith   PetscFunctionReturn(0);
1690a871dcd8SBarry Smith }
1691a871dcd8SBarry Smith 
169274cdf0dfSBarry Smith #include "pinclude/blaslapack.h"
16935615d1e5SSatish Balay #undef __FUNC__
16945615d1e5SSatish Balay #define __FUNC__ "MatScale_SeqAIJ"
16958f6be9afSLois Curfman McInnes int MatScale_SeqAIJ(Scalar *alpha,Mat inA)
1696f0b747eeSBarry Smith {
1697f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
1698f0b747eeSBarry Smith   int        one = 1;
16993a40ed3dSBarry Smith 
17003a40ed3dSBarry Smith   PetscFunctionBegin;
1701f0b747eeSBarry Smith   BLscal_(&a->nz,alpha,a->a,&one);
1702f0b747eeSBarry Smith   PLogFlops(a->nz);
17033a40ed3dSBarry Smith   PetscFunctionReturn(0);
1704f0b747eeSBarry Smith }
1705f0b747eeSBarry Smith 
17065615d1e5SSatish Balay #undef __FUNC__
17075615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrices_SeqAIJ"
17087b2a1423SBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n,IS *irow,IS *icol,MatReuse scall,Mat **B)
1709cddf8d76SBarry Smith {
1710cddf8d76SBarry Smith   int ierr,i;
1711cddf8d76SBarry Smith 
17123a40ed3dSBarry Smith   PetscFunctionBegin;
1713cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
17140452661fSBarry Smith     *B = (Mat*)PetscMalloc((n+1)*sizeof(Mat));CHKPTRQ(*B);
1715cddf8d76SBarry Smith   }
1716cddf8d76SBarry Smith 
1717cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17186a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1719cddf8d76SBarry Smith   }
17203a40ed3dSBarry Smith   PetscFunctionReturn(0);
1721cddf8d76SBarry Smith }
1722cddf8d76SBarry Smith 
17235615d1e5SSatish Balay #undef __FUNC__
17245615d1e5SSatish Balay #define __FUNC__ "MatGetBlockSize_SeqAIJ"
17258f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A,int *bs)
17265a838052SSatish Balay {
1727f830108cSBarry Smith   PetscFunctionBegin;
17285a838052SSatish Balay   *bs = 1;
17293a40ed3dSBarry Smith   PetscFunctionReturn(0);
17305a838052SSatish Balay }
17315a838052SSatish Balay 
17325615d1e5SSatish Balay #undef __FUNC__
17335615d1e5SSatish Balay #define __FUNC__ "MatIncreaseOverlap_SeqAIJ"
17348f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A,int is_max,IS *is,int ov)
17354dcbc457SBarry Smith {
1736e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
173706763907SSatish Balay   int        shift,row,i,j,k,l,m,n,*idx,ierr,*nidx,isz,val;
17388a047759SSatish Balay   int        start,end,*ai,*aj;
1739f1af5d2fSBarry Smith   PetscBT    table;
1740bbd702dbSSatish Balay 
17413a40ed3dSBarry Smith   PetscFunctionBegin;
17428a047759SSatish Balay   shift = a->indexshift;
1743e4d965acSSatish Balay   m     = a->m;
1744e4d965acSSatish Balay   ai    = a->i;
17458a047759SSatish Balay   aj    = a->j+shift;
17468a047759SSatish Balay 
1747a8c6a408SBarry Smith   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"illegal overlap value used");
174806763907SSatish Balay 
174906763907SSatish Balay   nidx  = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(nidx);
17506831982aSBarry Smith   ierr  = PetscBTCreate(m,table);CHKERRQ(ierr);
175106763907SSatish Balay 
1752e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1753b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1754e4d965acSSatish Balay     isz  = 0;
17556831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1756e4d965acSSatish Balay 
1757e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17584dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
175977c4ece6SBarry Smith     ierr = ISGetSize(is[i],&n);CHKERRQ(ierr);
1760e4d965acSSatish Balay 
1761dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1762e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1763f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
17644dcbc457SBarry Smith     }
176506763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
176606763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1767e4d965acSSatish Balay 
176804a348a9SBarry Smith     k = 0;
176904a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
177004a348a9SBarry Smith       n = isz;
177106763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1772e4d965acSSatish Balay         row   = nidx[k];
1773e4d965acSSatish Balay         start = ai[row];
1774e4d965acSSatish Balay         end   = ai[row+1];
177504a348a9SBarry Smith         for (l = start; l<end ; l++){
17768a047759SSatish Balay           val = aj[l] + shift;
1777f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1778e4d965acSSatish Balay         }
1779e4d965acSSatish Balay       }
1780e4d965acSSatish Balay     }
1781029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1782e4d965acSSatish Balay   }
17836831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1784606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17853a40ed3dSBarry Smith   PetscFunctionReturn(0);
17864dcbc457SBarry Smith }
178717ab2063SBarry Smith 
17880513a670SBarry Smith /* -------------------------------------------------------------- */
17890513a670SBarry Smith #undef __FUNC__
17900513a670SBarry Smith #define __FUNC__ "MatPermute_SeqAIJ"
17910513a670SBarry Smith int MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
17920513a670SBarry Smith {
17930513a670SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
17940513a670SBarry Smith   Scalar     *vwork;
17950513a670SBarry Smith   int        i,ierr,nz,m = a->m,n = a->n,*cwork;
17960513a670SBarry Smith   int        *row,*col,*cnew,j,*lens;
179756cd22aeSBarry Smith   IS         icolp,irowp;
17980513a670SBarry Smith 
17993a40ed3dSBarry Smith   PetscFunctionBegin;
180056cd22aeSBarry Smith   ierr = ISInvertPermutation(rowp,&irowp);CHKERRQ(ierr);
180156cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
180256cd22aeSBarry Smith   ierr = ISInvertPermutation(colp,&icolp);CHKERRQ(ierr);
180356cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
18040513a670SBarry Smith 
18050513a670SBarry Smith   /* determine lengths of permuted rows */
18060513a670SBarry Smith   lens = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(lens);
18070513a670SBarry Smith   for (i=0; i<m; i++) {
18080513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
18090513a670SBarry Smith   }
18100513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
1811606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
18120513a670SBarry Smith 
18130513a670SBarry Smith   cnew = (int*)PetscMalloc(n*sizeof(int));CHKPTRQ(cnew);
18140513a670SBarry Smith   for (i=0; i<m; i++) {
18150513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18160513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
18170513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
18180513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18190513a670SBarry Smith   }
1820606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18210513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18220513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
182356cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
182456cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
182556cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
182656cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18273a40ed3dSBarry Smith   PetscFunctionReturn(0);
18280513a670SBarry Smith }
18290513a670SBarry Smith 
18305615d1e5SSatish Balay #undef __FUNC__
18315615d1e5SSatish Balay #define __FUNC__ "MatPrintHelp_SeqAIJ"
1832682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1833682d7d0cSBarry Smith {
1834c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1835682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1836d132466eSBarry Smith   int               ierr;
1837682d7d0cSBarry Smith 
18383a40ed3dSBarry Smith   PetscFunctionBegin;
1839c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1840d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1841d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1842d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
1843d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr);
1844d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr);
1845aa482453SBarry Smith #if defined(PETSC_HAVE_ESSL)
1846d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");CHKERRQ(ierr);
1847682d7d0cSBarry Smith #endif
18483a40ed3dSBarry Smith   PetscFunctionReturn(0);
1849682d7d0cSBarry Smith }
18508f6be9afSLois Curfman McInnes extern int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg);
1851a93ec695SBarry Smith extern int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
1852a93ec695SBarry Smith extern int MatColoringPatch_SeqAIJ(Mat,int,int *,ISColoring *);
185336db0b34SBarry Smith extern int MatILUDTFactor_SeqAIJ(Mat,MatILUInfo*,IS,IS,Mat*);
1854cb5b572fSBarry Smith #undef __FUNC__
1855cb5b572fSBarry Smith #define __FUNC__ "MatCopy_SeqAIJ"
1856cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1857cb5b572fSBarry Smith {
1858be6bf707SBarry Smith   int    ierr;
1859cb5b572fSBarry Smith 
1860cb5b572fSBarry Smith   PetscFunctionBegin;
1861be6bf707SBarry Smith   if (str == SAME_NONZERO_PATTERN && B->type == MATSEQAIJ) {
1862be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1863be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1864be6bf707SBarry Smith 
1865be6bf707SBarry Smith     if (a->i[a->m]+a->indexshift != b->i[b->m]+a->indexshift) {
1866be6bf707SBarry Smith       SETERRQ(1,1,"Number of nonzeros in two matrices are different");
1867cb5b572fSBarry Smith     }
1868549d3d68SSatish Balay     ierr = PetscMemcpy(b->a,a->a,(a->i[a->m]+a->indexshift)*sizeof(Scalar));CHKERRQ(ierr);
1869cb5b572fSBarry Smith   } else {
1870cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1871cb5b572fSBarry Smith   }
1872cb5b572fSBarry Smith   PetscFunctionReturn(0);
1873cb5b572fSBarry Smith }
1874cb5b572fSBarry Smith 
1875682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
18760a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
1877cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
1878cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
1879cb5b572fSBarry Smith        MatMult_SeqAIJ,
1880cb5b572fSBarry Smith        MatMultAdd_SeqAIJ,
18817c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
18827c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
1883cb5b572fSBarry Smith        MatSolve_SeqAIJ,
1884cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
18857c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
18867c922b88SBarry Smith        MatSolveTransposeAdd_SeqAIJ,
1887cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
1888cb5b572fSBarry Smith        0,
188917ab2063SBarry Smith        MatRelax_SeqAIJ,
189017ab2063SBarry Smith        MatTranspose_SeqAIJ,
1891cb5b572fSBarry Smith        MatGetInfo_SeqAIJ,
1892cb5b572fSBarry Smith        MatEqual_SeqAIJ,
1893cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
1894cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
1895cb5b572fSBarry Smith        MatNorm_SeqAIJ,
1896cb5b572fSBarry Smith        0,
1897cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
189817ab2063SBarry Smith        MatCompress_SeqAIJ,
1899cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
1900cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
1901cb5b572fSBarry Smith        MatZeroRows_SeqAIJ,
1902cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
1903cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
1904cb5b572fSBarry Smith        0,
1905cb5b572fSBarry Smith        0,
1906cb5b572fSBarry Smith        MatGetSize_SeqAIJ,
1907cb5b572fSBarry Smith        MatGetSize_SeqAIJ,
1908cb5b572fSBarry Smith        MatGetOwnershipRange_SeqAIJ,
1909cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
1910cb5b572fSBarry Smith        0,
1911cb5b572fSBarry Smith        0,
1912cb5b572fSBarry Smith        0,
1913be6bf707SBarry Smith        MatDuplicate_SeqAIJ,
1914cb5b572fSBarry Smith        0,
1915cb5b572fSBarry Smith        0,
1916cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
1917cb5b572fSBarry Smith        0,
1918cb5b572fSBarry Smith        0,
1919cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
1920cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
1921cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
1922cb5b572fSBarry Smith        MatCopy_SeqAIJ,
1923f0b747eeSBarry Smith        MatPrintHelp_SeqAIJ,
1924cb5b572fSBarry Smith        MatScale_SeqAIJ,
1925cb5b572fSBarry Smith        0,
1926cb5b572fSBarry Smith        0,
19276945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
19286945ee14SBarry Smith        MatGetBlockSize_SeqAIJ,
19293b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
19303b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
19313b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
1932a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
1933a93ec695SBarry Smith        MatFDColoringCreate_SeqAIJ,
19340513a670SBarry Smith        MatColoringPatch_SeqAIJ,
19350513a670SBarry Smith        0,
1936cda55fadSBarry Smith        MatPermute_SeqAIJ,
1937cda55fadSBarry Smith        0,
1938cda55fadSBarry Smith        0,
1939cda55fadSBarry Smith        0,
1940cda55fadSBarry Smith        0,
1941cda55fadSBarry Smith        MatGetMaps_Petsc};
194217ab2063SBarry Smith 
194317ab2063SBarry Smith extern int MatUseSuperLU_SeqAIJ(Mat);
194417ab2063SBarry Smith extern int MatUseEssl_SeqAIJ(Mat);
194517ab2063SBarry Smith extern int MatUseDXML_SeqAIJ(Mat);
194617ab2063SBarry Smith 
1947fb2e594dSBarry Smith EXTERN_C_BEGIN
19485615d1e5SSatish Balay #undef __FUNC__
1949bef8e0ddSBarry Smith #define __FUNC__ "MatSeqAIJSetColumnIndices_SeqAIJ"
195015091d37SBarry Smith 
1951bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices)
1952bef8e0ddSBarry Smith {
1953bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
1954bef8e0ddSBarry Smith   int        i,nz,n;
1955bef8e0ddSBarry Smith 
1956bef8e0ddSBarry Smith   PetscFunctionBegin;
1957bef8e0ddSBarry Smith   if (aij->indexshift) SETERRQ(1,1,"No support with 1 based indexing");
1958bef8e0ddSBarry Smith 
1959bef8e0ddSBarry Smith   nz = aij->maxnz;
1960bef8e0ddSBarry Smith   n  = aij->n;
1961bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
1962bef8e0ddSBarry Smith     aij->j[i] = indices[i];
1963bef8e0ddSBarry Smith   }
1964bef8e0ddSBarry Smith   aij->nz = nz;
1965bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
1966bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
1967bef8e0ddSBarry Smith   }
1968bef8e0ddSBarry Smith 
1969bef8e0ddSBarry Smith   PetscFunctionReturn(0);
1970bef8e0ddSBarry Smith }
1971fb2e594dSBarry Smith EXTERN_C_END
1972bef8e0ddSBarry Smith 
1973bef8e0ddSBarry Smith #undef __FUNC__
1974bef8e0ddSBarry Smith #define __FUNC__ "MatSeqAIJSetColumnIndices"
1975bef8e0ddSBarry Smith /*@
1976bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
1977bef8e0ddSBarry Smith        in the matrix.
1978bef8e0ddSBarry Smith 
1979bef8e0ddSBarry Smith   Input Parameters:
1980bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
1981bef8e0ddSBarry Smith -  indices - the column indices
1982bef8e0ddSBarry Smith 
198315091d37SBarry Smith   Level: advanced
198415091d37SBarry Smith 
1985bef8e0ddSBarry Smith   Notes:
1986bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
1987bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
1988bef8e0ddSBarry Smith   of the MatSetValues() operation.
1989bef8e0ddSBarry Smith 
1990bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
1991bef8e0ddSBarry Smith   MatCreateSeqAIJ().
1992bef8e0ddSBarry Smith 
1993bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
1994bef8e0ddSBarry Smith 
1995bef8e0ddSBarry Smith @*/
1996bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices)
1997bef8e0ddSBarry Smith {
1998bef8e0ddSBarry Smith   int ierr,(*f)(Mat,int *);
1999bef8e0ddSBarry Smith 
2000bef8e0ddSBarry Smith   PetscFunctionBegin;
2001bef8e0ddSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2002549d3d68SSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void **)&f);CHKERRQ(ierr);
2003bef8e0ddSBarry Smith   if (f) {
2004bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2005bef8e0ddSBarry Smith   } else {
2006bef8e0ddSBarry Smith     SETERRQ(1,1,"Wrong type of matrix to set column indices");
2007bef8e0ddSBarry Smith   }
2008bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2009bef8e0ddSBarry Smith }
2010bef8e0ddSBarry Smith 
2011be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2012be6bf707SBarry Smith 
2013fb2e594dSBarry Smith EXTERN_C_BEGIN
2014be6bf707SBarry Smith #undef __FUNC__
2015be6bf707SBarry Smith #define __FUNC__ "MatStoreValues_SeqAIJ"
2016be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat)
2017be6bf707SBarry Smith {
2018be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2019549d3d68SSatish Balay   int        nz = aij->i[aij->m]+aij->indexshift,ierr;
2020be6bf707SBarry Smith 
2021be6bf707SBarry Smith   PetscFunctionBegin;
2022be6bf707SBarry Smith   if (aij->nonew != 1) {
2023be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2024be6bf707SBarry Smith   }
2025be6bf707SBarry Smith 
2026be6bf707SBarry Smith   /* allocate space for values if not already there */
2027be6bf707SBarry Smith   if (!aij->saved_values) {
2028be6bf707SBarry Smith     aij->saved_values = (Scalar*)PetscMalloc(nz*sizeof(Scalar));CHKPTRQ(aij->saved_values);
2029be6bf707SBarry Smith   }
2030be6bf707SBarry Smith 
2031be6bf707SBarry Smith   /* copy values over */
2032549d3d68SSatish Balay   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(Scalar));CHKERRQ(ierr);
2033be6bf707SBarry Smith   PetscFunctionReturn(0);
2034be6bf707SBarry Smith }
2035fb2e594dSBarry Smith EXTERN_C_END
2036be6bf707SBarry Smith 
2037be6bf707SBarry Smith #undef __FUNC__
2038be6bf707SBarry Smith #define __FUNC__ "MatStoreValues"
2039be6bf707SBarry Smith /*@
2040be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2041be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2042be6bf707SBarry Smith        nonlinear portion.
2043be6bf707SBarry Smith 
2044be6bf707SBarry Smith    Collect on Mat
2045be6bf707SBarry Smith 
2046be6bf707SBarry Smith   Input Parameters:
2047be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2048be6bf707SBarry Smith 
204915091d37SBarry Smith   Level: advanced
205015091d37SBarry Smith 
2051be6bf707SBarry Smith   Common Usage, with SNESSolve():
2052be6bf707SBarry Smith $    Create Jacobian matrix
2053be6bf707SBarry Smith $    Set linear terms into matrix
2054be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2055be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2056be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2057be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2058be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2059be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2060be6bf707SBarry Smith $    In your Jacobian routine
2061be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2062be6bf707SBarry Smith $      Set nonlinear terms in matrix
2063be6bf707SBarry Smith 
2064be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2065be6bf707SBarry Smith $    // build linear portion of Jacobian
2066be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2067be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2068be6bf707SBarry Smith $    loop over nonlinear iterations
2069be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2070be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2071be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2072be6bf707SBarry Smith $       Solve linear system with Jacobian
2073be6bf707SBarry Smith $    endloop
2074be6bf707SBarry Smith 
2075be6bf707SBarry Smith   Notes:
2076be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2077be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2078be6bf707SBarry Smith     calling this routine.
2079be6bf707SBarry Smith 
2080be6bf707SBarry Smith .seealso: MatRetrieveValues()
2081be6bf707SBarry Smith 
2082be6bf707SBarry Smith @*/
2083be6bf707SBarry Smith int MatStoreValues(Mat mat)
2084be6bf707SBarry Smith {
2085be6bf707SBarry Smith   int ierr,(*f)(Mat);
2086be6bf707SBarry Smith 
2087be6bf707SBarry Smith   PetscFunctionBegin;
2088be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2089be6bf707SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2090be6bf707SBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2091be6bf707SBarry Smith 
2092c5d6004cSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void **)&f);CHKERRQ(ierr);
2093be6bf707SBarry Smith   if (f) {
2094be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2095be6bf707SBarry Smith   } else {
2096be6bf707SBarry Smith     SETERRQ(1,1,"Wrong type of matrix to store values");
2097be6bf707SBarry Smith   }
2098be6bf707SBarry Smith   PetscFunctionReturn(0);
2099be6bf707SBarry Smith }
2100be6bf707SBarry Smith 
2101fb2e594dSBarry Smith EXTERN_C_BEGIN
2102be6bf707SBarry Smith #undef __FUNC__
2103be6bf707SBarry Smith #define __FUNC__ "MatRetrieveValues_SeqAIJ"
2104be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat)
2105be6bf707SBarry Smith {
2106be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2107549d3d68SSatish Balay   int        nz = aij->i[aij->m]+aij->indexshift,ierr;
2108be6bf707SBarry Smith 
2109be6bf707SBarry Smith   PetscFunctionBegin;
2110be6bf707SBarry Smith   if (aij->nonew != 1) {
2111be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2112be6bf707SBarry Smith   }
2113be6bf707SBarry Smith   if (!aij->saved_values) {
2114be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatStoreValues(A);first");
2115be6bf707SBarry Smith   }
2116be6bf707SBarry Smith 
2117be6bf707SBarry Smith   /* copy values over */
2118549d3d68SSatish Balay   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(Scalar));CHKERRQ(ierr);
2119be6bf707SBarry Smith   PetscFunctionReturn(0);
2120be6bf707SBarry Smith }
2121fb2e594dSBarry Smith EXTERN_C_END
2122be6bf707SBarry Smith 
2123be6bf707SBarry Smith #undef __FUNC__
2124be6bf707SBarry Smith #define __FUNC__ "MatRetrieveValues"
2125be6bf707SBarry Smith /*@
2126be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2127be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2128be6bf707SBarry Smith        nonlinear portion.
2129be6bf707SBarry Smith 
2130be6bf707SBarry Smith    Collect on Mat
2131be6bf707SBarry Smith 
2132be6bf707SBarry Smith   Input Parameters:
2133be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2134be6bf707SBarry Smith 
213515091d37SBarry Smith   Level: advanced
213615091d37SBarry Smith 
2137be6bf707SBarry Smith .seealso: MatStoreValues()
2138be6bf707SBarry Smith 
2139be6bf707SBarry Smith @*/
2140be6bf707SBarry Smith int MatRetrieveValues(Mat mat)
2141be6bf707SBarry Smith {
2142be6bf707SBarry Smith   int ierr,(*f)(Mat);
2143be6bf707SBarry Smith 
2144be6bf707SBarry Smith   PetscFunctionBegin;
2145be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2146be6bf707SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2147be6bf707SBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2148be6bf707SBarry Smith 
2149c5d6004cSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void **)&f);CHKERRQ(ierr);
2150be6bf707SBarry Smith   if (f) {
2151be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2152be6bf707SBarry Smith   } else {
2153be6bf707SBarry Smith     SETERRQ(1,1,"Wrong type of matrix to retrieve values");
2154be6bf707SBarry Smith   }
2155be6bf707SBarry Smith   PetscFunctionReturn(0);
2156be6bf707SBarry Smith }
2157be6bf707SBarry Smith 
2158be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
2159cb5b572fSBarry Smith 
2160bef8e0ddSBarry Smith #undef __FUNC__
21615615d1e5SSatish Balay #define __FUNC__ "MatCreateSeqAIJ"
216217ab2063SBarry Smith /*@C
2163682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
21640d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
21656e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
216651c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
21672bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
216817ab2063SBarry Smith 
2169db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2170db81eaa0SLois Curfman McInnes 
217117ab2063SBarry Smith    Input Parameters:
2172db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
217317ab2063SBarry Smith .  m - number of rows
217417ab2063SBarry Smith .  n - number of columns
217517ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
217651c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
21772bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
217817ab2063SBarry Smith 
217917ab2063SBarry Smith    Output Parameter:
2180416022c9SBarry Smith .  A - the matrix
218117ab2063SBarry Smith 
2182b259b22eSLois Curfman McInnes    Notes:
218317ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
218417ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
21850002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
218644cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
218717ab2063SBarry Smith 
218817ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2189a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
21903d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
21916da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
219217ab2063SBarry Smith 
2193682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
21944fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2195682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
21966c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
21976c7ebb05SLois Curfman McInnes 
21986c7ebb05SLois Curfman McInnes    Options Database Keys:
2199db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2200db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2201db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2202db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2203db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
220417ab2063SBarry Smith 
2205027ccd11SLois Curfman McInnes    Level: intermediate
2206027ccd11SLois Curfman McInnes 
220736db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
220836db0b34SBarry Smith 
220917ab2063SBarry Smith @*/
2210416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz,Mat *A)
221117ab2063SBarry Smith {
2212416022c9SBarry Smith   Mat        B;
2213416022c9SBarry Smith   Mat_SeqAIJ *b;
2214f1af5d2fSBarry Smith   int        i,len,ierr,size;
2215f1af5d2fSBarry Smith   PetscTruth flg;
22166945ee14SBarry Smith 
22173a40ed3dSBarry Smith   PetscFunctionBegin;
2218d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2219a8c6a408SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Comm must be of size 1");
2220d5d45c9bSBarry Smith 
2221b73539f3SBarry Smith   if (nz < -2) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"nz cannot be less than -2: value %d",nz);
2222b73539f3SBarry Smith   if (nnz) {
2223b73539f3SBarry Smith     for (i=0; i<m; i++) {
2224b73539f3SBarry 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]);
2225b73539f3SBarry Smith     }
2226b73539f3SBarry Smith   }
2227b73539f3SBarry Smith 
2228416022c9SBarry Smith   *A                  = 0;
22293f1db9ecSBarry Smith   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATSEQAIJ,"Mat",comm,MatDestroy,MatView);
2230416022c9SBarry Smith   PLogObjectCreate(B);
22310452661fSBarry Smith   B->data             = (void*)(b = PetscNew(Mat_SeqAIJ));CHKPTRQ(b);
2232549d3d68SSatish Balay   ierr = PetscMemzero(b,sizeof(Mat_SeqAIJ));CHKERRQ(ierr);
2233549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2234e1311b90SBarry Smith   B->ops->destroy          = MatDestroy_SeqAIJ;
2235e1311b90SBarry Smith   B->ops->view             = MatView_SeqAIJ;
2236416022c9SBarry Smith   B->factor           = 0;
2237416022c9SBarry Smith   B->lupivotthreshold = 1.0;
223890f02eecSBarry Smith   B->mapping          = 0;
2239f1af5d2fSBarry Smith   ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr);
2240f1af5d2fSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2241416022c9SBarry Smith   b->row              = 0;
2242416022c9SBarry Smith   b->col              = 0;
224382bf6240SBarry Smith   b->icol             = 0;
2244416022c9SBarry Smith   b->indexshift       = 0;
2245b810aeb4SBarry Smith   b->reallocs         = 0;
224669957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_oneindex",&flg);CHKERRQ(ierr);
224769957df2SSatish Balay   if (flg) b->indexshift = -1;
224817ab2063SBarry Smith 
224944cd7ae7SLois Curfman McInnes   b->m = m; B->m = m; B->M = m;
225044cd7ae7SLois Curfman McInnes   b->n = n; B->n = n; B->N = n;
2251a5ae1ecdSBarry Smith 
22524d197716SBarry Smith   ierr = MapCreateMPI(comm,m,m,&B->rmap);CHKERRQ(ierr);
22534d197716SBarry Smith   ierr = MapCreateMPI(comm,n,n,&B->cmap);CHKERRQ(ierr);
2254a5ae1ecdSBarry Smith 
22550452661fSBarry Smith   b->imax = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(b->imax);
2256f1e2ffcdSBarry Smith   if (!nnz) {
22577b8455f0SLois Curfman McInnes     if (nz == PETSC_DEFAULT) nz = 10;
22587b8455f0SLois Curfman McInnes     else if (nz <= 0)        nz = 1;
2259416022c9SBarry Smith     for (i=0; i<m; i++) b->imax[i] = nz;
226017ab2063SBarry Smith     nz = nz*m;
22613a40ed3dSBarry Smith   } else {
226217ab2063SBarry Smith     nz = 0;
2263416022c9SBarry Smith     for (i=0; i<m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
226417ab2063SBarry Smith   }
226517ab2063SBarry Smith 
226617ab2063SBarry Smith   /* allocate the matrix space */
2267416022c9SBarry Smith   len             = nz*(sizeof(int) + sizeof(Scalar)) + (b->m+1)*sizeof(int);
22680452661fSBarry Smith   b->a            = (Scalar*)PetscMalloc(len);CHKPTRQ(b->a);
2269416022c9SBarry Smith   b->j            = (int*)(b->a + nz);
2270549d3d68SSatish Balay   ierr            = PetscMemzero(b->j,nz*sizeof(int));CHKERRQ(ierr);
2271416022c9SBarry Smith   b->i            = b->j + nz;
2272f1e2ffcdSBarry Smith   b->singlemalloc = PETSC_TRUE;
227336db0b34SBarry Smith   b->freedata     = PETSC_TRUE;
227417ab2063SBarry Smith 
2275416022c9SBarry Smith   b->i[0] = -b->indexshift;
227617ab2063SBarry Smith   for (i=1; i<m+1; i++) {
2277416022c9SBarry Smith     b->i[i] = b->i[i-1] + b->imax[i-1];
227817ab2063SBarry Smith   }
227917ab2063SBarry Smith 
2280416022c9SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
22810452661fSBarry Smith   b->ilen = (int*)PetscMalloc((m+1)*sizeof(int));
2282f09e8eb9SSatish Balay   PLogObjectMemory(B,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2283416022c9SBarry Smith   for (i=0; i<b->m; i++) { b->ilen[i] = 0;}
228417ab2063SBarry Smith 
2285416022c9SBarry Smith   b->nz                = 0;
2286416022c9SBarry Smith   b->maxnz             = nz;
2287f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
228836db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2289f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2290416022c9SBarry Smith   b->nonew             = 0;
2291416022c9SBarry Smith   b->diag              = 0;
2292416022c9SBarry Smith   b->solve_work        = 0;
229371bd300dSLois Curfman McInnes   b->spptr             = 0;
2294*b65db4caSBarry Smith   b->inode.use         = PETSC_TRUE;
2295754ec7b1SSatish Balay   b->inode.node_count  = 0;
2296754ec7b1SSatish Balay   b->inode.size        = 0;
22976c7ebb05SLois Curfman McInnes   b->inode.limit       = 5;
22986c7ebb05SLois Curfman McInnes   b->inode.max_limit   = 5;
2299be6bf707SBarry Smith   b->saved_values      = 0;
23004e220ebcSLois Curfman McInnes   B->info.nz_unneeded  = (double)b->maxnz;
2301d7f994e1SBarry Smith   b->idiag             = 0;
2302d7f994e1SBarry Smith   b->ssor              = 0;
2303f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
230417ab2063SBarry Smith 
2305416022c9SBarry Smith   *A = B;
23064e220ebcSLois Curfman McInnes 
23074b14c69eSBarry Smith   /*  SuperLU is not currently supported through PETSc */
2308aa482453SBarry Smith #if defined(PETSC_HAVE_SUPERLU)
230969957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_superlu",&flg);CHKERRQ(ierr);
231069957df2SSatish Balay   if (flg) { ierr = MatUseSuperLU_SeqAIJ(B);CHKERRQ(ierr); }
23114b14c69eSBarry Smith #endif
231269957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_essl",&flg);CHKERRQ(ierr);
231369957df2SSatish Balay   if (flg) { ierr = MatUseEssl_SeqAIJ(B);CHKERRQ(ierr); }
231469957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_dxml",&flg);CHKERRQ(ierr);
231569957df2SSatish Balay   if (flg) {
2316a8c6a408SBarry Smith     if (!b->indexshift) SETERRQ(PETSC_ERR_LIB,0,"need -mat_aij_oneindex with -mat_aij_dxml");
2317416022c9SBarry Smith     ierr = MatUseDXML_SeqAIJ(B);CHKERRQ(ierr);
231817ab2063SBarry Smith   }
231969957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
232069957df2SSatish Balay   if (flg) {ierr = MatPrintHelp(B);CHKERRQ(ierr); }
2321bef8e0ddSBarry Smith 
2322f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2323bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2324bef8e0ddSBarry Smith                                      (void*)MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2325f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2326be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2327be6bf707SBarry Smith                                      (void*)MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2328f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2329be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2330be6bf707SBarry Smith                                      (void*)MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
23313a40ed3dSBarry Smith   PetscFunctionReturn(0);
233217ab2063SBarry Smith }
233317ab2063SBarry Smith 
23345615d1e5SSatish Balay #undef __FUNC__
2335be6bf707SBarry Smith #define __FUNC__ "MatDuplicate_SeqAIJ"
2336be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
233717ab2063SBarry Smith {
2338416022c9SBarry Smith   Mat        C;
2339416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data;
2340bef8e0ddSBarry Smith   int        i,len,m = a->m,shift = a->indexshift,ierr;
234117ab2063SBarry Smith 
23423a40ed3dSBarry Smith   PetscFunctionBegin;
23434043dd9cSLois Curfman McInnes   *B = 0;
23443f1db9ecSBarry Smith   PetscHeaderCreate(C,_p_Mat,struct _MatOps,MAT_COOKIE,MATSEQAIJ,"Mat",A->comm,MatDestroy,MatView);
2345416022c9SBarry Smith   PLogObjectCreate(C);
23460452661fSBarry Smith   C->data           = (void*)(c = PetscNew(Mat_SeqAIJ));CHKPTRQ(c);
2347549d3d68SSatish Balay   ierr              = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
2348e1311b90SBarry Smith   C->ops->destroy   = MatDestroy_SeqAIJ;
2349e1311b90SBarry Smith   C->ops->view      = MatView_SeqAIJ;
2350416022c9SBarry Smith   C->factor         = A->factor;
2351416022c9SBarry Smith   c->row            = 0;
2352416022c9SBarry Smith   c->col            = 0;
235382bf6240SBarry Smith   c->icol           = 0;
2354416022c9SBarry Smith   c->indexshift     = shift;
2355f1e2ffcdSBarry Smith   c->keepzeroedrows = a->keepzeroedrows;
2356c456f294SBarry Smith   C->assembled      = PETSC_TRUE;
235717ab2063SBarry Smith 
235844cd7ae7SLois Curfman McInnes   c->m = C->m   = a->m;
235944cd7ae7SLois Curfman McInnes   c->n = C->n   = a->n;
236044cd7ae7SLois Curfman McInnes   C->M          = a->m;
236144cd7ae7SLois Curfman McInnes   C->N          = a->n;
236217ab2063SBarry Smith 
23630452661fSBarry Smith   c->imax       = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->imax);
23640452661fSBarry Smith   c->ilen       = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->ilen);
236517ab2063SBarry Smith   for (i=0; i<m; i++) {
2366416022c9SBarry Smith     c->imax[i] = a->imax[i];
2367416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
236817ab2063SBarry Smith   }
236917ab2063SBarry Smith 
237017ab2063SBarry Smith   /* allocate the matrix space */
2371f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
2372416022c9SBarry Smith   len     = (m+1)*sizeof(int)+(a->i[m])*(sizeof(Scalar)+sizeof(int));
23730452661fSBarry Smith   c->a  = (Scalar*)PetscMalloc(len);CHKPTRQ(c->a);
2374416022c9SBarry Smith   c->j  = (int*)(c->a + a->i[m] + shift);
2375416022c9SBarry Smith   c->i  = c->j + a->i[m] + shift;
2376549d3d68SSatish Balay   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));CHKERRQ(ierr);
237717ab2063SBarry Smith   if (m > 0) {
2378549d3d68SSatish Balay     ierr = PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int));CHKERRQ(ierr);
2379be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2380549d3d68SSatish Balay       ierr = PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(Scalar));CHKERRQ(ierr);
2381be6bf707SBarry Smith     } else {
2382549d3d68SSatish Balay       ierr = PetscMemzero(c->a,(a->i[m]+shift)*sizeof(Scalar));CHKERRQ(ierr);
238317ab2063SBarry Smith     }
238408480c60SBarry Smith   }
238517ab2063SBarry Smith 
2386f09e8eb9SSatish Balay   PLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2387416022c9SBarry Smith   c->sorted      = a->sorted;
2388416022c9SBarry Smith   c->roworiented = a->roworiented;
2389416022c9SBarry Smith   c->nonew       = a->nonew;
23907a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
2391be6bf707SBarry Smith   c->saved_values = 0;
2392d7f994e1SBarry Smith   c->idiag        = 0;
2393d7f994e1SBarry Smith   c->ssor         = 0;
239436db0b34SBarry Smith   c->ignorezeroentries = a->ignorezeroentries;
239536db0b34SBarry Smith   c->freedata     = PETSC_TRUE;
239617ab2063SBarry Smith 
2397416022c9SBarry Smith   if (a->diag) {
23980452661fSBarry Smith     c->diag = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->diag);
2399416022c9SBarry Smith     PLogObjectMemory(C,(m+1)*sizeof(int));
240017ab2063SBarry Smith     for (i=0; i<m; i++) {
2401416022c9SBarry Smith       c->diag[i] = a->diag[i];
240217ab2063SBarry Smith     }
24033a40ed3dSBarry Smith   } else c->diag        = 0;
2404*b65db4caSBarry Smith   c->inode.use          = a->inode.use;
24056c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
24066c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
2407754ec7b1SSatish Balay   if (a->inode.size){
2408daed632aSSatish Balay     c->inode.size       = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(c->inode.size);
2409754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
2410549d3d68SSatish Balay     ierr = PetscMemcpy(c->inode.size,a->inode.size,(m+1)*sizeof(int));CHKERRQ(ierr);
2411754ec7b1SSatish Balay   } else {
2412754ec7b1SSatish Balay     c->inode.size       = 0;
2413754ec7b1SSatish Balay     c->inode.node_count = 0;
2414754ec7b1SSatish Balay   }
2415416022c9SBarry Smith   c->nz                 = a->nz;
2416416022c9SBarry Smith   c->maxnz              = a->maxnz;
2417416022c9SBarry Smith   c->solve_work         = 0;
241876dd722bSSatish Balay   c->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
2419754ec7b1SSatish Balay 
2420416022c9SBarry Smith   *B = C;
24217b2a1423SBarry Smith   ierr = FListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
24223a40ed3dSBarry Smith   PetscFunctionReturn(0);
242317ab2063SBarry Smith }
242417ab2063SBarry Smith 
24255615d1e5SSatish Balay #undef __FUNC__
24265615d1e5SSatish Balay #define __FUNC__ "MatLoad_SeqAIJ"
242719bcc07fSBarry Smith int MatLoad_SeqAIJ(Viewer viewer,MatType type,Mat *A)
242817ab2063SBarry Smith {
2429416022c9SBarry Smith   Mat_SeqAIJ   *a;
2430416022c9SBarry Smith   Mat          B;
243117699dbbSLois Curfman McInnes   int          i,nz,ierr,fd,header[4],size,*rowlengths = 0,M,N,shift;
2432bcd2baecSBarry Smith   MPI_Comm     comm;
243317ab2063SBarry Smith 
24343a40ed3dSBarry Smith   PetscFunctionBegin;
2435e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2436d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2437a8c6a408SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,0,"view must have one processor");
243890ace30eSBarry Smith   ierr = ViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
24390752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2440a8c6a408SBarry Smith   if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"not matrix object in file");
244117ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
244217ab2063SBarry Smith 
2443d64ed03dSBarry Smith   if (nz < 0) {
2444a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,1,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2445d64ed03dSBarry Smith   }
2446d64ed03dSBarry Smith 
244717ab2063SBarry Smith   /* read in row lengths */
24480452661fSBarry Smith   rowlengths = (int*)PetscMalloc(M*sizeof(int));CHKPTRQ(rowlengths);
24490752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
245017ab2063SBarry Smith 
245117ab2063SBarry Smith   /* create our matrix */
2452416022c9SBarry Smith   ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A);CHKERRQ(ierr);
2453416022c9SBarry Smith   B = *A;
2454416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
2455416022c9SBarry Smith   shift = a->indexshift;
245617ab2063SBarry Smith 
245717ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
24580752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
245917ab2063SBarry Smith   if (shift) {
246017ab2063SBarry Smith     for (i=0; i<nz; i++) {
2461416022c9SBarry Smith       a->j[i] += 1;
246217ab2063SBarry Smith     }
246317ab2063SBarry Smith   }
246417ab2063SBarry Smith 
246517ab2063SBarry Smith   /* read in nonzero values */
24660752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
246717ab2063SBarry Smith 
246817ab2063SBarry Smith   /* set matrix "i" values */
2469416022c9SBarry Smith   a->i[0] = -shift;
247017ab2063SBarry Smith   for (i=1; i<= M; i++) {
2471416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2472416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
247317ab2063SBarry Smith   }
2474606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
247517ab2063SBarry Smith 
24766d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24776d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24783a40ed3dSBarry Smith   PetscFunctionReturn(0);
247917ab2063SBarry Smith }
248017ab2063SBarry Smith 
24815615d1e5SSatish Balay #undef __FUNC__
24825615d1e5SSatish Balay #define __FUNC__ "MatEqual_SeqAIJ"
24838f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
24847264ac53SSatish Balay {
24857264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
24860f5bd95cSBarry Smith   int        ierr;
24877264ac53SSatish Balay 
24883a40ed3dSBarry Smith   PetscFunctionBegin;
2489a8c6a408SBarry Smith   if (B->type !=MATSEQAIJ)SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type");
24907264ac53SSatish Balay 
24917264ac53SSatish Balay   /* If the  matrix dimensions are not equal,or no of nonzeros or shift */
24927264ac53SSatish Balay   if ((a->m != b->m) || (a->n !=b->n) ||(a->nz != b->nz)||
2493bcd2baecSBarry Smith       (a->indexshift != b->indexshift)) {
24943a40ed3dSBarry Smith     *flg = PETSC_FALSE; PetscFunctionReturn(0);
2495bcd2baecSBarry Smith   }
24967264ac53SSatish Balay 
24977264ac53SSatish Balay   /* if the a->i are the same */
24980f5bd95cSBarry Smith   ierr = PetscMemcmp(a->i,b->i,(a->m+1)*sizeof(int),flg);CHKERRQ(ierr);
24990f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
25007264ac53SSatish Balay 
25017264ac53SSatish Balay   /* if a->j are the same */
25020f5bd95cSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),flg);CHKERRQ(ierr);
25030f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
2504bcd2baecSBarry Smith 
2505bcd2baecSBarry Smith   /* if a->a are the same */
25060f5bd95cSBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(Scalar),flg);CHKERRQ(ierr);
25070f5bd95cSBarry Smith 
25083a40ed3dSBarry Smith   PetscFunctionReturn(0);
25097264ac53SSatish Balay 
25107264ac53SSatish Balay }
251136db0b34SBarry Smith 
251236db0b34SBarry Smith #undef __FUNC__
251336db0b34SBarry Smith #define __FUNC__ "MatCreateSeqAIJWithArrays"
251436db0b34SBarry Smith /*@C
251536db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
251636db0b34SBarry Smith               provided by the user.
251736db0b34SBarry Smith 
251836db0b34SBarry Smith       Coolective on MPI_Comm
251936db0b34SBarry Smith 
252036db0b34SBarry Smith    Input Parameters:
252136db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
252236db0b34SBarry Smith .   m - number of rows
252336db0b34SBarry Smith .   n - number of columns
252436db0b34SBarry Smith .   i - row indices
252536db0b34SBarry Smith .   j - column indices
252636db0b34SBarry Smith -   a - matrix values
252736db0b34SBarry Smith 
252836db0b34SBarry Smith    Output Parameter:
252936db0b34SBarry Smith .   mat - the matrix
253036db0b34SBarry Smith 
253136db0b34SBarry Smith    Level: intermediate
253236db0b34SBarry Smith 
253336db0b34SBarry Smith    Notes:
253436db0b34SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these routines
253536db0b34SBarry Smith     once the matrix is destroyed
253636db0b34SBarry Smith 
253736db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
253836db0b34SBarry Smith 
253936db0b34SBarry Smith        The i and j indices can be either 0- or 1 based
254036db0b34SBarry Smith 
254136db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
254236db0b34SBarry Smith 
254336db0b34SBarry Smith @*/
254436db0b34SBarry Smith int MatCreateSeqAIJWithArrays(MPI_Comm comm,int m,int n,int* i,int*j,Scalar *a,Mat *mat)
254536db0b34SBarry Smith {
254636db0b34SBarry Smith   int        ierr,ii;
254736db0b34SBarry Smith   Mat_SeqAIJ *aij;
254836db0b34SBarry Smith 
254936db0b34SBarry Smith   PetscFunctionBegin;
255036db0b34SBarry Smith   ierr = MatCreateSeqAIJ(comm,m,n,0,0,mat);CHKERRQ(ierr);
255136db0b34SBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
255236db0b34SBarry Smith   ierr = PetscFree(aij->a);CHKERRQ(ierr);
255336db0b34SBarry Smith 
255436db0b34SBarry Smith   if (i[0] == 1) {
255536db0b34SBarry Smith     aij->indexshift = -1;
255636db0b34SBarry Smith   } else if (i[0]) {
255736db0b34SBarry Smith     SETERRQ(1,1,"i (row indices) do not start with 0 or 1");
255836db0b34SBarry Smith   }
255936db0b34SBarry Smith   aij->i = i;
256036db0b34SBarry Smith   aij->j = j;
256136db0b34SBarry Smith   aij->a = a;
256236db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
256336db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
256436db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
256536db0b34SBarry Smith 
256636db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
256736db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
256836db0b34SBarry Smith #if defined(PETSC_BOPT_g)
256936db0b34SBarry 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]);
257036db0b34SBarry Smith #endif
257136db0b34SBarry Smith   }
257236db0b34SBarry Smith #if defined(PETSC_BOPT_g)
257336db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
257436db0b34SBarry Smith     if (j[ii] < -aij->indexshift) SETERRQ2(1,1,"Negative column index at location = %d index = %d",ii,j[ii]);
257536db0b34SBarry Smith     if (j[ii] > n - 1 -aij->indexshift) SETERRQ2(1,1,"Column index to large at location = %d index = %d",ii,j[ii]);
257636db0b34SBarry Smith   }
257736db0b34SBarry Smith #endif
257836db0b34SBarry Smith 
2579*b65db4caSBarry Smith   /* changes indices to start at 0 */
2580*b65db4caSBarry Smith   if (i[0]) {
2581*b65db4caSBarry Smith     aij->indexshift = 0;
2582*b65db4caSBarry Smith     for (ii=0; ii<m; ii++) {
2583*b65db4caSBarry Smith       i[ii]--;
2584*b65db4caSBarry Smith     }
2585*b65db4caSBarry Smith     for (ii=0; ii<i[m]; ii++) {
2586*b65db4caSBarry Smith       j[ii]--;
2587*b65db4caSBarry Smith     }
2588*b65db4caSBarry Smith   }
2589*b65db4caSBarry Smith 
2590*b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2591*b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
259236db0b34SBarry Smith   PetscFunctionReturn(0);
259336db0b34SBarry Smith }
259436db0b34SBarry Smith 
259536db0b34SBarry Smith 
259636db0b34SBarry Smith 
2597