xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 4fca80b981dbd550524e879074cd7c2a612b9743)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*4fca80b9SLois Curfman McInnes static char vcid[] = "$Id: aij.c,v 1.233 1997/08/22 15:13:29 bsmith Exp curfman $";
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 "pinclude/pviewer.h"
113369ce9aSBarry Smith #include "sys.h"
1270f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
13f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
14f5eb4b81SSatish Balay #include "src/inline/spops.h"
15f5eb4b81SSatish Balay #include "src/inline/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   int        ierr = 1;
26a2ce50c7SBarry Smith 
27e3372554SBarry Smith   SETERRQ(ierr,0,"Not implemented");
28a2ce50c7SBarry Smith }
29a2ce50c7SBarry Smith 
30bcd2baecSBarry Smith extern int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**);
3117ab2063SBarry Smith 
325615d1e5SSatish Balay #undef __FUNC__
335615d1e5SSatish Balay #define __FUNC__ "MatGetRowIJ_SeqAIJ"
348f6be9afSLois Curfman McInnes int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int **ia,int **ja,
356945ee14SBarry Smith                            PetscTruth *done)
3617ab2063SBarry Smith {
37416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
386945ee14SBarry Smith   int        ierr,i,ishift;
3917ab2063SBarry Smith 
4031625ec5SSatish Balay   *m     = A->m;
416945ee14SBarry Smith   if (!ia) return 0;
426945ee14SBarry Smith   ishift = a->indexshift;
436945ee14SBarry Smith   if (symmetric) {
4431625ec5SSatish Balay     ierr = MatToSymmetricIJ_SeqAIJ(a->m,a->i,a->j,ishift,oshift,ia,ja); CHKERRQ(ierr);
456945ee14SBarry Smith   } else if (oshift == 0 && ishift == -1) {
4631625ec5SSatish Balay     int nz = a->i[a->m];
473b2fbd54SBarry Smith     /* malloc space and  subtract 1 from i and j indices */
4831625ec5SSatish Balay     *ia = (int *) PetscMalloc( (a->m+1)*sizeof(int) ); CHKPTRQ(*ia);
493b2fbd54SBarry Smith     *ja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(*ja);
503b2fbd54SBarry Smith     for ( i=0; i<nz; i++ ) (*ja)[i] = a->j[i] - 1;
5131625ec5SSatish Balay     for ( i=0; i<a->m+1; i++ ) (*ia)[i] = a->i[i] - 1;
526945ee14SBarry Smith   } else if (oshift == 1 && ishift == 0) {
5331625ec5SSatish Balay     int nz = a->i[a->m] + 1;
543b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
5531625ec5SSatish Balay     *ia = (int *) PetscMalloc( (a->m+1)*sizeof(int) ); CHKPTRQ(*ia);
563b2fbd54SBarry Smith     *ja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(*ja);
573b2fbd54SBarry Smith     for ( i=0; i<nz; i++ ) (*ja)[i] = a->j[i] + 1;
5831625ec5SSatish Balay     for ( i=0; i<a->m+1; i++ ) (*ia)[i] = a->i[i] + 1;
596945ee14SBarry Smith   } else {
606945ee14SBarry Smith     *ia = a->i; *ja = a->j;
61a2ce50c7SBarry Smith   }
62a2ce50c7SBarry Smith 
63a2744918SBarry Smith   return 0;
64a2744918SBarry Smith }
65a2744918SBarry Smith 
665615d1e5SSatish Balay #undef __FUNC__
675615d1e5SSatish Balay #define __FUNC__ "MatRestoreRowIJ_SeqAIJ"
688f6be9afSLois Curfman McInnes int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,
696945ee14SBarry Smith                                PetscTruth *done)
706945ee14SBarry Smith {
716945ee14SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
723b2fbd54SBarry Smith   int        ishift = a->indexshift;
736945ee14SBarry Smith 
746945ee14SBarry Smith   if (!ia) return 0;
753b2fbd54SBarry Smith   if (symmetric || (oshift == 0 && ishift == -1) || (oshift == 1 && ishift == 0)) {
766945ee14SBarry Smith     PetscFree(*ia);
776945ee14SBarry Smith     PetscFree(*ja);
78bcd2baecSBarry Smith   }
7917ab2063SBarry Smith   return 0;
8017ab2063SBarry Smith }
8117ab2063SBarry Smith 
825615d1e5SSatish Balay #undef __FUNC__
835615d1e5SSatish Balay #define __FUNC__ "MatGetColumnIJ_SeqAIJ"
8443a90d84SBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int **ia,int **ja,
853b2fbd54SBarry Smith                            PetscTruth *done)
863b2fbd54SBarry Smith {
873b2fbd54SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
88a93ec695SBarry Smith   int        ierr,i,ishift = a->indexshift,*collengths,*cia,*cja,n = A->n,m = A->m;
89a93ec695SBarry Smith   int        nz = a->i[m]+ishift,row,*jj,mr,col;
903b2fbd54SBarry Smith 
913b2fbd54SBarry Smith   *nn     = A->n;
923b2fbd54SBarry Smith   if (!ia) return 0;
933b2fbd54SBarry Smith   if (symmetric) {
94179192dfSSatish Balay     ierr = MatToSymmetricIJ_SeqAIJ(a->m,a->i,a->j,ishift,oshift,ia,ja); CHKERRQ(ierr);
953b2fbd54SBarry Smith   } else {
9661d2ded1SBarry Smith     collengths = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(collengths);
973b2fbd54SBarry Smith     PetscMemzero(collengths,n*sizeof(int));
983b2fbd54SBarry Smith     cia        = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(cia);
99a93ec695SBarry Smith     cja        = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(cja);
1003b2fbd54SBarry Smith     jj = a->j;
1013b2fbd54SBarry Smith     for ( i=0; i<nz; i++ ) {
1023b2fbd54SBarry Smith       collengths[jj[i] + ishift]++;
1033b2fbd54SBarry Smith     }
1043b2fbd54SBarry Smith     cia[0] = oshift;
1053b2fbd54SBarry Smith     for ( i=0; i<n; i++) {
1063b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1073b2fbd54SBarry Smith     }
1083b2fbd54SBarry Smith     PetscMemzero(collengths,n*sizeof(int));
1093b2fbd54SBarry Smith     jj = a->j;
110a93ec695SBarry Smith     for ( row=0; row<m; row++ ) {
111a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
112a93ec695SBarry Smith       for ( i=0; i<mr; i++ ) {
1133b2fbd54SBarry Smith         col = *jj++ + ishift;
1143b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1153b2fbd54SBarry Smith       }
1163b2fbd54SBarry Smith     }
1173b2fbd54SBarry Smith     PetscFree(collengths);
1183b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1193b2fbd54SBarry Smith   }
1203b2fbd54SBarry Smith 
1213b2fbd54SBarry Smith   return 0;
1223b2fbd54SBarry Smith }
1233b2fbd54SBarry Smith 
1245615d1e5SSatish Balay #undef __FUNC__
1255615d1e5SSatish Balay #define __FUNC__ "MatRestoreColumnIJ_SeqAIJ"
12643a90d84SBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,
1273b2fbd54SBarry Smith                                      int **ja,PetscTruth *done)
1283b2fbd54SBarry Smith {
1293b2fbd54SBarry Smith   if (!ia) return 0;
1303b2fbd54SBarry Smith 
1313b2fbd54SBarry Smith   PetscFree(*ia);
1323b2fbd54SBarry Smith   PetscFree(*ja);
1333b2fbd54SBarry Smith 
1343b2fbd54SBarry Smith   return 0;
1353b2fbd54SBarry Smith }
1363b2fbd54SBarry Smith 
137227d817aSBarry Smith #define CHUNKSIZE   15
13817ab2063SBarry Smith 
1395615d1e5SSatish Balay #undef __FUNC__
1405615d1e5SSatish Balay #define __FUNC__ "MatSetValues_SeqAIJ"
14161d2ded1SBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v,InsertMode is)
14217ab2063SBarry Smith {
143416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
144416022c9SBarry Smith   int        *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax, N, sorted = a->sorted;
1454b0e389bSBarry Smith   int        *imax = a->imax, *ai = a->i, *ailen = a->ilen,roworiented = a->roworiented;
146d5d45c9bSBarry Smith   int        *aj = a->j, nonew = a->nonew,shift = a->indexshift;
147416022c9SBarry Smith   Scalar     *ap,value, *aa = a->a;
14817ab2063SBarry Smith 
14917ab2063SBarry Smith   for ( k=0; k<m; k++ ) { /* loop over added rows */
150416022c9SBarry Smith     row  = im[k];
1513b2fbd54SBarry Smith #if defined(PETSC_BOPT_g)
152e3372554SBarry Smith     if (row < 0) SETERRQ(1,0,"Negative row");
153e3372554SBarry Smith     if (row >= a->m) SETERRQ(1,0,"Row too large");
1543b2fbd54SBarry Smith #endif
15517ab2063SBarry Smith     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
15617ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
157416022c9SBarry Smith     low = 0;
15817ab2063SBarry Smith     for ( l=0; l<n; l++ ) { /* loop over added columns */
1593b2fbd54SBarry Smith #if defined(PETSC_BOPT_g)
160e3372554SBarry Smith       if (in[l] < 0) SETERRQ(1,0,"Negative column");
161e3372554SBarry Smith       if (in[l] >= a->n) SETERRQ(1,0,"Column too large");
1623b2fbd54SBarry Smith #endif
1634b0e389bSBarry Smith       col = in[l] - shift;
1644b0e389bSBarry Smith       if (roworiented) {
1654b0e389bSBarry Smith         value = *v++;
1664b0e389bSBarry Smith       }
1674b0e389bSBarry Smith       else {
1684b0e389bSBarry Smith         value = v[k + l*m];
1694b0e389bSBarry Smith       }
170416022c9SBarry Smith       if (!sorted) low = 0; high = nrow;
171416022c9SBarry Smith       while (high-low > 5) {
172416022c9SBarry Smith         t = (low+high)/2;
173416022c9SBarry Smith         if (rp[t] > col) high = t;
174416022c9SBarry Smith         else             low  = t;
17517ab2063SBarry Smith       }
176416022c9SBarry Smith       for ( i=low; i<high; i++ ) {
17717ab2063SBarry Smith         if (rp[i] > col) break;
17817ab2063SBarry Smith         if (rp[i] == col) {
179416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
18017ab2063SBarry Smith           else                  ap[i] = value;
18117ab2063SBarry Smith           goto noinsert;
18217ab2063SBarry Smith         }
18317ab2063SBarry Smith       }
184c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
18511ebbc71SLois Curfman McInnes       else if (nonew == -1) SETERRQ(1,0,"Inserting a new nonzero in the matrix");
18617ab2063SBarry Smith       if (nrow >= rmax) {
18717ab2063SBarry Smith         /* there is no extra room in row, therefore enlarge */
188416022c9SBarry Smith         int    new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j;
18917ab2063SBarry Smith         Scalar *new_a;
19017ab2063SBarry Smith 
19111ebbc71SLois Curfman McInnes         if (nonew == -2) SETERRQ(1,0,"Inserting a new nonzero in the matrix");
19296854ed6SLois Curfman McInnes 
19317ab2063SBarry Smith         /* malloc new storage space */
194416022c9SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int);
1950452661fSBarry Smith         new_a   = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a);
19617ab2063SBarry Smith         new_j   = (int *) (new_a + new_nz);
19717ab2063SBarry Smith         new_i   = new_j + new_nz;
19817ab2063SBarry Smith 
19917ab2063SBarry Smith         /* copy over old data into new slots */
20017ab2063SBarry Smith         for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = ai[ii];}
201416022c9SBarry Smith         for ( ii=row+1; ii<a->m+1; ii++ ) {new_i[ii] = ai[ii]+CHUNKSIZE;}
202416022c9SBarry Smith         PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));
203416022c9SBarry Smith         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift);
204416022c9SBarry Smith         PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow,
20517ab2063SBarry Smith                                                            len*sizeof(int));
206416022c9SBarry Smith         PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar));
207416022c9SBarry Smith         PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow,
20817ab2063SBarry Smith                                                            len*sizeof(Scalar));
20917ab2063SBarry Smith         /* free up old matrix storage */
2100452661fSBarry Smith         PetscFree(a->a);
2110452661fSBarry Smith         if (!a->singlemalloc) {PetscFree(a->i);PetscFree(a->j);}
212416022c9SBarry Smith         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;
213416022c9SBarry Smith         a->singlemalloc = 1;
21417ab2063SBarry Smith 
21517ab2063SBarry Smith         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
216416022c9SBarry Smith         rmax = imax[row] = imax[row] + CHUNKSIZE;
217416022c9SBarry Smith         PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar)));
218416022c9SBarry Smith         a->maxnz += CHUNKSIZE;
219b810aeb4SBarry Smith         a->reallocs++;
22017ab2063SBarry Smith       }
221416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
222416022c9SBarry Smith       /* shift up all the later entries in this row */
223416022c9SBarry Smith       for ( ii=N; ii>=i; ii-- ) {
22417ab2063SBarry Smith         rp[ii+1] = rp[ii];
22517ab2063SBarry Smith         ap[ii+1] = ap[ii];
22617ab2063SBarry Smith       }
22717ab2063SBarry Smith       rp[i] = col;
22817ab2063SBarry Smith       ap[i] = value;
22917ab2063SBarry Smith       noinsert:;
230416022c9SBarry Smith       low = i + 1;
23117ab2063SBarry Smith     }
23217ab2063SBarry Smith     ailen[row] = nrow;
23317ab2063SBarry Smith   }
23417ab2063SBarry Smith   return 0;
23517ab2063SBarry Smith }
23617ab2063SBarry Smith 
2375615d1e5SSatish Balay #undef __FUNC__
2385615d1e5SSatish Balay #define __FUNC__ "MatGetValues_SeqAIJ"
2398f6be9afSLois Curfman McInnes int MatGetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v)
2407eb43aa7SLois Curfman McInnes {
2417eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
242b49de8d1SLois Curfman McInnes   int        *rp, k, low, high, t, row, nrow, i, col, l, *aj = a->j;
2437eb43aa7SLois Curfman McInnes   int        *ai = a->i, *ailen = a->ilen, shift = a->indexshift;
2447eb43aa7SLois Curfman McInnes   Scalar     *ap, *aa = a->a, zero = 0.0;
2457eb43aa7SLois Curfman McInnes 
2467eb43aa7SLois Curfman McInnes   for ( k=0; k<m; k++ ) { /* loop over rows */
2477eb43aa7SLois Curfman McInnes     row  = im[k];
248e3372554SBarry Smith     if (row < 0) SETERRQ(1,0,"Negative row");
249e3372554SBarry Smith     if (row >= a->m) SETERRQ(1,0,"Row too large");
2507eb43aa7SLois Curfman McInnes     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
2517eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2527eb43aa7SLois Curfman McInnes     for ( l=0; l<n; l++ ) { /* loop over columns */
253e3372554SBarry Smith       if (in[l] < 0) SETERRQ(1,0,"Negative column");
254e3372554SBarry Smith       if (in[l] >= a->n) SETERRQ(1,0,"Column too large");
2557eb43aa7SLois Curfman McInnes       col = in[l] - shift;
2567eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2577eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2587eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2597eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2607eb43aa7SLois Curfman McInnes         else             low  = t;
2617eb43aa7SLois Curfman McInnes       }
2627eb43aa7SLois Curfman McInnes       for ( i=low; i<high; i++ ) {
2637eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2647eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
265b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2667eb43aa7SLois Curfman McInnes           goto finished;
2677eb43aa7SLois Curfman McInnes         }
2687eb43aa7SLois Curfman McInnes       }
269b49de8d1SLois Curfman McInnes       *v++ = zero;
2707eb43aa7SLois Curfman McInnes       finished:;
2717eb43aa7SLois Curfman McInnes     }
2727eb43aa7SLois Curfman McInnes   }
2737eb43aa7SLois Curfman McInnes   return 0;
2747eb43aa7SLois Curfman McInnes }
2757eb43aa7SLois Curfman McInnes 
27617ab2063SBarry Smith 
2775615d1e5SSatish Balay #undef __FUNC__
2785615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_Binary"
2798f6be9afSLois Curfman McInnes extern int MatView_SeqAIJ_Binary(Mat A,Viewer viewer)
28017ab2063SBarry Smith {
281416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
282416022c9SBarry Smith   int        i, fd, *col_lens, ierr;
28317ab2063SBarry Smith 
28490ace30eSBarry Smith   ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr);
2850452661fSBarry Smith   col_lens = (int *) PetscMalloc( (4+a->m)*sizeof(int) ); CHKPTRQ(col_lens);
286416022c9SBarry Smith   col_lens[0] = MAT_COOKIE;
287416022c9SBarry Smith   col_lens[1] = a->m;
288416022c9SBarry Smith   col_lens[2] = a->n;
289416022c9SBarry Smith   col_lens[3] = a->nz;
290416022c9SBarry Smith 
291416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
292416022c9SBarry Smith   for ( i=0; i<a->m; i++ ) {
293416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
29417ab2063SBarry Smith   }
29577c4ece6SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+a->m,BINARY_INT,1); CHKERRQ(ierr);
2960452661fSBarry Smith   PetscFree(col_lens);
297416022c9SBarry Smith 
298416022c9SBarry Smith   /* store column indices (zero start index) */
299416022c9SBarry Smith   if (a->indexshift) {
300416022c9SBarry Smith     for ( i=0; i<a->nz; i++ ) a->j[i]--;
30117ab2063SBarry Smith   }
30277c4ece6SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,BINARY_INT,0); CHKERRQ(ierr);
303416022c9SBarry Smith   if (a->indexshift) {
304416022c9SBarry Smith     for ( i=0; i<a->nz; i++ ) a->j[i]++;
30517ab2063SBarry Smith   }
306416022c9SBarry Smith 
307416022c9SBarry Smith   /* store nonzero values */
30877c4ece6SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,BINARY_SCALAR,0); CHKERRQ(ierr);
30917ab2063SBarry Smith   return 0;
31017ab2063SBarry Smith }
311416022c9SBarry Smith 
3125615d1e5SSatish Balay #undef __FUNC__
3135615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_ASCII"
3148f6be9afSLois Curfman McInnes extern int MatView_SeqAIJ_ASCII(Mat A,Viewer viewer)
315416022c9SBarry Smith {
316416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ *) A->data;
317496e697eSBarry Smith   int         ierr, i,j, m = a->m, shift = a->indexshift, format, flg1,flg2;
31817ab2063SBarry Smith   FILE        *fd;
31917ab2063SBarry Smith   char        *outputname;
32017ab2063SBarry Smith 
32190ace30eSBarry Smith   ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr);
322416022c9SBarry Smith   ierr = ViewerFileGetOutputname_Private(viewer,&outputname); CHKERRQ(ierr);
32390ace30eSBarry Smith   ierr = ViewerGetFormat(viewer,&format);
324a93ec695SBarry Smith   if (format == VIEWER_FORMAT_ASCII_INFO) {
32595e01e2fSLois Curfman McInnes     return 0;
32695e01e2fSLois Curfman McInnes   }
327a93ec695SBarry Smith   else if (format == VIEWER_FORMAT_ASCII_INFO_LONG) {
328496e697eSBarry Smith     ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg1); CHKERRQ(ierr);
329496e697eSBarry Smith     ierr = OptionsHasName(PETSC_NULL,"-mat_no_unroll",&flg2); CHKERRQ(ierr);
330496e697eSBarry Smith     if (flg1 || flg2) fprintf(fd,"  not using I-node routines\n");
33195e01e2fSLois Curfman McInnes     else     fprintf(fd,"  using I-node routines: found %d nodes, limit used is %d\n",
33295e01e2fSLois Curfman McInnes         a->inode.node_count,a->inode.limit);
33317ab2063SBarry Smith   }
334a93ec695SBarry Smith   else if (format == VIEWER_FORMAT_ASCII_MATLAB) {
335d00d2cf4SBarry Smith     int nofinalvalue = 0;
336d00d2cf4SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != a->n-!shift)) {
337d00d2cf4SBarry Smith       nofinalvalue = 1;
338d00d2cf4SBarry Smith     }
339416022c9SBarry Smith     fprintf(fd,"%% Size = %d %d \n",m,a->n);
3404e220ebcSLois Curfman McInnes     fprintf(fd,"%% Nonzeros = %d \n",a->nz);
341d00d2cf4SBarry Smith     fprintf(fd,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);
34217ab2063SBarry Smith     fprintf(fd,"zzz = [\n");
34317ab2063SBarry Smith 
34417ab2063SBarry Smith     for (i=0; i<m; i++) {
345416022c9SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
34617ab2063SBarry Smith #if defined(PETSC_COMPLEX)
3476945ee14SBarry Smith         fprintf(fd,"%d %d  %18.16e + %18.16e i \n",i+1,a->j[j]+!shift,real(a->a[j]),
348416022c9SBarry Smith                    imag(a->a[j]));
34917ab2063SBarry Smith #else
3507a743949SBarry Smith         fprintf(fd,"%d %d  %18.16e\n", i+1, a->j[j]+!shift, a->a[j]);
35117ab2063SBarry Smith #endif
35217ab2063SBarry Smith       }
35317ab2063SBarry Smith     }
354d00d2cf4SBarry Smith     if (nofinalvalue) {
355d00d2cf4SBarry Smith       fprintf(fd,"%d %d  %18.16e\n", m, a->n, 0.0);
356d00d2cf4SBarry Smith     }
35717ab2063SBarry Smith     fprintf(fd,"];\n %s = spconvert(zzz);\n",outputname);
35817ab2063SBarry Smith   }
359a93ec695SBarry Smith   else if (format == VIEWER_FORMAT_ASCII_COMMON) {
36044cd7ae7SLois Curfman McInnes     for ( i=0; i<m; i++ ) {
36144cd7ae7SLois Curfman McInnes       fprintf(fd,"row %d:",i);
36244cd7ae7SLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
36344cd7ae7SLois Curfman McInnes #if defined(PETSC_COMPLEX)
364766eeae4SLois Curfman McInnes         if (imag(a->a[j]) > 0.0 && real(a->a[j]) != 0.0)
36544cd7ae7SLois Curfman McInnes           fprintf(fd," %d %g + %g i",a->j[j]+shift,real(a->a[j]),imag(a->a[j]));
366766eeae4SLois Curfman McInnes         else if (imag(a->a[j]) < 0.0 && real(a->a[j]) != 0.0)
367766eeae4SLois Curfman McInnes           fprintf(fd," %d %g - %g i",a->j[j]+shift,real(a->a[j]),-imag(a->a[j]));
36844cd7ae7SLois Curfman McInnes         else if (real(a->a[j]) != 0.0)
36944cd7ae7SLois Curfman McInnes           fprintf(fd," %d %g ",a->j[j]+shift,real(a->a[j]));
37044cd7ae7SLois Curfman McInnes #else
37144cd7ae7SLois Curfman McInnes         if (a->a[j] != 0.0) fprintf(fd," %d %g ",a->j[j]+shift,a->a[j]);
37244cd7ae7SLois Curfman McInnes #endif
37344cd7ae7SLois Curfman McInnes       }
37444cd7ae7SLois Curfman McInnes       fprintf(fd,"\n");
37544cd7ae7SLois Curfman McInnes     }
37644cd7ae7SLois Curfman McInnes   }
377496be53dSLois Curfman McInnes   else if (format == VIEWER_FORMAT_ASCII_SYMMODU) {
378496be53dSLois Curfman McInnes     int nzd=0, fshift=1, *sptr;
3792e44a96cSLois Curfman McInnes     sptr = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(sptr);
380496be53dSLois Curfman McInnes     for ( i=0; i<m; i++ ) {
381496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
382496be53dSLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
383496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
384496be53dSLois Curfman McInnes #if defined(PETSC_COMPLEX)
385496be53dSLois Curfman McInnes           if (imag(a->a[j]) != 0.0 || real(a->a[j]) != 0.0) nzd++;
386496be53dSLois Curfman McInnes #else
387496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
388496be53dSLois Curfman McInnes #endif
389496be53dSLois Curfman McInnes         }
390496be53dSLois Curfman McInnes       }
391496be53dSLois Curfman McInnes     }
3922e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
393496be53dSLois Curfman McInnes     fprintf(fd," %d %d\n\n",m,nzd);
3942e44a96cSLois Curfman McInnes     for ( i=0; i<m+1; i+=6 ) {
3952e44a96cSLois 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]);
3962e44a96cSLois 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]);
3972e44a96cSLois 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]);
3982e44a96cSLois Curfman McInnes       else if (i+1<m) fprintf(fd," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);
3992e44a96cSLois Curfman McInnes       else if (i<m)   fprintf(fd," %d %d\n",sptr[i],sptr[i+1]);
4007272d637SLois Curfman McInnes       else            fprintf(fd," %d\n",sptr[i]);
401496be53dSLois Curfman McInnes     }
402496be53dSLois Curfman McInnes     fprintf(fd,"\n");
403496be53dSLois Curfman McInnes     PetscFree(sptr);
404496be53dSLois Curfman McInnes     for ( i=0; i<m; i++ ) {
405496be53dSLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
406496be53dSLois Curfman McInnes         if (a->j[j] >= i) fprintf(fd," %d ",a->j[j]+fshift);
407496be53dSLois Curfman McInnes       }
408496be53dSLois Curfman McInnes       fprintf(fd,"\n");
409496be53dSLois Curfman McInnes     }
410496be53dSLois Curfman McInnes     fprintf(fd,"\n");
411496be53dSLois Curfman McInnes     for ( i=0; i<m; i++ ) {
412496be53dSLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
413496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
414496be53dSLois Curfman McInnes #if defined(PETSC_COMPLEX)
415496be53dSLois Curfman McInnes           if (imag(a->a[j]) != 0.0 || real(a->a[j]) != 0.0)
416496be53dSLois Curfman McInnes             fprintf(fd," %18.16e %18.16e ",real(a->a[j]),imag(a->a[j]));
417496be53dSLois Curfman McInnes #else
418496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) fprintf(fd," %18.16e ",a->a[j]);
419496be53dSLois Curfman McInnes #endif
420496be53dSLois Curfman McInnes         }
421496be53dSLois Curfman McInnes       }
422496be53dSLois Curfman McInnes       fprintf(fd,"\n");
423496be53dSLois Curfman McInnes     }
424496be53dSLois Curfman McInnes   }
42517ab2063SBarry Smith   else {
42617ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
42717ab2063SBarry Smith       fprintf(fd,"row %d:",i);
428416022c9SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
42917ab2063SBarry Smith #if defined(PETSC_COMPLEX)
430766eeae4SLois Curfman McInnes         if (imag(a->a[j]) > 0.0) {
431416022c9SBarry Smith           fprintf(fd," %d %g + %g i",a->j[j]+shift,real(a->a[j]),imag(a->a[j]));
432766eeae4SLois Curfman McInnes         } else if (imag(a->a[j]) < 0.0) {
433766eeae4SLois Curfman McInnes           fprintf(fd," %d %g - %g i",a->j[j]+shift,real(a->a[j]),-imag(a->a[j]));
43417ab2063SBarry Smith         }
43517ab2063SBarry Smith         else {
436416022c9SBarry Smith           fprintf(fd," %d %g ",a->j[j]+shift,real(a->a[j]));
43717ab2063SBarry Smith         }
43817ab2063SBarry Smith #else
439416022c9SBarry Smith         fprintf(fd," %d %g ",a->j[j]+shift,a->a[j]);
44017ab2063SBarry Smith #endif
44117ab2063SBarry Smith       }
44217ab2063SBarry Smith       fprintf(fd,"\n");
44317ab2063SBarry Smith     }
44417ab2063SBarry Smith   }
44517ab2063SBarry Smith   fflush(fd);
446416022c9SBarry Smith   return 0;
447416022c9SBarry Smith }
448416022c9SBarry Smith 
4495615d1e5SSatish Balay #undef __FUNC__
4505615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_Draw"
4518f6be9afSLois Curfman McInnes extern int MatView_SeqAIJ_Draw(Mat A,Viewer viewer)
452416022c9SBarry Smith {
453416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ *) A->data;
454cddf8d76SBarry Smith   int         ierr, i,j, m = a->m, shift = a->indexshift,pause,color;
4550513a670SBarry Smith   int         format;
45694a9d846SBarry Smith   double      xl,yl,xr,yr,w,h,xc,yc,scale = 1.0,x_l,x_r,y_l,y_r,maxv = 0.0;
457bcd2baecSBarry Smith   Draw        draw;
458cddf8d76SBarry Smith   DrawButton  button;
45919bcc07fSBarry Smith   PetscTruth  isnull;
460cddf8d76SBarry Smith 
4610513a670SBarry Smith   ierr = ViewerDrawGetDraw(viewer,&draw); CHKERRQ(ierr);
4620513a670SBarry Smith   ierr = DrawClear(draw); CHKERRQ(ierr);
4630513a670SBarry Smith   ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr);
46419bcc07fSBarry Smith   ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr); if (isnull) return 0;
46519bcc07fSBarry Smith 
466416022c9SBarry Smith   xr  = a->n; yr = a->m; h = yr/10.0; w = xr/10.0;
467416022c9SBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
468416022c9SBarry Smith   ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr);
469416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4700513a670SBarry Smith 
4710513a670SBarry Smith   if (format != VIEWER_FORMAT_DRAW_CONTOUR) {
4720513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
473cddf8d76SBarry Smith     color = DRAW_BLUE;
474416022c9SBarry Smith     for ( i=0; i<m; i++ ) {
475cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
476416022c9SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
477cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
478cddf8d76SBarry Smith #if defined(PETSC_COMPLEX)
479cddf8d76SBarry Smith         if (real(a->a[j]) >=  0.) continue;
480cddf8d76SBarry Smith #else
481cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
482cddf8d76SBarry Smith #endif
483cddf8d76SBarry Smith         DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
484cddf8d76SBarry Smith       }
485cddf8d76SBarry Smith     }
486cddf8d76SBarry Smith     color = DRAW_CYAN;
487cddf8d76SBarry Smith     for ( i=0; i<m; i++ ) {
488cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
489cddf8d76SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
490cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
491cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
492cddf8d76SBarry Smith         DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
493cddf8d76SBarry Smith       }
494cddf8d76SBarry Smith     }
495cddf8d76SBarry Smith     color = DRAW_RED;
496cddf8d76SBarry Smith     for ( i=0; i<m; i++ ) {
497cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
498cddf8d76SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
499cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
500cddf8d76SBarry Smith #if defined(PETSC_COMPLEX)
501cddf8d76SBarry Smith         if (real(a->a[j]) <=  0.) continue;
502cddf8d76SBarry Smith #else
503cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
504cddf8d76SBarry Smith #endif
505cddf8d76SBarry Smith         DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
506416022c9SBarry Smith       }
507416022c9SBarry Smith     }
5080513a670SBarry Smith   } else {
5090513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5100513a670SBarry Smith     /* first determine max of all nonzero values */
5110513a670SBarry Smith     int    nz = a->nz,count;
5120513a670SBarry Smith     Draw   popup;
5130513a670SBarry Smith 
5140513a670SBarry Smith     for ( i=0; i<nz; i++ ) {
5150513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5160513a670SBarry Smith     }
5170513a670SBarry Smith     ierr = DrawCreatePopUp(draw,&popup); CHKERRQ(ierr);
5180513a670SBarry Smith     ierr = DrawScalePopup(popup,0.0,maxv); CHKERRQ(ierr);
5190513a670SBarry Smith     count = 0;
5200513a670SBarry Smith     for ( i=0; i<m; i++ ) {
5210513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
5220513a670SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
5230513a670SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
5240513a670SBarry Smith         color = 32 + (int) ((200.0 - 32.0)*PetscAbsScalar(a->a[count])/maxv);
5250513a670SBarry Smith         DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
5260513a670SBarry Smith         count++;
5270513a670SBarry Smith       }
5280513a670SBarry Smith     }
5290513a670SBarry Smith   }
530416022c9SBarry Smith   DrawFlush(draw);
531cddf8d76SBarry Smith   DrawGetPause(draw,&pause);
532cddf8d76SBarry Smith   if (pause >= 0) { PetscSleep(pause); return 0;}
533cddf8d76SBarry Smith 
534cddf8d76SBarry Smith   /* allow the matrix to zoom or shrink */
5356945ee14SBarry Smith   ierr = DrawCheckResizedWindow(draw);
536cddf8d76SBarry Smith   ierr = DrawGetMouseButton(draw,&button,&xc,&yc,0,0);
537cddf8d76SBarry Smith   while (button != BUTTON_RIGHT) {
538cddf8d76SBarry Smith     DrawClear(draw);
539cddf8d76SBarry Smith     if (button == BUTTON_LEFT) scale = .5;
540cddf8d76SBarry Smith     else if (button == BUTTON_CENTER) scale = 2.;
541cddf8d76SBarry Smith     xl = scale*(xl + w - xc) + xc - w*scale;
542cddf8d76SBarry Smith     xr = scale*(xr - w - xc) + xc + w*scale;
543cddf8d76SBarry Smith     yl = scale*(yl + h - yc) + yc - h*scale;
544cddf8d76SBarry Smith     yr = scale*(yr - h - yc) + yc + h*scale;
545cddf8d76SBarry Smith     w *= scale; h *= scale;
546cddf8d76SBarry Smith     ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr);
5470513a670SBarry Smith     if (format != VIEWER_FORMAT_DRAW_CONTOUR) {
5480513a670SBarry Smith       /* Blue for negative, Cyan for zero and  Red for positive */
549cddf8d76SBarry Smith       color = DRAW_BLUE;
550cddf8d76SBarry Smith       for ( i=0; i<m; i++ ) {
551cddf8d76SBarry Smith         y_l = m - i - 1.0; y_r = y_l + 1.0;
552cddf8d76SBarry Smith         for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
553cddf8d76SBarry Smith           x_l = a->j[j] + shift; x_r = x_l + 1.0;
554cddf8d76SBarry Smith #if defined(PETSC_COMPLEX)
555cddf8d76SBarry Smith           if (real(a->a[j]) >=  0.) continue;
556cddf8d76SBarry Smith #else
557cddf8d76SBarry Smith           if (a->a[j] >=  0.) continue;
558cddf8d76SBarry Smith #endif
559cddf8d76SBarry Smith           DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
560cddf8d76SBarry Smith         }
561cddf8d76SBarry Smith       }
562cddf8d76SBarry Smith       color = DRAW_CYAN;
563cddf8d76SBarry Smith       for ( i=0; i<m; i++ ) {
564cddf8d76SBarry Smith         y_l = m - i - 1.0; y_r = y_l + 1.0;
565cddf8d76SBarry Smith         for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
566cddf8d76SBarry Smith           x_l = a->j[j] + shift; x_r = x_l + 1.0;
567cddf8d76SBarry Smith           if (a->a[j] !=  0.) continue;
568cddf8d76SBarry Smith           DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
569cddf8d76SBarry Smith         }
570cddf8d76SBarry Smith       }
571cddf8d76SBarry Smith       color = DRAW_RED;
572cddf8d76SBarry Smith       for ( i=0; i<m; i++ ) {
573cddf8d76SBarry Smith         y_l = m - i - 1.0; y_r = y_l + 1.0;
574cddf8d76SBarry Smith         for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
575cddf8d76SBarry Smith           x_l = a->j[j] + shift; x_r = x_l + 1.0;
576cddf8d76SBarry Smith #if defined(PETSC_COMPLEX)
577cddf8d76SBarry Smith           if (real(a->a[j]) <=  0.) continue;
578cddf8d76SBarry Smith #else
579cddf8d76SBarry Smith           if (a->a[j] <=  0.) continue;
580cddf8d76SBarry Smith #endif
581cddf8d76SBarry Smith           DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
582cddf8d76SBarry Smith         }
583cddf8d76SBarry Smith       }
5840513a670SBarry Smith     } else {
5850513a670SBarry Smith       /* use contour shading to indicate magnitude of values */
5860513a670SBarry Smith       int count = 0;
5870513a670SBarry Smith       for ( i=0; i<m; i++ ) {
5880513a670SBarry Smith         y_l = m - i - 1.0; y_r = y_l + 1.0;
5890513a670SBarry Smith         for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
5900513a670SBarry Smith           x_l = a->j[j] + shift; x_r = x_l + 1.0;
5910513a670SBarry Smith           color = 32 + (int) ((200.0 - 32.0)*PetscAbsScalar(a->a[count])/maxv);
5920513a670SBarry Smith           DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
5930513a670SBarry Smith           count++;
5940513a670SBarry Smith         }
5950513a670SBarry Smith       }
5960513a670SBarry Smith     }
5970513a670SBarry Smith 
5986945ee14SBarry Smith     ierr = DrawCheckResizedWindow(draw);
599cddf8d76SBarry Smith     ierr = DrawGetMouseButton(draw,&button,&xc,&yc,0,0);
600cddf8d76SBarry Smith   }
601416022c9SBarry Smith   return 0;
602416022c9SBarry Smith }
603416022c9SBarry Smith 
6045615d1e5SSatish Balay #undef __FUNC__
605d4bb536fSBarry Smith #define __FUNC__ "MatView_SeqAIJ"
6068f6be9afSLois Curfman McInnes int MatView_SeqAIJ(PetscObject obj,Viewer viewer)
607416022c9SBarry Smith {
608416022c9SBarry Smith   Mat         A = (Mat) obj;
609416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*) A->data;
610bcd2baecSBarry Smith   ViewerType  vtype;
611bcd2baecSBarry Smith   int         ierr;
612416022c9SBarry Smith 
613bcd2baecSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
614bcd2baecSBarry Smith   if (vtype == MATLAB_VIEWER) {
615416022c9SBarry Smith     return ViewerMatlabPutSparse_Private(viewer,a->m,a->n,a->nz,a->a,a->i,a->j);
616416022c9SBarry Smith   }
617bcd2baecSBarry Smith   else if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER){
618416022c9SBarry Smith     return MatView_SeqAIJ_ASCII(A,viewer);
619416022c9SBarry Smith   }
620bcd2baecSBarry Smith   else if (vtype == BINARY_FILE_VIEWER) {
621416022c9SBarry Smith     return MatView_SeqAIJ_Binary(A,viewer);
622416022c9SBarry Smith   }
623bcd2baecSBarry Smith   else if (vtype == DRAW_VIEWER) {
624bcd2baecSBarry Smith     return MatView_SeqAIJ_Draw(A,viewer);
62517ab2063SBarry Smith   }
62617ab2063SBarry Smith   return 0;
62717ab2063SBarry Smith }
62819bcc07fSBarry Smith 
629c456f294SBarry Smith extern int Mat_AIJ_CheckInode(Mat);
6305615d1e5SSatish Balay #undef __FUNC__
6315615d1e5SSatish Balay #define __FUNC__ "MatAssemblyEnd_SeqAIJ"
6328f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
63317ab2063SBarry Smith {
634416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
63541c01911SSatish Balay   int        fshift = 0,i,j,*ai = a->i, *aj = a->j, *imax = a->imax,ierr;
63643ee02c3SBarry Smith   int        m = a->m, *ip, N, *ailen = a->ilen,shift = a->indexshift,rmax = 0;
637416022c9SBarry Smith   Scalar     *aa = a->a, *ap;
63817ab2063SBarry Smith 
6396d4a8577SBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) return 0;
64017ab2063SBarry Smith 
64143ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
64217ab2063SBarry Smith   for ( i=1; i<m; i++ ) {
643416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
64417ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
64594a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
64617ab2063SBarry Smith     if (fshift) {
647416022c9SBarry Smith       ip = aj + ai[i] + shift; ap = aa + ai[i] + shift;
64817ab2063SBarry Smith       N = ailen[i];
64917ab2063SBarry Smith       for ( j=0; j<N; j++ ) {
65017ab2063SBarry Smith         ip[j-fshift] = ip[j];
65117ab2063SBarry Smith         ap[j-fshift] = ap[j];
65217ab2063SBarry Smith       }
65317ab2063SBarry Smith     }
65417ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
65517ab2063SBarry Smith   }
65617ab2063SBarry Smith   if (m) {
65717ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
65817ab2063SBarry Smith     ai[m] = ai[m-1] + ailen[m-1];
65917ab2063SBarry Smith   }
66017ab2063SBarry Smith   /* reset ilen and imax for each row */
66117ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
66217ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
66317ab2063SBarry Smith   }
664416022c9SBarry Smith   a->nz = ai[m] + shift;
66517ab2063SBarry Smith 
66617ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
667416022c9SBarry Smith   if (fshift && a->diag) {
6680452661fSBarry Smith     PetscFree(a->diag);
669416022c9SBarry Smith     PLogObjectMemory(A,-(m+1)*sizeof(int));
670416022c9SBarry Smith     a->diag = 0;
67117ab2063SBarry Smith   }
6724e220ebcSLois Curfman McInnes   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded, %d used\n",
6734e220ebcSLois Curfman McInnes            m,a->n,fshift,a->nz);
6744e220ebcSLois Curfman McInnes   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues is %d\n",
675b810aeb4SBarry Smith            a->reallocs);
67694a9d846SBarry Smith   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax);
677dd5f02e7SSatish Balay   a->reallocs          = 0;
6784e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
6794e220ebcSLois Curfman McInnes 
68076dd722bSSatish Balay   /* check out for identical nodes. If found, use inode functions */
68141c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(A); CHKERRQ(ierr);
68217ab2063SBarry Smith   return 0;
68317ab2063SBarry Smith }
68417ab2063SBarry Smith 
6855615d1e5SSatish Balay #undef __FUNC__
6865615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_SeqAIJ"
6878f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A)
68817ab2063SBarry Smith {
689416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
690cddf8d76SBarry Smith   PetscMemzero(a->a,(a->i[a->m]+a->indexshift)*sizeof(Scalar));
69117ab2063SBarry Smith   return 0;
69217ab2063SBarry Smith }
693416022c9SBarry Smith 
6945615d1e5SSatish Balay #undef __FUNC__
6955615d1e5SSatish Balay #define __FUNC__ "MatDestroy_SeqAIJ"
69617ab2063SBarry Smith int MatDestroy_SeqAIJ(PetscObject obj)
69717ab2063SBarry Smith {
698416022c9SBarry Smith   Mat        A  = (Mat) obj;
699416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
700d5d45c9bSBarry Smith 
70117ab2063SBarry Smith #if defined(PETSC_LOG)
702416022c9SBarry Smith   PLogObjectState(obj,"Rows=%d, Cols=%d, NZ=%d",a->m,a->n,a->nz);
70317ab2063SBarry Smith #endif
7040452661fSBarry Smith   PetscFree(a->a);
7050452661fSBarry Smith   if (!a->singlemalloc) { PetscFree(a->i); PetscFree(a->j);}
7060452661fSBarry Smith   if (a->diag) PetscFree(a->diag);
7070452661fSBarry Smith   if (a->ilen) PetscFree(a->ilen);
7080452661fSBarry Smith   if (a->imax) PetscFree(a->imax);
7090452661fSBarry Smith   if (a->solve_work) PetscFree(a->solve_work);
71076dd722bSSatish Balay   if (a->inode.size) PetscFree(a->inode.size);
7110452661fSBarry Smith   PetscFree(a);
712eed86810SBarry Smith 
713f2655603SLois Curfman McInnes   PLogObjectDestroy(A);
714f2655603SLois Curfman McInnes   PetscHeaderDestroy(A);
71517ab2063SBarry Smith   return 0;
71617ab2063SBarry Smith }
71717ab2063SBarry Smith 
7185615d1e5SSatish Balay #undef __FUNC__
7195615d1e5SSatish Balay #define __FUNC__ "MatCompress_SeqAIJ"
7208f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
72117ab2063SBarry Smith {
72217ab2063SBarry Smith   return 0;
72317ab2063SBarry Smith }
72417ab2063SBarry Smith 
7255615d1e5SSatish Balay #undef __FUNC__
7265615d1e5SSatish Balay #define __FUNC__ "MatSetOption_SeqAIJ"
7278f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
72817ab2063SBarry Smith {
729416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
7306d4a8577SBarry Smith   if      (op == MAT_ROW_ORIENTED)                 a->roworiented = 1;
7316d4a8577SBarry Smith   else if (op == MAT_COLUMN_ORIENTED)              a->roworiented = 0;
7326d4a8577SBarry Smith   else if (op == MAT_COLUMNS_SORTED)               a->sorted      = 1;
733219d9a1aSLois Curfman McInnes   else if (op == MAT_COLUMNS_UNSORTED)             a->sorted      = 0;
7346d4a8577SBarry Smith   else if (op == MAT_NO_NEW_NONZERO_LOCATIONS)     a->nonew       = 1;
735c2653b3dSLois Curfman McInnes   else if (op == MAT_NEW_NONZERO_LOCATION_ERROR)   a->nonew       = -1;
73696854ed6SLois Curfman McInnes   else if (op == MAT_NEW_NONZERO_ALLOCATION_ERROR) a->nonew       = -2;
7376d4a8577SBarry Smith   else if (op == MAT_YES_NEW_NONZERO_LOCATIONS)    a->nonew       = 0;
7386d4a8577SBarry Smith   else if (op == MAT_ROWS_SORTED ||
739219d9a1aSLois Curfman McInnes            op == MAT_ROWS_UNSORTED ||
7406d4a8577SBarry Smith            op == MAT_SYMMETRIC ||
7416d4a8577SBarry Smith            op == MAT_STRUCTURALLY_SYMMETRIC ||
74290f02eecSBarry Smith            op == MAT_YES_NEW_DIAGONALS ||
7432b362799SSatish Balay            op == MAT_IGNORE_OFF_PROC_ENTRIES)
74494a424c1SBarry Smith     PLogInfo(A,"Info:MatSetOption_SeqAIJ:Option ignored\n");
7456d4a8577SBarry Smith   else if (op == MAT_NO_NEW_DIAGONALS)
746e3372554SBarry Smith     {SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS");}
7476d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_1)            a->inode.limit  = 1;
7486d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_2)            a->inode.limit  = 2;
7496d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_3)            a->inode.limit  = 3;
7506d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_4)            a->inode.limit  = 4;
7516d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_5)            a->inode.limit  = 5;
752e2f28af5SBarry Smith   else
753e3372554SBarry Smith     {SETERRQ(PETSC_ERR_SUP,0,"unknown option");}
75417ab2063SBarry Smith   return 0;
75517ab2063SBarry Smith }
75617ab2063SBarry Smith 
7575615d1e5SSatish Balay #undef __FUNC__
7585615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_SeqAIJ"
7598f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
76017ab2063SBarry Smith {
761416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
762416022c9SBarry Smith   int        i,j, n,shift = a->indexshift;
76317ab2063SBarry Smith   Scalar     *x, zero = 0.0;
76417ab2063SBarry Smith 
76517ab2063SBarry Smith   VecSet(&zero,v);
76690f02eecSBarry Smith   VecGetArray_Fast(v,x); VecGetLocalSize(v,&n);
767e3372554SBarry Smith   if (n != a->m) SETERRQ(1,0,"Nonconforming matrix and vector");
768416022c9SBarry Smith   for ( i=0; i<a->m; i++ ) {
769416022c9SBarry Smith     for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
770416022c9SBarry Smith       if (a->j[j]+shift == i) {
771416022c9SBarry Smith         x[i] = a->a[j];
77217ab2063SBarry Smith         break;
77317ab2063SBarry Smith       }
77417ab2063SBarry Smith     }
77517ab2063SBarry Smith   }
77617ab2063SBarry Smith   return 0;
77717ab2063SBarry Smith }
77817ab2063SBarry Smith 
77917ab2063SBarry Smith /* -------------------------------------------------------*/
78017ab2063SBarry Smith /* Should check that shapes of vectors and matrices match */
78117ab2063SBarry Smith /* -------------------------------------------------------*/
7825615d1e5SSatish Balay #undef __FUNC__
7835615d1e5SSatish Balay #define __FUNC__ "MatMultTrans_SeqAIJ"
78444cd7ae7SLois Curfman McInnes int MatMultTrans_SeqAIJ(Mat A,Vec xx,Vec yy)
78517ab2063SBarry Smith {
786416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
78717ab2063SBarry Smith   Scalar     *x, *y, *v, alpha;
788416022c9SBarry Smith   int        m = a->m, n, i, *idx, shift = a->indexshift;
78917ab2063SBarry Smith 
79090f02eecSBarry Smith   VecGetArray_Fast(xx,x); VecGetArray_Fast(yy,y);
791cddf8d76SBarry Smith   PetscMemzero(y,a->n*sizeof(Scalar));
79217ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
79317ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
794416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
795416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
796416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
79717ab2063SBarry Smith     alpha = x[i];
79817ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
79917ab2063SBarry Smith   }
800416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
80117ab2063SBarry Smith   return 0;
80217ab2063SBarry Smith }
803d5d45c9bSBarry Smith 
8045615d1e5SSatish Balay #undef __FUNC__
8055615d1e5SSatish Balay #define __FUNC__ "MatMultTransAdd_SeqAIJ"
80644cd7ae7SLois Curfman McInnes int MatMultTransAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
80717ab2063SBarry Smith {
808416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
80917ab2063SBarry Smith   Scalar     *x, *y, *v, alpha;
810416022c9SBarry Smith   int        m = a->m, n, i, *idx,shift = a->indexshift;
81117ab2063SBarry Smith 
81290f02eecSBarry Smith   VecGetArray_Fast(xx,x); VecGetArray_Fast(yy,y);
81317ab2063SBarry Smith   if (zz != yy) VecCopy(zz,yy);
81417ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
81517ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
816416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
817416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
818416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
81917ab2063SBarry Smith     alpha = x[i];
82017ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
82117ab2063SBarry Smith   }
82290f02eecSBarry Smith   PLogFlops(2*a->nz);
82317ab2063SBarry Smith   return 0;
82417ab2063SBarry Smith }
82517ab2063SBarry Smith 
8265615d1e5SSatish Balay #undef __FUNC__
8275615d1e5SSatish Balay #define __FUNC__ "MatMult_SeqAIJ"
82844cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
82917ab2063SBarry Smith {
830416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
83117ab2063SBarry Smith   Scalar     *x, *y, *v, sum;
8329ea0dfa2SSatish Balay   int        m = a->m, n, i, *idx, shift = a->indexshift,*ii,jrow,j;
83317ab2063SBarry Smith 
83490f02eecSBarry Smith   VecGetArray_Fast(xx,x); VecGetArray_Fast(yy,y);
83517ab2063SBarry Smith   x    = x + shift; /* shift for Fortran start by 1 indexing */
836416022c9SBarry Smith   idx  = a->j;
8379b88f4a7SBarry Smith   v    = a->a + shift; /* shift for Fortran start by 1 indexing */
838416022c9SBarry Smith   ii   = a->i;
83917ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
8409ea0dfa2SSatish Balay     jrow = ii[i];
8419ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
84217ab2063SBarry Smith     sum  = 0.0;
8439ea0dfa2SSatish Balay     for ( j=0; j<n; j++) {
8449ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8459ea0dfa2SSatish Balay      }
84617ab2063SBarry Smith     y[i] = sum;
84717ab2063SBarry Smith   }
848416022c9SBarry Smith   PLogFlops(2*a->nz - m);
84917ab2063SBarry Smith   return 0;
85017ab2063SBarry Smith }
85117ab2063SBarry Smith 
8525615d1e5SSatish Balay #undef __FUNC__
8535615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_SeqAIJ"
85444cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
85517ab2063SBarry Smith {
856416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
85717ab2063SBarry Smith   Scalar     *x, *y, *z, *v, sum;
8589ea0dfa2SSatish Balay   int        m = a->m, n, i, *idx, shift = a->indexshift,*ii,jrow,j;
8599ea0dfa2SSatish Balay 
86017ab2063SBarry Smith 
86190f02eecSBarry Smith   VecGetArray_Fast(xx,x); VecGetArray_Fast(yy,y); VecGetArray_Fast(zz,z);
86217ab2063SBarry Smith   x    = x + shift; /* shift for Fortran start by 1 indexing */
863cddf8d76SBarry Smith   idx  = a->j;
8649b88f4a7SBarry Smith   v    = a->a + shift; /* shift for Fortran start by 1 indexing */
865cddf8d76SBarry Smith   ii   = a->i;
86617ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
8679ea0dfa2SSatish Balay     jrow = ii[i];
8689ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
86917ab2063SBarry Smith     sum  = y[i];
8709ea0dfa2SSatish Balay     for ( j=0; j<n; j++) {
8719ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8729ea0dfa2SSatish Balay      }
87317ab2063SBarry Smith     z[i] = sum;
87417ab2063SBarry Smith   }
875416022c9SBarry Smith   PLogFlops(2*a->nz);
87617ab2063SBarry Smith   return 0;
87717ab2063SBarry Smith }
87817ab2063SBarry Smith 
87917ab2063SBarry Smith /*
88017ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
88117ab2063SBarry Smith */
88217ab2063SBarry Smith 
8835615d1e5SSatish Balay #undef __FUNC__
8845615d1e5SSatish Balay #define __FUNC__ "MatMarkDiag_SeqAIJ"
885416022c9SBarry Smith int MatMarkDiag_SeqAIJ(Mat A)
88617ab2063SBarry Smith {
887416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
888416022c9SBarry Smith   int        i,j, *diag, m = a->m,shift = a->indexshift;
88917ab2063SBarry Smith 
8900452661fSBarry Smith   diag = (int *) PetscMalloc( (m+1)*sizeof(int)); CHKPTRQ(diag);
891416022c9SBarry Smith   PLogObjectMemory(A,(m+1)*sizeof(int));
892416022c9SBarry Smith   for ( i=0; i<a->m; i++ ) {
893416022c9SBarry Smith     for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
894416022c9SBarry Smith       if (a->j[j]+shift == i) {
89517ab2063SBarry Smith         diag[i] = j - shift;
89617ab2063SBarry Smith         break;
89717ab2063SBarry Smith       }
89817ab2063SBarry Smith     }
89917ab2063SBarry Smith   }
900416022c9SBarry Smith   a->diag = diag;
90117ab2063SBarry Smith   return 0;
90217ab2063SBarry Smith }
90317ab2063SBarry Smith 
9045615d1e5SSatish Balay #undef __FUNC__
9055615d1e5SSatish Balay #define __FUNC__ "MatRelax_SeqAIJ"
90644cd7ae7SLois Curfman McInnes int MatRelax_SeqAIJ(Mat A,Vec bb,double omega,MatSORType flag,
90717ab2063SBarry Smith                            double fshift,int its,Vec xx)
90817ab2063SBarry Smith {
909416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
910416022c9SBarry Smith   Scalar     *x, *b, *bs,  d, *xs, sum, *v = a->a,*t,scale,*ts, *xb;
911d5d45c9bSBarry Smith   int        ierr, *idx, *diag,n = a->n, m = a->m, i, shift = a->indexshift;
91217ab2063SBarry Smith 
91390f02eecSBarry Smith   VecGetArray_Fast(xx,x); VecGetArray_Fast(bb,b);
914d00d2cf4SBarry Smith   if (!a->diag) {ierr = MatMarkDiag_SeqAIJ(A);CHKERRQ(ierr);}
915416022c9SBarry Smith   diag = a->diag;
916416022c9SBarry Smith   xs   = x + shift; /* shifted by one for index start of a or a->j*/
91717ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
91817ab2063SBarry Smith    /* apply ( U + D/omega) to the vector */
91917ab2063SBarry Smith     bs = b + shift;
92017ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
921416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
922416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
923416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
924416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
92517ab2063SBarry Smith         sum  = b[i]*d/omega;
92617ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
92717ab2063SBarry Smith         x[i] = sum;
92817ab2063SBarry Smith     }
92917ab2063SBarry Smith     return 0;
93017ab2063SBarry Smith   }
93117ab2063SBarry Smith   if (flag == SOR_APPLY_LOWER) {
932e3372554SBarry Smith     SETERRQ(1,0,"SOR_APPLY_LOWER is not done");
93317ab2063SBarry Smith   }
934416022c9SBarry Smith   else if (flag & SOR_EISENSTAT) {
93517ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
93617ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
93717ab2063SBarry Smith 
93817ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
93917ab2063SBarry Smith 
94017ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
94117ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
94217ab2063SBarry Smith     is the relaxation factor.
94317ab2063SBarry Smith     */
9440452661fSBarry Smith     t = (Scalar *) PetscMalloc( m*sizeof(Scalar) ); CHKPTRQ(t);
94517ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
94617ab2063SBarry Smith 
94717ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
94817ab2063SBarry Smith     for ( i=m-1; i>=0; i-- ) {
949416022c9SBarry Smith       d    = fshift + a->a[diag[i] + shift];
950416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
951416022c9SBarry Smith       idx  = a->j + diag[i] + (!shift);
952416022c9SBarry Smith       v    = a->a + diag[i] + (!shift);
95317ab2063SBarry Smith       sum  = b[i];
95417ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
95517ab2063SBarry Smith       x[i] = omega*(sum/d);
95617ab2063SBarry Smith     }
95717ab2063SBarry Smith 
95817ab2063SBarry Smith     /*  t = b - (2*E - D)x */
959416022c9SBarry Smith     v = a->a;
96017ab2063SBarry Smith     for ( i=0; i<m; i++ ) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; }
96117ab2063SBarry Smith 
96217ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
963416022c9SBarry Smith     ts = t + shift; /* shifted by one for index start of a or a->j*/
964416022c9SBarry Smith     diag = a->diag;
96517ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
966416022c9SBarry Smith       d    = fshift + a->a[diag[i]+shift];
967416022c9SBarry Smith       n    = diag[i] - a->i[i];
968416022c9SBarry Smith       idx  = a->j + a->i[i] + shift;
969416022c9SBarry Smith       v    = a->a + a->i[i] + shift;
97017ab2063SBarry Smith       sum  = t[i];
97117ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
97217ab2063SBarry Smith       t[i] = omega*(sum/d);
97317ab2063SBarry Smith     }
97417ab2063SBarry Smith 
97517ab2063SBarry Smith     /*  x = x + t */
97617ab2063SBarry Smith     for ( i=0; i<m; i++ ) { x[i] += t[i]; }
9770452661fSBarry Smith     PetscFree(t);
97817ab2063SBarry Smith     return 0;
97917ab2063SBarry Smith   }
98017ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
98117ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
98217ab2063SBarry Smith       for ( i=0; i<m; i++ ) {
983416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
984416022c9SBarry Smith         n    = diag[i] - a->i[i];
985416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
986416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
98717ab2063SBarry Smith         sum  = b[i];
98817ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
98917ab2063SBarry Smith         x[i] = omega*(sum/d);
99017ab2063SBarry Smith       }
99117ab2063SBarry Smith       xb = x;
99217ab2063SBarry Smith     }
99317ab2063SBarry Smith     else xb = b;
99417ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
99517ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
99617ab2063SBarry Smith       for ( i=0; i<m; i++ ) {
997416022c9SBarry Smith         x[i] *= a->a[diag[i]+shift];
99817ab2063SBarry Smith       }
99917ab2063SBarry Smith     }
100017ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
100117ab2063SBarry Smith       for ( i=m-1; i>=0; i-- ) {
1002416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1003416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1004416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1005416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
100617ab2063SBarry Smith         sum  = xb[i];
100717ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
100817ab2063SBarry Smith         x[i] = omega*(sum/d);
100917ab2063SBarry Smith       }
101017ab2063SBarry Smith     }
101117ab2063SBarry Smith     its--;
101217ab2063SBarry Smith   }
101317ab2063SBarry Smith   while (its--) {
101417ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
101517ab2063SBarry Smith       for ( i=0; i<m; i++ ) {
1016416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1017416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1018416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1019416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
102017ab2063SBarry Smith         sum  = b[i];
102117ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
10227e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
102317ab2063SBarry Smith       }
102417ab2063SBarry Smith     }
102517ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
102617ab2063SBarry Smith       for ( i=m-1; i>=0; i-- ) {
1027416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1028416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1029416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1030416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
103117ab2063SBarry Smith         sum  = b[i];
103217ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
10337e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
103417ab2063SBarry Smith       }
103517ab2063SBarry Smith     }
103617ab2063SBarry Smith   }
103717ab2063SBarry Smith   return 0;
103817ab2063SBarry Smith }
103917ab2063SBarry Smith 
10405615d1e5SSatish Balay #undef __FUNC__
10415615d1e5SSatish Balay #define __FUNC__ "MatGetInfo_SeqAIJ"
10428f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
104317ab2063SBarry Smith {
1044416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
10454e220ebcSLois Curfman McInnes 
10464e220ebcSLois Curfman McInnes   info->rows_global    = (double)a->m;
10474e220ebcSLois Curfman McInnes   info->columns_global = (double)a->n;
10484e220ebcSLois Curfman McInnes   info->rows_local     = (double)a->m;
10494e220ebcSLois Curfman McInnes   info->columns_local  = (double)a->n;
10504e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
10514e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
10524e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
10534e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
10544e220ebcSLois Curfman McInnes   /*  if (info->nz_unneeded != A->info.nz_unneeded)
10554e220ebcSLois Curfman McInnes     printf("space descrepancy: maxnz-nz = %d, nz_unneeded = %d\n",(int)info->nz_unneeded,(int)A->info.nz_unneeded); */
10564e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
10574e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
10584e220ebcSLois Curfman McInnes   info->memory         = A->mem;
10594e220ebcSLois Curfman McInnes   if (A->factor) {
10604e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
10614e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
10624e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
10634e220ebcSLois Curfman McInnes   } else {
10644e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
10654e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
10664e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
10674e220ebcSLois Curfman McInnes   }
106817ab2063SBarry Smith   return 0;
106917ab2063SBarry Smith }
107017ab2063SBarry Smith 
107117ab2063SBarry Smith extern int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,double,Mat*);
107217ab2063SBarry Smith extern int MatLUFactorNumeric_SeqAIJ(Mat,Mat*);
107317ab2063SBarry Smith extern int MatLUFactor_SeqAIJ(Mat,IS,IS,double);
107417ab2063SBarry Smith extern int MatSolve_SeqAIJ(Mat,Vec,Vec);
107517ab2063SBarry Smith extern int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
107617ab2063SBarry Smith extern int MatSolveTrans_SeqAIJ(Mat,Vec,Vec);
107717ab2063SBarry Smith extern int MatSolveTransAdd_SeqAIJ(Mat,Vec,Vec,Vec);
107817ab2063SBarry Smith 
10795615d1e5SSatish Balay #undef __FUNC__
10805615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_SeqAIJ"
10818f6be9afSLois Curfman McInnes int MatZeroRows_SeqAIJ(Mat A,IS is,Scalar *diag)
108217ab2063SBarry Smith {
1083416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1084416022c9SBarry Smith   int         i,ierr,N, *rows,m = a->m - 1,shift = a->indexshift;
108517ab2063SBarry Smith 
108677c4ece6SBarry Smith   ierr = ISGetSize(is,&N); CHKERRQ(ierr);
108717ab2063SBarry Smith   ierr = ISGetIndices(is,&rows); CHKERRQ(ierr);
108817ab2063SBarry Smith   if (diag) {
108917ab2063SBarry Smith     for ( i=0; i<N; i++ ) {
1090e3372554SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(1,0,"row out of range");
1091416022c9SBarry Smith       if (a->ilen[rows[i]] > 0) { /* in case row was completely empty */
1092416022c9SBarry Smith         a->ilen[rows[i]] = 1;
1093416022c9SBarry Smith         a->a[a->i[rows[i]]+shift] = *diag;
1094416022c9SBarry Smith         a->j[a->i[rows[i]]+shift] = rows[i]+shift;
109517ab2063SBarry Smith       }
109617ab2063SBarry Smith       else {
109717ab2063SBarry Smith         ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);
109817ab2063SBarry Smith         CHKERRQ(ierr);
109917ab2063SBarry Smith       }
110017ab2063SBarry Smith     }
110117ab2063SBarry Smith   }
110217ab2063SBarry Smith   else {
110317ab2063SBarry Smith     for ( i=0; i<N; i++ ) {
1104e3372554SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(1,0,"row out of range");
1105416022c9SBarry Smith       a->ilen[rows[i]] = 0;
110617ab2063SBarry Smith     }
110717ab2063SBarry Smith   }
110817ab2063SBarry Smith   ISRestoreIndices(is,&rows);
110943a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
111017ab2063SBarry Smith   return 0;
111117ab2063SBarry Smith }
111217ab2063SBarry Smith 
11135615d1e5SSatish Balay #undef __FUNC__
11145615d1e5SSatish Balay #define __FUNC__ "MatGetSize_SeqAIJ"
11158f6be9afSLois Curfman McInnes int MatGetSize_SeqAIJ(Mat A,int *m,int *n)
111617ab2063SBarry Smith {
1117416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1118416022c9SBarry Smith   *m = a->m; *n = a->n;
111917ab2063SBarry Smith   return 0;
112017ab2063SBarry Smith }
112117ab2063SBarry Smith 
11225615d1e5SSatish Balay #undef __FUNC__
11235615d1e5SSatish Balay #define __FUNC__ "MatGetOwnershipRange_SeqAIJ"
11248f6be9afSLois Curfman McInnes int MatGetOwnershipRange_SeqAIJ(Mat A,int *m,int *n)
112517ab2063SBarry Smith {
1126416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1127416022c9SBarry Smith   *m = 0; *n = a->m;
112817ab2063SBarry Smith   return 0;
112917ab2063SBarry Smith }
1130026e39d0SSatish Balay 
11315615d1e5SSatish Balay #undef __FUNC__
11325615d1e5SSatish Balay #define __FUNC__ "MatGetRow_SeqAIJ"
11334e093b46SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
113417ab2063SBarry Smith {
1135416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1136c456f294SBarry Smith   int        *itmp,i,shift = a->indexshift;
113717ab2063SBarry Smith 
1138e3372554SBarry Smith   if (row < 0 || row >= a->m) SETERRQ(1,0,"Row out of range");
113917ab2063SBarry Smith 
1140416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1141416022c9SBarry Smith   if (v) *v = a->a + a->i[row] + shift;
114217ab2063SBarry Smith   if (idx) {
1143416022c9SBarry Smith     itmp = a->j + a->i[row] + shift;
11444e093b46SBarry Smith     if (*nz && shift) {
11450452661fSBarry Smith       *idx = (int *) PetscMalloc( (*nz)*sizeof(int) ); CHKPTRQ(*idx);
114617ab2063SBarry Smith       for ( i=0; i<(*nz); i++ ) {(*idx)[i] = itmp[i] + shift;}
11474e093b46SBarry Smith     } else if (*nz) {
11484e093b46SBarry Smith       *idx = itmp;
114917ab2063SBarry Smith     }
115017ab2063SBarry Smith     else *idx = 0;
115117ab2063SBarry Smith   }
115217ab2063SBarry Smith   return 0;
115317ab2063SBarry Smith }
115417ab2063SBarry Smith 
11555615d1e5SSatish Balay #undef __FUNC__
11565615d1e5SSatish Balay #define __FUNC__ "MatRestoreRow_SeqAIJ"
11574e093b46SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
115817ab2063SBarry Smith {
11594e093b46SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
11604e093b46SBarry Smith   if (idx) {if (*idx && a->indexshift) PetscFree(*idx);}
116117ab2063SBarry Smith   return 0;
116217ab2063SBarry Smith }
116317ab2063SBarry Smith 
11645615d1e5SSatish Balay #undef __FUNC__
11655615d1e5SSatish Balay #define __FUNC__ "MatNorm_SeqAIJ"
11668f6be9afSLois Curfman McInnes int MatNorm_SeqAIJ(Mat A,NormType type,double *norm)
116717ab2063SBarry Smith {
1168416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1169416022c9SBarry Smith   Scalar     *v = a->a;
117017ab2063SBarry Smith   double     sum = 0.0;
1171416022c9SBarry Smith   int        i, j,shift = a->indexshift;
117217ab2063SBarry Smith 
117317ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1174416022c9SBarry Smith     for (i=0; i<a->nz; i++ ) {
117517ab2063SBarry Smith #if defined(PETSC_COMPLEX)
117617ab2063SBarry Smith       sum += real(conj(*v)*(*v)); v++;
117717ab2063SBarry Smith #else
117817ab2063SBarry Smith       sum += (*v)*(*v); v++;
117917ab2063SBarry Smith #endif
118017ab2063SBarry Smith     }
118117ab2063SBarry Smith     *norm = sqrt(sum);
118217ab2063SBarry Smith   }
118317ab2063SBarry Smith   else if (type == NORM_1) {
118417ab2063SBarry Smith     double *tmp;
1185416022c9SBarry Smith     int    *jj = a->j;
118666963ce1SSatish Balay     tmp = (double *) PetscMalloc( (a->n+1)*sizeof(double) ); CHKPTRQ(tmp);
1187cddf8d76SBarry Smith     PetscMemzero(tmp,a->n*sizeof(double));
118817ab2063SBarry Smith     *norm = 0.0;
1189416022c9SBarry Smith     for ( j=0; j<a->nz; j++ ) {
1190a2744918SBarry Smith         tmp[*jj++ + shift] += PetscAbsScalar(*v);  v++;
119117ab2063SBarry Smith     }
1192416022c9SBarry Smith     for ( j=0; j<a->n; j++ ) {
119317ab2063SBarry Smith       if (tmp[j] > *norm) *norm = tmp[j];
119417ab2063SBarry Smith     }
11950452661fSBarry Smith     PetscFree(tmp);
119617ab2063SBarry Smith   }
119717ab2063SBarry Smith   else if (type == NORM_INFINITY) {
119817ab2063SBarry Smith     *norm = 0.0;
1199416022c9SBarry Smith     for ( j=0; j<a->m; j++ ) {
1200416022c9SBarry Smith       v = a->a + a->i[j] + shift;
120117ab2063SBarry Smith       sum = 0.0;
1202416022c9SBarry Smith       for ( i=0; i<a->i[j+1]-a->i[j]; i++ ) {
1203cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
120417ab2063SBarry Smith       }
120517ab2063SBarry Smith       if (sum > *norm) *norm = sum;
120617ab2063SBarry Smith     }
120717ab2063SBarry Smith   }
120817ab2063SBarry Smith   else {
1209e3372554SBarry Smith     SETERRQ(1,0,"No support for two norm yet");
121017ab2063SBarry Smith   }
121117ab2063SBarry Smith   return 0;
121217ab2063SBarry Smith }
121317ab2063SBarry Smith 
12145615d1e5SSatish Balay #undef __FUNC__
12155615d1e5SSatish Balay #define __FUNC__ "MatTranspose_SeqAIJ"
12168f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
121717ab2063SBarry Smith {
1218416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1219416022c9SBarry Smith   Mat        C;
1220416022c9SBarry Smith   int        i, ierr, *aj = a->j, *ai = a->i, m = a->m, len, *col;
1221416022c9SBarry Smith   int        shift = a->indexshift;
1222d5d45c9bSBarry Smith   Scalar     *array = a->a;
122317ab2063SBarry Smith 
12243638b69dSLois Curfman McInnes   if (B == PETSC_NULL && m != a->n)
1225e3372554SBarry Smith     SETERRQ(1,0,"Square matrix only for in-place");
12260452661fSBarry Smith   col = (int *) PetscMalloc((1+a->n)*sizeof(int)); CHKPTRQ(col);
1227cddf8d76SBarry Smith   PetscMemzero(col,(1+a->n)*sizeof(int));
122817ab2063SBarry Smith   if (shift) {
122917ab2063SBarry Smith     for ( i=0; i<ai[m]-1; i++ ) aj[i] -= 1;
123017ab2063SBarry Smith   }
123117ab2063SBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) col[aj[i]] += 1;
1232416022c9SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,a->n,m,0,col,&C); CHKERRQ(ierr);
12330452661fSBarry Smith   PetscFree(col);
123417ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
123517ab2063SBarry Smith     len = ai[i+1]-ai[i];
1236416022c9SBarry Smith     ierr = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES); CHKERRQ(ierr);
123717ab2063SBarry Smith     array += len; aj += len;
123817ab2063SBarry Smith   }
123917ab2063SBarry Smith   if (shift) {
124017ab2063SBarry Smith     for ( i=0; i<ai[m]-1; i++ ) aj[i] += 1;
124117ab2063SBarry Smith   }
124217ab2063SBarry Smith 
12436d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
12446d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
124517ab2063SBarry Smith 
12463638b69dSLois Curfman McInnes   if (B != PETSC_NULL) {
1247416022c9SBarry Smith     *B = C;
124817ab2063SBarry Smith   } else {
1249416022c9SBarry Smith     /* This isn't really an in-place transpose */
12500452661fSBarry Smith     PetscFree(a->a);
12510452661fSBarry Smith     if (!a->singlemalloc) {PetscFree(a->i); PetscFree(a->j);}
12520452661fSBarry Smith     if (a->diag) PetscFree(a->diag);
12530452661fSBarry Smith     if (a->ilen) PetscFree(a->ilen);
12540452661fSBarry Smith     if (a->imax) PetscFree(a->imax);
12550452661fSBarry Smith     if (a->solve_work) PetscFree(a->solve_work);
12561073c447SSatish Balay     if (a->inode.size) PetscFree(a->inode.size);
12570452661fSBarry Smith     PetscFree(a);
1258f09e8eb9SSatish Balay     PetscMemcpy(A,C,sizeof(struct _p_Mat));
12590452661fSBarry Smith     PetscHeaderDestroy(C);
126017ab2063SBarry Smith   }
126117ab2063SBarry Smith   return 0;
126217ab2063SBarry Smith }
126317ab2063SBarry Smith 
12645615d1e5SSatish Balay #undef __FUNC__
12655615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_SeqAIJ"
12668f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
126717ab2063SBarry Smith {
1268416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
126917ab2063SBarry Smith   Scalar     *l,*r,x,*v;
1270d5d45c9bSBarry Smith   int        i,j,m = a->m, n = a->n, M, nz = a->nz, *jj,shift = a->indexshift;
127117ab2063SBarry Smith 
127217ab2063SBarry Smith   if (ll) {
12733ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
12743ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
12759b1297e1SSatish Balay     VecGetLocalSize_Fast(ll,m);
1276e3372554SBarry Smith     if (m != a->m) SETERRQ(1,0,"Left scaling vector wrong length");
127790f02eecSBarry Smith     VecGetArray_Fast(ll,l);
1278416022c9SBarry Smith     v = a->a;
127917ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
128017ab2063SBarry Smith       x = l[i];
1281416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
128217ab2063SBarry Smith       for ( j=0; j<M; j++ ) { (*v++) *= x;}
128317ab2063SBarry Smith     }
128444cd7ae7SLois Curfman McInnes     PLogFlops(nz);
128517ab2063SBarry Smith   }
128617ab2063SBarry Smith   if (rr) {
12879b1297e1SSatish Balay     VecGetLocalSize_Fast(rr,n);
1288e3372554SBarry Smith     if (n != a->n) SETERRQ(1,0,"Right scaling vector wrong length");
128990f02eecSBarry Smith     VecGetArray_Fast(rr,r);
1290416022c9SBarry Smith     v = a->a; jj = a->j;
129117ab2063SBarry Smith     for ( i=0; i<nz; i++ ) {
129217ab2063SBarry Smith       (*v++) *= r[*jj++ + shift];
129317ab2063SBarry Smith     }
129444cd7ae7SLois Curfman McInnes     PLogFlops(nz);
129517ab2063SBarry Smith   }
129617ab2063SBarry Smith   return 0;
129717ab2063SBarry Smith }
129817ab2063SBarry Smith 
12995615d1e5SSatish Balay #undef __FUNC__
13005615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrix_SeqAIJ"
13018f6be9afSLois Curfman McInnes int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,MatGetSubMatrixCall scall,Mat *B)
130217ab2063SBarry Smith {
1303db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ *) A->data,*c;
130402834360SBarry Smith   int          nznew, *smap, i, k, kstart, kend, ierr, oldcols = a->n,*lens;
130599141d43SSatish Balay   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen;
1306a2744918SBarry Smith   register int sum,lensi;
130799141d43SSatish Balay   int          *irow, *icol, nrows, ncols, shift = a->indexshift,*ssmap;
130899141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j, *ai = a->i,ii,*ailen = a->ilen;
130999141d43SSatish Balay   Scalar       *a_new,*mat_a;
1310416022c9SBarry Smith   Mat          C;
131117ab2063SBarry Smith 
1312b48a1e75SSatish Balay   ierr = ISSorted(isrow,(PetscTruth*)&i);
1313e3372554SBarry Smith   if (!i) SETERRQ(1,0,"ISrow is not sorted");
131499141d43SSatish Balay   ierr = ISSorted(iscol,(PetscTruth*)&i);
1315e3372554SBarry Smith   if (!i) SETERRQ(1,0,"IScol is not sorted");
131699141d43SSatish Balay 
131717ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow); CHKERRQ(ierr);
131817ab2063SBarry Smith   ierr = ISGetSize(isrow,&nrows); CHKERRQ(ierr);
131917ab2063SBarry Smith   ierr = ISGetSize(iscol,&ncols); CHKERRQ(ierr);
132017ab2063SBarry Smith 
13217264ac53SSatish Balay   if (ISStrideGetInfo(iscol,&first,&step) && step == 1) { /* no need to sort */
132202834360SBarry Smith     /* special case of contiguous rows */
132357faeb66SBarry Smith     lens   = (int *) PetscMalloc((ncols+nrows+1)*sizeof(int)); CHKPTRQ(lens);
132402834360SBarry Smith     starts = lens + ncols;
132502834360SBarry Smith     /* loop over new rows determining lens and starting points */
132602834360SBarry Smith     for (i=0; i<nrows; i++) {
1327a2744918SBarry Smith       kstart  = ai[irow[i]]+shift;
1328a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
132902834360SBarry Smith       for ( k=kstart; k<kend; k++ ) {
1330d8ced48eSBarry Smith         if (aj[k]+shift >= first) {
133102834360SBarry Smith           starts[i] = k;
133202834360SBarry Smith           break;
133302834360SBarry Smith 	}
133402834360SBarry Smith       }
1335a2744918SBarry Smith       sum = 0;
133602834360SBarry Smith       while (k < kend) {
1337d8ced48eSBarry Smith         if (aj[k++]+shift >= first+ncols) break;
1338a2744918SBarry Smith         sum++;
133902834360SBarry Smith       }
1340a2744918SBarry Smith       lens[i] = sum;
134102834360SBarry Smith     }
134202834360SBarry Smith     /* create submatrix */
1343cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
134408480c60SBarry Smith       int n_cols,n_rows;
134508480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols); CHKERRQ(ierr);
1346e3372554SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(1,0,"");
1347d8ced48eSBarry Smith       ierr = MatZeroEntries(*B); CHKERRQ(ierr);
134808480c60SBarry Smith       C = *B;
134908480c60SBarry Smith     }
135008480c60SBarry Smith     else {
135102834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
135208480c60SBarry Smith     }
1353db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*) C->data;
1354db02288aSLois Curfman McInnes 
135502834360SBarry Smith     /* loop over rows inserting into submatrix */
1356db02288aSLois Curfman McInnes     a_new    = c->a;
1357db02288aSLois Curfman McInnes     j_new    = c->j;
1358db02288aSLois Curfman McInnes     i_new    = c->i;
1359db02288aSLois Curfman McInnes     i_new[0] = -shift;
136002834360SBarry Smith     for (i=0; i<nrows; i++) {
1361a2744918SBarry Smith       ii    = starts[i];
1362a2744918SBarry Smith       lensi = lens[i];
1363a2744918SBarry Smith       for ( k=0; k<lensi; k++ ) {
1364a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
136502834360SBarry Smith       }
1366a2744918SBarry Smith       PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(Scalar));
1367a2744918SBarry Smith       a_new      += lensi;
1368a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1369a2744918SBarry Smith       c->ilen[i]  = lensi;
137002834360SBarry Smith     }
13710452661fSBarry Smith     PetscFree(lens);
137202834360SBarry Smith   }
137302834360SBarry Smith   else {
137402834360SBarry Smith     ierr = ISGetIndices(iscol,&icol); CHKERRQ(ierr);
13750452661fSBarry Smith     smap  = (int *) PetscMalloc((1+oldcols)*sizeof(int)); CHKPTRQ(smap);
137602834360SBarry Smith     ssmap = smap + shift;
137799141d43SSatish Balay     lens  = (int *) PetscMalloc((1+nrows)*sizeof(int)); CHKPTRQ(lens);
1378cddf8d76SBarry Smith     PetscMemzero(smap,oldcols*sizeof(int));
137917ab2063SBarry Smith     for ( i=0; i<ncols; i++ ) smap[icol[i]] = i+1;
138002834360SBarry Smith     /* determine lens of each row */
138102834360SBarry Smith     for (i=0; i<nrows; i++) {
1382d8ced48eSBarry Smith       kstart  = ai[irow[i]]+shift;
138302834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
138402834360SBarry Smith       lens[i] = 0;
138502834360SBarry Smith       for ( k=kstart; k<kend; k++ ) {
1386d8ced48eSBarry Smith         if (ssmap[aj[k]]) {
138702834360SBarry Smith           lens[i]++;
138802834360SBarry Smith         }
138902834360SBarry Smith       }
139002834360SBarry Smith     }
139117ab2063SBarry Smith     /* Create and fill new matrix */
1392a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
139399141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
139499141d43SSatish Balay 
1395e3372554SBarry Smith       if (c->m  != nrows || c->n != ncols) SETERRQ(1,0,"");
139699141d43SSatish Balay       if (PetscMemcmp(c->ilen,lens, c->m *sizeof(int))) {
1397e3372554SBarry Smith         SETERRQ(1,0,"Cannot reuse matrix. wrong no of nonzeros");
139899141d43SSatish Balay       }
139999141d43SSatish Balay       PetscMemzero(c->ilen,c->m*sizeof(int));
140008480c60SBarry Smith       C = *B;
140108480c60SBarry Smith     }
140208480c60SBarry Smith     else {
140302834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
140408480c60SBarry Smith     }
140599141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
140617ab2063SBarry Smith     for (i=0; i<nrows; i++) {
140799141d43SSatish Balay       row    = irow[i];
140817ab2063SBarry Smith       nznew  = 0;
140999141d43SSatish Balay       kstart = ai[row]+shift;
141099141d43SSatish Balay       kend   = kstart + a->ilen[row];
141199141d43SSatish Balay       mat_i  = c->i[i]+shift;
141299141d43SSatish Balay       mat_j  = c->j + mat_i;
141399141d43SSatish Balay       mat_a  = c->a + mat_i;
141499141d43SSatish Balay       mat_ilen = c->ilen + i;
141517ab2063SBarry Smith       for ( k=kstart; k<kend; k++ ) {
141699141d43SSatish Balay         if ((tcol=ssmap[a->j[k]])) {
141799141d43SSatish Balay           *mat_j++ = tcol - (!shift);
141899141d43SSatish Balay           *mat_a++ = a->a[k];
141999141d43SSatish Balay           (*mat_ilen)++;
142099141d43SSatish Balay 
142117ab2063SBarry Smith         }
142217ab2063SBarry Smith       }
142317ab2063SBarry Smith     }
142402834360SBarry Smith     /* Free work space */
142502834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol); CHKERRQ(ierr);
142699141d43SSatish Balay     PetscFree(smap); PetscFree(lens);
142702834360SBarry Smith   }
14286d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
14296d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
143017ab2063SBarry Smith 
143117ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow); CHKERRQ(ierr);
1432416022c9SBarry Smith   *B = C;
143317ab2063SBarry Smith   return 0;
143417ab2063SBarry Smith }
143517ab2063SBarry Smith 
1436a871dcd8SBarry Smith /*
143763b91edcSBarry Smith      note: This can only work for identity for row and col. It would
143863b91edcSBarry Smith    be good to check this and otherwise generate an error.
1439a871dcd8SBarry Smith */
14405615d1e5SSatish Balay #undef __FUNC__
14415615d1e5SSatish Balay #define __FUNC__ "MatILUFactor_SeqAIJ"
14428f6be9afSLois Curfman McInnes int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,double efill,int fill)
1443a871dcd8SBarry Smith {
144463b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) inA->data;
144508480c60SBarry Smith   int        ierr;
144663b91edcSBarry Smith   Mat        outA;
144763b91edcSBarry Smith 
1448e3372554SBarry Smith   if (fill != 0) SETERRQ(1,0,"Only fill=0 supported");
1449a871dcd8SBarry Smith 
145063b91edcSBarry Smith   outA          = inA;
145163b91edcSBarry Smith   inA->factor   = FACTOR_LU;
145263b91edcSBarry Smith   a->row        = row;
145363b91edcSBarry Smith   a->col        = col;
145463b91edcSBarry Smith 
145594a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
14560452661fSBarry Smith     a->solve_work = (Scalar *) PetscMalloc( (a->m+1)*sizeof(Scalar)); CHKPTRQ(a->solve_work);
145794a9d846SBarry Smith   }
145863b91edcSBarry Smith 
145908480c60SBarry Smith   if (!a->diag) {
146008480c60SBarry Smith     ierr = MatMarkDiag_SeqAIJ(inA); CHKERRQ(ierr);
146163b91edcSBarry Smith   }
146263b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA); CHKERRQ(ierr);
1463a871dcd8SBarry Smith   return 0;
1464a871dcd8SBarry Smith }
1465a871dcd8SBarry Smith 
1466f0b747eeSBarry Smith #include "pinclude/plapack.h"
14675615d1e5SSatish Balay #undef __FUNC__
14685615d1e5SSatish Balay #define __FUNC__ "MatScale_SeqAIJ"
14698f6be9afSLois Curfman McInnes int MatScale_SeqAIJ(Scalar *alpha,Mat inA)
1470f0b747eeSBarry Smith {
1471f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) inA->data;
1472f0b747eeSBarry Smith   int        one = 1;
1473f0b747eeSBarry Smith   BLscal_( &a->nz, alpha, a->a, &one );
1474f0b747eeSBarry Smith   PLogFlops(a->nz);
1475f0b747eeSBarry Smith   return 0;
1476f0b747eeSBarry Smith }
1477f0b747eeSBarry Smith 
14785615d1e5SSatish Balay #undef __FUNC__
14795615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrices_SeqAIJ"
14808f6be9afSLois Curfman McInnes int MatGetSubMatrices_SeqAIJ(Mat A,int n, IS *irow,IS *icol,MatGetSubMatrixCall scall,
1481cddf8d76SBarry Smith                                     Mat **B)
1482cddf8d76SBarry Smith {
1483cddf8d76SBarry Smith   int ierr,i;
1484cddf8d76SBarry Smith 
1485cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
14860452661fSBarry Smith     *B = (Mat *) PetscMalloc( (n+1)*sizeof(Mat) ); CHKPTRQ(*B);
1487cddf8d76SBarry Smith   }
1488cddf8d76SBarry Smith 
1489cddf8d76SBarry Smith   for ( i=0; i<n; i++ ) {
1490905e6a2fSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],scall,&(*B)[i]);CHKERRQ(ierr);
1491cddf8d76SBarry Smith   }
1492cddf8d76SBarry Smith   return 0;
1493cddf8d76SBarry Smith }
1494cddf8d76SBarry Smith 
14955615d1e5SSatish Balay #undef __FUNC__
14965615d1e5SSatish Balay #define __FUNC__ "MatGetBlockSize_SeqAIJ"
14978f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A, int *bs)
14985a838052SSatish Balay {
14995a838052SSatish Balay   *bs = 1;
15005a838052SSatish Balay   return 0;
15015a838052SSatish Balay }
15025a838052SSatish Balay 
15035615d1e5SSatish Balay #undef __FUNC__
15045615d1e5SSatish Balay #define __FUNC__ "MatIncreaseOverlap_SeqAIJ"
15058f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A, int is_max, IS *is, int ov)
15064dcbc457SBarry Smith {
1507e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
150806763907SSatish Balay   int        shift, row, i,j,k,l,m,n, *idx,ierr, *nidx, isz, val;
15098a047759SSatish Balay   int        start, end, *ai, *aj;
151006763907SSatish Balay   char       *table;
15118a047759SSatish Balay   shift = a->indexshift;
1512e4d965acSSatish Balay   m     = a->m;
1513e4d965acSSatish Balay   ai    = a->i;
15148a047759SSatish Balay   aj    = a->j+shift;
15158a047759SSatish Balay 
1516e3372554SBarry Smith   if (ov < 0)  SETERRQ(1,0,"illegal overlap value used");
151706763907SSatish Balay 
151806763907SSatish Balay   table = (char *) PetscMalloc((m/BITSPERBYTE +1)*sizeof(char)); CHKPTRQ(table);
151906763907SSatish Balay   nidx  = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(nidx);
152006763907SSatish Balay 
1521e4d965acSSatish Balay   for ( i=0; i<is_max; i++ ) {
1522b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1523e4d965acSSatish Balay     isz  = 0;
152406763907SSatish Balay     PetscMemzero(table,(m/BITSPERBYTE +1)*sizeof(char));
1525e4d965acSSatish Balay 
1526e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
15274dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);  CHKERRQ(ierr);
152877c4ece6SBarry Smith     ierr = ISGetSize(is[i],&n);  CHKERRQ(ierr);
1529e4d965acSSatish Balay 
1530dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1531e4d965acSSatish Balay     for ( j=0; j<n ; ++j){
153206763907SSatish Balay       if(!BT_LOOKUP(table, idx[j])) { nidx[isz++] = idx[j];}
15334dcbc457SBarry Smith     }
153406763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);  CHKERRQ(ierr);
153506763907SSatish Balay     ierr = ISDestroy(is[i]); CHKERRQ(ierr);
1536e4d965acSSatish Balay 
153704a348a9SBarry Smith     k = 0;
153804a348a9SBarry Smith     for ( j=0; j<ov; j++){ /* for each overlap */
153904a348a9SBarry Smith       n = isz;
154006763907SSatish Balay       for ( ; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1541e4d965acSSatish Balay         row   = nidx[k];
1542e4d965acSSatish Balay         start = ai[row];
1543e4d965acSSatish Balay         end   = ai[row+1];
154404a348a9SBarry Smith         for ( l = start; l<end ; l++){
15458a047759SSatish Balay           val = aj[l] + shift;
154606763907SSatish Balay           if (!BT_LOOKUP(table,val)) {nidx[isz++] = val;}
1547e4d965acSSatish Balay         }
1548e4d965acSSatish Balay       }
1549e4d965acSSatish Balay     }
1550029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF, isz, nidx, (is+i)); CHKERRQ(ierr);
1551e4d965acSSatish Balay   }
155204a348a9SBarry Smith   PetscFree(table);
155306763907SSatish Balay   PetscFree(nidx);
1554e4d965acSSatish Balay   return 0;
15554dcbc457SBarry Smith }
155617ab2063SBarry Smith 
15570513a670SBarry Smith /* -------------------------------------------------------------- */
15580513a670SBarry Smith #undef __FUNC__
15590513a670SBarry Smith #define __FUNC__ "MatPermute_SeqAIJ"
15600513a670SBarry Smith int MatPermute_SeqAIJ(Mat A, IS rowp, IS colp, Mat *B)
15610513a670SBarry Smith {
15620513a670SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
15630513a670SBarry Smith   Scalar     *vwork;
15640513a670SBarry Smith   int        i, ierr, nz, m = a->m, n = a->n, *cwork;
15650513a670SBarry Smith   int        *row,*col,*cnew,j,*lens;
156656cd22aeSBarry Smith   IS         icolp,irowp;
15670513a670SBarry Smith 
156856cd22aeSBarry Smith   ierr = ISInvertPermutation(rowp,&irowp); CHKERRQ(ierr);
156956cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row); CHKERRQ(ierr);
157056cd22aeSBarry Smith   ierr = ISInvertPermutation(colp,&icolp); CHKERRQ(ierr);
157156cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col); CHKERRQ(ierr);
15720513a670SBarry Smith 
15730513a670SBarry Smith   /* determine lengths of permuted rows */
15740513a670SBarry Smith   lens = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(lens);
15750513a670SBarry Smith   for (i=0; i<m; i++ ) {
15760513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
15770513a670SBarry Smith   }
15780513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
15790513a670SBarry Smith   PetscFree(lens);
15800513a670SBarry Smith 
15810513a670SBarry Smith   cnew = (int *) PetscMalloc( n*sizeof(int) ); CHKPTRQ(cnew);
15820513a670SBarry Smith   for (i=0; i<m; i++) {
15830513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr);
15840513a670SBarry Smith     for (j=0; j<nz; j++ ) { cnew[j] = col[cwork[j]];}
15850513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES); CHKERRQ(ierr);
15860513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr);
15870513a670SBarry Smith   }
15880513a670SBarry Smith   PetscFree(cnew);
15890513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
15900513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
159156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row); CHKERRQ(ierr);
159256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col); CHKERRQ(ierr);
159356cd22aeSBarry Smith   ierr = ISDestroy(irowp); CHKERRQ(ierr);
159456cd22aeSBarry Smith   ierr = ISDestroy(icolp); CHKERRQ(ierr);
15950513a670SBarry Smith   return 0;
15960513a670SBarry Smith }
15970513a670SBarry Smith 
15985615d1e5SSatish Balay #undef __FUNC__
15995615d1e5SSatish Balay #define __FUNC__ "MatPrintHelp_SeqAIJ"
1600682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1601682d7d0cSBarry Smith {
1602682d7d0cSBarry Smith   static int called = 0;
1603682d7d0cSBarry Smith   MPI_Comm   comm = A->comm;
1604682d7d0cSBarry Smith 
1605682d7d0cSBarry Smith   if (called) return 0; else called = 1;
160677c4ece6SBarry Smith   PetscPrintf(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");
16070f665d81SLois Curfman McInnes   PetscPrintf(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");
16080f665d81SLois Curfman McInnes   PetscPrintf(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");
16090f665d81SLois Curfman McInnes   PetscPrintf(comm,"  -mat_aij_no_inode: Do not use inodes\n");
16100f665d81SLois Curfman McInnes   PetscPrintf(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");
1611682d7d0cSBarry Smith #if defined(HAVE_ESSL)
16120f665d81SLois Curfman McInnes   PetscPrintf(comm,"  -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");
1613682d7d0cSBarry Smith #endif
1614682d7d0cSBarry Smith   return 0;
1615682d7d0cSBarry Smith }
16168f6be9afSLois Curfman McInnes extern int MatEqual_SeqAIJ(Mat A,Mat B, PetscTruth* flg);
1617a93ec695SBarry Smith extern int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
1618a93ec695SBarry Smith extern int MatColoringPatch_SeqAIJ(Mat,int,int *,ISColoring *);
1619a93ec695SBarry Smith 
1620682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
162117ab2063SBarry Smith static struct _MatOps MatOps = {MatSetValues_SeqAIJ,
162217ab2063SBarry Smith        MatGetRow_SeqAIJ,MatRestoreRow_SeqAIJ,
1623416022c9SBarry Smith        MatMult_SeqAIJ,MatMultAdd_SeqAIJ,
1624416022c9SBarry Smith        MatMultTrans_SeqAIJ,MatMultTransAdd_SeqAIJ,
162517ab2063SBarry Smith        MatSolve_SeqAIJ,MatSolveAdd_SeqAIJ,
162617ab2063SBarry Smith        MatSolveTrans_SeqAIJ,MatSolveTransAdd_SeqAIJ,
162717ab2063SBarry Smith        MatLUFactor_SeqAIJ,0,
162817ab2063SBarry Smith        MatRelax_SeqAIJ,
162917ab2063SBarry Smith        MatTranspose_SeqAIJ,
16307264ac53SSatish Balay        MatGetInfo_SeqAIJ,MatEqual_SeqAIJ,
1631f0b747eeSBarry Smith        MatGetDiagonal_SeqAIJ,MatDiagonalScale_SeqAIJ,MatNorm_SeqAIJ,
163217ab2063SBarry Smith        0,MatAssemblyEnd_SeqAIJ,
163317ab2063SBarry Smith        MatCompress_SeqAIJ,
163417ab2063SBarry Smith        MatSetOption_SeqAIJ,MatZeroEntries_SeqAIJ,MatZeroRows_SeqAIJ,
163517ab2063SBarry Smith        MatLUFactorSymbolic_SeqAIJ,MatLUFactorNumeric_SeqAIJ,0,0,
163617ab2063SBarry Smith        MatGetSize_SeqAIJ,MatGetSize_SeqAIJ,MatGetOwnershipRange_SeqAIJ,
163717ab2063SBarry Smith        MatILUFactorSymbolic_SeqAIJ,0,
163894a9d846SBarry Smith        0,0,
16393d1612f7SBarry Smith        MatConvertSameType_SeqAIJ,0,0,
1640cddf8d76SBarry Smith        MatILUFactor_SeqAIJ,0,0,
16417eb43aa7SLois Curfman McInnes        MatGetSubMatrices_SeqAIJ,MatIncreaseOverlap_SeqAIJ,
1642682d7d0cSBarry Smith        MatGetValues_SeqAIJ,0,
1643f0b747eeSBarry Smith        MatPrintHelp_SeqAIJ,
16445a838052SSatish Balay        MatScale_SeqAIJ,0,0,
16456945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
16466945ee14SBarry Smith        MatGetBlockSize_SeqAIJ,
16473b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
16483b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
16493b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
1650a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
1651a93ec695SBarry Smith        MatFDColoringCreate_SeqAIJ,
16520513a670SBarry Smith        MatColoringPatch_SeqAIJ,
16530513a670SBarry Smith        0,
16540513a670SBarry Smith        MatPermute_SeqAIJ};
165517ab2063SBarry Smith 
165617ab2063SBarry Smith extern int MatUseSuperLU_SeqAIJ(Mat);
165717ab2063SBarry Smith extern int MatUseEssl_SeqAIJ(Mat);
165817ab2063SBarry Smith extern int MatUseDXML_SeqAIJ(Mat);
165917ab2063SBarry Smith 
16605615d1e5SSatish Balay #undef __FUNC__
16615615d1e5SSatish Balay #define __FUNC__ "MatCreateSeqAIJ"
166217ab2063SBarry Smith /*@C
1663682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
16640d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
16656e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
16662bd5e0b2SLois Curfman McInnes    (or the array nzz).  By setting these parameters accurately, performance
16672bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
166817ab2063SBarry Smith 
166917ab2063SBarry Smith    Input Parameters:
1670029af93fSBarry Smith .  comm - MPI communicator, set to PETSC_COMM_SELF
167117ab2063SBarry Smith .  m - number of rows
167217ab2063SBarry Smith .  n - number of columns
167317ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
16742bd5e0b2SLois Curfman McInnes .  nzz - array containing the number of nonzeros in the various rows
16752bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
167617ab2063SBarry Smith 
167717ab2063SBarry Smith    Output Parameter:
1678416022c9SBarry Smith .  A - the matrix
167917ab2063SBarry Smith 
168017ab2063SBarry Smith    Notes:
168117ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
168217ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
16830002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
168444cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
168517ab2063SBarry Smith 
168617ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
1687a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
16883d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
16896da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
169017ab2063SBarry Smith 
1691682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
1692*4fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
1693682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
16946c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
16956c7ebb05SLois Curfman McInnes 
16966c7ebb05SLois Curfman McInnes    Options Database Keys:
16976c7ebb05SLois Curfman McInnes $    -mat_aij_no_inode  - Do not use inodes
16986e62573dSLois Curfman McInnes $    -mat_aij_inode_limit <limit> - Set inode limit.
16996e62573dSLois Curfman McInnes $        (max limit=5)
17006e62573dSLois Curfman McInnes $    -mat_aij_oneindex - Internally use indexing starting at 1
17016e62573dSLois Curfman McInnes $        rather than 0.  Note: When calling MatSetValues(),
17026e62573dSLois Curfman McInnes $        the user still MUST index entries starting at 0!
170317ab2063SBarry Smith 
170417ab2063SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues()
170517ab2063SBarry Smith @*/
1706416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz, Mat *A)
170717ab2063SBarry Smith {
1708416022c9SBarry Smith   Mat        B;
1709416022c9SBarry Smith   Mat_SeqAIJ *b;
17106945ee14SBarry Smith   int        i, len, ierr, flg,size;
17116945ee14SBarry Smith 
17126945ee14SBarry Smith   MPI_Comm_size(comm,&size);
1713e3372554SBarry Smith   if (size > 1) SETERRQ(1,0,"Comm must be of size 1");
1714d5d45c9bSBarry Smith 
1715416022c9SBarry Smith   *A                  = 0;
1716d4bb536fSBarry Smith   PetscHeaderCreate(B,_p_Mat,MAT_COOKIE,MATSEQAIJ,comm,MatDestroy,MatView);
1717416022c9SBarry Smith   PLogObjectCreate(B);
17180452661fSBarry Smith   B->data             = (void *) (b = PetscNew(Mat_SeqAIJ)); CHKPTRQ(b);
171944cd7ae7SLois Curfman McInnes   PetscMemzero(b,sizeof(Mat_SeqAIJ));
1720416022c9SBarry Smith   PetscMemcpy(&B->ops,&MatOps,sizeof(struct _MatOps));
1721416022c9SBarry Smith   B->destroy          = MatDestroy_SeqAIJ;
1722416022c9SBarry Smith   B->view             = MatView_SeqAIJ;
1723416022c9SBarry Smith   B->factor           = 0;
1724416022c9SBarry Smith   B->lupivotthreshold = 1.0;
172590f02eecSBarry Smith   B->mapping          = 0;
17267a743949SBarry Smith   ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold,
172769957df2SSatish Balay                           &flg); CHKERRQ(ierr);
17287a743949SBarry Smith   b->ilu_preserve_row_sums = PETSC_FALSE;
17297a743949SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums",
17307a743949SBarry Smith                         (int*) &b->ilu_preserve_row_sums); CHKERRQ(ierr);
1731416022c9SBarry Smith   b->row              = 0;
1732416022c9SBarry Smith   b->col              = 0;
1733416022c9SBarry Smith   b->indexshift       = 0;
1734b810aeb4SBarry Smith   b->reallocs         = 0;
173569957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_oneindex", &flg); CHKERRQ(ierr);
173669957df2SSatish Balay   if (flg) b->indexshift = -1;
173717ab2063SBarry Smith 
173844cd7ae7SLois Curfman McInnes   b->m = m; B->m = m; B->M = m;
173944cd7ae7SLois Curfman McInnes   b->n = n; B->n = n; B->N = n;
17400452661fSBarry Smith   b->imax = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(b->imax);
1741b4fd4287SBarry Smith   if (nnz == PETSC_NULL) {
17427b8455f0SLois Curfman McInnes     if (nz == PETSC_DEFAULT) nz = 10;
17437b8455f0SLois Curfman McInnes     else if (nz <= 0)        nz = 1;
1744416022c9SBarry Smith     for ( i=0; i<m; i++ ) b->imax[i] = nz;
174517ab2063SBarry Smith     nz = nz*m;
174617ab2063SBarry Smith   }
174717ab2063SBarry Smith   else {
174817ab2063SBarry Smith     nz = 0;
1749416022c9SBarry Smith     for ( i=0; i<m; i++ ) {b->imax[i] = nnz[i]; nz += nnz[i];}
175017ab2063SBarry Smith   }
175117ab2063SBarry Smith 
175217ab2063SBarry Smith   /* allocate the matrix space */
1753416022c9SBarry Smith   len     = nz*(sizeof(int) + sizeof(Scalar)) + (b->m+1)*sizeof(int);
17540452661fSBarry Smith   b->a  = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
1755416022c9SBarry Smith   b->j  = (int *) (b->a + nz);
1756cddf8d76SBarry Smith   PetscMemzero(b->j,nz*sizeof(int));
1757416022c9SBarry Smith   b->i  = b->j + nz;
1758416022c9SBarry Smith   b->singlemalloc = 1;
175917ab2063SBarry Smith 
1760416022c9SBarry Smith   b->i[0] = -b->indexshift;
176117ab2063SBarry Smith   for (i=1; i<m+1; i++) {
1762416022c9SBarry Smith     b->i[i] = b->i[i-1] + b->imax[i-1];
176317ab2063SBarry Smith   }
176417ab2063SBarry Smith 
1765416022c9SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
17660452661fSBarry Smith   b->ilen = (int *) PetscMalloc((m+1)*sizeof(int));
1767f09e8eb9SSatish Balay   PLogObjectMemory(B,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
1768416022c9SBarry Smith   for ( i=0; i<b->m; i++ ) { b->ilen[i] = 0;}
176917ab2063SBarry Smith 
1770416022c9SBarry Smith   b->nz               = 0;
1771416022c9SBarry Smith   b->maxnz            = nz;
1772416022c9SBarry Smith   b->sorted           = 0;
1773416022c9SBarry Smith   b->roworiented      = 1;
1774416022c9SBarry Smith   b->nonew            = 0;
1775416022c9SBarry Smith   b->diag             = 0;
1776416022c9SBarry Smith   b->solve_work       = 0;
177771bd300dSLois Curfman McInnes   b->spptr            = 0;
1778754ec7b1SSatish Balay   b->inode.node_count = 0;
1779754ec7b1SSatish Balay   b->inode.size       = 0;
17806c7ebb05SLois Curfman McInnes   b->inode.limit      = 5;
17816c7ebb05SLois Curfman McInnes   b->inode.max_limit  = 5;
17824e220ebcSLois Curfman McInnes   B->info.nz_unneeded = (double)b->maxnz;
178317ab2063SBarry Smith 
1784416022c9SBarry Smith   *A = B;
17854e220ebcSLois Curfman McInnes 
17864b14c69eSBarry Smith   /*  SuperLU is not currently supported through PETSc */
17874b14c69eSBarry Smith #if defined(HAVE_SUPERLU)
178869957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_superlu", &flg); CHKERRQ(ierr);
178969957df2SSatish Balay   if (flg) { ierr = MatUseSuperLU_SeqAIJ(B); CHKERRQ(ierr); }
17904b14c69eSBarry Smith #endif
179169957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_essl", &flg); CHKERRQ(ierr);
179269957df2SSatish Balay   if (flg) { ierr = MatUseEssl_SeqAIJ(B); CHKERRQ(ierr); }
179369957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_dxml", &flg); CHKERRQ(ierr);
179469957df2SSatish Balay   if (flg) {
1795e3372554SBarry Smith     if (!b->indexshift) SETERRQ(1,0,"need -mat_aij_oneindex with -mat_aij_dxml");
1796416022c9SBarry Smith     ierr = MatUseDXML_SeqAIJ(B); CHKERRQ(ierr);
179717ab2063SBarry Smith   }
179869957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr);
179969957df2SSatish Balay   if (flg) {ierr = MatPrintHelp(B); CHKERRQ(ierr); }
180017ab2063SBarry Smith   return 0;
180117ab2063SBarry Smith }
180217ab2063SBarry Smith 
18035615d1e5SSatish Balay #undef __FUNC__
18045615d1e5SSatish Balay #define __FUNC__ "MatConvertSameType_SeqAIJ"
18053d1612f7SBarry Smith int MatConvertSameType_SeqAIJ(Mat A,Mat *B,int cpvalues)
180617ab2063SBarry Smith {
1807416022c9SBarry Smith   Mat        C;
1808416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ *) A->data;
180908480c60SBarry Smith   int        i,len, m = a->m,shift = a->indexshift;
181017ab2063SBarry Smith 
18114043dd9cSLois Curfman McInnes   *B = 0;
1812d4bb536fSBarry Smith   PetscHeaderCreate(C,_p_Mat,MAT_COOKIE,MATSEQAIJ,A->comm,MatDestroy,MatView);
1813416022c9SBarry Smith   PLogObjectCreate(C);
18140452661fSBarry Smith   C->data       = (void *) (c = PetscNew(Mat_SeqAIJ)); CHKPTRQ(c);
181541c01911SSatish Balay   PetscMemcpy(&C->ops,&A->ops,sizeof(struct _MatOps));
1816416022c9SBarry Smith   C->destroy    = MatDestroy_SeqAIJ;
1817416022c9SBarry Smith   C->view       = MatView_SeqAIJ;
1818416022c9SBarry Smith   C->factor     = A->factor;
1819416022c9SBarry Smith   c->row        = 0;
1820416022c9SBarry Smith   c->col        = 0;
1821416022c9SBarry Smith   c->indexshift = shift;
1822c456f294SBarry Smith   C->assembled  = PETSC_TRUE;
182317ab2063SBarry Smith 
182444cd7ae7SLois Curfman McInnes   c->m = C->m   = a->m;
182544cd7ae7SLois Curfman McInnes   c->n = C->n   = a->n;
182644cd7ae7SLois Curfman McInnes   C->M          = a->m;
182744cd7ae7SLois Curfman McInnes   C->N          = a->n;
182817ab2063SBarry Smith 
18290452661fSBarry Smith   c->imax       = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(c->imax);
18300452661fSBarry Smith   c->ilen       = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(c->ilen);
183117ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
1832416022c9SBarry Smith     c->imax[i] = a->imax[i];
1833416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
183417ab2063SBarry Smith   }
183517ab2063SBarry Smith 
183617ab2063SBarry Smith   /* allocate the matrix space */
1837416022c9SBarry Smith   c->singlemalloc = 1;
1838416022c9SBarry Smith   len     = (m+1)*sizeof(int)+(a->i[m])*(sizeof(Scalar)+sizeof(int));
18390452661fSBarry Smith   c->a  = (Scalar *) PetscMalloc( len ); CHKPTRQ(c->a);
1840416022c9SBarry Smith   c->j  = (int *) (c->a + a->i[m] + shift);
1841416022c9SBarry Smith   c->i  = c->j + a->i[m] + shift;
1842416022c9SBarry Smith   PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));
184317ab2063SBarry Smith   if (m > 0) {
1844416022c9SBarry Smith     PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int));
184508480c60SBarry Smith     if (cpvalues == COPY_VALUES) {
1846416022c9SBarry Smith       PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(Scalar));
184717ab2063SBarry Smith     }
184808480c60SBarry Smith   }
184917ab2063SBarry Smith 
1850f09e8eb9SSatish Balay   PLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
1851416022c9SBarry Smith   c->sorted      = a->sorted;
1852416022c9SBarry Smith   c->roworiented = a->roworiented;
1853416022c9SBarry Smith   c->nonew       = a->nonew;
18547a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
185517ab2063SBarry Smith 
1856416022c9SBarry Smith   if (a->diag) {
18570452661fSBarry Smith     c->diag = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(c->diag);
1858416022c9SBarry Smith     PLogObjectMemory(C,(m+1)*sizeof(int));
185917ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
1860416022c9SBarry Smith       c->diag[i] = a->diag[i];
186117ab2063SBarry Smith     }
186217ab2063SBarry Smith   }
1863416022c9SBarry Smith   else c->diag          = 0;
18646c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
18656c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
1866754ec7b1SSatish Balay   if (a->inode.size){
1867daed632aSSatish Balay     c->inode.size       = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(c->inode.size);
1868754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
1869daed632aSSatish Balay     PetscMemcpy( c->inode.size, a->inode.size, (m+1)*sizeof(int));
1870754ec7b1SSatish Balay   } else {
1871754ec7b1SSatish Balay     c->inode.size       = 0;
1872754ec7b1SSatish Balay     c->inode.node_count = 0;
1873754ec7b1SSatish Balay   }
1874416022c9SBarry Smith   c->nz                 = a->nz;
1875416022c9SBarry Smith   c->maxnz              = a->maxnz;
1876416022c9SBarry Smith   c->solve_work         = 0;
187776dd722bSSatish Balay   c->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
1878754ec7b1SSatish Balay 
1879416022c9SBarry Smith   *B = C;
188017ab2063SBarry Smith   return 0;
188117ab2063SBarry Smith }
188217ab2063SBarry Smith 
18835615d1e5SSatish Balay #undef __FUNC__
18845615d1e5SSatish Balay #define __FUNC__ "MatLoad_SeqAIJ"
188519bcc07fSBarry Smith int MatLoad_SeqAIJ(Viewer viewer,MatType type,Mat *A)
188617ab2063SBarry Smith {
1887416022c9SBarry Smith   Mat_SeqAIJ   *a;
1888416022c9SBarry Smith   Mat          B;
188917699dbbSLois Curfman McInnes   int          i, nz, ierr, fd, header[4],size,*rowlengths = 0,M,N,shift;
1890bcd2baecSBarry Smith   MPI_Comm     comm;
189117ab2063SBarry Smith 
189219bcc07fSBarry Smith   PetscObjectGetComm((PetscObject) viewer,&comm);
189317699dbbSLois Curfman McInnes   MPI_Comm_size(comm,&size);
1894e3372554SBarry Smith   if (size > 1) SETERRQ(1,0,"view must have one processor");
189590ace30eSBarry Smith   ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr);
189677c4ece6SBarry Smith   ierr = PetscBinaryRead(fd,header,4,BINARY_INT); CHKERRQ(ierr);
1897e3372554SBarry Smith   if (header[0] != MAT_COOKIE) SETERRQ(1,0,"not matrix object in file");
189817ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
189917ab2063SBarry Smith 
190017ab2063SBarry Smith   /* read in row lengths */
19010452661fSBarry Smith   rowlengths = (int*) PetscMalloc( M*sizeof(int) ); CHKPTRQ(rowlengths);
190277c4ece6SBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,BINARY_INT); CHKERRQ(ierr);
190317ab2063SBarry Smith 
190417ab2063SBarry Smith   /* create our matrix */
1905416022c9SBarry Smith   ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A); CHKERRQ(ierr);
1906416022c9SBarry Smith   B = *A;
1907416022c9SBarry Smith   a = (Mat_SeqAIJ *) B->data;
1908416022c9SBarry Smith   shift = a->indexshift;
190917ab2063SBarry Smith 
191017ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
191177c4ece6SBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,BINARY_INT); CHKERRQ(ierr);
191217ab2063SBarry Smith   if (shift) {
191317ab2063SBarry Smith     for ( i=0; i<nz; i++ ) {
1914416022c9SBarry Smith       a->j[i] += 1;
191517ab2063SBarry Smith     }
191617ab2063SBarry Smith   }
191717ab2063SBarry Smith 
191817ab2063SBarry Smith   /* read in nonzero values */
191977c4ece6SBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,BINARY_SCALAR); CHKERRQ(ierr);
192017ab2063SBarry Smith 
192117ab2063SBarry Smith   /* set matrix "i" values */
1922416022c9SBarry Smith   a->i[0] = -shift;
192317ab2063SBarry Smith   for ( i=1; i<= M; i++ ) {
1924416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
1925416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
192617ab2063SBarry Smith   }
19270452661fSBarry Smith   PetscFree(rowlengths);
192817ab2063SBarry Smith 
19296d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
19306d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
193117ab2063SBarry Smith   return 0;
193217ab2063SBarry Smith }
193317ab2063SBarry Smith 
19345615d1e5SSatish Balay #undef __FUNC__
19355615d1e5SSatish Balay #define __FUNC__ "MatEqual_SeqAIJ"
19368f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B, PetscTruth* flg)
19377264ac53SSatish Balay {
19387264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data, *b = (Mat_SeqAIJ *)B->data;
19397264ac53SSatish Balay 
1940e3372554SBarry Smith   if (B->type !=MATSEQAIJ)SETERRQ(1,0,"Matrices must be same type");
19417264ac53SSatish Balay 
19427264ac53SSatish Balay   /* If the  matrix dimensions are not equal, or no of nonzeros or shift */
19437264ac53SSatish Balay   if ((a->m != b->m ) || (a->n !=b->n) ||( a->nz != b->nz)||
1944bcd2baecSBarry Smith       (a->indexshift != b->indexshift)) {
194577c4ece6SBarry Smith     *flg = PETSC_FALSE; return 0;
1946bcd2baecSBarry Smith   }
19477264ac53SSatish Balay 
19487264ac53SSatish Balay   /* if the a->i are the same */
19498108c231SLois Curfman McInnes   if (PetscMemcmp(a->i,b->i,(a->m+1)*sizeof(int))) {
195077c4ece6SBarry Smith     *flg = PETSC_FALSE; return 0;
19517264ac53SSatish Balay   }
19527264ac53SSatish Balay 
19537264ac53SSatish Balay   /* if a->j are the same */
1954bcd2baecSBarry Smith   if (PetscMemcmp(a->j, b->j, (a->nz)*sizeof(int))) {
195577c4ece6SBarry Smith     *flg = PETSC_FALSE; return 0;
1956bcd2baecSBarry Smith   }
1957bcd2baecSBarry Smith 
1958bcd2baecSBarry Smith   /* if a->a are the same */
195919bcc07fSBarry Smith   if (PetscMemcmp(a->a, b->a, (a->nz)*sizeof(Scalar))) {
196077c4ece6SBarry Smith     *flg = PETSC_FALSE; return 0;
19617264ac53SSatish Balay   }
196277c4ece6SBarry Smith   *flg = PETSC_TRUE;
19637264ac53SSatish Balay   return 0;
19647264ac53SSatish Balay 
19657264ac53SSatish Balay }
1966