xref: /petsc/src/mat/impls/aij/seq/aij.c (revision b8a78c4a112c1f7cf1d4f71aab23e55422181bbb)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*b8a78c4aSBarry Smith static char vcid[] = "$Id: aij.c,v 1.319 1999/04/19 22:11:59 bsmith Exp bsmith $";
317ab2063SBarry Smith #endif
417ab2063SBarry Smith 
5d5d45c9bSBarry Smith /*
63369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
7d5d45c9bSBarry Smith   matrix storage format.
8d5d45c9bSBarry Smith */
93369ce9aSBarry Smith 
103369ce9aSBarry Smith #include "sys.h"
1170f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
12f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
13f5eb4b81SSatish Balay #include "src/inline/spops.h"
148d195f9aSBarry Smith #include "src/inline/dot.h"
15eeb667a2SSatish Balay #include "bitarray.h"
1617ab2063SBarry Smith 
17a2ce50c7SBarry Smith /*
18a2ce50c7SBarry Smith     Basic AIJ format ILU based on drop tolerance
19a2ce50c7SBarry Smith */
205615d1e5SSatish Balay #undef __FUNC__
215615d1e5SSatish Balay #define __FUNC__ "MatILUDTFactor_SeqAIJ"
22a2ce50c7SBarry Smith int MatILUDTFactor_SeqAIJ(Mat A,double dt,int maxnz,IS row,IS col,Mat *fact)
23a2ce50c7SBarry Smith {
24a2ce50c7SBarry Smith   /* Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; */
25a2ce50c7SBarry Smith 
263a40ed3dSBarry Smith   PetscFunctionBegin;
273f1db9ecSBarry Smith   SETERRQ(1,0,"Not implemented");
28d64ed03dSBarry Smith #if !defined(USE_PETSC_DEBUG)
29d64ed03dSBarry Smith   PetscFunctionReturn(0);
30d64ed03dSBarry Smith #endif
31a2ce50c7SBarry Smith }
32a2ce50c7SBarry Smith 
33bcd2baecSBarry Smith extern int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**);
3417ab2063SBarry Smith 
355615d1e5SSatish Balay #undef __FUNC__
365615d1e5SSatish Balay #define __FUNC__ "MatGetRowIJ_SeqAIJ"
378f6be9afSLois Curfman McInnes int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int **ia,int **ja,
386945ee14SBarry Smith                            PetscTruth *done)
3917ab2063SBarry Smith {
40416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
416945ee14SBarry Smith   int        ierr,i,ishift;
4217ab2063SBarry Smith 
433a40ed3dSBarry Smith   PetscFunctionBegin;
4431625ec5SSatish Balay   *m     = A->m;
453a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
466945ee14SBarry Smith   ishift = a->indexshift;
476945ee14SBarry Smith   if (symmetric) {
4831625ec5SSatish Balay     ierr = MatToSymmetricIJ_SeqAIJ(a->m,a->i,a->j,ishift,oshift,ia,ja); CHKERRQ(ierr);
496945ee14SBarry Smith   } else if (oshift == 0 && ishift == -1) {
5031625ec5SSatish Balay     int nz = a->i[a->m];
513b2fbd54SBarry Smith     /* malloc space and  subtract 1 from i and j indices */
5231625ec5SSatish Balay     *ia = (int *) PetscMalloc( (a->m+1)*sizeof(int) ); CHKPTRQ(*ia);
533b2fbd54SBarry Smith     *ja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(*ja);
543b2fbd54SBarry Smith     for ( i=0; i<nz; i++ ) (*ja)[i] = a->j[i] - 1;
5531625ec5SSatish Balay     for ( i=0; i<a->m+1; i++ ) (*ia)[i] = a->i[i] - 1;
566945ee14SBarry Smith   } else if (oshift == 1 && ishift == 0) {
5731625ec5SSatish Balay     int nz = a->i[a->m] + 1;
583b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
5931625ec5SSatish Balay     *ia = (int *) PetscMalloc( (a->m+1)*sizeof(int) ); CHKPTRQ(*ia);
603b2fbd54SBarry Smith     *ja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(*ja);
613b2fbd54SBarry Smith     for ( i=0; i<nz; i++ ) (*ja)[i] = a->j[i] + 1;
6231625ec5SSatish Balay     for ( i=0; i<a->m+1; i++ ) (*ia)[i] = a->i[i] + 1;
636945ee14SBarry Smith   } else {
646945ee14SBarry Smith     *ia = a->i; *ja = a->j;
65a2ce50c7SBarry Smith   }
66a2ce50c7SBarry Smith 
673a40ed3dSBarry Smith   PetscFunctionReturn(0);
68a2744918SBarry Smith }
69a2744918SBarry Smith 
705615d1e5SSatish Balay #undef __FUNC__
715615d1e5SSatish Balay #define __FUNC__ "MatRestoreRowIJ_SeqAIJ"
728f6be9afSLois Curfman McInnes int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,
736945ee14SBarry Smith                                PetscTruth *done)
746945ee14SBarry Smith {
756945ee14SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
763b2fbd54SBarry Smith   int        ishift = a->indexshift;
776945ee14SBarry Smith 
783a40ed3dSBarry Smith   PetscFunctionBegin;
793a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
803b2fbd54SBarry Smith   if (symmetric || (oshift == 0 && ishift == -1) || (oshift == 1 && ishift == 0)) {
816945ee14SBarry Smith     PetscFree(*ia);
826945ee14SBarry Smith     PetscFree(*ja);
83bcd2baecSBarry Smith   }
843a40ed3dSBarry Smith   PetscFunctionReturn(0);
8517ab2063SBarry Smith }
8617ab2063SBarry Smith 
875615d1e5SSatish Balay #undef __FUNC__
885615d1e5SSatish Balay #define __FUNC__ "MatGetColumnIJ_SeqAIJ"
8943a90d84SBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int **ia,int **ja,
903b2fbd54SBarry Smith                            PetscTruth *done)
913b2fbd54SBarry Smith {
923b2fbd54SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
93a93ec695SBarry Smith   int        ierr,i,ishift = a->indexshift,*collengths,*cia,*cja,n = A->n,m = A->m;
94a93ec695SBarry Smith   int        nz = a->i[m]+ishift,row,*jj,mr,col;
953b2fbd54SBarry Smith 
963a40ed3dSBarry Smith   PetscFunctionBegin;
973b2fbd54SBarry Smith   *nn     = A->n;
983a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
993b2fbd54SBarry Smith   if (symmetric) {
100179192dfSSatish Balay     ierr = MatToSymmetricIJ_SeqAIJ(a->m,a->i,a->j,ishift,oshift,ia,ja); CHKERRQ(ierr);
1013b2fbd54SBarry Smith   } else {
10261d2ded1SBarry Smith     collengths = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(collengths);
1033b2fbd54SBarry Smith     PetscMemzero(collengths,n*sizeof(int));
1043b2fbd54SBarry Smith     cia        = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(cia);
105a93ec695SBarry Smith     cja        = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(cja);
1063b2fbd54SBarry Smith     jj = a->j;
1073b2fbd54SBarry Smith     for ( i=0; i<nz; i++ ) {
1083b2fbd54SBarry Smith       collengths[jj[i] + ishift]++;
1093b2fbd54SBarry Smith     }
1103b2fbd54SBarry Smith     cia[0] = oshift;
1113b2fbd54SBarry Smith     for ( i=0; i<n; i++) {
1123b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1133b2fbd54SBarry Smith     }
1143b2fbd54SBarry Smith     PetscMemzero(collengths,n*sizeof(int));
1153b2fbd54SBarry Smith     jj = a->j;
116a93ec695SBarry Smith     for ( row=0; row<m; row++ ) {
117a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
118a93ec695SBarry Smith       for ( i=0; i<mr; i++ ) {
1193b2fbd54SBarry Smith         col = *jj++ + ishift;
1203b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1213b2fbd54SBarry Smith       }
1223b2fbd54SBarry Smith     }
1233b2fbd54SBarry Smith     PetscFree(collengths);
1243b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1253b2fbd54SBarry Smith   }
1263b2fbd54SBarry Smith 
1273a40ed3dSBarry Smith   PetscFunctionReturn(0);
1283b2fbd54SBarry Smith }
1293b2fbd54SBarry Smith 
1305615d1e5SSatish Balay #undef __FUNC__
1315615d1e5SSatish Balay #define __FUNC__ "MatRestoreColumnIJ_SeqAIJ"
13243a90d84SBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,
1333b2fbd54SBarry Smith                                      int **ja,PetscTruth *done)
1343b2fbd54SBarry Smith {
1353a40ed3dSBarry Smith   PetscFunctionBegin;
1363a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1373b2fbd54SBarry Smith 
1383b2fbd54SBarry Smith   PetscFree(*ia);
1393b2fbd54SBarry Smith   PetscFree(*ja);
1403b2fbd54SBarry Smith 
1413a40ed3dSBarry Smith   PetscFunctionReturn(0);
1423b2fbd54SBarry Smith }
1433b2fbd54SBarry Smith 
144227d817aSBarry Smith #define CHUNKSIZE   15
14517ab2063SBarry Smith 
1465615d1e5SSatish Balay #undef __FUNC__
1475615d1e5SSatish Balay #define __FUNC__ "MatSetValues_SeqAIJ"
14861d2ded1SBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v,InsertMode is)
14917ab2063SBarry Smith {
150416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
151416022c9SBarry Smith   int        *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax, N, sorted = a->sorted;
1524b0e389bSBarry Smith   int        *imax = a->imax, *ai = a->i, *ailen = a->ilen,roworiented = a->roworiented;
153d5d45c9bSBarry Smith   int        *aj = a->j, nonew = a->nonew,shift = a->indexshift;
154416022c9SBarry Smith   Scalar     *ap,value, *aa = a->a;
15517ab2063SBarry Smith 
1563a40ed3dSBarry Smith   PetscFunctionBegin;
15717ab2063SBarry Smith   for ( k=0; k<m; k++ ) { /* loop over added rows */
158416022c9SBarry Smith     row  = im[k];
1595ef9f2a5SBarry Smith     if (row < 0) continue;
1603a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g)
16132e87ba3SBarry Smith     if (row >= a->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large: row %d max %d",row,a->m);
1623b2fbd54SBarry Smith #endif
16317ab2063SBarry Smith     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
16417ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
165416022c9SBarry Smith     low = 0;
16617ab2063SBarry Smith     for ( l=0; l<n; l++ ) { /* loop over added columns */
1675ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1683a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g)
16932e87ba3SBarry Smith       if (in[l] >= a->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large: col %d max %d",in[l],a->n);
1703b2fbd54SBarry Smith #endif
1714b0e389bSBarry Smith       col = in[l] - shift;
1724b0e389bSBarry Smith       if (roworiented) {
1735ef9f2a5SBarry Smith         value = v[l + k*n];
174bef8e0ddSBarry Smith       } else {
1754b0e389bSBarry Smith         value = v[k + l*m];
1764b0e389bSBarry Smith       }
177416022c9SBarry Smith       if (!sorted) low = 0; high = nrow;
178416022c9SBarry Smith       while (high-low > 5) {
179416022c9SBarry Smith         t = (low+high)/2;
180416022c9SBarry Smith         if (rp[t] > col) high = t;
181416022c9SBarry Smith         else             low  = t;
18217ab2063SBarry Smith       }
183416022c9SBarry Smith       for ( i=low; i<high; i++ ) {
18417ab2063SBarry Smith         if (rp[i] > col) break;
18517ab2063SBarry Smith         if (rp[i] == col) {
186416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
18717ab2063SBarry Smith           else                  ap[i] = value;
18817ab2063SBarry Smith           goto noinsert;
18917ab2063SBarry Smith         }
19017ab2063SBarry Smith       }
191c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
192a8c6a408SBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix");
19317ab2063SBarry Smith       if (nrow >= rmax) {
19417ab2063SBarry Smith         /* there is no extra room in row, therefore enlarge */
195416022c9SBarry Smith         int    new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j;
19617ab2063SBarry Smith         Scalar *new_a;
19717ab2063SBarry Smith 
198a8c6a408SBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix");
19996854ed6SLois Curfman McInnes 
20017ab2063SBarry Smith         /* malloc new storage space */
201416022c9SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int);
2020452661fSBarry Smith         new_a   = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a);
20317ab2063SBarry Smith         new_j   = (int *) (new_a + new_nz);
20417ab2063SBarry Smith         new_i   = new_j + new_nz;
20517ab2063SBarry Smith 
20617ab2063SBarry Smith         /* copy over old data into new slots */
20717ab2063SBarry Smith         for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = ai[ii];}
208416022c9SBarry Smith         for ( ii=row+1; ii<a->m+1; ii++ ) {new_i[ii] = ai[ii]+CHUNKSIZE;}
209416022c9SBarry Smith         PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));
210416022c9SBarry Smith         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift);
21132e87ba3SBarry Smith         PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow,len*sizeof(int));
212416022c9SBarry Smith         PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar));
21332e87ba3SBarry Smith         PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow,len*sizeof(Scalar));
21417ab2063SBarry Smith         /* free up old matrix storage */
2150452661fSBarry Smith         PetscFree(a->a);
2160452661fSBarry Smith         if (!a->singlemalloc) {PetscFree(a->i);PetscFree(a->j);}
217416022c9SBarry Smith         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;
218416022c9SBarry Smith         a->singlemalloc = 1;
21917ab2063SBarry Smith 
22017ab2063SBarry Smith         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
221416022c9SBarry Smith         rmax = imax[row] = imax[row] + CHUNKSIZE;
222416022c9SBarry Smith         PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar)));
223416022c9SBarry Smith         a->maxnz += CHUNKSIZE;
224b810aeb4SBarry Smith         a->reallocs++;
22517ab2063SBarry Smith       }
226416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
227416022c9SBarry Smith       /* shift up all the later entries in this row */
228416022c9SBarry Smith       for ( ii=N; ii>=i; ii-- ) {
22917ab2063SBarry Smith         rp[ii+1] = rp[ii];
23017ab2063SBarry Smith         ap[ii+1] = ap[ii];
23117ab2063SBarry Smith       }
23217ab2063SBarry Smith       rp[i] = col;
23317ab2063SBarry Smith       ap[i] = value;
23417ab2063SBarry Smith       noinsert:;
235416022c9SBarry Smith       low = i + 1;
23617ab2063SBarry Smith     }
23717ab2063SBarry Smith     ailen[row] = nrow;
23817ab2063SBarry Smith   }
2393a40ed3dSBarry Smith   PetscFunctionReturn(0);
24017ab2063SBarry Smith }
24117ab2063SBarry Smith 
2425615d1e5SSatish Balay #undef __FUNC__
2435615d1e5SSatish Balay #define __FUNC__ "MatGetValues_SeqAIJ"
2448f6be9afSLois Curfman McInnes int MatGetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v)
2457eb43aa7SLois Curfman McInnes {
2467eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
247b49de8d1SLois Curfman McInnes   int        *rp, k, low, high, t, row, nrow, i, col, l, *aj = a->j;
2487eb43aa7SLois Curfman McInnes   int        *ai = a->i, *ailen = a->ilen, shift = a->indexshift;
2497eb43aa7SLois Curfman McInnes   Scalar     *ap, *aa = a->a, zero = 0.0;
2507eb43aa7SLois Curfman McInnes 
2513a40ed3dSBarry Smith   PetscFunctionBegin;
2527eb43aa7SLois Curfman McInnes   for ( k=0; k<m; k++ ) { /* loop over rows */
2537eb43aa7SLois Curfman McInnes     row  = im[k];
254a8c6a408SBarry Smith     if (row < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row");
255a8c6a408SBarry Smith     if (row >= a->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large");
2567eb43aa7SLois Curfman McInnes     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
2577eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2587eb43aa7SLois Curfman McInnes     for ( l=0; l<n; l++ ) { /* loop over columns */
259a8c6a408SBarry Smith       if (in[l] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column");
260a8c6a408SBarry Smith       if (in[l] >= a->n) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");
2617eb43aa7SLois Curfman McInnes       col = in[l] - shift;
2627eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2637eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2647eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2657eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2667eb43aa7SLois Curfman McInnes         else             low  = t;
2677eb43aa7SLois Curfman McInnes       }
2687eb43aa7SLois Curfman McInnes       for ( i=low; i<high; i++ ) {
2697eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2707eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
271b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2727eb43aa7SLois Curfman McInnes           goto finished;
2737eb43aa7SLois Curfman McInnes         }
2747eb43aa7SLois Curfman McInnes       }
275b49de8d1SLois Curfman McInnes       *v++ = zero;
2767eb43aa7SLois Curfman McInnes       finished:;
2777eb43aa7SLois Curfman McInnes     }
2787eb43aa7SLois Curfman McInnes   }
2793a40ed3dSBarry Smith   PetscFunctionReturn(0);
2807eb43aa7SLois Curfman McInnes }
2817eb43aa7SLois Curfman McInnes 
28217ab2063SBarry Smith 
2835615d1e5SSatish Balay #undef __FUNC__
2845615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_Binary"
285480ef9eaSBarry Smith int MatView_SeqAIJ_Binary(Mat A,Viewer viewer)
28617ab2063SBarry Smith {
287416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
288416022c9SBarry Smith   int        i, fd, *col_lens, ierr;
28917ab2063SBarry Smith 
2903a40ed3dSBarry Smith   PetscFunctionBegin;
29190ace30eSBarry Smith   ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr);
2920452661fSBarry Smith   col_lens = (int *) PetscMalloc( (4+a->m)*sizeof(int) ); CHKPTRQ(col_lens);
293416022c9SBarry Smith   col_lens[0] = MAT_COOKIE;
294416022c9SBarry Smith   col_lens[1] = a->m;
295416022c9SBarry Smith   col_lens[2] = a->n;
296416022c9SBarry Smith   col_lens[3] = a->nz;
297416022c9SBarry Smith 
298416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
299416022c9SBarry Smith   for ( i=0; i<a->m; i++ ) {
300416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
30117ab2063SBarry Smith   }
3020752156aSBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+a->m,PETSC_INT,1); CHKERRQ(ierr);
3030452661fSBarry Smith   PetscFree(col_lens);
304416022c9SBarry Smith 
305416022c9SBarry Smith   /* store column indices (zero start index) */
306416022c9SBarry Smith   if (a->indexshift) {
307416022c9SBarry Smith     for ( i=0; i<a->nz; i++ ) a->j[i]--;
30817ab2063SBarry Smith   }
3090752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,0); CHKERRQ(ierr);
310416022c9SBarry Smith   if (a->indexshift) {
311416022c9SBarry Smith     for ( i=0; i<a->nz; i++ ) a->j[i]++;
31217ab2063SBarry Smith   }
313416022c9SBarry Smith 
314416022c9SBarry Smith   /* store nonzero values */
3150752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,0); CHKERRQ(ierr);
3163a40ed3dSBarry Smith   PetscFunctionReturn(0);
31717ab2063SBarry Smith }
318416022c9SBarry Smith 
3195615d1e5SSatish Balay #undef __FUNC__
3205615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_ASCII"
321480ef9eaSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,Viewer viewer)
322416022c9SBarry Smith {
323416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ *) A->data;
324496e697eSBarry Smith   int         ierr, i,j, m = a->m, shift = a->indexshift, format, flg1,flg2;
32517ab2063SBarry Smith   FILE        *fd;
32617ab2063SBarry Smith   char        *outputname;
32717ab2063SBarry Smith 
3283a40ed3dSBarry Smith   PetscFunctionBegin;
32990ace30eSBarry Smith   ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr);
330fb4b0f7fSBarry Smith   ierr = ViewerGetOutputname(viewer,&outputname); CHKERRQ(ierr);
33190ace30eSBarry Smith   ierr = ViewerGetFormat(viewer,&format);
332a93ec695SBarry Smith   if (format == VIEWER_FORMAT_ASCII_INFO) {
3333a40ed3dSBarry Smith     PetscFunctionReturn(0);
3343a40ed3dSBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_INFO_LONG) {
335496e697eSBarry Smith     ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg1); CHKERRQ(ierr);
336496e697eSBarry Smith     ierr = OptionsHasName(PETSC_NULL,"-mat_no_unroll",&flg2); CHKERRQ(ierr);
337d132466eSBarry Smith     if (flg1 || flg2) {ierr = ViewerASCIIPrintf(viewer,"  not using I-node routines\n");CHKERRQ(ierr);}
338d132466eSBarry Smith     else {ierr = ViewerASCIIPrintf(viewer,"  using I-node routines: found %d nodes, limit used is %d\n",a->inode.node_count,a->inode.limit);CHKERRQ(ierr);}
3393a40ed3dSBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_MATLAB) {
340d00d2cf4SBarry Smith     int nofinalvalue = 0;
341d00d2cf4SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != a->n-!shift)) {
342d00d2cf4SBarry Smith       nofinalvalue = 1;
343d00d2cf4SBarry Smith     }
344416022c9SBarry Smith     fprintf(fd,"%% Size = %d %d \n",m,a->n);
3454e220ebcSLois Curfman McInnes     fprintf(fd,"%% Nonzeros = %d \n",a->nz);
346d00d2cf4SBarry Smith     fprintf(fd,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);
34717ab2063SBarry Smith     fprintf(fd,"zzz = [\n");
34817ab2063SBarry Smith 
34917ab2063SBarry Smith     for (i=0; i<m; i++) {
350416022c9SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
3513a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
352e20fef11SSatish Balay         fprintf(fd,"%d %d  %18.16e + %18.16e i \n",i+1,a->j[j]+!shift,PetscReal(a->a[j]),PetscImaginary(a->a[j]));
35317ab2063SBarry Smith #else
3547a743949SBarry Smith         fprintf(fd,"%d %d  %18.16e\n", i+1, a->j[j]+!shift, a->a[j]);
35517ab2063SBarry Smith #endif
35617ab2063SBarry Smith       }
35717ab2063SBarry Smith     }
358d00d2cf4SBarry Smith     if (nofinalvalue) {
359d00d2cf4SBarry Smith       fprintf(fd,"%d %d  %18.16e\n", m, a->n, 0.0);
360d00d2cf4SBarry Smith     }
361e24b481bSBarry Smith     if (outputname) fprintf(fd,"];\n %s = spconvert(zzz);\n",outputname);
362e24b481bSBarry Smith     else            fprintf(fd,"];\n M = spconvert(zzz);\n");
3633a40ed3dSBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_COMMON) {
36444cd7ae7SLois Curfman McInnes     for ( i=0; i<m; i++ ) {
36544cd7ae7SLois Curfman McInnes       fprintf(fd,"row %d:",i);
36644cd7ae7SLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
3673a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
368e20fef11SSatish Balay         if (PetscImaginary(a->a[j]) > 0.0 && PetscReal(a->a[j]) != 0.0)
369e20fef11SSatish Balay           fprintf(fd," %d %g + %g i",a->j[j]+shift,PetscReal(a->a[j]),PetscImaginary(a->a[j]));
370e20fef11SSatish Balay         else if (PetscImaginary(a->a[j]) < 0.0 && PetscReal(a->a[j]) != 0.0)
371e20fef11SSatish Balay           fprintf(fd," %d %g - %g i",a->j[j]+shift,PetscReal(a->a[j]),-PetscImaginary(a->a[j]));
372e20fef11SSatish Balay         else if (PetscReal(a->a[j]) != 0.0)
373e20fef11SSatish Balay           fprintf(fd," %d %g ",a->j[j]+shift,PetscReal(a->a[j]));
37444cd7ae7SLois Curfman McInnes #else
37544cd7ae7SLois Curfman McInnes         if (a->a[j] != 0.0) fprintf(fd," %d %g ",a->j[j]+shift,a->a[j]);
37644cd7ae7SLois Curfman McInnes #endif
37744cd7ae7SLois Curfman McInnes       }
37844cd7ae7SLois Curfman McInnes       fprintf(fd,"\n");
37944cd7ae7SLois Curfman McInnes     }
38002594712SBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_SYMMODU) {
381496be53dSLois Curfman McInnes     int nzd=0, fshift=1, *sptr;
3822e44a96cSLois Curfman McInnes     sptr = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(sptr);
383496be53dSLois Curfman McInnes     for ( i=0; i<m; i++ ) {
384496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
385496be53dSLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
386496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
3873a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
388e20fef11SSatish Balay           if (PetscImaginary(a->a[j]) != 0.0 || PetscReal(a->a[j]) != 0.0) nzd++;
389496be53dSLois Curfman McInnes #else
390496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
391496be53dSLois Curfman McInnes #endif
392496be53dSLois Curfman McInnes         }
393496be53dSLois Curfman McInnes       }
394496be53dSLois Curfman McInnes     }
3952e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
396496be53dSLois Curfman McInnes     fprintf(fd," %d %d\n\n",m,nzd);
3972e44a96cSLois Curfman McInnes     for ( i=0; i<m+1; i+=6 ) {
3982e44a96cSLois Curfman McInnes       if (i+4<m) fprintf(fd," %d %d %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]);
3992e44a96cSLois Curfman McInnes       else if (i+3<m) fprintf(fd," %d %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);
4002e44a96cSLois Curfman McInnes       else if (i+2<m) fprintf(fd," %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);
4012e44a96cSLois Curfman McInnes       else if (i+1<m) fprintf(fd," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);
4022e44a96cSLois Curfman McInnes       else if (i<m)   fprintf(fd," %d %d\n",sptr[i],sptr[i+1]);
4037272d637SLois Curfman McInnes       else            fprintf(fd," %d\n",sptr[i]);
404496be53dSLois Curfman McInnes     }
405496be53dSLois Curfman McInnes     fprintf(fd,"\n");
406496be53dSLois Curfman McInnes     PetscFree(sptr);
407496be53dSLois Curfman McInnes     for ( i=0; i<m; i++ ) {
408496be53dSLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
409496be53dSLois Curfman McInnes         if (a->j[j] >= i) fprintf(fd," %d ",a->j[j]+fshift);
410496be53dSLois Curfman McInnes       }
411496be53dSLois Curfman McInnes       fprintf(fd,"\n");
412496be53dSLois Curfman McInnes     }
413496be53dSLois Curfman McInnes     fprintf(fd,"\n");
414496be53dSLois Curfman McInnes     for ( i=0; i<m; i++ ) {
415496be53dSLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
416496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
4173a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
418e20fef11SSatish Balay           if (PetscImaginary(a->a[j]) != 0.0 || PetscReal(a->a[j]) != 0.0)
419e20fef11SSatish Balay             fprintf(fd," %18.16e %18.16e ",PetscReal(a->a[j]),PetscImaginary(a->a[j]));
420496be53dSLois Curfman McInnes #else
421496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) fprintf(fd," %18.16e ",a->a[j]);
422496be53dSLois Curfman McInnes #endif
423496be53dSLois Curfman McInnes         }
424496be53dSLois Curfman McInnes       }
425496be53dSLois Curfman McInnes       fprintf(fd,"\n");
426496be53dSLois Curfman McInnes     }
42702594712SBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_DENSE) {
42802594712SBarry Smith     int    cnt = 0,jcnt;
42902594712SBarry Smith     Scalar value;
43002594712SBarry Smith 
43102594712SBarry Smith     for ( i=0; i<m; i++ ) {
43202594712SBarry Smith       jcnt = 0;
43302594712SBarry Smith       for ( j=0; j<a->n; j++ ) {
434e24b481bSBarry Smith         if ( jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
43502594712SBarry Smith           value = a->a[cnt++];
436e24b481bSBarry Smith           jcnt++;
43702594712SBarry Smith         } else {
43802594712SBarry Smith           value = 0.0;
43902594712SBarry Smith         }
44002594712SBarry Smith #if defined(USE_PETSC_COMPLEX)
44102594712SBarry Smith         fprintf(fd," %7.5e+%7.5e i ",PetscReal(value),PetscImaginary(value));
44202594712SBarry Smith #else
44302594712SBarry Smith         fprintf(fd," %7.5e ",value);
44402594712SBarry Smith #endif
44502594712SBarry Smith       }
44602594712SBarry Smith         fprintf(fd,"\n");
44702594712SBarry Smith     }
4483a40ed3dSBarry Smith   } else {
44917ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
45017ab2063SBarry Smith       fprintf(fd,"row %d:",i);
451416022c9SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
4523a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
453e20fef11SSatish Balay         if (PetscImaginary(a->a[j]) > 0.0) {
454e20fef11SSatish Balay           fprintf(fd," %d %g + %g i",a->j[j]+shift,PetscReal(a->a[j]),PetscImaginary(a->a[j]));
455e20fef11SSatish Balay         } else if (PetscImaginary(a->a[j]) < 0.0) {
456e20fef11SSatish Balay           fprintf(fd," %d %g - %g i",a->j[j]+shift,PetscReal(a->a[j]),-PetscImaginary(a->a[j]));
4573a40ed3dSBarry Smith         } else {
458e20fef11SSatish Balay           fprintf(fd," %d %g ",a->j[j]+shift,PetscReal(a->a[j]));
45917ab2063SBarry Smith         }
46017ab2063SBarry Smith #else
461416022c9SBarry Smith         fprintf(fd," %d %g ",a->j[j]+shift,a->a[j]);
46217ab2063SBarry Smith #endif
46317ab2063SBarry Smith       }
46417ab2063SBarry Smith       fprintf(fd,"\n");
46517ab2063SBarry Smith     }
46617ab2063SBarry Smith   }
46717ab2063SBarry Smith   fflush(fd);
4683a40ed3dSBarry Smith   PetscFunctionReturn(0);
469416022c9SBarry Smith }
470416022c9SBarry Smith 
4715615d1e5SSatish Balay #undef __FUNC__
472480ef9eaSBarry Smith #define __FUNC__ "MatView_SeqAIJ_Draw_Zoom"
473480ef9eaSBarry Smith int MatView_SeqAIJ_Draw_Zoom(Draw draw,void *Aa)
474416022c9SBarry Smith {
475480ef9eaSBarry Smith   Mat         A = (Mat) Aa;
476416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ *) A->data;
47777ed5343SBarry Smith   int         ierr, i,j, m = a->m, shift = a->indexshift,color,rank;
4780513a670SBarry Smith   int         format;
479480ef9eaSBarry Smith   double      xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
480480ef9eaSBarry Smith   Viewer      viewer;
48177ed5343SBarry Smith   MPI_Comm    comm;
482cddf8d76SBarry Smith 
4833a40ed3dSBarry Smith   PetscFunctionBegin;
48477ed5343SBarry Smith   /*
48577ed5343SBarry Smith       This is nasty. If this is called from an originally parallel matrix
48677ed5343SBarry Smith    then all processes call this, but only the first has the matrix so the
48777ed5343SBarry Smith    rest should return immediately.
48877ed5343SBarry Smith   */
48977ed5343SBarry Smith   ierr = PetscObjectGetComm((PetscObject)draw,&comm);CHKERRQ(ierr);
490d132466eSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
49177ed5343SBarry Smith   if (rank) PetscFunctionReturn(0);
49277ed5343SBarry Smith 
493480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*) &viewer);CHKERRQ(ierr);
4940513a670SBarry Smith   ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr);
49519bcc07fSBarry Smith 
496480ef9eaSBarry Smith   ierr = DrawGetCoordinates(draw,&xl,&yl,&xr,&yr); CHKERRQ(ierr);
497416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4980513a670SBarry Smith 
4990513a670SBarry Smith   if (format != VIEWER_FORMAT_DRAW_CONTOUR) {
5000513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
501cddf8d76SBarry Smith     color = DRAW_BLUE;
502416022c9SBarry Smith     for ( i=0; i<m; i++ ) {
503cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
504416022c9SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
505cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
5063a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
507e20fef11SSatish Balay         if (PetscReal(a->a[j]) >=  0.) continue;
508cddf8d76SBarry Smith #else
509cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
510cddf8d76SBarry Smith #endif
511480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
512cddf8d76SBarry Smith       }
513cddf8d76SBarry Smith     }
514cddf8d76SBarry Smith     color = DRAW_CYAN;
515cddf8d76SBarry Smith     for ( i=0; i<m; i++ ) {
516cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
517cddf8d76SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
518cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
519cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
520480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
521cddf8d76SBarry Smith       }
522cddf8d76SBarry Smith     }
523cddf8d76SBarry Smith     color = DRAW_RED;
524cddf8d76SBarry Smith     for ( i=0; i<m; i++ ) {
525cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
526cddf8d76SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
527cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
5283a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
529e20fef11SSatish Balay         if (PetscReal(a->a[j]) <=  0.) continue;
530cddf8d76SBarry Smith #else
531cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
532cddf8d76SBarry Smith #endif
533480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
534416022c9SBarry Smith       }
535416022c9SBarry Smith     }
5360513a670SBarry Smith   } else {
5370513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5380513a670SBarry Smith     /* first determine max of all nonzero values */
5390513a670SBarry Smith     int    nz = a->nz,count;
5400513a670SBarry Smith     Draw   popup;
541480ef9eaSBarry Smith     double scale;
5420513a670SBarry Smith 
5430513a670SBarry Smith     for ( i=0; i<nz; i++ ) {
5440513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5450513a670SBarry Smith     }
546480ef9eaSBarry Smith     scale = (245.0 - DRAW_BASIC_COLORS)/maxv;
547522c5e43SBarry Smith     ierr  = DrawGetPopup(draw,&popup); CHKERRQ(ierr);
5480513a670SBarry Smith     ierr  = DrawScalePopup(popup,0.0,maxv); CHKERRQ(ierr);
5490513a670SBarry Smith     count = 0;
5500513a670SBarry Smith     for ( i=0; i<m; i++ ) {
5510513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
5520513a670SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
5530513a670SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
5546d6b6c30SSatish Balay         color = DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count]));
555480ef9eaSBarry Smith         ierr  = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5560513a670SBarry Smith         count++;
5570513a670SBarry Smith       }
5580513a670SBarry Smith     }
5590513a670SBarry Smith   }
560480ef9eaSBarry Smith   PetscFunctionReturn(0);
561480ef9eaSBarry Smith }
562cddf8d76SBarry Smith 
563480ef9eaSBarry Smith #undef __FUNC__
564480ef9eaSBarry Smith #define __FUNC__ "MatView_SeqAIJ_Draw"
565480ef9eaSBarry Smith int MatView_SeqAIJ_Draw(Mat A,Viewer viewer)
566480ef9eaSBarry Smith {
567480ef9eaSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data;
568480ef9eaSBarry Smith   int        ierr;
569480ef9eaSBarry Smith   Draw       draw;
570480ef9eaSBarry Smith   double     xr,yr,xl,yl,h,w;
571480ef9eaSBarry Smith   PetscTruth isnull;
572480ef9eaSBarry Smith 
573480ef9eaSBarry Smith   PetscFunctionBegin;
57477ed5343SBarry Smith   ierr = ViewerDrawGetDraw(viewer,0,&draw); CHKERRQ(ierr);
575480ef9eaSBarry Smith   ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr);
576480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
577480ef9eaSBarry Smith 
578480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
579480ef9eaSBarry Smith   xr  = a->n; yr = a->m; h = yr/10.0; w = xr/10.0;
580480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
581cddf8d76SBarry Smith   ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr);
582480ef9eaSBarry Smith   ierr = DrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A); CHKERRQ(ierr);
583480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5843a40ed3dSBarry Smith   PetscFunctionReturn(0);
585416022c9SBarry Smith }
586416022c9SBarry Smith 
5875615d1e5SSatish Balay #undef __FUNC__
588d4bb536fSBarry Smith #define __FUNC__ "MatView_SeqAIJ"
589e1311b90SBarry Smith int MatView_SeqAIJ(Mat A,Viewer viewer)
590416022c9SBarry Smith {
591416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*) A->data;
592bcd2baecSBarry Smith   ViewerType  vtype;
593bcd2baecSBarry Smith   int         ierr;
594416022c9SBarry Smith 
5953a40ed3dSBarry Smith   PetscFunctionBegin;
596bcd2baecSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
5977b2a1423SBarry Smith   if (PetscTypeCompare(vtype,SOCKET_VIEWER)) {
5987b2a1423SBarry Smith     ierr = ViewerSocketPutSparse_Private(viewer,a->m,a->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr);
5993f1db9ecSBarry Smith   } else if (PetscTypeCompare(vtype,ASCII_VIEWER)){
6003a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer); CHKERRQ(ierr);
6013f1db9ecSBarry Smith   } else if (PetscTypeCompare(vtype,BINARY_VIEWER)) {
6023a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer); CHKERRQ(ierr);
6033f1db9ecSBarry Smith   } else if (PetscTypeCompare(vtype,DRAW_VIEWER)) {
6043a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer); CHKERRQ(ierr);
6055cd90555SBarry Smith   } else {
6065cd90555SBarry Smith     SETERRQ(1,1,"Viewer type not supported by PETSc object");
60717ab2063SBarry Smith   }
6083a40ed3dSBarry Smith   PetscFunctionReturn(0);
60917ab2063SBarry Smith }
61019bcc07fSBarry Smith 
611c456f294SBarry Smith extern int Mat_AIJ_CheckInode(Mat);
6125615d1e5SSatish Balay #undef __FUNC__
6135615d1e5SSatish Balay #define __FUNC__ "MatAssemblyEnd_SeqAIJ"
6148f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
61517ab2063SBarry Smith {
616416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
61741c01911SSatish Balay   int        fshift = 0,i,j,*ai = a->i, *aj = a->j, *imax = a->imax,ierr;
61843ee02c3SBarry Smith   int        m = a->m, *ip, N, *ailen = a->ilen,shift = a->indexshift,rmax = 0;
619416022c9SBarry Smith   Scalar     *aa = a->a, *ap;
62017ab2063SBarry Smith 
6213a40ed3dSBarry Smith   PetscFunctionBegin;
6223a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
62317ab2063SBarry Smith 
62443ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
62517ab2063SBarry Smith   for ( i=1; i<m; i++ ) {
626416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
62717ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
62894a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
62917ab2063SBarry Smith     if (fshift) {
630416022c9SBarry Smith       ip = aj + ai[i] + shift; ap = aa + ai[i] + shift;
63117ab2063SBarry Smith       N = ailen[i];
63217ab2063SBarry Smith       for ( j=0; j<N; j++ ) {
63317ab2063SBarry Smith         ip[j-fshift] = ip[j];
63417ab2063SBarry Smith         ap[j-fshift] = ap[j];
63517ab2063SBarry Smith       }
63617ab2063SBarry Smith     }
63717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
63817ab2063SBarry Smith   }
63917ab2063SBarry Smith   if (m) {
64017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
64117ab2063SBarry Smith     ai[m] = ai[m-1] + ailen[m-1];
64217ab2063SBarry Smith   }
64317ab2063SBarry Smith   /* reset ilen and imax for each row */
64417ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
64517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
64617ab2063SBarry Smith   }
647416022c9SBarry Smith   a->nz = ai[m] + shift;
64817ab2063SBarry Smith 
64917ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
650416022c9SBarry Smith   if (fshift && a->diag) {
6510452661fSBarry Smith     PetscFree(a->diag);
652416022c9SBarry Smith     PLogObjectMemory(A,-(m+1)*sizeof(int));
653416022c9SBarry Smith     a->diag = 0;
65417ab2063SBarry Smith   }
6554e220ebcSLois Curfman McInnes   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded, %d used\n",
6564e220ebcSLois Curfman McInnes            m,a->n,fshift,a->nz);
65714bcb312SBarry Smith   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %d\n",
658b810aeb4SBarry Smith            a->reallocs);
65994a9d846SBarry Smith   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax);
660dd5f02e7SSatish Balay   a->reallocs          = 0;
6614e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
6624e220ebcSLois Curfman McInnes 
66376dd722bSSatish Balay   /* check out for identical nodes. If found, use inode functions */
66441c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(A); CHKERRQ(ierr);
6653a40ed3dSBarry Smith   PetscFunctionReturn(0);
66617ab2063SBarry Smith }
66717ab2063SBarry Smith 
6685615d1e5SSatish Balay #undef __FUNC__
6695615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_SeqAIJ"
6708f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A)
67117ab2063SBarry Smith {
672416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
6733a40ed3dSBarry Smith 
6743a40ed3dSBarry Smith   PetscFunctionBegin;
675cddf8d76SBarry Smith   PetscMemzero(a->a,(a->i[a->m]+a->indexshift)*sizeof(Scalar));
6763a40ed3dSBarry Smith   PetscFunctionReturn(0);
67717ab2063SBarry Smith }
678416022c9SBarry Smith 
6795615d1e5SSatish Balay #undef __FUNC__
6805615d1e5SSatish Balay #define __FUNC__ "MatDestroy_SeqAIJ"
681e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A)
68217ab2063SBarry Smith {
683416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
68482bf6240SBarry Smith   int        ierr;
685d5d45c9bSBarry Smith 
6863a40ed3dSBarry Smith   PetscFunctionBegin;
68794d884c6SBarry Smith   if (--A->refct > 0) PetscFunctionReturn(0);
68894d884c6SBarry Smith 
68994d884c6SBarry Smith   if (A->mapping) {
69094d884c6SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(A->mapping); CHKERRQ(ierr);
69194d884c6SBarry Smith   }
69294d884c6SBarry Smith   if (A->bmapping) {
69394d884c6SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(A->bmapping); CHKERRQ(ierr);
69494d884c6SBarry Smith   }
69561b13de0SBarry Smith   if (A->rmap) {
69661b13de0SBarry Smith     ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
69761b13de0SBarry Smith   }
69861b13de0SBarry Smith   if (A->cmap) {
69961b13de0SBarry Smith     ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
70061b13de0SBarry Smith   }
70166d5ce97SBarry Smith   if (a->idiag) PetscFree(a->idiag);
7023a40ed3dSBarry Smith #if defined(USE_PETSC_LOG)
703e1311b90SBarry Smith   PLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",a->m,a->n,a->nz);
70417ab2063SBarry Smith #endif
7050452661fSBarry Smith   PetscFree(a->a);
7060452661fSBarry Smith   if (!a->singlemalloc) { PetscFree(a->i); PetscFree(a->j);}
7070452661fSBarry Smith   if (a->diag) PetscFree(a->diag);
7080452661fSBarry Smith   if (a->ilen) PetscFree(a->ilen);
7090452661fSBarry Smith   if (a->imax) PetscFree(a->imax);
7100452661fSBarry Smith   if (a->solve_work) PetscFree(a->solve_work);
71176dd722bSSatish Balay   if (a->inode.size) PetscFree(a->inode.size);
71282bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
713be6bf707SBarry Smith   if (a->saved_values) PetscFree(a->saved_values);
7140452661fSBarry Smith   PetscFree(a);
715eed86810SBarry Smith 
716f2655603SLois Curfman McInnes   PLogObjectDestroy(A);
717f2655603SLois Curfman McInnes   PetscHeaderDestroy(A);
7183a40ed3dSBarry Smith   PetscFunctionReturn(0);
71917ab2063SBarry Smith }
72017ab2063SBarry Smith 
7215615d1e5SSatish Balay #undef __FUNC__
7225615d1e5SSatish Balay #define __FUNC__ "MatCompress_SeqAIJ"
7238f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
72417ab2063SBarry Smith {
7253a40ed3dSBarry Smith   PetscFunctionBegin;
7263a40ed3dSBarry Smith   PetscFunctionReturn(0);
72717ab2063SBarry Smith }
72817ab2063SBarry Smith 
7295615d1e5SSatish Balay #undef __FUNC__
7305615d1e5SSatish Balay #define __FUNC__ "MatSetOption_SeqAIJ"
7318f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
73217ab2063SBarry Smith {
733416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
7343a40ed3dSBarry Smith 
7353a40ed3dSBarry Smith   PetscFunctionBegin;
7366d4a8577SBarry Smith   if      (op == MAT_ROW_ORIENTED)                 a->roworiented = 1;
7376d4a8577SBarry Smith   else if (op == MAT_COLUMN_ORIENTED)              a->roworiented = 0;
7386d4a8577SBarry Smith   else if (op == MAT_COLUMNS_SORTED)               a->sorted      = 1;
739219d9a1aSLois Curfman McInnes   else if (op == MAT_COLUMNS_UNSORTED)             a->sorted      = 0;
7406d4a8577SBarry Smith   else if (op == MAT_NO_NEW_NONZERO_LOCATIONS)     a->nonew       = 1;
7414787f768SSatish Balay   else if (op == MAT_NEW_NONZERO_LOCATION_ERR)     a->nonew       = -1;
7424787f768SSatish Balay   else if (op == MAT_NEW_NONZERO_ALLOCATION_ERR)   a->nonew       = -2;
7436d4a8577SBarry Smith   else if (op == MAT_YES_NEW_NONZERO_LOCATIONS)    a->nonew       = 0;
7446d4a8577SBarry Smith   else if (op == MAT_ROWS_SORTED ||
745219d9a1aSLois Curfman McInnes            op == MAT_ROWS_UNSORTED ||
7466d4a8577SBarry Smith            op == MAT_SYMMETRIC ||
7476d4a8577SBarry Smith            op == MAT_STRUCTURALLY_SYMMETRIC ||
74890f02eecSBarry Smith            op == MAT_YES_NEW_DIAGONALS ||
749b51ba29fSSatish Balay            op == MAT_IGNORE_OFF_PROC_ENTRIES||
750b51ba29fSSatish Balay            op == MAT_USE_HASH_TABLE)
751981c4779SBarry Smith     PLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
7523a40ed3dSBarry Smith   else if (op == MAT_NO_NEW_DIAGONALS) {
7533a40ed3dSBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS");
7543a40ed3dSBarry Smith   } else if (op == MAT_INODE_LIMIT_1)            a->inode.limit  = 1;
7556d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_2)            a->inode.limit  = 2;
7566d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_3)            a->inode.limit  = 3;
7576d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_4)            a->inode.limit  = 4;
7586d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_5)            a->inode.limit  = 5;
7593a40ed3dSBarry Smith   else SETERRQ(PETSC_ERR_SUP,0,"unknown option");
7603a40ed3dSBarry Smith   PetscFunctionReturn(0);
76117ab2063SBarry Smith }
76217ab2063SBarry Smith 
7635615d1e5SSatish Balay #undef __FUNC__
7645615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_SeqAIJ"
7658f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
76617ab2063SBarry Smith {
767416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
7683a40ed3dSBarry Smith   int        i,j, n,shift = a->indexshift,ierr;
76917ab2063SBarry Smith   Scalar     *x, zero = 0.0;
77017ab2063SBarry Smith 
7713a40ed3dSBarry Smith   PetscFunctionBegin;
7723a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
773e1311b90SBarry Smith   ierr = VecGetArray(v,&x);;CHKERRQ(ierr);
774e1311b90SBarry Smith   ierr = VecGetLocalSize(v,&n);;CHKERRQ(ierr);
775a8c6a408SBarry Smith   if (n != a->m) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Nonconforming matrix and vector");
776416022c9SBarry Smith   for ( i=0; i<a->m; i++ ) {
777416022c9SBarry Smith     for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
778416022c9SBarry Smith       if (a->j[j]+shift == i) {
779416022c9SBarry Smith         x[i] = a->a[j];
78017ab2063SBarry Smith         break;
78117ab2063SBarry Smith       }
78217ab2063SBarry Smith     }
78317ab2063SBarry Smith   }
784e1311b90SBarry Smith   ierr = VecRestoreArray(v,&x);;CHKERRQ(ierr);
7853a40ed3dSBarry Smith   PetscFunctionReturn(0);
78617ab2063SBarry Smith }
78717ab2063SBarry Smith 
78817ab2063SBarry Smith /* -------------------------------------------------------*/
78917ab2063SBarry Smith /* Should check that shapes of vectors and matrices match */
79017ab2063SBarry Smith /* -------------------------------------------------------*/
7915615d1e5SSatish Balay #undef __FUNC__
7925615d1e5SSatish Balay #define __FUNC__ "MatMultTrans_SeqAIJ"
79344cd7ae7SLois Curfman McInnes int MatMultTrans_SeqAIJ(Mat A,Vec xx,Vec yy)
79417ab2063SBarry Smith {
795416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
79617ab2063SBarry Smith   Scalar     *x, *y, *v, alpha;
797e1311b90SBarry Smith   int        ierr,m = a->m, n, i, *idx, shift = a->indexshift;
79817ab2063SBarry Smith 
7993a40ed3dSBarry Smith   PetscFunctionBegin;
800e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
801e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
802cddf8d76SBarry Smith   PetscMemzero(y,a->n*sizeof(Scalar));
80317ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
80417ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
805416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
806416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
807416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
80817ab2063SBarry Smith     alpha = x[i];
80917ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
81017ab2063SBarry Smith   }
811416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
812e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
813e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8143a40ed3dSBarry Smith   PetscFunctionReturn(0);
81517ab2063SBarry Smith }
816d5d45c9bSBarry Smith 
8175615d1e5SSatish Balay #undef __FUNC__
8185615d1e5SSatish Balay #define __FUNC__ "MatMultTransAdd_SeqAIJ"
81944cd7ae7SLois Curfman McInnes int MatMultTransAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
82017ab2063SBarry Smith {
821416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
82217ab2063SBarry Smith   Scalar     *x, *y, *v, alpha;
823e1311b90SBarry Smith   int        ierr,m = a->m, n, i, *idx,shift = a->indexshift;
82417ab2063SBarry Smith 
8253a40ed3dSBarry Smith   PetscFunctionBegin;
8262e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
827e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
828e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
82917ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
83017ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
831416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
832416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
833416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
83417ab2063SBarry Smith     alpha = x[i];
83517ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
83617ab2063SBarry Smith   }
83790f02eecSBarry Smith   PLogFlops(2*a->nz);
838e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
839e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8403a40ed3dSBarry Smith   PetscFunctionReturn(0);
84117ab2063SBarry Smith }
84217ab2063SBarry Smith 
8435615d1e5SSatish Balay #undef __FUNC__
8445615d1e5SSatish Balay #define __FUNC__ "MatMult_SeqAIJ"
84544cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
84617ab2063SBarry Smith {
847416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
84817ab2063SBarry Smith   Scalar     *x, *y, *v, sum;
849e1311b90SBarry Smith   int        ierr,m = a->m, *idx, shift = a->indexshift,*ii;
8502e40c62eSSatish Balay #if !defined(USE_FORTRAN_KERNEL_MULTAIJ)
851e36a17ebSSatish Balay   int        n, i, jrow,j;
852e36a17ebSSatish Balay #endif
85317ab2063SBarry Smith 
854fee21e36SBarry Smith #if defined(HAVE_PRAGMA_DISJOINT)
855fee21e36SBarry Smith #pragma disjoint(*x,*y,*v)
856fee21e36SBarry Smith #endif
857fee21e36SBarry Smith 
8583a40ed3dSBarry Smith   PetscFunctionBegin;
859e1311b90SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
860e1311b90SBarry Smith   ierr = VecGetArray(yy,&y); CHKERRQ(ierr);
86117ab2063SBarry Smith   x    = x + shift;    /* shift for Fortran start by 1 indexing */
862416022c9SBarry Smith   idx  = a->j;
863d64ed03dSBarry Smith   v    = a->a;
864416022c9SBarry Smith   ii   = a->i;
865acc4d009SSatish Balay #if defined(USE_FORTRAN_KERNEL_MULTAIJ)
86629b5ca8aSSatish Balay   fortranmultaij_(&m,x,ii,idx+shift,v+shift,y);
8678d195f9aSBarry Smith #else
868d64ed03dSBarry Smith   v    += shift; /* shift for Fortran start by 1 indexing */
869d64ed03dSBarry Smith   idx  += shift;
87017ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
8719ea0dfa2SSatish Balay     jrow = ii[i];
8729ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
87317ab2063SBarry Smith     sum  = 0.0;
8749ea0dfa2SSatish Balay     for ( j=0; j<n; j++) {
8759ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8769ea0dfa2SSatish Balay      }
87717ab2063SBarry Smith     y[i] = sum;
87817ab2063SBarry Smith   }
8798d195f9aSBarry Smith #endif
880416022c9SBarry Smith   PLogFlops(2*a->nz - m);
881e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
882e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y); CHKERRQ(ierr);
8833a40ed3dSBarry Smith   PetscFunctionReturn(0);
88417ab2063SBarry Smith }
88517ab2063SBarry Smith 
8865615d1e5SSatish Balay #undef __FUNC__
8875615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_SeqAIJ"
88844cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
88917ab2063SBarry Smith {
890416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
89117ab2063SBarry Smith   Scalar     *x, *y, *z, *v, sum;
892e1311b90SBarry Smith   int        ierr,m = a->m, *idx, shift = a->indexshift,*ii;
8932e40c62eSSatish Balay #if !defined(USE_FORTRAN_KERNEL_MULTADDAIJ)
894e36a17ebSSatish Balay   int        n,i,jrow,j;
895e36a17ebSSatish Balay #endif
8969ea0dfa2SSatish Balay 
8973a40ed3dSBarry Smith   PetscFunctionBegin;
898e1311b90SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
899e1311b90SBarry Smith   ierr = VecGetArray(yy,&y); CHKERRQ(ierr);
9002e8a6d31SBarry Smith   if (zz != yy) {
901e1311b90SBarry Smith     ierr = VecGetArray(zz,&z); CHKERRQ(ierr);
9022e8a6d31SBarry Smith   } else {
9032e8a6d31SBarry Smith     z = y;
9042e8a6d31SBarry Smith   }
90517ab2063SBarry Smith   x    = x + shift; /* shift for Fortran start by 1 indexing */
906cddf8d76SBarry Smith   idx  = a->j;
907d64ed03dSBarry Smith   v    = a->a;
908cddf8d76SBarry Smith   ii   = a->i;
9092e40c62eSSatish Balay #if defined(USE_FORTRAN_KERNEL_MULTADDAIJ)
91029b5ca8aSSatish Balay   fortranmultaddaij_(&m,x,ii,idx+shift,v+shift,y,z);
91102ab625aSSatish Balay #else
912d64ed03dSBarry Smith   v   += shift; /* shift for Fortran start by 1 indexing */
913d64ed03dSBarry Smith   idx += shift;
91417ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
9159ea0dfa2SSatish Balay     jrow = ii[i];
9169ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
91717ab2063SBarry Smith     sum  = y[i];
9189ea0dfa2SSatish Balay     for ( j=0; j<n; j++) {
9199ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9209ea0dfa2SSatish Balay      }
92117ab2063SBarry Smith     z[i] = sum;
92217ab2063SBarry Smith   }
92302ab625aSSatish Balay #endif
924416022c9SBarry Smith   PLogFlops(2*a->nz);
925e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
926e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y); CHKERRQ(ierr);
9272e8a6d31SBarry Smith   if (zz != yy) {
928e1311b90SBarry Smith     ierr = VecRestoreArray(zz,&z); CHKERRQ(ierr);
9292e8a6d31SBarry Smith   }
9303a40ed3dSBarry Smith   PetscFunctionReturn(0);
93117ab2063SBarry Smith }
93217ab2063SBarry Smith 
93317ab2063SBarry Smith /*
93417ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
93517ab2063SBarry Smith */
9365615d1e5SSatish Balay #undef __FUNC__
9375615d1e5SSatish Balay #define __FUNC__ "MatMarkDiag_SeqAIJ"
938416022c9SBarry Smith int MatMarkDiag_SeqAIJ(Mat A)
93917ab2063SBarry Smith {
940416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
941416022c9SBarry Smith   int        i,j, *diag, m = a->m,shift = a->indexshift;
94217ab2063SBarry Smith 
9433a40ed3dSBarry Smith   PetscFunctionBegin;
9440452661fSBarry Smith   diag = (int *) PetscMalloc( (m+1)*sizeof(int)); CHKPTRQ(diag);
945416022c9SBarry Smith   PLogObjectMemory(A,(m+1)*sizeof(int));
946416022c9SBarry Smith   for ( i=0; i<a->m; i++ ) {
94735b0346bSBarry Smith     diag[i] = a->i[i+1];
948416022c9SBarry Smith     for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
949416022c9SBarry Smith       if (a->j[j]+shift == i) {
95017ab2063SBarry Smith         diag[i] = j - shift;
95117ab2063SBarry Smith         break;
95217ab2063SBarry Smith       }
95317ab2063SBarry Smith     }
95417ab2063SBarry Smith   }
955416022c9SBarry Smith   a->diag = diag;
9563a40ed3dSBarry Smith   PetscFunctionReturn(0);
95717ab2063SBarry Smith }
95817ab2063SBarry Smith 
959be5855fcSBarry Smith /*
960be5855fcSBarry Smith      Checks for missing diagonals
961be5855fcSBarry Smith */
962be5855fcSBarry Smith #undef __FUNC__
963be5855fcSBarry Smith #define __FUNC__ "MatMissingDiag_SeqAIJ"
964be5855fcSBarry Smith int MatMissingDiag_SeqAIJ(Mat A)
965be5855fcSBarry Smith {
966be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
967be5855fcSBarry Smith   int        *diag = a->diag, *jj = a->j,i,shift = a->indexshift;
968be5855fcSBarry Smith 
969be5855fcSBarry Smith   PetscFunctionBegin;
970be5855fcSBarry Smith   for ( i=0; i<a->m; i++ ) {
971be5855fcSBarry Smith     if (jj[diag[i]+shift] != i-shift) {
972be5855fcSBarry Smith       SETERRQ1(1,1,"Matrix is missing diagonal number %d",i);
973be5855fcSBarry Smith     }
974be5855fcSBarry Smith   }
975be5855fcSBarry Smith   PetscFunctionReturn(0);
976be5855fcSBarry Smith }
977be5855fcSBarry Smith 
9785615d1e5SSatish Balay #undef __FUNC__
9795615d1e5SSatish Balay #define __FUNC__ "MatRelax_SeqAIJ"
980be5855fcSBarry Smith int MatRelax_SeqAIJ(Mat A,Vec bb,double omega,MatSORType flag,double fshift,int its,Vec xx)
98117ab2063SBarry Smith {
982416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
983d6ed3216SSatish Balay   Scalar     *x, *b, *bs,  d, *xs, sum, *v = a->a,*t=0,scale,*ts, *xb,*idiag=0;
984d5d45c9bSBarry Smith   int        ierr, *idx, *diag,n = a->n, m = a->m, i, shift = a->indexshift;
98517ab2063SBarry Smith 
9863a40ed3dSBarry Smith   PetscFunctionBegin;
987e1311b90SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
988fb2e594dSBarry Smith   if (xx != bb) {
989e1311b90SBarry Smith     ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
990fb2e594dSBarry Smith   } else {
991fb2e594dSBarry Smith     b = x;
992fb2e594dSBarry Smith   }
993fb2e594dSBarry Smith 
994d00d2cf4SBarry Smith   if (!a->diag) {ierr = MatMarkDiag_SeqAIJ(A);CHKERRQ(ierr);}
995416022c9SBarry Smith   diag = a->diag;
996416022c9SBarry Smith   xs   = x + shift; /* shifted by one for index start of a or a->j*/
99717ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
99817ab2063SBarry Smith    /* apply ( U + D/omega) to the vector */
99917ab2063SBarry Smith     bs = b + shift;
100017ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
1001416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1002416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1003488ecbafSBarry Smith 	PLogFlops(2*n-1);
1004416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1005416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
100617ab2063SBarry Smith         sum  = b[i]*d/omega;
100717ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
100817ab2063SBarry Smith         x[i] = sum;
100917ab2063SBarry Smith     }
1010cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
1011fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
10123a40ed3dSBarry Smith     PetscFunctionReturn(0);
101317ab2063SBarry Smith   }
1014c783ea89SBarry Smith 
1015c783ea89SBarry Smith   /* setup workspace for Eisenstat */
1016c783ea89SBarry Smith   if (flag & SOR_EISENSTAT) {
1017c783ea89SBarry Smith     if (!a->idiag) {
1018c783ea89SBarry Smith       a->idiag = (Scalar *) PetscMalloc(2*m*sizeof(Scalar));CHKPTRQ(a->idiag);
1019c783ea89SBarry Smith       a->ssor  = a->idiag + m;
1020c783ea89SBarry Smith       v        = a->a;
1021c783ea89SBarry Smith       for ( i=0; i<m; i++ ) { a->idiag[i] = 1.0/v[diag[i]];}
1022c783ea89SBarry Smith     }
1023c783ea89SBarry Smith     t     = a->ssor;
1024c783ea89SBarry Smith     idiag = a->idiag;
1025c783ea89SBarry Smith   }
1026fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1027fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1028fc3d8934SBarry Smith 
1029fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1030fc3d8934SBarry Smith 
1031fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1032fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
103348af12d7SBarry Smith     is the relaxation factor.
1034fc3d8934SBarry Smith     */
1035fc3d8934SBarry Smith 
103648af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
103748af12d7SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"SOR_APPLY_LOWER is not done");
103848af12d7SBarry Smith   } else if ((flag & SOR_EISENSTAT) && omega == 1.0 && shift == 0 && fshift == 0.0) {
103948af12d7SBarry Smith     /* special case for omega = 1.0 saves flops and some integer ops */
1040733d66baSBarry Smith     Scalar *v2;
104148af12d7SBarry Smith 
1042733d66baSBarry Smith     v2    = a->a;
1043fc3d8934SBarry Smith     /*  x = (E + U)^{-1} b */
1044fc3d8934SBarry Smith     for ( i=m-1; i>=0; i-- ) {
1045fc3d8934SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1046fc3d8934SBarry Smith       idx  = a->j + diag[i] + 1;
1047fc3d8934SBarry Smith       v    = a->a + diag[i] + 1;
1048fc3d8934SBarry Smith       sum  = b[i];
1049fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1050b4632166SBarry Smith       x[i] = sum*idiag[i];
1051fc3d8934SBarry Smith 
1052fc3d8934SBarry Smith       /*  t = b - (2*E - D)x */
1053733d66baSBarry Smith       t[i] = b[i] - (v2[diag[i]])*x[i];
1054733d66baSBarry Smith     }
1055fc3d8934SBarry Smith 
1056fc3d8934SBarry Smith     /*  t = (E + L)^{-1}t */
1057fc3d8934SBarry Smith     diag = a->diag;
1058fc3d8934SBarry Smith     for ( i=0; i<m; i++ ) {
1059fc3d8934SBarry Smith       n    = diag[i] - a->i[i];
1060fc3d8934SBarry Smith       idx  = a->j + a->i[i];
1061fc3d8934SBarry Smith       v    = a->a + a->i[i];
1062fc3d8934SBarry Smith       sum  = t[i];
1063fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,t,v,idx,n);
1064b4632166SBarry Smith       t[i]  = sum*idiag[i];
1065fc3d8934SBarry Smith 
1066fc3d8934SBarry Smith       /*  x = x + t */
1067733d66baSBarry Smith       x[i] += t[i];
1068733d66baSBarry Smith     }
1069733d66baSBarry Smith 
1070a4d9cbf6SBarry Smith     PLogFlops(3*m-1 + 2*a->nz);
1071fc3d8934SBarry Smith     ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
1072fc3d8934SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
1073fc3d8934SBarry Smith     PetscFunctionReturn(0);
10743a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
107517ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
107617ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
107717ab2063SBarry Smith 
107817ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
107917ab2063SBarry Smith 
108017ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
108117ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
108217ab2063SBarry Smith     is the relaxation factor.
108317ab2063SBarry Smith     */
108417ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
108517ab2063SBarry Smith 
108617ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
108717ab2063SBarry Smith     for ( i=m-1; i>=0; i-- ) {
1088416022c9SBarry Smith       d    = fshift + a->a[diag[i] + shift];
1089416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1090416022c9SBarry Smith       idx  = a->j + diag[i] + (!shift);
1091416022c9SBarry Smith       v    = a->a + diag[i] + (!shift);
109217ab2063SBarry Smith       sum  = b[i];
109317ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
109417ab2063SBarry Smith       x[i] = omega*(sum/d);
109517ab2063SBarry Smith     }
109617ab2063SBarry Smith 
109717ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1098416022c9SBarry Smith     v = a->a;
109917ab2063SBarry Smith     for ( i=0; i<m; i++ ) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; }
110017ab2063SBarry Smith 
110117ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1102416022c9SBarry Smith     ts = t + shift; /* shifted by one for index start of a or a->j*/
1103416022c9SBarry Smith     diag = a->diag;
110417ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
1105416022c9SBarry Smith       d    = fshift + a->a[diag[i]+shift];
1106416022c9SBarry Smith       n    = diag[i] - a->i[i];
1107416022c9SBarry Smith       idx  = a->j + a->i[i] + shift;
1108416022c9SBarry Smith       v    = a->a + a->i[i] + shift;
110917ab2063SBarry Smith       sum  = t[i];
111017ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
111117ab2063SBarry Smith       t[i] = omega*(sum/d);
1112733d66baSBarry Smith       /*  x = x + t */
1113733d66baSBarry Smith       x[i] += t[i];
111417ab2063SBarry Smith     }
111517ab2063SBarry Smith 
1116c783ea89SBarry Smith     PLogFlops(6*m-1 + 2*a->nz);
1117cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
1118fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
11193a40ed3dSBarry Smith     PetscFunctionReturn(0);
112017ab2063SBarry Smith   }
112117ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
112217ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
112317ab2063SBarry Smith       for ( i=0; i<m; i++ ) {
1124416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1125416022c9SBarry Smith         n    = diag[i] - a->i[i];
1126488ecbafSBarry Smith 	PLogFlops(2*n-1);
1127416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1128416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
112917ab2063SBarry Smith         sum  = b[i];
113017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
113117ab2063SBarry Smith         x[i] = omega*(sum/d);
113217ab2063SBarry Smith       }
113317ab2063SBarry Smith       xb = x;
11343a40ed3dSBarry Smith     } else xb = b;
113517ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
113617ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
113717ab2063SBarry Smith       for ( i=0; i<m; i++ ) {
1138416022c9SBarry Smith         x[i] *= a->a[diag[i]+shift];
113917ab2063SBarry Smith       }
1140488ecbafSBarry Smith       PLogFlops(m);
114117ab2063SBarry Smith     }
114217ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
114317ab2063SBarry Smith       for ( i=m-1; i>=0; i-- ) {
1144416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1145416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1146488ecbafSBarry Smith 	PLogFlops(2*n-1);
1147416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1148416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
114917ab2063SBarry Smith         sum  = xb[i];
115017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
115117ab2063SBarry Smith         x[i] = omega*(sum/d);
115217ab2063SBarry Smith       }
115317ab2063SBarry Smith     }
115417ab2063SBarry Smith     its--;
115517ab2063SBarry Smith   }
115617ab2063SBarry Smith   while (its--) {
115717ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
115817ab2063SBarry Smith       for ( i=0; i<m; i++ ) {
1159416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1160416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1161488ecbafSBarry Smith 	PLogFlops(2*n-1);
1162416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1163416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
116417ab2063SBarry Smith         sum  = b[i];
116517ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
11667e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
116717ab2063SBarry Smith       }
116817ab2063SBarry Smith     }
116917ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
117017ab2063SBarry Smith       for ( i=m-1; i>=0; i-- ) {
1171416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1172416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1173488ecbafSBarry Smith 	PLogFlops(2*n-1);
1174416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1175416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
117617ab2063SBarry Smith         sum  = b[i];
117717ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
11787e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
117917ab2063SBarry Smith       }
118017ab2063SBarry Smith     }
118117ab2063SBarry Smith   }
1182cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
1183fb2e594dSBarry Smith   if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
11843a40ed3dSBarry Smith   PetscFunctionReturn(0);
118517ab2063SBarry Smith }
118617ab2063SBarry Smith 
11875615d1e5SSatish Balay #undef __FUNC__
11885615d1e5SSatish Balay #define __FUNC__ "MatGetInfo_SeqAIJ"
11898f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
119017ab2063SBarry Smith {
1191416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
11924e220ebcSLois Curfman McInnes 
11933a40ed3dSBarry Smith   PetscFunctionBegin;
11944e220ebcSLois Curfman McInnes   info->rows_global    = (double)a->m;
11954e220ebcSLois Curfman McInnes   info->columns_global = (double)a->n;
11964e220ebcSLois Curfman McInnes   info->rows_local     = (double)a->m;
11974e220ebcSLois Curfman McInnes   info->columns_local  = (double)a->n;
11984e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
11994e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12004e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12014e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12024e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12034e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12044e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12054e220ebcSLois Curfman McInnes   if (A->factor) {
12064e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12074e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12084e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12094e220ebcSLois Curfman McInnes   } else {
12104e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12114e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12124e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12134e220ebcSLois Curfman McInnes   }
12143a40ed3dSBarry Smith   PetscFunctionReturn(0);
121517ab2063SBarry Smith }
121617ab2063SBarry Smith 
121717ab2063SBarry Smith extern int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,double,Mat*);
121817ab2063SBarry Smith extern int MatLUFactorNumeric_SeqAIJ(Mat,Mat*);
121917ab2063SBarry Smith extern int MatLUFactor_SeqAIJ(Mat,IS,IS,double);
122017ab2063SBarry Smith extern int MatSolve_SeqAIJ(Mat,Vec,Vec);
122117ab2063SBarry Smith extern int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
122217ab2063SBarry Smith extern int MatSolveTrans_SeqAIJ(Mat,Vec,Vec);
122317ab2063SBarry Smith extern int MatSolveTransAdd_SeqAIJ(Mat,Vec,Vec,Vec);
122417ab2063SBarry Smith 
12255615d1e5SSatish Balay #undef __FUNC__
12265615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_SeqAIJ"
12278f6be9afSLois Curfman McInnes int MatZeroRows_SeqAIJ(Mat A,IS is,Scalar *diag)
122817ab2063SBarry Smith {
1229416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1230416022c9SBarry Smith   int         i,ierr,N, *rows,m = a->m - 1,shift = a->indexshift;
123117ab2063SBarry Smith 
12323a40ed3dSBarry Smith   PetscFunctionBegin;
123377c4ece6SBarry Smith   ierr = ISGetSize(is,&N); CHKERRQ(ierr);
123417ab2063SBarry Smith   ierr = ISGetIndices(is,&rows); CHKERRQ(ierr);
123517ab2063SBarry Smith   if (diag) {
123617ab2063SBarry Smith     for ( i=0; i<N; i++ ) {
1237a8c6a408SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range");
12387ae801bdSBarry Smith       if (a->ilen[rows[i]] > 0) {
1239416022c9SBarry Smith         a->ilen[rows[i]] = 1;
1240416022c9SBarry Smith         a->a[a->i[rows[i]]+shift] = *diag;
1241416022c9SBarry Smith         a->j[a->i[rows[i]]+shift] = rows[i]+shift;
12427ae801bdSBarry Smith       } else { /* in case row was completely empty */
1243d64ed03dSBarry Smith         ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
124417ab2063SBarry Smith       }
124517ab2063SBarry Smith     }
12463a40ed3dSBarry Smith   } else {
124717ab2063SBarry Smith     for ( i=0; i<N; i++ ) {
1248a8c6a408SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range");
1249416022c9SBarry Smith       a->ilen[rows[i]] = 0;
125017ab2063SBarry Smith     }
125117ab2063SBarry Smith   }
12527ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
125343a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
12543a40ed3dSBarry Smith   PetscFunctionReturn(0);
125517ab2063SBarry Smith }
125617ab2063SBarry Smith 
12575615d1e5SSatish Balay #undef __FUNC__
12585615d1e5SSatish Balay #define __FUNC__ "MatGetSize_SeqAIJ"
12598f6be9afSLois Curfman McInnes int MatGetSize_SeqAIJ(Mat A,int *m,int *n)
126017ab2063SBarry Smith {
1261416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
12623a40ed3dSBarry Smith 
12633a40ed3dSBarry Smith   PetscFunctionBegin;
12640752156aSBarry Smith   if (m) *m = a->m;
12650752156aSBarry Smith   if (n) *n = a->n;
12663a40ed3dSBarry Smith   PetscFunctionReturn(0);
126717ab2063SBarry Smith }
126817ab2063SBarry Smith 
12695615d1e5SSatish Balay #undef __FUNC__
12705615d1e5SSatish Balay #define __FUNC__ "MatGetOwnershipRange_SeqAIJ"
12718f6be9afSLois Curfman McInnes int MatGetOwnershipRange_SeqAIJ(Mat A,int *m,int *n)
127217ab2063SBarry Smith {
1273416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
12743a40ed3dSBarry Smith 
12753a40ed3dSBarry Smith   PetscFunctionBegin;
1276416022c9SBarry Smith   *m = 0; *n = a->m;
12773a40ed3dSBarry Smith   PetscFunctionReturn(0);
127817ab2063SBarry Smith }
1279026e39d0SSatish Balay 
12805615d1e5SSatish Balay #undef __FUNC__
12815615d1e5SSatish Balay #define __FUNC__ "MatGetRow_SeqAIJ"
12824e093b46SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
128317ab2063SBarry Smith {
1284416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1285c456f294SBarry Smith   int        *itmp,i,shift = a->indexshift;
128617ab2063SBarry Smith 
12873a40ed3dSBarry Smith   PetscFunctionBegin;
1288a8c6a408SBarry Smith   if (row < 0 || row >= a->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row out of range");
128917ab2063SBarry Smith 
1290416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1291416022c9SBarry Smith   if (v) *v = a->a + a->i[row] + shift;
129217ab2063SBarry Smith   if (idx) {
1293416022c9SBarry Smith     itmp = a->j + a->i[row] + shift;
12944e093b46SBarry Smith     if (*nz && shift) {
12950452661fSBarry Smith       *idx = (int *) PetscMalloc( (*nz)*sizeof(int) ); CHKPTRQ(*idx);
129617ab2063SBarry Smith       for ( i=0; i<(*nz); i++ ) {(*idx)[i] = itmp[i] + shift;}
12974e093b46SBarry Smith     } else if (*nz) {
12984e093b46SBarry Smith       *idx = itmp;
129917ab2063SBarry Smith     }
130017ab2063SBarry Smith     else *idx = 0;
130117ab2063SBarry Smith   }
13023a40ed3dSBarry Smith   PetscFunctionReturn(0);
130317ab2063SBarry Smith }
130417ab2063SBarry Smith 
13055615d1e5SSatish Balay #undef __FUNC__
13065615d1e5SSatish Balay #define __FUNC__ "MatRestoreRow_SeqAIJ"
13074e093b46SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
130817ab2063SBarry Smith {
13094e093b46SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
13103a40ed3dSBarry Smith 
13113a40ed3dSBarry Smith   PetscFunctionBegin;
13124e093b46SBarry Smith   if (idx) {if (*idx && a->indexshift) PetscFree(*idx);}
13133a40ed3dSBarry Smith   PetscFunctionReturn(0);
131417ab2063SBarry Smith }
131517ab2063SBarry Smith 
13165615d1e5SSatish Balay #undef __FUNC__
13175615d1e5SSatish Balay #define __FUNC__ "MatNorm_SeqAIJ"
13188f6be9afSLois Curfman McInnes int MatNorm_SeqAIJ(Mat A,NormType type,double *norm)
131917ab2063SBarry Smith {
1320416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1321416022c9SBarry Smith   Scalar     *v = a->a;
132217ab2063SBarry Smith   double     sum = 0.0;
1323416022c9SBarry Smith   int        i, j,shift = a->indexshift;
132417ab2063SBarry Smith 
13253a40ed3dSBarry Smith   PetscFunctionBegin;
132617ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1327416022c9SBarry Smith     for (i=0; i<a->nz; i++ ) {
13283a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
1329e20fef11SSatish Balay       sum += PetscReal(PetscConj(*v)*(*v)); v++;
133017ab2063SBarry Smith #else
133117ab2063SBarry Smith       sum += (*v)*(*v); v++;
133217ab2063SBarry Smith #endif
133317ab2063SBarry Smith     }
133417ab2063SBarry Smith     *norm = sqrt(sum);
13353a40ed3dSBarry Smith   } else if (type == NORM_1) {
133617ab2063SBarry Smith     double *tmp;
1337416022c9SBarry Smith     int    *jj = a->j;
133866963ce1SSatish Balay     tmp = (double *) PetscMalloc( (a->n+1)*sizeof(double) ); CHKPTRQ(tmp);
1339cddf8d76SBarry Smith     PetscMemzero(tmp,a->n*sizeof(double));
134017ab2063SBarry Smith     *norm = 0.0;
1341416022c9SBarry Smith     for ( j=0; j<a->nz; j++ ) {
1342a2744918SBarry Smith         tmp[*jj++ + shift] += PetscAbsScalar(*v);  v++;
134317ab2063SBarry Smith     }
1344416022c9SBarry Smith     for ( j=0; j<a->n; j++ ) {
134517ab2063SBarry Smith       if (tmp[j] > *norm) *norm = tmp[j];
134617ab2063SBarry Smith     }
13470452661fSBarry Smith     PetscFree(tmp);
13483a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
134917ab2063SBarry Smith     *norm = 0.0;
1350416022c9SBarry Smith     for ( j=0; j<a->m; j++ ) {
1351416022c9SBarry Smith       v = a->a + a->i[j] + shift;
135217ab2063SBarry Smith       sum = 0.0;
1353416022c9SBarry Smith       for ( i=0; i<a->i[j+1]-a->i[j]; i++ ) {
1354cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
135517ab2063SBarry Smith       }
135617ab2063SBarry Smith       if (sum > *norm) *norm = sum;
135717ab2063SBarry Smith     }
13583a40ed3dSBarry Smith   } else {
1359a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"No support for two norm");
136017ab2063SBarry Smith   }
13613a40ed3dSBarry Smith   PetscFunctionReturn(0);
136217ab2063SBarry Smith }
136317ab2063SBarry Smith 
13645615d1e5SSatish Balay #undef __FUNC__
13655615d1e5SSatish Balay #define __FUNC__ "MatTranspose_SeqAIJ"
13668f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
136717ab2063SBarry Smith {
1368416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1369416022c9SBarry Smith   Mat        C;
1370416022c9SBarry Smith   int        i, ierr, *aj = a->j, *ai = a->i, m = a->m, len, *col;
1371416022c9SBarry Smith   int        shift = a->indexshift;
1372d5d45c9bSBarry Smith   Scalar     *array = a->a;
137317ab2063SBarry Smith 
13743a40ed3dSBarry Smith   PetscFunctionBegin;
1375a8c6a408SBarry Smith   if (B == PETSC_NULL && m != a->n) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Square matrix only for in-place");
13760452661fSBarry Smith   col = (int *) PetscMalloc((1+a->n)*sizeof(int)); CHKPTRQ(col);
1377cddf8d76SBarry Smith   PetscMemzero(col,(1+a->n)*sizeof(int));
137817ab2063SBarry Smith   if (shift) {
137917ab2063SBarry Smith     for ( i=0; i<ai[m]-1; i++ ) aj[i] -= 1;
138017ab2063SBarry Smith   }
138117ab2063SBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) col[aj[i]] += 1;
1382416022c9SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,a->n,m,0,col,&C); CHKERRQ(ierr);
13830452661fSBarry Smith   PetscFree(col);
138417ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
138517ab2063SBarry Smith     len = ai[i+1]-ai[i];
1386416022c9SBarry Smith     ierr = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES); CHKERRQ(ierr);
138717ab2063SBarry Smith     array += len; aj += len;
138817ab2063SBarry Smith   }
138917ab2063SBarry Smith   if (shift) {
139017ab2063SBarry Smith     for ( i=0; i<ai[m]-1; i++ ) aj[i] += 1;
139117ab2063SBarry Smith   }
139217ab2063SBarry Smith 
13936d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
13946d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
139517ab2063SBarry Smith 
13963638b69dSLois Curfman McInnes   if (B != PETSC_NULL) {
1397416022c9SBarry Smith     *B = C;
139817ab2063SBarry Smith   } else {
1399f830108cSBarry Smith     PetscOps *Abops;
14000a6ffc59SBarry Smith     MatOps   Aops;
1401f830108cSBarry Smith 
1402416022c9SBarry Smith     /* This isn't really an in-place transpose */
14030452661fSBarry Smith     PetscFree(a->a);
14040452661fSBarry Smith     if (!a->singlemalloc) {PetscFree(a->i); PetscFree(a->j);}
14050452661fSBarry Smith     if (a->diag) PetscFree(a->diag);
14060452661fSBarry Smith     if (a->ilen) PetscFree(a->ilen);
14070452661fSBarry Smith     if (a->imax) PetscFree(a->imax);
14080452661fSBarry Smith     if (a->solve_work) PetscFree(a->solve_work);
14091073c447SSatish Balay     if (a->inode.size) PetscFree(a->inode.size);
14100452661fSBarry Smith     PetscFree(a);
1411f830108cSBarry Smith 
1412488ecbafSBarry Smith 
1413488ecbafSBarry Smith     ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
1414488ecbafSBarry Smith     ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
1415488ecbafSBarry Smith 
1416f830108cSBarry Smith     /*
1417f830108cSBarry Smith       This is horrible, horrible code. We need to keep the
14188f0f457cSSatish Balay       the bops and ops Structures, copy everything from C
14198f0f457cSSatish Balay       including the function pointers..
1420f830108cSBarry Smith     */
14218f0f457cSSatish Balay     PetscMemcpy(A->ops,C->ops,sizeof(struct _MatOps));
14228f0f457cSSatish Balay     PetscMemcpy(A->bops,C->bops,sizeof(PetscOps));
1423f830108cSBarry Smith     Abops    = A->bops;
1424f830108cSBarry Smith     Aops     = A->ops;
1425f09e8eb9SSatish Balay     PetscMemcpy(A,C,sizeof(struct _p_Mat));
1426f830108cSBarry Smith     A->bops  = Abops;
1427f830108cSBarry Smith     A->ops   = Aops;
142827a8da17SBarry Smith     A->qlist = 0;
1429f830108cSBarry Smith 
14300452661fSBarry Smith     PetscHeaderDestroy(C);
143117ab2063SBarry Smith   }
14323a40ed3dSBarry Smith   PetscFunctionReturn(0);
143317ab2063SBarry Smith }
143417ab2063SBarry Smith 
14355615d1e5SSatish Balay #undef __FUNC__
14365615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_SeqAIJ"
14378f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
143817ab2063SBarry Smith {
1439416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
144017ab2063SBarry Smith   Scalar     *l,*r,x,*v;
1441e1311b90SBarry Smith   int        ierr,i,j,m = a->m, n = a->n, M, nz = a->nz, *jj,shift = a->indexshift;
144217ab2063SBarry Smith 
14433a40ed3dSBarry Smith   PetscFunctionBegin;
144417ab2063SBarry Smith   if (ll) {
14453ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14463ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1447e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1448a8c6a408SBarry Smith     if (m != a->m) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Left scaling vector wrong length");
1449e1311b90SBarry Smith     ierr = VecGetArray(ll,&l); CHKERRQ(ierr);
1450416022c9SBarry Smith     v = a->a;
145117ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
145217ab2063SBarry Smith       x = l[i];
1453416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
145417ab2063SBarry Smith       for ( j=0; j<M; j++ ) { (*v++) *= x;}
145517ab2063SBarry Smith     }
1456e1311b90SBarry Smith     ierr = VecRestoreArray(ll,&l); CHKERRQ(ierr);
145744cd7ae7SLois Curfman McInnes     PLogFlops(nz);
145817ab2063SBarry Smith   }
145917ab2063SBarry Smith   if (rr) {
1460e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1461a8c6a408SBarry Smith     if (n != a->n) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Right scaling vector wrong length");
1462e1311b90SBarry Smith     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1463416022c9SBarry Smith     v = a->a; jj = a->j;
146417ab2063SBarry Smith     for ( i=0; i<nz; i++ ) {
146517ab2063SBarry Smith       (*v++) *= r[*jj++ + shift];
146617ab2063SBarry Smith     }
1467e1311b90SBarry Smith     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
146844cd7ae7SLois Curfman McInnes     PLogFlops(nz);
146917ab2063SBarry Smith   }
14703a40ed3dSBarry Smith   PetscFunctionReturn(0);
147117ab2063SBarry Smith }
147217ab2063SBarry Smith 
14735615d1e5SSatish Balay #undef __FUNC__
14745615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrix_SeqAIJ"
14757b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B)
147617ab2063SBarry Smith {
1477db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ *) A->data,*c;
1478d5db1b72SBarry Smith   int          *smap, i, k, kstart, kend, ierr, oldcols = a->n,*lens;
147999141d43SSatish Balay   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen;
1480a2744918SBarry Smith   register int sum,lensi;
148199141d43SSatish Balay   int          *irow, *icol, nrows, ncols, shift = a->indexshift,*ssmap;
148299141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j, *ai = a->i,ii,*ailen = a->ilen;
148399141d43SSatish Balay   Scalar       *a_new,*mat_a;
1484416022c9SBarry Smith   Mat          C;
1485fee21e36SBarry Smith   PetscTruth   stride;
148617ab2063SBarry Smith 
14873a40ed3dSBarry Smith   PetscFunctionBegin;
1488d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
1489a8c6a408SBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"ISrow is not sorted");
1490d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
1491a8c6a408SBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"IScol is not sorted");
149299141d43SSatish Balay 
149317ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow); CHKERRQ(ierr);
149417ab2063SBarry Smith   ierr = ISGetSize(isrow,&nrows); CHKERRQ(ierr);
149517ab2063SBarry Smith   ierr = ISGetSize(iscol,&ncols); CHKERRQ(ierr);
149617ab2063SBarry Smith 
1497fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step); CHKERRQ(ierr);
1498fee21e36SBarry Smith   ierr = ISStride(iscol,&stride); CHKERRQ(ierr);
1499fee21e36SBarry Smith   if (stride && step == 1) {
150002834360SBarry Smith     /* special case of contiguous rows */
150157faeb66SBarry Smith     lens   = (int *) PetscMalloc((ncols+nrows+1)*sizeof(int)); CHKPTRQ(lens);
150202834360SBarry Smith     starts = lens + ncols;
150302834360SBarry Smith     /* loop over new rows determining lens and starting points */
150402834360SBarry Smith     for (i=0; i<nrows; i++) {
1505a2744918SBarry Smith       kstart  = ai[irow[i]]+shift;
1506a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
150702834360SBarry Smith       for ( k=kstart; k<kend; k++ ) {
1508d8ced48eSBarry Smith         if (aj[k]+shift >= first) {
150902834360SBarry Smith           starts[i] = k;
151002834360SBarry Smith           break;
151102834360SBarry Smith 	}
151202834360SBarry Smith       }
1513a2744918SBarry Smith       sum = 0;
151402834360SBarry Smith       while (k < kend) {
1515d8ced48eSBarry Smith         if (aj[k++]+shift >= first+ncols) break;
1516a2744918SBarry Smith         sum++;
151702834360SBarry Smith       }
1518a2744918SBarry Smith       lens[i] = sum;
151902834360SBarry Smith     }
152002834360SBarry Smith     /* create submatrix */
1521cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
152208480c60SBarry Smith       int n_cols,n_rows;
152308480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols); CHKERRQ(ierr);
1524a8c6a408SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Reused submatrix wrong size");
1525d8ced48eSBarry Smith       ierr = MatZeroEntries(*B); CHKERRQ(ierr);
152608480c60SBarry Smith       C = *B;
15273a40ed3dSBarry Smith     } else {
152802834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
152908480c60SBarry Smith     }
1530db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*) C->data;
1531db02288aSLois Curfman McInnes 
153202834360SBarry Smith     /* loop over rows inserting into submatrix */
1533db02288aSLois Curfman McInnes     a_new    = c->a;
1534db02288aSLois Curfman McInnes     j_new    = c->j;
1535db02288aSLois Curfman McInnes     i_new    = c->i;
1536db02288aSLois Curfman McInnes     i_new[0] = -shift;
153702834360SBarry Smith     for (i=0; i<nrows; i++) {
1538a2744918SBarry Smith       ii    = starts[i];
1539a2744918SBarry Smith       lensi = lens[i];
1540a2744918SBarry Smith       for ( k=0; k<lensi; k++ ) {
1541a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
154202834360SBarry Smith       }
1543a2744918SBarry Smith       PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(Scalar));
1544a2744918SBarry Smith       a_new      += lensi;
1545a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1546a2744918SBarry Smith       c->ilen[i]  = lensi;
154702834360SBarry Smith     }
15480452661fSBarry Smith     PetscFree(lens);
15493a40ed3dSBarry Smith   } else {
155002834360SBarry Smith     ierr = ISGetIndices(iscol,&icol); CHKERRQ(ierr);
15510452661fSBarry Smith     smap  = (int *) PetscMalloc((1+oldcols)*sizeof(int)); CHKPTRQ(smap);
155202834360SBarry Smith     ssmap = smap + shift;
155399141d43SSatish Balay     lens  = (int *) PetscMalloc((1+nrows)*sizeof(int)); CHKPTRQ(lens);
1554cddf8d76SBarry Smith     PetscMemzero(smap,oldcols*sizeof(int));
155517ab2063SBarry Smith     for ( i=0; i<ncols; i++ ) smap[icol[i]] = i+1;
155602834360SBarry Smith     /* determine lens of each row */
155702834360SBarry Smith     for (i=0; i<nrows; i++) {
1558d8ced48eSBarry Smith       kstart  = ai[irow[i]]+shift;
155902834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
156002834360SBarry Smith       lens[i] = 0;
156102834360SBarry Smith       for ( k=kstart; k<kend; k++ ) {
1562d8ced48eSBarry Smith         if (ssmap[aj[k]]) {
156302834360SBarry Smith           lens[i]++;
156402834360SBarry Smith         }
156502834360SBarry Smith       }
156602834360SBarry Smith     }
156717ab2063SBarry Smith     /* Create and fill new matrix */
1568a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
156999141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1570a8c6a408SBarry Smith       if (c->m  != nrows || c->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong size");
157199141d43SSatish Balay       if (PetscMemcmp(c->ilen,lens, c->m *sizeof(int))) {
1572a8c6a408SBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong no of nonzeros");
157399141d43SSatish Balay       }
157499141d43SSatish Balay       PetscMemzero(c->ilen,c->m*sizeof(int));
157508480c60SBarry Smith       C = *B;
15763a40ed3dSBarry Smith     } else {
157702834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
157808480c60SBarry Smith     }
157999141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
158017ab2063SBarry Smith     for (i=0; i<nrows; i++) {
158199141d43SSatish Balay       row    = irow[i];
158299141d43SSatish Balay       kstart = ai[row]+shift;
158399141d43SSatish Balay       kend   = kstart + a->ilen[row];
158499141d43SSatish Balay       mat_i  = c->i[i]+shift;
158599141d43SSatish Balay       mat_j  = c->j + mat_i;
158699141d43SSatish Balay       mat_a  = c->a + mat_i;
158799141d43SSatish Balay       mat_ilen = c->ilen + i;
158817ab2063SBarry Smith       for ( k=kstart; k<kend; k++ ) {
158999141d43SSatish Balay         if ((tcol=ssmap[a->j[k]])) {
159099141d43SSatish Balay           *mat_j++ = tcol - (!shift);
159199141d43SSatish Balay           *mat_a++ = a->a[k];
159299141d43SSatish Balay           (*mat_ilen)++;
159399141d43SSatish Balay 
159417ab2063SBarry Smith         }
159517ab2063SBarry Smith       }
159617ab2063SBarry Smith     }
159702834360SBarry Smith     /* Free work space */
159802834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol); CHKERRQ(ierr);
159999141d43SSatish Balay     PetscFree(smap); PetscFree(lens);
160002834360SBarry Smith   }
16016d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
16026d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
160317ab2063SBarry Smith 
160417ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow); CHKERRQ(ierr);
1605416022c9SBarry Smith   *B = C;
16063a40ed3dSBarry Smith   PetscFunctionReturn(0);
160717ab2063SBarry Smith }
160817ab2063SBarry Smith 
1609a871dcd8SBarry Smith /*
1610a871dcd8SBarry Smith */
16115615d1e5SSatish Balay #undef __FUNC__
16125615d1e5SSatish Balay #define __FUNC__ "MatILUFactor_SeqAIJ"
16135ef9f2a5SBarry Smith int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatILUInfo *info)
1614a871dcd8SBarry Smith {
161563b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) inA->data;
161608480c60SBarry Smith   int        ierr;
161763b91edcSBarry Smith   Mat        outA;
1618*b8a78c4aSBarry Smith   PetscTruth row_identity, col_identity;
161963b91edcSBarry Smith 
16203a40ed3dSBarry Smith   PetscFunctionBegin;
1621*b8a78c4aSBarry Smith   if (info && info->levels != 0) SETERRQ(PETSC_ERR_SUP,0,"Only levels=0 supported for in-place ilu");
1622*b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity); CHKERRQ(ierr);
1623*b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity); CHKERRQ(ierr);
1624*b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
1625*b8a78c4aSBarry Smith     SETERRQ(1,1,"Row and column permutations must be identity for in-place ILU");
1626*b8a78c4aSBarry Smith   }
1627a871dcd8SBarry Smith 
162863b91edcSBarry Smith   outA          = inA;
162963b91edcSBarry Smith   inA->factor   = FACTOR_LU;
163063b91edcSBarry Smith   a->row        = row;
163163b91edcSBarry Smith   a->col        = col;
163263b91edcSBarry Smith 
1633f0ec6fceSSatish Balay   /* Create the invert permutation so that it can be used in MatLUFactorNumeric() */
1634f0ec6fceSSatish Balay   ierr = ISInvertPermutation(col,&(a->icol)); CHKERRQ(ierr);
16351e4dd532SSatish Balay   PLogObjectParent(inA,a->icol);
1636f0ec6fceSSatish Balay 
163794a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
16380452661fSBarry Smith     a->solve_work = (Scalar *) PetscMalloc( (a->m+1)*sizeof(Scalar));CHKPTRQ(a->solve_work);
163994a9d846SBarry Smith   }
164063b91edcSBarry Smith 
164108480c60SBarry Smith   if (!a->diag) {
164208480c60SBarry Smith     ierr = MatMarkDiag_SeqAIJ(inA); CHKERRQ(ierr);
164363b91edcSBarry Smith   }
164463b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA); CHKERRQ(ierr);
16453a40ed3dSBarry Smith   PetscFunctionReturn(0);
1646a871dcd8SBarry Smith }
1647a871dcd8SBarry Smith 
164874cdf0dfSBarry Smith #include "pinclude/blaslapack.h"
16495615d1e5SSatish Balay #undef __FUNC__
16505615d1e5SSatish Balay #define __FUNC__ "MatScale_SeqAIJ"
16518f6be9afSLois Curfman McInnes int MatScale_SeqAIJ(Scalar *alpha,Mat inA)
1652f0b747eeSBarry Smith {
1653f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) inA->data;
1654f0b747eeSBarry Smith   int        one = 1;
16553a40ed3dSBarry Smith 
16563a40ed3dSBarry Smith   PetscFunctionBegin;
1657f0b747eeSBarry Smith   BLscal_( &a->nz, alpha, a->a, &one );
1658f0b747eeSBarry Smith   PLogFlops(a->nz);
16593a40ed3dSBarry Smith   PetscFunctionReturn(0);
1660f0b747eeSBarry Smith }
1661f0b747eeSBarry Smith 
16625615d1e5SSatish Balay #undef __FUNC__
16635615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrices_SeqAIJ"
16647b2a1423SBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n, IS *irow,IS *icol,MatReuse scall,Mat **B)
1665cddf8d76SBarry Smith {
1666cddf8d76SBarry Smith   int ierr,i;
1667cddf8d76SBarry Smith 
16683a40ed3dSBarry Smith   PetscFunctionBegin;
1669cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
16700452661fSBarry Smith     *B = (Mat *) PetscMalloc( (n+1)*sizeof(Mat) ); CHKPTRQ(*B);
1671cddf8d76SBarry Smith   }
1672cddf8d76SBarry Smith 
1673cddf8d76SBarry Smith   for ( i=0; i<n; i++ ) {
16746a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1675cddf8d76SBarry Smith   }
16763a40ed3dSBarry Smith   PetscFunctionReturn(0);
1677cddf8d76SBarry Smith }
1678cddf8d76SBarry Smith 
16795615d1e5SSatish Balay #undef __FUNC__
16805615d1e5SSatish Balay #define __FUNC__ "MatGetBlockSize_SeqAIJ"
16818f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A, int *bs)
16825a838052SSatish Balay {
1683f830108cSBarry Smith   PetscFunctionBegin;
16845a838052SSatish Balay   *bs = 1;
16853a40ed3dSBarry Smith   PetscFunctionReturn(0);
16865a838052SSatish Balay }
16875a838052SSatish Balay 
16885615d1e5SSatish Balay #undef __FUNC__
16895615d1e5SSatish Balay #define __FUNC__ "MatIncreaseOverlap_SeqAIJ"
16908f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A, int is_max, IS *is, int ov)
16914dcbc457SBarry Smith {
1692e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
169306763907SSatish Balay   int        shift, row, i,j,k,l,m,n, *idx,ierr, *nidx, isz, val;
16948a047759SSatish Balay   int        start, end, *ai, *aj;
1695bbd702dbSSatish Balay   BT         table;
1696bbd702dbSSatish Balay 
16973a40ed3dSBarry Smith   PetscFunctionBegin;
16988a047759SSatish Balay   shift = a->indexshift;
1699e4d965acSSatish Balay   m     = a->m;
1700e4d965acSSatish Balay   ai    = a->i;
17018a047759SSatish Balay   aj    = a->j+shift;
17028a047759SSatish Balay 
1703a8c6a408SBarry Smith   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"illegal overlap value used");
170406763907SSatish Balay 
170506763907SSatish Balay   nidx  = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(nidx);
1706bbd702dbSSatish Balay   ierr  = BTCreate(m,table); CHKERRQ(ierr);
170706763907SSatish Balay 
1708e4d965acSSatish Balay   for ( i=0; i<is_max; i++ ) {
1709b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1710e4d965acSSatish Balay     isz  = 0;
1711bbd702dbSSatish Balay     BTMemzero(m,table);
1712e4d965acSSatish Balay 
1713e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17144dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);  CHKERRQ(ierr);
171577c4ece6SBarry Smith     ierr = ISGetSize(is[i],&n);  CHKERRQ(ierr);
1716e4d965acSSatish Balay 
1717dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1718e4d965acSSatish Balay     for ( j=0; j<n ; ++j){
1719bbd702dbSSatish Balay       if(!BTLookupSet(table, idx[j])) { nidx[isz++] = idx[j];}
17204dcbc457SBarry Smith     }
172106763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);  CHKERRQ(ierr);
172206763907SSatish Balay     ierr = ISDestroy(is[i]); CHKERRQ(ierr);
1723e4d965acSSatish Balay 
172404a348a9SBarry Smith     k = 0;
172504a348a9SBarry Smith     for ( j=0; j<ov; j++){ /* for each overlap */
172604a348a9SBarry Smith       n = isz;
172706763907SSatish Balay       for ( ; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1728e4d965acSSatish Balay         row   = nidx[k];
1729e4d965acSSatish Balay         start = ai[row];
1730e4d965acSSatish Balay         end   = ai[row+1];
173104a348a9SBarry Smith         for ( l = start; l<end ; l++){
17328a047759SSatish Balay           val = aj[l] + shift;
17332a8f2162SSatish Balay           if (!BTLookupSet(table,val)) {nidx[isz++] = val;}
1734e4d965acSSatish Balay         }
1735e4d965acSSatish Balay       }
1736e4d965acSSatish Balay     }
1737029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF, isz, nidx, (is+i)); CHKERRQ(ierr);
1738e4d965acSSatish Balay   }
1739bbd702dbSSatish Balay   BTDestroy(table);
174006763907SSatish Balay   PetscFree(nidx);
17413a40ed3dSBarry Smith   PetscFunctionReturn(0);
17424dcbc457SBarry Smith }
174317ab2063SBarry Smith 
17440513a670SBarry Smith /* -------------------------------------------------------------- */
17450513a670SBarry Smith #undef __FUNC__
17460513a670SBarry Smith #define __FUNC__ "MatPermute_SeqAIJ"
17470513a670SBarry Smith int MatPermute_SeqAIJ(Mat A, IS rowp, IS colp, Mat *B)
17480513a670SBarry Smith {
17490513a670SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
17500513a670SBarry Smith   Scalar     *vwork;
17510513a670SBarry Smith   int        i, ierr, nz, m = a->m, n = a->n, *cwork;
17520513a670SBarry Smith   int        *row,*col,*cnew,j,*lens;
175356cd22aeSBarry Smith   IS         icolp,irowp;
17540513a670SBarry Smith 
17553a40ed3dSBarry Smith   PetscFunctionBegin;
175656cd22aeSBarry Smith   ierr = ISInvertPermutation(rowp,&irowp); CHKERRQ(ierr);
175756cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row); CHKERRQ(ierr);
175856cd22aeSBarry Smith   ierr = ISInvertPermutation(colp,&icolp); CHKERRQ(ierr);
175956cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col); CHKERRQ(ierr);
17600513a670SBarry Smith 
17610513a670SBarry Smith   /* determine lengths of permuted rows */
17620513a670SBarry Smith   lens = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(lens);
17630513a670SBarry Smith   for (i=0; i<m; i++ ) {
17640513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
17650513a670SBarry Smith   }
17660513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
17670513a670SBarry Smith   PetscFree(lens);
17680513a670SBarry Smith 
17690513a670SBarry Smith   cnew = (int *) PetscMalloc( n*sizeof(int) ); CHKPTRQ(cnew);
17700513a670SBarry Smith   for (i=0; i<m; i++) {
17710513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr);
17720513a670SBarry Smith     for (j=0; j<nz; j++ ) { cnew[j] = col[cwork[j]];}
17730513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES); CHKERRQ(ierr);
17740513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr);
17750513a670SBarry Smith   }
17760513a670SBarry Smith   PetscFree(cnew);
17770513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
17780513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
177956cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row); CHKERRQ(ierr);
178056cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col); CHKERRQ(ierr);
178156cd22aeSBarry Smith   ierr = ISDestroy(irowp); CHKERRQ(ierr);
178256cd22aeSBarry Smith   ierr = ISDestroy(icolp); CHKERRQ(ierr);
17833a40ed3dSBarry Smith   PetscFunctionReturn(0);
17840513a670SBarry Smith }
17850513a670SBarry Smith 
17865615d1e5SSatish Balay #undef __FUNC__
17875615d1e5SSatish Balay #define __FUNC__ "MatPrintHelp_SeqAIJ"
1788682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1789682d7d0cSBarry Smith {
1790682d7d0cSBarry Smith   static int called = 0;
1791682d7d0cSBarry Smith   MPI_Comm   comm = A->comm;
1792d132466eSBarry Smith   int        ierr;
1793682d7d0cSBarry Smith 
17943a40ed3dSBarry Smith   PetscFunctionBegin;
17953a40ed3dSBarry Smith   if (called) {PetscFunctionReturn(0);} else called = 1;
1796d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1797d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1798d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
1799d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr);
1800d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr);
1801682d7d0cSBarry Smith #if defined(HAVE_ESSL)
1802d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");CHKERRQ(ierr);
1803682d7d0cSBarry Smith #endif
18043a40ed3dSBarry Smith   PetscFunctionReturn(0);
1805682d7d0cSBarry Smith }
18068f6be9afSLois Curfman McInnes extern int MatEqual_SeqAIJ(Mat A,Mat B, PetscTruth* flg);
1807a93ec695SBarry Smith extern int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
1808a93ec695SBarry Smith extern int MatColoringPatch_SeqAIJ(Mat,int,int *,ISColoring *);
1809a93ec695SBarry Smith 
1810cb5b572fSBarry Smith #undef __FUNC__
1811cb5b572fSBarry Smith #define __FUNC__ "MatCopy_SeqAIJ"
1812cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1813cb5b572fSBarry Smith {
1814be6bf707SBarry Smith   int    ierr;
1815cb5b572fSBarry Smith 
1816cb5b572fSBarry Smith   PetscFunctionBegin;
1817be6bf707SBarry Smith   if (str == SAME_NONZERO_PATTERN && B->type == MATSEQAIJ) {
1818be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1819be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ *) B->data;
1820be6bf707SBarry Smith 
1821be6bf707SBarry Smith     if (a->i[a->m]+a->indexshift != b->i[b->m]+a->indexshift) {
1822be6bf707SBarry Smith       SETERRQ(1,1,"Number of nonzeros in two matrices are different");
1823cb5b572fSBarry Smith     }
1824be6bf707SBarry Smith     PetscMemcpy(b->a,a->a,(a->i[a->m]+a->indexshift)*sizeof(Scalar));
1825cb5b572fSBarry Smith   } else {
1826cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1827cb5b572fSBarry Smith   }
1828cb5b572fSBarry Smith   PetscFunctionReturn(0);
1829cb5b572fSBarry Smith }
1830cb5b572fSBarry Smith 
1831682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
18320a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
1833cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
1834cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
1835cb5b572fSBarry Smith        MatMult_SeqAIJ,
1836cb5b572fSBarry Smith        MatMultAdd_SeqAIJ,
1837cb5b572fSBarry Smith        MatMultTrans_SeqAIJ,
1838cb5b572fSBarry Smith        MatMultTransAdd_SeqAIJ,
1839cb5b572fSBarry Smith        MatSolve_SeqAIJ,
1840cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
1841cb5b572fSBarry Smith        MatSolveTrans_SeqAIJ,
1842cb5b572fSBarry Smith        MatSolveTransAdd_SeqAIJ,
1843cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
1844cb5b572fSBarry Smith        0,
184517ab2063SBarry Smith        MatRelax_SeqAIJ,
184617ab2063SBarry Smith        MatTranspose_SeqAIJ,
1847cb5b572fSBarry Smith        MatGetInfo_SeqAIJ,
1848cb5b572fSBarry Smith        MatEqual_SeqAIJ,
1849cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
1850cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
1851cb5b572fSBarry Smith        MatNorm_SeqAIJ,
1852cb5b572fSBarry Smith        0,
1853cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
185417ab2063SBarry Smith        MatCompress_SeqAIJ,
1855cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
1856cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
1857cb5b572fSBarry Smith        MatZeroRows_SeqAIJ,
1858cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
1859cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
1860cb5b572fSBarry Smith        0,
1861cb5b572fSBarry Smith        0,
1862cb5b572fSBarry Smith        MatGetSize_SeqAIJ,
1863cb5b572fSBarry Smith        MatGetSize_SeqAIJ,
1864cb5b572fSBarry Smith        MatGetOwnershipRange_SeqAIJ,
1865cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
1866cb5b572fSBarry Smith        0,
1867cb5b572fSBarry Smith        0,
1868cb5b572fSBarry Smith        0,
1869be6bf707SBarry Smith        MatDuplicate_SeqAIJ,
1870cb5b572fSBarry Smith        0,
1871cb5b572fSBarry Smith        0,
1872cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
1873cb5b572fSBarry Smith        0,
1874cb5b572fSBarry Smith        0,
1875cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
1876cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
1877cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
1878cb5b572fSBarry Smith        MatCopy_SeqAIJ,
1879f0b747eeSBarry Smith        MatPrintHelp_SeqAIJ,
1880cb5b572fSBarry Smith        MatScale_SeqAIJ,
1881cb5b572fSBarry Smith        0,
1882cb5b572fSBarry Smith        0,
18836945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
18846945ee14SBarry Smith        MatGetBlockSize_SeqAIJ,
18853b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
18863b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
18873b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
1888a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
1889a93ec695SBarry Smith        MatFDColoringCreate_SeqAIJ,
18900513a670SBarry Smith        MatColoringPatch_SeqAIJ,
18910513a670SBarry Smith        0,
1892cda55fadSBarry Smith        MatPermute_SeqAIJ,
1893cda55fadSBarry Smith        0,
1894cda55fadSBarry Smith        0,
1895cda55fadSBarry Smith        0,
1896cda55fadSBarry Smith        0,
1897cda55fadSBarry Smith        MatGetMaps_Petsc};
189817ab2063SBarry Smith 
189917ab2063SBarry Smith extern int MatUseSuperLU_SeqAIJ(Mat);
190017ab2063SBarry Smith extern int MatUseEssl_SeqAIJ(Mat);
190117ab2063SBarry Smith extern int MatUseDXML_SeqAIJ(Mat);
190217ab2063SBarry Smith 
1903fb2e594dSBarry Smith EXTERN_C_BEGIN
19045615d1e5SSatish Balay #undef __FUNC__
1905bef8e0ddSBarry Smith #define __FUNC__ "MatSeqAIJSetColumnIndices_SeqAIJ"
190615091d37SBarry Smith 
1907bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices)
1908bef8e0ddSBarry Smith {
1909bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
1910bef8e0ddSBarry Smith   int        i,nz,n;
1911bef8e0ddSBarry Smith 
1912bef8e0ddSBarry Smith   PetscFunctionBegin;
1913bef8e0ddSBarry Smith   if (aij->indexshift) SETERRQ(1,1,"No support with 1 based indexing");
1914bef8e0ddSBarry Smith 
1915bef8e0ddSBarry Smith   nz = aij->maxnz;
1916bef8e0ddSBarry Smith   n  = aij->n;
1917bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
1918bef8e0ddSBarry Smith     aij->j[i] = indices[i];
1919bef8e0ddSBarry Smith   }
1920bef8e0ddSBarry Smith   aij->nz = nz;
1921bef8e0ddSBarry Smith   for ( i=0; i<n; i++ ) {
1922bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
1923bef8e0ddSBarry Smith   }
1924bef8e0ddSBarry Smith 
1925bef8e0ddSBarry Smith   PetscFunctionReturn(0);
1926bef8e0ddSBarry Smith }
1927fb2e594dSBarry Smith EXTERN_C_END
1928bef8e0ddSBarry Smith 
1929bef8e0ddSBarry Smith #undef __FUNC__
1930bef8e0ddSBarry Smith #define __FUNC__ "MatSeqAIJSetColumnIndices"
1931bef8e0ddSBarry Smith /*@
1932bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
1933bef8e0ddSBarry Smith        in the matrix.
1934bef8e0ddSBarry Smith 
1935bef8e0ddSBarry Smith   Input Parameters:
1936bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
1937bef8e0ddSBarry Smith -  indices - the column indices
1938bef8e0ddSBarry Smith 
193915091d37SBarry Smith   Level: advanced
194015091d37SBarry Smith 
1941bef8e0ddSBarry Smith   Notes:
1942bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
1943bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
1944bef8e0ddSBarry Smith   of the MatSetValues() operation.
1945bef8e0ddSBarry Smith 
1946bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
1947bef8e0ddSBarry Smith   MatCreateSeqAIJ().
1948bef8e0ddSBarry Smith 
1949bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
1950bef8e0ddSBarry Smith 
1951bef8e0ddSBarry Smith @*/
1952bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices)
1953bef8e0ddSBarry Smith {
1954bef8e0ddSBarry Smith   int ierr,(*f)(Mat,int *);
1955bef8e0ddSBarry Smith 
1956bef8e0ddSBarry Smith   PetscFunctionBegin;
1957bef8e0ddSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1958bef8e0ddSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void **)&f);
1959bef8e0ddSBarry Smith          CHKERRQ(ierr);
1960bef8e0ddSBarry Smith   if (f) {
1961bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
1962bef8e0ddSBarry Smith   } else {
1963bef8e0ddSBarry Smith     SETERRQ(1,1,"Wrong type of matrix to set column indices");
1964bef8e0ddSBarry Smith   }
1965bef8e0ddSBarry Smith   PetscFunctionReturn(0);
1966bef8e0ddSBarry Smith }
1967bef8e0ddSBarry Smith 
1968be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
1969be6bf707SBarry Smith 
1970fb2e594dSBarry Smith EXTERN_C_BEGIN
1971be6bf707SBarry Smith #undef __FUNC__
1972be6bf707SBarry Smith #define __FUNC__ "MatStoreValues_SeqAIJ"
1973be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat)
1974be6bf707SBarry Smith {
1975be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
1976be6bf707SBarry Smith   int        nz = aij->i[aij->m]+aij->indexshift;
1977be6bf707SBarry Smith 
1978be6bf707SBarry Smith   PetscFunctionBegin;
1979be6bf707SBarry Smith   if (aij->nonew != 1) {
1980be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
1981be6bf707SBarry Smith   }
1982be6bf707SBarry Smith 
1983be6bf707SBarry Smith   /* allocate space for values if not already there */
1984be6bf707SBarry Smith   if (!aij->saved_values) {
1985be6bf707SBarry Smith     aij->saved_values = (Scalar *) PetscMalloc(nz*sizeof(Scalar));CHKPTRQ(aij->saved_values);
1986be6bf707SBarry Smith   }
1987be6bf707SBarry Smith 
1988be6bf707SBarry Smith   /* copy values over */
1989be6bf707SBarry Smith   PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(Scalar));
1990be6bf707SBarry Smith   PetscFunctionReturn(0);
1991be6bf707SBarry Smith }
1992fb2e594dSBarry Smith EXTERN_C_END
1993be6bf707SBarry Smith 
1994be6bf707SBarry Smith #undef __FUNC__
1995be6bf707SBarry Smith #define __FUNC__ "MatStoreValues"
1996be6bf707SBarry Smith /*@
1997be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
1998be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
1999be6bf707SBarry Smith        nonlinear portion.
2000be6bf707SBarry Smith 
2001be6bf707SBarry Smith    Collect on Mat
2002be6bf707SBarry Smith 
2003be6bf707SBarry Smith   Input Parameters:
2004be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2005be6bf707SBarry Smith 
200615091d37SBarry Smith   Level: advanced
200715091d37SBarry Smith 
2008be6bf707SBarry Smith   Common Usage, with SNESSolve():
2009be6bf707SBarry Smith $    Create Jacobian matrix
2010be6bf707SBarry Smith $    Set linear terms into matrix
2011be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2012be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2013be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2014be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2015be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2016be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2017be6bf707SBarry Smith $    In your Jacobian routine
2018be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2019be6bf707SBarry Smith $      Set nonlinear terms in matrix
2020be6bf707SBarry Smith 
2021be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2022be6bf707SBarry Smith $    // build linear portion of Jacobian
2023be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2024be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2025be6bf707SBarry Smith $    loop over nonlinear iterations
2026be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2027be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2028be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2029be6bf707SBarry Smith $       Solve linear system with Jacobian
2030be6bf707SBarry Smith $    endloop
2031be6bf707SBarry Smith 
2032be6bf707SBarry Smith   Notes:
2033be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2034be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2035be6bf707SBarry Smith     calling this routine.
2036be6bf707SBarry Smith 
2037be6bf707SBarry Smith .seealso: MatRetrieveValues()
2038be6bf707SBarry Smith 
2039be6bf707SBarry Smith @*/
2040be6bf707SBarry Smith int MatStoreValues(Mat mat)
2041be6bf707SBarry Smith {
2042be6bf707SBarry Smith   int ierr,(*f)(Mat);
2043be6bf707SBarry Smith 
2044be6bf707SBarry Smith   PetscFunctionBegin;
2045be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2046be6bf707SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2047be6bf707SBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2048be6bf707SBarry Smith 
2049c5d6004cSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void **)&f);CHKERRQ(ierr);
2050be6bf707SBarry Smith   if (f) {
2051be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2052be6bf707SBarry Smith   } else {
2053be6bf707SBarry Smith     SETERRQ(1,1,"Wrong type of matrix to store values");
2054be6bf707SBarry Smith   }
2055be6bf707SBarry Smith   PetscFunctionReturn(0);
2056be6bf707SBarry Smith }
2057be6bf707SBarry Smith 
2058fb2e594dSBarry Smith EXTERN_C_BEGIN
2059be6bf707SBarry Smith #undef __FUNC__
2060be6bf707SBarry Smith #define __FUNC__ "MatRetrieveValues_SeqAIJ"
2061be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat)
2062be6bf707SBarry Smith {
2063be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2064be6bf707SBarry Smith   int        nz = aij->i[aij->m]+aij->indexshift;
2065be6bf707SBarry Smith 
2066be6bf707SBarry Smith   PetscFunctionBegin;
2067be6bf707SBarry Smith   if (aij->nonew != 1) {
2068be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2069be6bf707SBarry Smith   }
2070be6bf707SBarry Smith   if (!aij->saved_values) {
2071be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatStoreValues(A);first");
2072be6bf707SBarry Smith   }
2073be6bf707SBarry Smith 
2074be6bf707SBarry Smith   /* copy values over */
2075be6bf707SBarry Smith   PetscMemcpy(aij->a, aij->saved_values,nz*sizeof(Scalar));
2076be6bf707SBarry Smith   PetscFunctionReturn(0);
2077be6bf707SBarry Smith }
2078fb2e594dSBarry Smith EXTERN_C_END
2079be6bf707SBarry Smith 
2080be6bf707SBarry Smith #undef __FUNC__
2081be6bf707SBarry Smith #define __FUNC__ "MatRetrieveValues"
2082be6bf707SBarry Smith /*@
2083be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2084be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2085be6bf707SBarry Smith        nonlinear portion.
2086be6bf707SBarry Smith 
2087be6bf707SBarry Smith    Collect on Mat
2088be6bf707SBarry Smith 
2089be6bf707SBarry Smith   Input Parameters:
2090be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2091be6bf707SBarry Smith 
209215091d37SBarry Smith   Level: advanced
209315091d37SBarry Smith 
2094be6bf707SBarry Smith .seealso: MatStoreValues()
2095be6bf707SBarry Smith 
2096be6bf707SBarry Smith @*/
2097be6bf707SBarry Smith int MatRetrieveValues(Mat mat)
2098be6bf707SBarry Smith {
2099be6bf707SBarry Smith   int ierr,(*f)(Mat);
2100be6bf707SBarry Smith 
2101be6bf707SBarry Smith   PetscFunctionBegin;
2102be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2103be6bf707SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
2104be6bf707SBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
2105be6bf707SBarry Smith 
2106c5d6004cSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void **)&f);CHKERRQ(ierr);
2107be6bf707SBarry Smith   if (f) {
2108be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2109be6bf707SBarry Smith   } else {
2110be6bf707SBarry Smith     SETERRQ(1,1,"Wrong type of matrix to retrieve values");
2111be6bf707SBarry Smith   }
2112be6bf707SBarry Smith   PetscFunctionReturn(0);
2113be6bf707SBarry Smith }
2114be6bf707SBarry Smith 
2115be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
2116cb5b572fSBarry Smith 
2117bef8e0ddSBarry Smith #undef __FUNC__
21185615d1e5SSatish Balay #define __FUNC__ "MatCreateSeqAIJ"
211917ab2063SBarry Smith /*@C
2120682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
21210d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
21226e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
212351c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
21242bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
212517ab2063SBarry Smith 
2126db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2127db81eaa0SLois Curfman McInnes 
212817ab2063SBarry Smith    Input Parameters:
2129db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
213017ab2063SBarry Smith .  m - number of rows
213117ab2063SBarry Smith .  n - number of columns
213217ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
213351c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
21342bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
213517ab2063SBarry Smith 
213617ab2063SBarry Smith    Output Parameter:
2137416022c9SBarry Smith .  A - the matrix
213817ab2063SBarry Smith 
2139b259b22eSLois Curfman McInnes    Notes:
214017ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
214117ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
21420002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
214344cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
214417ab2063SBarry Smith 
214517ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2146a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
21473d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
21486da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
214917ab2063SBarry Smith 
2150682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
21514fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2152682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
21536c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
21546c7ebb05SLois Curfman McInnes 
21556c7ebb05SLois Curfman McInnes    Options Database Keys:
2156db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2157db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2158db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2159db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2160db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
216117ab2063SBarry Smith 
2162027ccd11SLois Curfman McInnes    Level: intermediate
2163027ccd11SLois Curfman McInnes 
2164bef8e0ddSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices()
216517ab2063SBarry Smith @*/
2166416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz, Mat *A)
216717ab2063SBarry Smith {
2168416022c9SBarry Smith   Mat        B;
2169416022c9SBarry Smith   Mat_SeqAIJ *b;
21706945ee14SBarry Smith   int        i, len, ierr, flg,size;
21716945ee14SBarry Smith 
21723a40ed3dSBarry Smith   PetscFunctionBegin;
2173d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2174a8c6a408SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Comm must be of size 1");
2175d5d45c9bSBarry Smith 
2176416022c9SBarry Smith   *A                  = 0;
21773f1db9ecSBarry Smith   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATSEQAIJ,"Mat",comm,MatDestroy,MatView);
2178416022c9SBarry Smith   PLogObjectCreate(B);
21790452661fSBarry Smith   B->data             = (void *) (b = PetscNew(Mat_SeqAIJ)); CHKPTRQ(b);
218044cd7ae7SLois Curfman McInnes   PetscMemzero(b,sizeof(Mat_SeqAIJ));
21810a6ffc59SBarry Smith   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
2182e1311b90SBarry Smith   B->ops->destroy          = MatDestroy_SeqAIJ;
2183e1311b90SBarry Smith   B->ops->view             = MatView_SeqAIJ;
2184416022c9SBarry Smith   B->factor           = 0;
2185416022c9SBarry Smith   B->lupivotthreshold = 1.0;
218690f02eecSBarry Smith   B->mapping          = 0;
2187e1311b90SBarry Smith   ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold,&flg);CHKERRQ(ierr);
21887a743949SBarry Smith   b->ilu_preserve_row_sums = PETSC_FALSE;
2189e1311b90SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums",(int*)&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2190416022c9SBarry Smith   b->row              = 0;
2191416022c9SBarry Smith   b->col              = 0;
219282bf6240SBarry Smith   b->icol             = 0;
2193416022c9SBarry Smith   b->indexshift       = 0;
2194b810aeb4SBarry Smith   b->reallocs         = 0;
219569957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_oneindex", &flg); CHKERRQ(ierr);
219669957df2SSatish Balay   if (flg) b->indexshift = -1;
219717ab2063SBarry Smith 
219844cd7ae7SLois Curfman McInnes   b->m = m; B->m = m; B->M = m;
219944cd7ae7SLois Curfman McInnes   b->n = n; B->n = n; B->N = n;
2200a5ae1ecdSBarry Smith 
22014d197716SBarry Smith   ierr = MapCreateMPI(comm,m,m,&B->rmap);CHKERRQ(ierr);
22024d197716SBarry Smith   ierr = MapCreateMPI(comm,n,n,&B->cmap);CHKERRQ(ierr);
2203a5ae1ecdSBarry Smith 
22040452661fSBarry Smith   b->imax = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(b->imax);
2205b4fd4287SBarry Smith   if (nnz == PETSC_NULL) {
22067b8455f0SLois Curfman McInnes     if (nz == PETSC_DEFAULT) nz = 10;
22077b8455f0SLois Curfman McInnes     else if (nz <= 0)        nz = 1;
2208416022c9SBarry Smith     for ( i=0; i<m; i++ ) b->imax[i] = nz;
220917ab2063SBarry Smith     nz = nz*m;
22103a40ed3dSBarry Smith   } else {
221117ab2063SBarry Smith     nz = 0;
2212416022c9SBarry Smith     for ( i=0; i<m; i++ ) {b->imax[i] = nnz[i]; nz += nnz[i];}
221317ab2063SBarry Smith   }
221417ab2063SBarry Smith 
221517ab2063SBarry Smith   /* allocate the matrix space */
2216416022c9SBarry Smith   len             = nz*(sizeof(int) + sizeof(Scalar)) + (b->m+1)*sizeof(int);
22170452661fSBarry Smith   b->a            = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
2218416022c9SBarry Smith   b->j            = (int *) (b->a + nz);
2219cddf8d76SBarry Smith   PetscMemzero(b->j,nz*sizeof(int));
2220416022c9SBarry Smith   b->i            = b->j + nz;
2221416022c9SBarry Smith   b->singlemalloc = 1;
222217ab2063SBarry Smith 
2223416022c9SBarry Smith   b->i[0] = -b->indexshift;
222417ab2063SBarry Smith   for (i=1; i<m+1; i++) {
2225416022c9SBarry Smith     b->i[i] = b->i[i-1] + b->imax[i-1];
222617ab2063SBarry Smith   }
222717ab2063SBarry Smith 
2228416022c9SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
22290452661fSBarry Smith   b->ilen = (int *) PetscMalloc((m+1)*sizeof(int));
2230f09e8eb9SSatish Balay   PLogObjectMemory(B,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2231416022c9SBarry Smith   for ( i=0; i<b->m; i++ ) { b->ilen[i] = 0;}
223217ab2063SBarry Smith 
2233416022c9SBarry Smith   b->nz               = 0;
2234416022c9SBarry Smith   b->maxnz            = nz;
2235416022c9SBarry Smith   b->sorted           = 0;
2236416022c9SBarry Smith   b->roworiented      = 1;
2237416022c9SBarry Smith   b->nonew            = 0;
2238416022c9SBarry Smith   b->diag             = 0;
2239416022c9SBarry Smith   b->solve_work       = 0;
224071bd300dSLois Curfman McInnes   b->spptr            = 0;
2241754ec7b1SSatish Balay   b->inode.node_count = 0;
2242754ec7b1SSatish Balay   b->inode.size       = 0;
22436c7ebb05SLois Curfman McInnes   b->inode.limit      = 5;
22446c7ebb05SLois Curfman McInnes   b->inode.max_limit  = 5;
2245be6bf707SBarry Smith   b->saved_values     = 0;
22464e220ebcSLois Curfman McInnes   B->info.nz_unneeded = (double)b->maxnz;
2247d7f994e1SBarry Smith   b->idiag            = 0;
2248d7f994e1SBarry Smith   b->ssor             = 0;
224917ab2063SBarry Smith 
2250416022c9SBarry Smith   *A = B;
22514e220ebcSLois Curfman McInnes 
22524b14c69eSBarry Smith   /*  SuperLU is not currently supported through PETSc */
22534b14c69eSBarry Smith #if defined(HAVE_SUPERLU)
225469957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_superlu", &flg); CHKERRQ(ierr);
225569957df2SSatish Balay   if (flg) { ierr = MatUseSuperLU_SeqAIJ(B); CHKERRQ(ierr); }
22564b14c69eSBarry Smith #endif
225769957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_essl", &flg); CHKERRQ(ierr);
225869957df2SSatish Balay   if (flg) { ierr = MatUseEssl_SeqAIJ(B); CHKERRQ(ierr); }
225969957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_dxml", &flg); CHKERRQ(ierr);
226069957df2SSatish Balay   if (flg) {
2261a8c6a408SBarry Smith     if (!b->indexshift) SETERRQ( PETSC_ERR_LIB,0,"need -mat_aij_oneindex with -mat_aij_dxml");
2262416022c9SBarry Smith     ierr = MatUseDXML_SeqAIJ(B); CHKERRQ(ierr);
226317ab2063SBarry Smith   }
226469957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr);
226569957df2SSatish Balay   if (flg) {ierr = MatPrintHelp(B); CHKERRQ(ierr); }
2266bef8e0ddSBarry Smith 
2267bef8e0ddSBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2268bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2269bef8e0ddSBarry Smith                                      (void*)MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2270be6bf707SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",
2271be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2272be6bf707SBarry Smith                                      (void*)MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2273be6bf707SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",
2274be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2275be6bf707SBarry Smith                                      (void*)MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
22763a40ed3dSBarry Smith   PetscFunctionReturn(0);
227717ab2063SBarry Smith }
227817ab2063SBarry Smith 
22795615d1e5SSatish Balay #undef __FUNC__
2280be6bf707SBarry Smith #define __FUNC__ "MatDuplicate_SeqAIJ"
2281be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
228217ab2063SBarry Smith {
2283416022c9SBarry Smith   Mat        C;
2284416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ *) A->data;
2285bef8e0ddSBarry Smith   int        i,len, m = a->m,shift = a->indexshift,ierr;
228617ab2063SBarry Smith 
22873a40ed3dSBarry Smith   PetscFunctionBegin;
22884043dd9cSLois Curfman McInnes   *B = 0;
22893f1db9ecSBarry Smith   PetscHeaderCreate(C,_p_Mat,struct _MatOps,MAT_COOKIE,MATSEQAIJ,"Mat",A->comm,MatDestroy,MatView);
2290416022c9SBarry Smith   PLogObjectCreate(C);
22910452661fSBarry Smith   C->data       = (void *) (c = PetscNew(Mat_SeqAIJ)); CHKPTRQ(c);
2292f830108cSBarry Smith   PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));
2293e1311b90SBarry Smith   C->ops->destroy = MatDestroy_SeqAIJ;
2294e1311b90SBarry Smith   C->ops->view    = MatView_SeqAIJ;
2295416022c9SBarry Smith   C->factor       = A->factor;
2296416022c9SBarry Smith   c->row          = 0;
2297416022c9SBarry Smith   c->col          = 0;
229882bf6240SBarry Smith   c->icol         = 0;
2299416022c9SBarry Smith   c->indexshift   = shift;
2300c456f294SBarry Smith   C->assembled    = PETSC_TRUE;
230117ab2063SBarry Smith 
230244cd7ae7SLois Curfman McInnes   c->m = C->m   = a->m;
230344cd7ae7SLois Curfman McInnes   c->n = C->n   = a->n;
230444cd7ae7SLois Curfman McInnes   C->M          = a->m;
230544cd7ae7SLois Curfman McInnes   C->N          = a->n;
230617ab2063SBarry Smith 
23070452661fSBarry Smith   c->imax       = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(c->imax);
23080452661fSBarry Smith   c->ilen       = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(c->ilen);
230917ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
2310416022c9SBarry Smith     c->imax[i] = a->imax[i];
2311416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
231217ab2063SBarry Smith   }
231317ab2063SBarry Smith 
231417ab2063SBarry Smith   /* allocate the matrix space */
2315416022c9SBarry Smith   c->singlemalloc = 1;
2316416022c9SBarry Smith   len     = (m+1)*sizeof(int)+(a->i[m])*(sizeof(Scalar)+sizeof(int));
23170452661fSBarry Smith   c->a  = (Scalar *) PetscMalloc( len ); CHKPTRQ(c->a);
2318416022c9SBarry Smith   c->j  = (int *) (c->a + a->i[m] + shift);
2319416022c9SBarry Smith   c->i  = c->j + a->i[m] + shift;
2320416022c9SBarry Smith   PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));
232117ab2063SBarry Smith   if (m > 0) {
2322416022c9SBarry Smith     PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int));
2323be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2324416022c9SBarry Smith       PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(Scalar));
2325be6bf707SBarry Smith     } else {
2326be6bf707SBarry Smith       PetscMemzero(c->a,(a->i[m]+shift)*sizeof(Scalar));
232717ab2063SBarry Smith     }
232808480c60SBarry Smith   }
232917ab2063SBarry Smith 
2330f09e8eb9SSatish Balay   PLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2331416022c9SBarry Smith   c->sorted      = a->sorted;
2332416022c9SBarry Smith   c->roworiented = a->roworiented;
2333416022c9SBarry Smith   c->nonew       = a->nonew;
23347a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
2335be6bf707SBarry Smith   c->saved_values = 0;
2336d7f994e1SBarry Smith   c->idiag        = 0;
2337d7f994e1SBarry Smith   c->ssor         = 0;
233817ab2063SBarry Smith 
2339416022c9SBarry Smith   if (a->diag) {
23400452661fSBarry Smith     c->diag = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(c->diag);
2341416022c9SBarry Smith     PLogObjectMemory(C,(m+1)*sizeof(int));
234217ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
2343416022c9SBarry Smith       c->diag[i] = a->diag[i];
234417ab2063SBarry Smith     }
23453a40ed3dSBarry Smith   } else c->diag          = 0;
23466c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
23476c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
2348754ec7b1SSatish Balay   if (a->inode.size){
2349daed632aSSatish Balay     c->inode.size       = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(c->inode.size);
2350754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
2351daed632aSSatish Balay     PetscMemcpy( c->inode.size, a->inode.size, (m+1)*sizeof(int));
2352754ec7b1SSatish Balay   } else {
2353754ec7b1SSatish Balay     c->inode.size       = 0;
2354754ec7b1SSatish Balay     c->inode.node_count = 0;
2355754ec7b1SSatish Balay   }
2356416022c9SBarry Smith   c->nz                 = a->nz;
2357416022c9SBarry Smith   c->maxnz              = a->maxnz;
2358416022c9SBarry Smith   c->solve_work         = 0;
235976dd722bSSatish Balay   c->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
2360754ec7b1SSatish Balay 
2361416022c9SBarry Smith   *B = C;
23627b2a1423SBarry Smith   ierr = FListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
23633a40ed3dSBarry Smith   PetscFunctionReturn(0);
236417ab2063SBarry Smith }
236517ab2063SBarry Smith 
23665615d1e5SSatish Balay #undef __FUNC__
23675615d1e5SSatish Balay #define __FUNC__ "MatLoad_SeqAIJ"
236819bcc07fSBarry Smith int MatLoad_SeqAIJ(Viewer viewer,MatType type,Mat *A)
236917ab2063SBarry Smith {
2370416022c9SBarry Smith   Mat_SeqAIJ   *a;
2371416022c9SBarry Smith   Mat          B;
237217699dbbSLois Curfman McInnes   int          i, nz, ierr, fd, header[4],size,*rowlengths = 0,M,N,shift;
2373bcd2baecSBarry Smith   MPI_Comm     comm;
237417ab2063SBarry Smith 
23753a40ed3dSBarry Smith   PetscFunctionBegin;
2376e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject) viewer,&comm);CHKERRQ(ierr);
2377d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2378a8c6a408SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,0,"view must have one processor");
237990ace30eSBarry Smith   ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr);
23800752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT); CHKERRQ(ierr);
2381a8c6a408SBarry Smith   if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"not matrix object in file");
238217ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
238317ab2063SBarry Smith 
2384d64ed03dSBarry Smith   if (nz < 0) {
2385a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,1,"Matrix stored in special format on disk, cannot load as SeqAIJ");
2386d64ed03dSBarry Smith   }
2387d64ed03dSBarry Smith 
238817ab2063SBarry Smith   /* read in row lengths */
23890452661fSBarry Smith   rowlengths = (int*) PetscMalloc( M*sizeof(int) ); CHKPTRQ(rowlengths);
23900752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT); CHKERRQ(ierr);
239117ab2063SBarry Smith 
239217ab2063SBarry Smith   /* create our matrix */
2393416022c9SBarry Smith   ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A); CHKERRQ(ierr);
2394416022c9SBarry Smith   B = *A;
2395416022c9SBarry Smith   a = (Mat_SeqAIJ *) B->data;
2396416022c9SBarry Smith   shift = a->indexshift;
239717ab2063SBarry Smith 
239817ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
23990752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT); CHKERRQ(ierr);
240017ab2063SBarry Smith   if (shift) {
240117ab2063SBarry Smith     for ( i=0; i<nz; i++ ) {
2402416022c9SBarry Smith       a->j[i] += 1;
240317ab2063SBarry Smith     }
240417ab2063SBarry Smith   }
240517ab2063SBarry Smith 
240617ab2063SBarry Smith   /* read in nonzero values */
24070752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR); CHKERRQ(ierr);
240817ab2063SBarry Smith 
240917ab2063SBarry Smith   /* set matrix "i" values */
2410416022c9SBarry Smith   a->i[0] = -shift;
241117ab2063SBarry Smith   for ( i=1; i<= M; i++ ) {
2412416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2413416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
241417ab2063SBarry Smith   }
24150452661fSBarry Smith   PetscFree(rowlengths);
241617ab2063SBarry Smith 
24176d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
24186d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
24193a40ed3dSBarry Smith   PetscFunctionReturn(0);
242017ab2063SBarry Smith }
242117ab2063SBarry Smith 
24225615d1e5SSatish Balay #undef __FUNC__
24235615d1e5SSatish Balay #define __FUNC__ "MatEqual_SeqAIJ"
24248f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B, PetscTruth* flg)
24257264ac53SSatish Balay {
24267264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data, *b = (Mat_SeqAIJ *)B->data;
24277264ac53SSatish Balay 
24283a40ed3dSBarry Smith   PetscFunctionBegin;
2429a8c6a408SBarry Smith   if (B->type !=MATSEQAIJ)SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type");
24307264ac53SSatish Balay 
24317264ac53SSatish Balay   /* If the  matrix dimensions are not equal, or no of nonzeros or shift */
24327264ac53SSatish Balay   if ((a->m != b->m ) || (a->n !=b->n) ||( a->nz != b->nz)||
2433bcd2baecSBarry Smith       (a->indexshift != b->indexshift)) {
24343a40ed3dSBarry Smith     *flg = PETSC_FALSE; PetscFunctionReturn(0);
2435bcd2baecSBarry Smith   }
24367264ac53SSatish Balay 
24377264ac53SSatish Balay   /* if the a->i are the same */
24388108c231SLois Curfman McInnes   if (PetscMemcmp(a->i,b->i,(a->m+1)*sizeof(int))) {
24393a40ed3dSBarry Smith     *flg = PETSC_FALSE; PetscFunctionReturn(0);
24407264ac53SSatish Balay   }
24417264ac53SSatish Balay 
24427264ac53SSatish Balay   /* if a->j are the same */
2443bcd2baecSBarry Smith   if (PetscMemcmp(a->j, b->j, (a->nz)*sizeof(int))) {
24443a40ed3dSBarry Smith     *flg = PETSC_FALSE; PetscFunctionReturn(0);
2445bcd2baecSBarry Smith   }
2446bcd2baecSBarry Smith 
2447bcd2baecSBarry Smith   /* if a->a are the same */
244819bcc07fSBarry Smith   if (PetscMemcmp(a->a, b->a, (a->nz)*sizeof(Scalar))) {
24493a40ed3dSBarry Smith     *flg = PETSC_FALSE; PetscFunctionReturn(0);
24507264ac53SSatish Balay   }
245177c4ece6SBarry Smith   *flg = PETSC_TRUE;
24523a40ed3dSBarry Smith   PetscFunctionReturn(0);
24537264ac53SSatish Balay 
24547264ac53SSatish Balay }
2455