xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 2e8a6d31cfff42dc61c2443ddfa186a411c27ca7)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*2e8a6d31SBarry Smith static char vcid[] = "$Id: aij.c,v 1.283 1998/09/26 21:57:04 bsmith Exp bsmith $";
317ab2063SBarry Smith #endif
417ab2063SBarry Smith 
5d5d45c9bSBarry Smith /*
63369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
7d5d45c9bSBarry Smith   matrix storage format.
8d5d45c9bSBarry Smith */
93369ce9aSBarry Smith 
103369ce9aSBarry Smith #include "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"
158d195f9aSBarry Smith #include "src/inline/dot.h"
16eeb667a2SSatish Balay #include "bitarray.h"
1717ab2063SBarry Smith 
18a2ce50c7SBarry Smith /*
19a2ce50c7SBarry Smith     Basic AIJ format ILU based on drop tolerance
20a2ce50c7SBarry Smith */
215615d1e5SSatish Balay #undef __FUNC__
225615d1e5SSatish Balay #define __FUNC__ "MatILUDTFactor_SeqAIJ"
23a2ce50c7SBarry Smith int MatILUDTFactor_SeqAIJ(Mat A,double dt,int maxnz,IS row,IS col,Mat *fact)
24a2ce50c7SBarry Smith {
25a2ce50c7SBarry Smith   /* Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; */
26a2ce50c7SBarry Smith   int        ierr = 1;
27a2ce50c7SBarry Smith 
283a40ed3dSBarry Smith   PetscFunctionBegin;
29e3372554SBarry Smith   SETERRQ(ierr,0,"Not implemented");
30d64ed03dSBarry Smith #if !defined(USE_PETSC_DEBUG)
31d64ed03dSBarry Smith   PetscFunctionReturn(0);
32d64ed03dSBarry Smith #endif
33a2ce50c7SBarry Smith }
34a2ce50c7SBarry Smith 
35bcd2baecSBarry Smith extern int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**);
3617ab2063SBarry Smith 
375615d1e5SSatish Balay #undef __FUNC__
385615d1e5SSatish Balay #define __FUNC__ "MatGetRowIJ_SeqAIJ"
398f6be9afSLois Curfman McInnes int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int **ia,int **ja,
406945ee14SBarry Smith                            PetscTruth *done)
4117ab2063SBarry Smith {
42416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
436945ee14SBarry Smith   int        ierr,i,ishift;
4417ab2063SBarry Smith 
453a40ed3dSBarry Smith   PetscFunctionBegin;
4631625ec5SSatish Balay   *m     = A->m;
473a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
486945ee14SBarry Smith   ishift = a->indexshift;
496945ee14SBarry Smith   if (symmetric) {
5031625ec5SSatish Balay     ierr = MatToSymmetricIJ_SeqAIJ(a->m,a->i,a->j,ishift,oshift,ia,ja); CHKERRQ(ierr);
516945ee14SBarry Smith   } else if (oshift == 0 && ishift == -1) {
5231625ec5SSatish Balay     int nz = a->i[a->m];
533b2fbd54SBarry Smith     /* malloc space and  subtract 1 from i and j indices */
5431625ec5SSatish Balay     *ia = (int *) PetscMalloc( (a->m+1)*sizeof(int) ); CHKPTRQ(*ia);
553b2fbd54SBarry Smith     *ja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(*ja);
563b2fbd54SBarry Smith     for ( i=0; i<nz; i++ ) (*ja)[i] = a->j[i] - 1;
5731625ec5SSatish Balay     for ( i=0; i<a->m+1; i++ ) (*ia)[i] = a->i[i] - 1;
586945ee14SBarry Smith   } else if (oshift == 1 && ishift == 0) {
5931625ec5SSatish Balay     int nz = a->i[a->m] + 1;
603b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
6131625ec5SSatish Balay     *ia = (int *) PetscMalloc( (a->m+1)*sizeof(int) ); CHKPTRQ(*ia);
623b2fbd54SBarry Smith     *ja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(*ja);
633b2fbd54SBarry Smith     for ( i=0; i<nz; i++ ) (*ja)[i] = a->j[i] + 1;
6431625ec5SSatish Balay     for ( i=0; i<a->m+1; i++ ) (*ia)[i] = a->i[i] + 1;
656945ee14SBarry Smith   } else {
666945ee14SBarry Smith     *ia = a->i; *ja = a->j;
67a2ce50c7SBarry Smith   }
68a2ce50c7SBarry Smith 
693a40ed3dSBarry Smith   PetscFunctionReturn(0);
70a2744918SBarry Smith }
71a2744918SBarry Smith 
725615d1e5SSatish Balay #undef __FUNC__
735615d1e5SSatish Balay #define __FUNC__ "MatRestoreRowIJ_SeqAIJ"
748f6be9afSLois Curfman McInnes int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,
756945ee14SBarry Smith                                PetscTruth *done)
766945ee14SBarry Smith {
776945ee14SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
783b2fbd54SBarry Smith   int        ishift = a->indexshift;
796945ee14SBarry Smith 
803a40ed3dSBarry Smith   PetscFunctionBegin;
813a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
823b2fbd54SBarry Smith   if (symmetric || (oshift == 0 && ishift == -1) || (oshift == 1 && ishift == 0)) {
836945ee14SBarry Smith     PetscFree(*ia);
846945ee14SBarry Smith     PetscFree(*ja);
85bcd2baecSBarry Smith   }
863a40ed3dSBarry Smith   PetscFunctionReturn(0);
8717ab2063SBarry Smith }
8817ab2063SBarry Smith 
895615d1e5SSatish Balay #undef __FUNC__
905615d1e5SSatish Balay #define __FUNC__ "MatGetColumnIJ_SeqAIJ"
9143a90d84SBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int **ia,int **ja,
923b2fbd54SBarry Smith                            PetscTruth *done)
933b2fbd54SBarry Smith {
943b2fbd54SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
95a93ec695SBarry Smith   int        ierr,i,ishift = a->indexshift,*collengths,*cia,*cja,n = A->n,m = A->m;
96a93ec695SBarry Smith   int        nz = a->i[m]+ishift,row,*jj,mr,col;
973b2fbd54SBarry Smith 
983a40ed3dSBarry Smith   PetscFunctionBegin;
993b2fbd54SBarry Smith   *nn     = A->n;
1003a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1013b2fbd54SBarry Smith   if (symmetric) {
102179192dfSSatish Balay     ierr = MatToSymmetricIJ_SeqAIJ(a->m,a->i,a->j,ishift,oshift,ia,ja); CHKERRQ(ierr);
1033b2fbd54SBarry Smith   } else {
10461d2ded1SBarry Smith     collengths = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(collengths);
1053b2fbd54SBarry Smith     PetscMemzero(collengths,n*sizeof(int));
1063b2fbd54SBarry Smith     cia        = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(cia);
107a93ec695SBarry Smith     cja        = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(cja);
1083b2fbd54SBarry Smith     jj = a->j;
1093b2fbd54SBarry Smith     for ( i=0; i<nz; i++ ) {
1103b2fbd54SBarry Smith       collengths[jj[i] + ishift]++;
1113b2fbd54SBarry Smith     }
1123b2fbd54SBarry Smith     cia[0] = oshift;
1133b2fbd54SBarry Smith     for ( i=0; i<n; i++) {
1143b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1153b2fbd54SBarry Smith     }
1163b2fbd54SBarry Smith     PetscMemzero(collengths,n*sizeof(int));
1173b2fbd54SBarry Smith     jj = a->j;
118a93ec695SBarry Smith     for ( row=0; row<m; row++ ) {
119a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
120a93ec695SBarry Smith       for ( i=0; i<mr; i++ ) {
1213b2fbd54SBarry Smith         col = *jj++ + ishift;
1223b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1233b2fbd54SBarry Smith       }
1243b2fbd54SBarry Smith     }
1253b2fbd54SBarry Smith     PetscFree(collengths);
1263b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1273b2fbd54SBarry Smith   }
1283b2fbd54SBarry Smith 
1293a40ed3dSBarry Smith   PetscFunctionReturn(0);
1303b2fbd54SBarry Smith }
1313b2fbd54SBarry Smith 
1325615d1e5SSatish Balay #undef __FUNC__
1335615d1e5SSatish Balay #define __FUNC__ "MatRestoreColumnIJ_SeqAIJ"
13443a90d84SBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,
1353b2fbd54SBarry Smith                                      int **ja,PetscTruth *done)
1363b2fbd54SBarry Smith {
1373a40ed3dSBarry Smith   PetscFunctionBegin;
1383a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1393b2fbd54SBarry Smith 
1403b2fbd54SBarry Smith   PetscFree(*ia);
1413b2fbd54SBarry Smith   PetscFree(*ja);
1423b2fbd54SBarry Smith 
1433a40ed3dSBarry Smith   PetscFunctionReturn(0);
1443b2fbd54SBarry Smith }
1453b2fbd54SBarry Smith 
146227d817aSBarry Smith #define CHUNKSIZE   15
14717ab2063SBarry Smith 
1485615d1e5SSatish Balay #undef __FUNC__
1495615d1e5SSatish Balay #define __FUNC__ "MatSetValues_SeqAIJ"
15061d2ded1SBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v,InsertMode is)
15117ab2063SBarry Smith {
152416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
153416022c9SBarry Smith   int        *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax, N, sorted = a->sorted;
1544b0e389bSBarry Smith   int        *imax = a->imax, *ai = a->i, *ailen = a->ilen,roworiented = a->roworiented;
155d5d45c9bSBarry Smith   int        *aj = a->j, nonew = a->nonew,shift = a->indexshift;
156416022c9SBarry Smith   Scalar     *ap,value, *aa = a->a;
15717ab2063SBarry Smith 
1583a40ed3dSBarry Smith   PetscFunctionBegin;
15917ab2063SBarry Smith   for ( k=0; k<m; k++ ) { /* loop over added rows */
160416022c9SBarry Smith     row  = im[k];
1613a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g)
162a8c6a408SBarry Smith     if (row < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row");
163a8c6a408SBarry Smith     if (row >= a->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large");
1643b2fbd54SBarry Smith #endif
16517ab2063SBarry Smith     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
16617ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
167416022c9SBarry Smith     low = 0;
16817ab2063SBarry Smith     for ( l=0; l<n; l++ ) { /* loop over added columns */
1693a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g)
170a8c6a408SBarry Smith       if (in[l] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column");
171a8c6a408SBarry Smith       if (in[l] >= a->n) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");
1723b2fbd54SBarry Smith #endif
1734b0e389bSBarry Smith       col = in[l] - shift;
1744b0e389bSBarry Smith       if (roworiented) {
1754b0e389bSBarry Smith         value = *v++;
176bef8e0ddSBarry Smith       } else {
1774b0e389bSBarry Smith         value = v[k + l*m];
1784b0e389bSBarry Smith       }
179416022c9SBarry Smith       if (!sorted) low = 0; high = nrow;
180416022c9SBarry Smith       while (high-low > 5) {
181416022c9SBarry Smith         t = (low+high)/2;
182416022c9SBarry Smith         if (rp[t] > col) high = t;
183416022c9SBarry Smith         else             low  = t;
18417ab2063SBarry Smith       }
185416022c9SBarry Smith       for ( i=low; i<high; i++ ) {
18617ab2063SBarry Smith         if (rp[i] > col) break;
18717ab2063SBarry Smith         if (rp[i] == col) {
188416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
18917ab2063SBarry Smith           else                  ap[i] = value;
19017ab2063SBarry Smith           goto noinsert;
19117ab2063SBarry Smith         }
19217ab2063SBarry Smith       }
193c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
194a8c6a408SBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix");
19517ab2063SBarry Smith       if (nrow >= rmax) {
19617ab2063SBarry Smith         /* there is no extra room in row, therefore enlarge */
197416022c9SBarry Smith         int    new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j;
19817ab2063SBarry Smith         Scalar *new_a;
19917ab2063SBarry Smith 
200a8c6a408SBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix");
20196854ed6SLois Curfman McInnes 
20217ab2063SBarry Smith         /* malloc new storage space */
203416022c9SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int);
2040452661fSBarry Smith         new_a   = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a);
20517ab2063SBarry Smith         new_j   = (int *) (new_a + new_nz);
20617ab2063SBarry Smith         new_i   = new_j + new_nz;
20717ab2063SBarry Smith 
20817ab2063SBarry Smith         /* copy over old data into new slots */
20917ab2063SBarry Smith         for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = ai[ii];}
210416022c9SBarry Smith         for ( ii=row+1; ii<a->m+1; ii++ ) {new_i[ii] = ai[ii]+CHUNKSIZE;}
211416022c9SBarry Smith         PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));
212416022c9SBarry Smith         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift);
213416022c9SBarry Smith         PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow,
21417ab2063SBarry Smith                                                            len*sizeof(int));
215416022c9SBarry Smith         PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar));
216416022c9SBarry Smith         PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow,
21717ab2063SBarry Smith                                                            len*sizeof(Scalar));
21817ab2063SBarry Smith         /* free up old matrix storage */
2190452661fSBarry Smith         PetscFree(a->a);
2200452661fSBarry Smith         if (!a->singlemalloc) {PetscFree(a->i);PetscFree(a->j);}
221416022c9SBarry Smith         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;
222416022c9SBarry Smith         a->singlemalloc = 1;
22317ab2063SBarry Smith 
22417ab2063SBarry Smith         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
225416022c9SBarry Smith         rmax = imax[row] = imax[row] + CHUNKSIZE;
226416022c9SBarry Smith         PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar)));
227416022c9SBarry Smith         a->maxnz += CHUNKSIZE;
228b810aeb4SBarry Smith         a->reallocs++;
22917ab2063SBarry Smith       }
230416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
231416022c9SBarry Smith       /* shift up all the later entries in this row */
232416022c9SBarry Smith       for ( ii=N; ii>=i; ii-- ) {
23317ab2063SBarry Smith         rp[ii+1] = rp[ii];
23417ab2063SBarry Smith         ap[ii+1] = ap[ii];
23517ab2063SBarry Smith       }
23617ab2063SBarry Smith       rp[i] = col;
23717ab2063SBarry Smith       ap[i] = value;
23817ab2063SBarry Smith       noinsert:;
239416022c9SBarry Smith       low = i + 1;
24017ab2063SBarry Smith     }
24117ab2063SBarry Smith     ailen[row] = nrow;
24217ab2063SBarry Smith   }
2433a40ed3dSBarry Smith   PetscFunctionReturn(0);
24417ab2063SBarry Smith }
24517ab2063SBarry Smith 
2465615d1e5SSatish Balay #undef __FUNC__
2475615d1e5SSatish Balay #define __FUNC__ "MatGetValues_SeqAIJ"
2488f6be9afSLois Curfman McInnes int MatGetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v)
2497eb43aa7SLois Curfman McInnes {
2507eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
251b49de8d1SLois Curfman McInnes   int        *rp, k, low, high, t, row, nrow, i, col, l, *aj = a->j;
2527eb43aa7SLois Curfman McInnes   int        *ai = a->i, *ailen = a->ilen, shift = a->indexshift;
2537eb43aa7SLois Curfman McInnes   Scalar     *ap, *aa = a->a, zero = 0.0;
2547eb43aa7SLois Curfman McInnes 
2553a40ed3dSBarry Smith   PetscFunctionBegin;
2567eb43aa7SLois Curfman McInnes   for ( k=0; k<m; k++ ) { /* loop over rows */
2577eb43aa7SLois Curfman McInnes     row  = im[k];
258a8c6a408SBarry Smith     if (row < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row");
259a8c6a408SBarry Smith     if (row >= a->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large");
2607eb43aa7SLois Curfman McInnes     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
2617eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2627eb43aa7SLois Curfman McInnes     for ( l=0; l<n; l++ ) { /* loop over columns */
263a8c6a408SBarry Smith       if (in[l] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column");
264a8c6a408SBarry Smith       if (in[l] >= a->n) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");
2657eb43aa7SLois Curfman McInnes       col = in[l] - shift;
2667eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2677eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2687eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2697eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2707eb43aa7SLois Curfman McInnes         else             low  = t;
2717eb43aa7SLois Curfman McInnes       }
2727eb43aa7SLois Curfman McInnes       for ( i=low; i<high; i++ ) {
2737eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2747eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
275b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2767eb43aa7SLois Curfman McInnes           goto finished;
2777eb43aa7SLois Curfman McInnes         }
2787eb43aa7SLois Curfman McInnes       }
279b49de8d1SLois Curfman McInnes       *v++ = zero;
2807eb43aa7SLois Curfman McInnes       finished:;
2817eb43aa7SLois Curfman McInnes     }
2827eb43aa7SLois Curfman McInnes   }
2833a40ed3dSBarry Smith   PetscFunctionReturn(0);
2847eb43aa7SLois Curfman McInnes }
2857eb43aa7SLois Curfman McInnes 
28617ab2063SBarry Smith 
2875615d1e5SSatish Balay #undef __FUNC__
2885615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_Binary"
289480ef9eaSBarry Smith int MatView_SeqAIJ_Binary(Mat A,Viewer viewer)
29017ab2063SBarry Smith {
291416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
292416022c9SBarry Smith   int        i, fd, *col_lens, ierr;
29317ab2063SBarry Smith 
2943a40ed3dSBarry Smith   PetscFunctionBegin;
29590ace30eSBarry Smith   ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr);
2960452661fSBarry Smith   col_lens = (int *) PetscMalloc( (4+a->m)*sizeof(int) ); CHKPTRQ(col_lens);
297416022c9SBarry Smith   col_lens[0] = MAT_COOKIE;
298416022c9SBarry Smith   col_lens[1] = a->m;
299416022c9SBarry Smith   col_lens[2] = a->n;
300416022c9SBarry Smith   col_lens[3] = a->nz;
301416022c9SBarry Smith 
302416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
303416022c9SBarry Smith   for ( i=0; i<a->m; i++ ) {
304416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
30517ab2063SBarry Smith   }
3060752156aSBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+a->m,PETSC_INT,1); CHKERRQ(ierr);
3070452661fSBarry Smith   PetscFree(col_lens);
308416022c9SBarry Smith 
309416022c9SBarry Smith   /* store column indices (zero start index) */
310416022c9SBarry Smith   if (a->indexshift) {
311416022c9SBarry Smith     for ( i=0; i<a->nz; i++ ) a->j[i]--;
31217ab2063SBarry Smith   }
3130752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,0); CHKERRQ(ierr);
314416022c9SBarry Smith   if (a->indexshift) {
315416022c9SBarry Smith     for ( i=0; i<a->nz; i++ ) a->j[i]++;
31617ab2063SBarry Smith   }
317416022c9SBarry Smith 
318416022c9SBarry Smith   /* store nonzero values */
3190752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,0); CHKERRQ(ierr);
3203a40ed3dSBarry Smith   PetscFunctionReturn(0);
32117ab2063SBarry Smith }
322416022c9SBarry Smith 
3235615d1e5SSatish Balay #undef __FUNC__
3245615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_ASCII"
325480ef9eaSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,Viewer viewer)
326416022c9SBarry Smith {
327416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ *) A->data;
328496e697eSBarry Smith   int         ierr, i,j, m = a->m, shift = a->indexshift, format, flg1,flg2;
32917ab2063SBarry Smith   FILE        *fd;
33017ab2063SBarry Smith   char        *outputname;
33117ab2063SBarry Smith 
3323a40ed3dSBarry Smith   PetscFunctionBegin;
33390ace30eSBarry Smith   ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr);
334416022c9SBarry Smith   ierr = ViewerFileGetOutputname_Private(viewer,&outputname); CHKERRQ(ierr);
33590ace30eSBarry Smith   ierr = ViewerGetFormat(viewer,&format);
336a93ec695SBarry Smith   if (format == VIEWER_FORMAT_ASCII_INFO) {
3373a40ed3dSBarry Smith     PetscFunctionReturn(0);
3383a40ed3dSBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_INFO_LONG) {
339496e697eSBarry Smith     ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg1); CHKERRQ(ierr);
340496e697eSBarry Smith     ierr = OptionsHasName(PETSC_NULL,"-mat_no_unroll",&flg2); CHKERRQ(ierr);
341496e697eSBarry Smith     if (flg1 || flg2) fprintf(fd,"  not using I-node routines\n");
34295e01e2fSLois Curfman McInnes     else     fprintf(fd,"  using I-node routines: found %d nodes, limit used is %d\n",
34395e01e2fSLois Curfman McInnes         a->inode.node_count,a->inode.limit);
3443a40ed3dSBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_MATLAB) {
345d00d2cf4SBarry Smith     int nofinalvalue = 0;
346d00d2cf4SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != a->n-!shift)) {
347d00d2cf4SBarry Smith       nofinalvalue = 1;
348d00d2cf4SBarry Smith     }
349416022c9SBarry Smith     fprintf(fd,"%% Size = %d %d \n",m,a->n);
3504e220ebcSLois Curfman McInnes     fprintf(fd,"%% Nonzeros = %d \n",a->nz);
351d00d2cf4SBarry Smith     fprintf(fd,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);
35217ab2063SBarry Smith     fprintf(fd,"zzz = [\n");
35317ab2063SBarry Smith 
35417ab2063SBarry Smith     for (i=0; i<m; i++) {
355416022c9SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
3563a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
357e20fef11SSatish Balay         fprintf(fd,"%d %d  %18.16e + %18.16e i \n",i+1,a->j[j]+!shift,PetscReal(a->a[j]),PetscImaginary(a->a[j]));
35817ab2063SBarry Smith #else
3597a743949SBarry Smith         fprintf(fd,"%d %d  %18.16e\n", i+1, a->j[j]+!shift, a->a[j]);
36017ab2063SBarry Smith #endif
36117ab2063SBarry Smith       }
36217ab2063SBarry Smith     }
363d00d2cf4SBarry Smith     if (nofinalvalue) {
364d00d2cf4SBarry Smith       fprintf(fd,"%d %d  %18.16e\n", m, a->n, 0.0);
365d00d2cf4SBarry Smith     }
36617ab2063SBarry Smith     fprintf(fd,"];\n %s = spconvert(zzz);\n",outputname);
3673a40ed3dSBarry Smith   } else if (format == VIEWER_FORMAT_ASCII_COMMON) {
36844cd7ae7SLois Curfman McInnes     for ( i=0; i<m; i++ ) {
36944cd7ae7SLois Curfman McInnes       fprintf(fd,"row %d:",i);
37044cd7ae7SLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
3713a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
372e20fef11SSatish Balay         if (PetscImaginary(a->a[j]) > 0.0 && PetscReal(a->a[j]) != 0.0)
373e20fef11SSatish Balay           fprintf(fd," %d %g + %g i",a->j[j]+shift,PetscReal(a->a[j]),PetscImaginary(a->a[j]));
374e20fef11SSatish Balay         else if (PetscImaginary(a->a[j]) < 0.0 && PetscReal(a->a[j]) != 0.0)
375e20fef11SSatish Balay           fprintf(fd," %d %g - %g i",a->j[j]+shift,PetscReal(a->a[j]),-PetscImaginary(a->a[j]));
376e20fef11SSatish Balay         else if (PetscReal(a->a[j]) != 0.0)
377e20fef11SSatish Balay           fprintf(fd," %d %g ",a->j[j]+shift,PetscReal(a->a[j]));
37844cd7ae7SLois Curfman McInnes #else
37944cd7ae7SLois Curfman McInnes         if (a->a[j] != 0.0) fprintf(fd," %d %g ",a->j[j]+shift,a->a[j]);
38044cd7ae7SLois Curfman McInnes #endif
38144cd7ae7SLois Curfman McInnes       }
38244cd7ae7SLois Curfman McInnes       fprintf(fd,"\n");
38344cd7ae7SLois Curfman McInnes     }
38444cd7ae7SLois Curfman McInnes   }
385496be53dSLois Curfman McInnes   else if (format == VIEWER_FORMAT_ASCII_SYMMODU) {
386496be53dSLois Curfman McInnes     int nzd=0, fshift=1, *sptr;
3872e44a96cSLois Curfman McInnes     sptr = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(sptr);
388496be53dSLois Curfman McInnes     for ( i=0; i<m; i++ ) {
389496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
390496be53dSLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
391496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
3923a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
393e20fef11SSatish Balay           if (PetscImaginary(a->a[j]) != 0.0 || PetscReal(a->a[j]) != 0.0) nzd++;
394496be53dSLois Curfman McInnes #else
395496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
396496be53dSLois Curfman McInnes #endif
397496be53dSLois Curfman McInnes         }
398496be53dSLois Curfman McInnes       }
399496be53dSLois Curfman McInnes     }
4002e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
401496be53dSLois Curfman McInnes     fprintf(fd," %d %d\n\n",m,nzd);
4022e44a96cSLois Curfman McInnes     for ( i=0; i<m+1; i+=6 ) {
4032e44a96cSLois 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]);
4042e44a96cSLois 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]);
4052e44a96cSLois 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]);
4062e44a96cSLois Curfman McInnes       else if (i+1<m) fprintf(fd," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);
4072e44a96cSLois Curfman McInnes       else if (i<m)   fprintf(fd," %d %d\n",sptr[i],sptr[i+1]);
4087272d637SLois Curfman McInnes       else            fprintf(fd," %d\n",sptr[i]);
409496be53dSLois Curfman McInnes     }
410496be53dSLois Curfman McInnes     fprintf(fd,"\n");
411496be53dSLois Curfman McInnes     PetscFree(sptr);
412496be53dSLois Curfman McInnes     for ( i=0; i<m; i++ ) {
413496be53dSLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
414496be53dSLois Curfman McInnes         if (a->j[j] >= i) fprintf(fd," %d ",a->j[j]+fshift);
415496be53dSLois Curfman McInnes       }
416496be53dSLois Curfman McInnes       fprintf(fd,"\n");
417496be53dSLois Curfman McInnes     }
418496be53dSLois Curfman McInnes     fprintf(fd,"\n");
419496be53dSLois Curfman McInnes     for ( i=0; i<m; i++ ) {
420496be53dSLois Curfman McInnes       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
421496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
4223a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
423e20fef11SSatish Balay           if (PetscImaginary(a->a[j]) != 0.0 || PetscReal(a->a[j]) != 0.0)
424e20fef11SSatish Balay             fprintf(fd," %18.16e %18.16e ",PetscReal(a->a[j]),PetscImaginary(a->a[j]));
425496be53dSLois Curfman McInnes #else
426496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) fprintf(fd," %18.16e ",a->a[j]);
427496be53dSLois Curfman McInnes #endif
428496be53dSLois Curfman McInnes         }
429496be53dSLois Curfman McInnes       }
430496be53dSLois Curfman McInnes       fprintf(fd,"\n");
431496be53dSLois Curfman McInnes     }
4323a40ed3dSBarry Smith   } else {
43317ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
43417ab2063SBarry Smith       fprintf(fd,"row %d:",i);
435416022c9SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
4363a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
437e20fef11SSatish Balay         if (PetscImaginary(a->a[j]) > 0.0) {
438e20fef11SSatish Balay           fprintf(fd," %d %g + %g i",a->j[j]+shift,PetscReal(a->a[j]),PetscImaginary(a->a[j]));
439e20fef11SSatish Balay         } else if (PetscImaginary(a->a[j]) < 0.0) {
440e20fef11SSatish Balay           fprintf(fd," %d %g - %g i",a->j[j]+shift,PetscReal(a->a[j]),-PetscImaginary(a->a[j]));
4413a40ed3dSBarry Smith         } else {
442e20fef11SSatish Balay           fprintf(fd," %d %g ",a->j[j]+shift,PetscReal(a->a[j]));
44317ab2063SBarry Smith         }
44417ab2063SBarry Smith #else
445416022c9SBarry Smith         fprintf(fd," %d %g ",a->j[j]+shift,a->a[j]);
44617ab2063SBarry Smith #endif
44717ab2063SBarry Smith       }
44817ab2063SBarry Smith       fprintf(fd,"\n");
44917ab2063SBarry Smith     }
45017ab2063SBarry Smith   }
45117ab2063SBarry Smith   fflush(fd);
4523a40ed3dSBarry Smith   PetscFunctionReturn(0);
453416022c9SBarry Smith }
454416022c9SBarry Smith 
4555615d1e5SSatish Balay #undef __FUNC__
456480ef9eaSBarry Smith #define __FUNC__ "MatView_SeqAIJ_Draw_Zoom"
457480ef9eaSBarry Smith int MatView_SeqAIJ_Draw_Zoom(Draw draw,void *Aa)
458416022c9SBarry Smith {
459480ef9eaSBarry Smith   Mat         A = (Mat) Aa;
460416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ *) A->data;
461480ef9eaSBarry Smith   int         ierr, i,j, m = a->m, shift = a->indexshift,color;
4620513a670SBarry Smith   int         format;
463480ef9eaSBarry Smith   double      xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
464480ef9eaSBarry Smith   Viewer      viewer;
465cddf8d76SBarry Smith 
4663a40ed3dSBarry Smith   PetscFunctionBegin;
467480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*) &viewer);CHKERRQ(ierr);
4680513a670SBarry Smith   ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr);
46919bcc07fSBarry Smith 
470480ef9eaSBarry Smith   ierr = DrawGetCoordinates(draw,&xl,&yl,&xr,&yr); CHKERRQ(ierr);
471416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4720513a670SBarry Smith 
4730513a670SBarry Smith   if (format != VIEWER_FORMAT_DRAW_CONTOUR) {
4740513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
475cddf8d76SBarry Smith     color = DRAW_BLUE;
476416022c9SBarry Smith     for ( i=0; i<m; i++ ) {
477cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
478416022c9SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
479cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
4803a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
481e20fef11SSatish Balay         if (PetscReal(a->a[j]) >=  0.) continue;
482cddf8d76SBarry Smith #else
483cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
484cddf8d76SBarry Smith #endif
485480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
486cddf8d76SBarry Smith       }
487cddf8d76SBarry Smith     }
488cddf8d76SBarry Smith     color = DRAW_CYAN;
489cddf8d76SBarry Smith     for ( i=0; i<m; i++ ) {
490cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
491cddf8d76SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
492cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
493cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
494480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
495cddf8d76SBarry Smith       }
496cddf8d76SBarry Smith     }
497cddf8d76SBarry Smith     color = DRAW_RED;
498cddf8d76SBarry Smith     for ( i=0; i<m; i++ ) {
499cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
500cddf8d76SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
501cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
5023a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
503e20fef11SSatish Balay         if (PetscReal(a->a[j]) <=  0.) continue;
504cddf8d76SBarry Smith #else
505cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
506cddf8d76SBarry Smith #endif
507480ef9eaSBarry Smith         ierr = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
508416022c9SBarry Smith       }
509416022c9SBarry Smith     }
5100513a670SBarry Smith   } else {
5110513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5120513a670SBarry Smith     /* first determine max of all nonzero values */
5130513a670SBarry Smith     int    nz = a->nz,count;
5140513a670SBarry Smith     Draw   popup;
515480ef9eaSBarry Smith     double scale;
5160513a670SBarry Smith 
5170513a670SBarry Smith     for ( i=0; i<nz; i++ ) {
5180513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5190513a670SBarry Smith     }
520480ef9eaSBarry Smith     scale = (245.0 - DRAW_BASIC_COLORS)/maxv;
521522c5e43SBarry Smith     ierr  = DrawGetPopup(draw,&popup); CHKERRQ(ierr);
5220513a670SBarry Smith     ierr  = DrawScalePopup(popup,0.0,maxv); CHKERRQ(ierr);
5230513a670SBarry Smith     count = 0;
5240513a670SBarry Smith     for ( i=0; i<m; i++ ) {
5250513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
5260513a670SBarry Smith       for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
5270513a670SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
5286d6b6c30SSatish Balay         color = DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count]));
529480ef9eaSBarry Smith         ierr  = DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5300513a670SBarry Smith         count++;
5310513a670SBarry Smith       }
5320513a670SBarry Smith     }
5330513a670SBarry Smith   }
534480ef9eaSBarry Smith   PetscFunctionReturn(0);
535480ef9eaSBarry Smith }
536cddf8d76SBarry Smith 
537480ef9eaSBarry Smith #undef __FUNC__
538480ef9eaSBarry Smith #define __FUNC__ "MatView_SeqAIJ_Draw"
539480ef9eaSBarry Smith int MatView_SeqAIJ_Draw(Mat A,Viewer viewer)
540480ef9eaSBarry Smith {
541480ef9eaSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data;
542480ef9eaSBarry Smith   int        ierr;
543480ef9eaSBarry Smith   Draw       draw;
544480ef9eaSBarry Smith   double     xr,yr,xl,yl,h,w;
545480ef9eaSBarry Smith 
546480ef9eaSBarry Smith   PetscTruth isnull;
547480ef9eaSBarry Smith 
548480ef9eaSBarry Smith   PetscFunctionBegin;
549480ef9eaSBarry Smith   ierr = ViewerDrawGetDraw(viewer,&draw); CHKERRQ(ierr);
550480ef9eaSBarry Smith   ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr);
551480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
552480ef9eaSBarry Smith 
553480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
554480ef9eaSBarry Smith   xr  = a->n; yr = a->m; h = yr/10.0; w = xr/10.0;
555480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
556cddf8d76SBarry Smith   ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr);
557480ef9eaSBarry Smith   ierr = DrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A); CHKERRQ(ierr);
558480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5593a40ed3dSBarry Smith   PetscFunctionReturn(0);
560416022c9SBarry Smith }
561416022c9SBarry Smith 
5625615d1e5SSatish Balay #undef __FUNC__
563d4bb536fSBarry Smith #define __FUNC__ "MatView_SeqAIJ"
564e1311b90SBarry Smith int MatView_SeqAIJ(Mat A,Viewer viewer)
565416022c9SBarry Smith {
566416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*) A->data;
567bcd2baecSBarry Smith   ViewerType  vtype;
568bcd2baecSBarry Smith   int         ierr;
569416022c9SBarry Smith 
5703a40ed3dSBarry Smith   PetscFunctionBegin;
571bcd2baecSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
572bcd2baecSBarry Smith   if (vtype == MATLAB_VIEWER) {
5733a40ed3dSBarry Smith     ierr = ViewerMatlabPutSparse_Private(viewer,a->m,a->n,a->nz,a->a,a->i,a->j);  CHKERRQ(ierr);
5745cd90555SBarry Smith   } else if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER){
5753a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer); CHKERRQ(ierr);
5765cd90555SBarry Smith   } else if (vtype == BINARY_FILE_VIEWER) {
5773a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer); CHKERRQ(ierr);
5785cd90555SBarry Smith   } else if (vtype == DRAW_VIEWER) {
5793a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer); CHKERRQ(ierr);
5805cd90555SBarry Smith   } else {
5815cd90555SBarry Smith     SETERRQ(1,1,"Viewer type not supported by PETSc object");
58217ab2063SBarry Smith   }
5833a40ed3dSBarry Smith   PetscFunctionReturn(0);
58417ab2063SBarry Smith }
58519bcc07fSBarry Smith 
586c456f294SBarry Smith extern int Mat_AIJ_CheckInode(Mat);
5875615d1e5SSatish Balay #undef __FUNC__
5885615d1e5SSatish Balay #define __FUNC__ "MatAssemblyEnd_SeqAIJ"
5898f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
59017ab2063SBarry Smith {
591416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
59241c01911SSatish Balay   int        fshift = 0,i,j,*ai = a->i, *aj = a->j, *imax = a->imax,ierr;
59343ee02c3SBarry Smith   int        m = a->m, *ip, N, *ailen = a->ilen,shift = a->indexshift,rmax = 0;
594416022c9SBarry Smith   Scalar     *aa = a->a, *ap;
59517ab2063SBarry Smith 
5963a40ed3dSBarry Smith   PetscFunctionBegin;
5973a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
59817ab2063SBarry Smith 
59943ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
60017ab2063SBarry Smith   for ( i=1; i<m; i++ ) {
601416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
60217ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
60394a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
60417ab2063SBarry Smith     if (fshift) {
605416022c9SBarry Smith       ip = aj + ai[i] + shift; ap = aa + ai[i] + shift;
60617ab2063SBarry Smith       N = ailen[i];
60717ab2063SBarry Smith       for ( j=0; j<N; j++ ) {
60817ab2063SBarry Smith         ip[j-fshift] = ip[j];
60917ab2063SBarry Smith         ap[j-fshift] = ap[j];
61017ab2063SBarry Smith       }
61117ab2063SBarry Smith     }
61217ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
61317ab2063SBarry Smith   }
61417ab2063SBarry Smith   if (m) {
61517ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
61617ab2063SBarry Smith     ai[m] = ai[m-1] + ailen[m-1];
61717ab2063SBarry Smith   }
61817ab2063SBarry Smith   /* reset ilen and imax for each row */
61917ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
62017ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
62117ab2063SBarry Smith   }
622416022c9SBarry Smith   a->nz = ai[m] + shift;
62317ab2063SBarry Smith 
62417ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
625416022c9SBarry Smith   if (fshift && a->diag) {
6260452661fSBarry Smith     PetscFree(a->diag);
627416022c9SBarry Smith     PLogObjectMemory(A,-(m+1)*sizeof(int));
628416022c9SBarry Smith     a->diag = 0;
62917ab2063SBarry Smith   }
6304e220ebcSLois Curfman McInnes   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded, %d used\n",
6314e220ebcSLois Curfman McInnes            m,a->n,fshift,a->nz);
6324e220ebcSLois Curfman McInnes   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues is %d\n",
633b810aeb4SBarry Smith            a->reallocs);
63494a9d846SBarry Smith   PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax);
635dd5f02e7SSatish Balay   a->reallocs          = 0;
6364e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
6374e220ebcSLois Curfman McInnes 
63876dd722bSSatish Balay   /* check out for identical nodes. If found, use inode functions */
63941c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(A); CHKERRQ(ierr);
6403a40ed3dSBarry Smith   PetscFunctionReturn(0);
64117ab2063SBarry Smith }
64217ab2063SBarry Smith 
6435615d1e5SSatish Balay #undef __FUNC__
6445615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_SeqAIJ"
6458f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A)
64617ab2063SBarry Smith {
647416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
6483a40ed3dSBarry Smith 
6493a40ed3dSBarry Smith   PetscFunctionBegin;
650cddf8d76SBarry Smith   PetscMemzero(a->a,(a->i[a->m]+a->indexshift)*sizeof(Scalar));
6513a40ed3dSBarry Smith   PetscFunctionReturn(0);
65217ab2063SBarry Smith }
653416022c9SBarry Smith 
6545615d1e5SSatish Balay #undef __FUNC__
6555615d1e5SSatish Balay #define __FUNC__ "MatDestroy_SeqAIJ"
656e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A)
65717ab2063SBarry Smith {
658416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
65982bf6240SBarry Smith   int        ierr;
660d5d45c9bSBarry Smith 
6613a40ed3dSBarry Smith   PetscFunctionBegin;
66294d884c6SBarry Smith   if (--A->refct > 0) PetscFunctionReturn(0);
66394d884c6SBarry Smith 
66494d884c6SBarry Smith   if (A->mapping) {
66594d884c6SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(A->mapping); CHKERRQ(ierr);
66694d884c6SBarry Smith   }
66794d884c6SBarry Smith   if (A->bmapping) {
66894d884c6SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(A->bmapping); CHKERRQ(ierr);
66994d884c6SBarry Smith   }
67061b13de0SBarry Smith   if (A->rmap) {
67161b13de0SBarry Smith     ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
67261b13de0SBarry Smith   }
67361b13de0SBarry Smith   if (A->cmap) {
67461b13de0SBarry Smith     ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
67561b13de0SBarry Smith   }
6763a40ed3dSBarry Smith #if defined(USE_PETSC_LOG)
677e1311b90SBarry Smith   PLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",a->m,a->n,a->nz);
67817ab2063SBarry Smith #endif
6790452661fSBarry Smith   PetscFree(a->a);
6800452661fSBarry Smith   if (!a->singlemalloc) { PetscFree(a->i); PetscFree(a->j);}
6810452661fSBarry Smith   if (a->diag) PetscFree(a->diag);
6820452661fSBarry Smith   if (a->ilen) PetscFree(a->ilen);
6830452661fSBarry Smith   if (a->imax) PetscFree(a->imax);
6840452661fSBarry Smith   if (a->solve_work) PetscFree(a->solve_work);
68576dd722bSSatish Balay   if (a->inode.size) PetscFree(a->inode.size);
68682bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
687be6bf707SBarry Smith   if (a->saved_values) PetscFree(a->saved_values);
6880452661fSBarry Smith   PetscFree(a);
689eed86810SBarry Smith 
690f2655603SLois Curfman McInnes   PLogObjectDestroy(A);
691f2655603SLois Curfman McInnes   PetscHeaderDestroy(A);
6923a40ed3dSBarry Smith   PetscFunctionReturn(0);
69317ab2063SBarry Smith }
69417ab2063SBarry Smith 
6955615d1e5SSatish Balay #undef __FUNC__
6965615d1e5SSatish Balay #define __FUNC__ "MatCompress_SeqAIJ"
6978f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
69817ab2063SBarry Smith {
6993a40ed3dSBarry Smith   PetscFunctionBegin;
7003a40ed3dSBarry Smith   PetscFunctionReturn(0);
70117ab2063SBarry Smith }
70217ab2063SBarry Smith 
7035615d1e5SSatish Balay #undef __FUNC__
7045615d1e5SSatish Balay #define __FUNC__ "MatSetOption_SeqAIJ"
7058f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
70617ab2063SBarry Smith {
707416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
7083a40ed3dSBarry Smith 
7093a40ed3dSBarry Smith   PetscFunctionBegin;
7106d4a8577SBarry Smith   if      (op == MAT_ROW_ORIENTED)                 a->roworiented = 1;
7116d4a8577SBarry Smith   else if (op == MAT_COLUMN_ORIENTED)              a->roworiented = 0;
7126d4a8577SBarry Smith   else if (op == MAT_COLUMNS_SORTED)               a->sorted      = 1;
713219d9a1aSLois Curfman McInnes   else if (op == MAT_COLUMNS_UNSORTED)             a->sorted      = 0;
7146d4a8577SBarry Smith   else if (op == MAT_NO_NEW_NONZERO_LOCATIONS)     a->nonew       = 1;
715c2653b3dSLois Curfman McInnes   else if (op == MAT_NEW_NONZERO_LOCATION_ERROR)   a->nonew       = -1;
71696854ed6SLois Curfman McInnes   else if (op == MAT_NEW_NONZERO_ALLOCATION_ERROR) a->nonew       = -2;
7176d4a8577SBarry Smith   else if (op == MAT_YES_NEW_NONZERO_LOCATIONS)    a->nonew       = 0;
7186d4a8577SBarry Smith   else if (op == MAT_ROWS_SORTED ||
719219d9a1aSLois Curfman McInnes            op == MAT_ROWS_UNSORTED ||
7206d4a8577SBarry Smith            op == MAT_SYMMETRIC ||
7216d4a8577SBarry Smith            op == MAT_STRUCTURALLY_SYMMETRIC ||
72290f02eecSBarry Smith            op == MAT_YES_NEW_DIAGONALS ||
723b51ba29fSSatish Balay            op == MAT_IGNORE_OFF_PROC_ENTRIES||
724b51ba29fSSatish Balay            op == MAT_USE_HASH_TABLE)
725981c4779SBarry Smith     PLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
7263a40ed3dSBarry Smith   else if (op == MAT_NO_NEW_DIAGONALS) {
7273a40ed3dSBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS");
7283a40ed3dSBarry Smith   } else if (op == MAT_INODE_LIMIT_1)            a->inode.limit  = 1;
7296d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_2)            a->inode.limit  = 2;
7306d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_3)            a->inode.limit  = 3;
7316d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_4)            a->inode.limit  = 4;
7326d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_5)            a->inode.limit  = 5;
7333a40ed3dSBarry Smith   else SETERRQ(PETSC_ERR_SUP,0,"unknown option");
7343a40ed3dSBarry Smith   PetscFunctionReturn(0);
73517ab2063SBarry Smith }
73617ab2063SBarry Smith 
7375615d1e5SSatish Balay #undef __FUNC__
7385615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_SeqAIJ"
7398f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
74017ab2063SBarry Smith {
741416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
7423a40ed3dSBarry Smith   int        i,j, n,shift = a->indexshift,ierr;
74317ab2063SBarry Smith   Scalar     *x, zero = 0.0;
74417ab2063SBarry Smith 
7453a40ed3dSBarry Smith   PetscFunctionBegin;
7463a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
747e1311b90SBarry Smith   ierr = VecGetArray(v,&x);;CHKERRQ(ierr);
748e1311b90SBarry Smith   ierr = VecGetLocalSize(v,&n);;CHKERRQ(ierr);
749a8c6a408SBarry Smith   if (n != a->m) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Nonconforming matrix and vector");
750416022c9SBarry Smith   for ( i=0; i<a->m; i++ ) {
751416022c9SBarry Smith     for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
752416022c9SBarry Smith       if (a->j[j]+shift == i) {
753416022c9SBarry Smith         x[i] = a->a[j];
75417ab2063SBarry Smith         break;
75517ab2063SBarry Smith       }
75617ab2063SBarry Smith     }
75717ab2063SBarry Smith   }
758e1311b90SBarry Smith   ierr = VecRestoreArray(v,&x);;CHKERRQ(ierr);
7593a40ed3dSBarry Smith   PetscFunctionReturn(0);
76017ab2063SBarry Smith }
76117ab2063SBarry Smith 
76217ab2063SBarry Smith /* -------------------------------------------------------*/
76317ab2063SBarry Smith /* Should check that shapes of vectors and matrices match */
76417ab2063SBarry Smith /* -------------------------------------------------------*/
7655615d1e5SSatish Balay #undef __FUNC__
7665615d1e5SSatish Balay #define __FUNC__ "MatMultTrans_SeqAIJ"
76744cd7ae7SLois Curfman McInnes int MatMultTrans_SeqAIJ(Mat A,Vec xx,Vec yy)
76817ab2063SBarry Smith {
769416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
77017ab2063SBarry Smith   Scalar     *x, *y, *v, alpha;
771e1311b90SBarry Smith   int        ierr,m = a->m, n, i, *idx, shift = a->indexshift;
77217ab2063SBarry Smith 
7733a40ed3dSBarry Smith   PetscFunctionBegin;
774e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
775e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
776cddf8d76SBarry Smith   PetscMemzero(y,a->n*sizeof(Scalar));
77717ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
77817ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
779416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
780416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
781416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
78217ab2063SBarry Smith     alpha = x[i];
78317ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
78417ab2063SBarry Smith   }
785416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
786e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
787e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
7883a40ed3dSBarry Smith   PetscFunctionReturn(0);
78917ab2063SBarry Smith }
790d5d45c9bSBarry Smith 
7915615d1e5SSatish Balay #undef __FUNC__
7925615d1e5SSatish Balay #define __FUNC__ "MatMultTransAdd_SeqAIJ"
79344cd7ae7SLois Curfman McInnes int MatMultTransAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
79417ab2063SBarry Smith {
795416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
79617ab2063SBarry Smith   Scalar     *x, *y, *v, alpha;
797e1311b90SBarry Smith   int        ierr,m = a->m, n, i, *idx,shift = a->indexshift;
79817ab2063SBarry Smith 
7993a40ed3dSBarry Smith   PetscFunctionBegin;
800*2e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
801e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
802e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
80317ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
80417ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
805416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
806416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
807416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
80817ab2063SBarry Smith     alpha = x[i];
80917ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
81017ab2063SBarry Smith   }
81190f02eecSBarry Smith   PLogFlops(2*a->nz);
812e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
813e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8143a40ed3dSBarry Smith   PetscFunctionReturn(0);
81517ab2063SBarry Smith }
81617ab2063SBarry Smith 
8175615d1e5SSatish Balay #undef __FUNC__
8185615d1e5SSatish Balay #define __FUNC__ "MatMult_SeqAIJ"
81944cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
82017ab2063SBarry Smith {
821416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
82217ab2063SBarry Smith   Scalar     *x, *y, *v, sum;
823e1311b90SBarry Smith   int        ierr,m = a->m, *idx, shift = a->indexshift,*ii;
8242e40c62eSSatish Balay #if !defined(USE_FORTRAN_KERNEL_MULTAIJ)
825e36a17ebSSatish Balay   int        n, i, jrow,j;
826e36a17ebSSatish Balay #endif
82717ab2063SBarry Smith 
828fee21e36SBarry Smith #if defined(HAVE_PRAGMA_DISJOINT)
829fee21e36SBarry Smith #pragma disjoint(*x,*y,*v)
830fee21e36SBarry Smith #endif
831fee21e36SBarry Smith 
8323a40ed3dSBarry Smith   PetscFunctionBegin;
833e1311b90SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
834e1311b90SBarry Smith   ierr = VecGetArray(yy,&y); CHKERRQ(ierr);
83517ab2063SBarry Smith   x    = x + shift;    /* shift for Fortran start by 1 indexing */
836416022c9SBarry Smith   idx  = a->j;
837d64ed03dSBarry Smith   v    = a->a;
838416022c9SBarry Smith   ii   = a->i;
839acc4d009SSatish Balay #if defined(USE_FORTRAN_KERNEL_MULTAIJ)
84029b5ca8aSSatish Balay   fortranmultaij_(&m,x,ii,idx+shift,v+shift,y);
8418d195f9aSBarry Smith #else
842d64ed03dSBarry Smith   v    += shift; /* shift for Fortran start by 1 indexing */
843d64ed03dSBarry Smith   idx  += shift;
84417ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
8459ea0dfa2SSatish Balay     jrow = ii[i];
8469ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
84717ab2063SBarry Smith     sum  = 0.0;
8489ea0dfa2SSatish Balay     for ( j=0; j<n; j++) {
8499ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8509ea0dfa2SSatish Balay      }
85117ab2063SBarry Smith     y[i] = sum;
85217ab2063SBarry Smith   }
8538d195f9aSBarry Smith #endif
854416022c9SBarry Smith   PLogFlops(2*a->nz - m);
855e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
856e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y); CHKERRQ(ierr);
8573a40ed3dSBarry Smith   PetscFunctionReturn(0);
85817ab2063SBarry Smith }
85917ab2063SBarry Smith 
8605615d1e5SSatish Balay #undef __FUNC__
8615615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_SeqAIJ"
86244cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
86317ab2063SBarry Smith {
864416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
86517ab2063SBarry Smith   Scalar     *x, *y, *z, *v, sum;
866e1311b90SBarry Smith   int        ierr,m = a->m, *idx, shift = a->indexshift,*ii;
8672e40c62eSSatish Balay #if !defined(USE_FORTRAN_KERNEL_MULTADDAIJ)
868e36a17ebSSatish Balay   int        n,i,jrow,j;
869e36a17ebSSatish Balay #endif
8709ea0dfa2SSatish Balay 
8713a40ed3dSBarry Smith   PetscFunctionBegin;
872e1311b90SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
873e1311b90SBarry Smith   ierr = VecGetArray(yy,&y); CHKERRQ(ierr);
874*2e8a6d31SBarry Smith   if (zz != yy) {
875e1311b90SBarry Smith     ierr = VecGetArray(zz,&z); CHKERRQ(ierr);
876*2e8a6d31SBarry Smith   } else {
877*2e8a6d31SBarry Smith     z = y;
878*2e8a6d31SBarry Smith   }
87917ab2063SBarry Smith   x    = x + shift; /* shift for Fortran start by 1 indexing */
880cddf8d76SBarry Smith   idx  = a->j;
881d64ed03dSBarry Smith   v    = a->a;
882cddf8d76SBarry Smith   ii   = a->i;
8832e40c62eSSatish Balay #if defined(USE_FORTRAN_KERNEL_MULTADDAIJ)
88429b5ca8aSSatish Balay   fortranmultaddaij_(&m,x,ii,idx+shift,v+shift,y,z);
88502ab625aSSatish Balay #else
886d64ed03dSBarry Smith   v   += shift; /* shift for Fortran start by 1 indexing */
887d64ed03dSBarry Smith   idx += shift;
88817ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
8899ea0dfa2SSatish Balay     jrow = ii[i];
8909ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
89117ab2063SBarry Smith     sum  = y[i];
8929ea0dfa2SSatish Balay     for ( j=0; j<n; j++) {
8939ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8949ea0dfa2SSatish Balay      }
89517ab2063SBarry Smith     z[i] = sum;
89617ab2063SBarry Smith   }
89702ab625aSSatish Balay #endif
898416022c9SBarry Smith   PLogFlops(2*a->nz);
899e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
900e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y); CHKERRQ(ierr);
901*2e8a6d31SBarry Smith   if (zz != yy) {
902e1311b90SBarry Smith     ierr = VecRestoreArray(zz,&z); CHKERRQ(ierr);
903*2e8a6d31SBarry Smith   }
9043a40ed3dSBarry Smith   PetscFunctionReturn(0);
90517ab2063SBarry Smith }
90617ab2063SBarry Smith 
90717ab2063SBarry Smith /*
90817ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
90917ab2063SBarry Smith */
91017ab2063SBarry Smith 
9115615d1e5SSatish Balay #undef __FUNC__
9125615d1e5SSatish Balay #define __FUNC__ "MatMarkDiag_SeqAIJ"
913416022c9SBarry Smith int MatMarkDiag_SeqAIJ(Mat A)
91417ab2063SBarry Smith {
915416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
916416022c9SBarry Smith   int        i,j, *diag, m = a->m,shift = a->indexshift;
91717ab2063SBarry Smith 
9183a40ed3dSBarry Smith   PetscFunctionBegin;
9190452661fSBarry Smith   diag = (int *) PetscMalloc( (m+1)*sizeof(int)); CHKPTRQ(diag);
920416022c9SBarry Smith   PLogObjectMemory(A,(m+1)*sizeof(int));
921416022c9SBarry Smith   for ( i=0; i<a->m; i++ ) {
92235b0346bSBarry Smith     diag[i] = a->i[i+1];
923416022c9SBarry Smith     for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) {
924416022c9SBarry Smith       if (a->j[j]+shift == i) {
92517ab2063SBarry Smith         diag[i] = j - shift;
92617ab2063SBarry Smith         break;
92717ab2063SBarry Smith       }
92817ab2063SBarry Smith     }
92917ab2063SBarry Smith   }
930416022c9SBarry Smith   a->diag = diag;
9313a40ed3dSBarry Smith   PetscFunctionReturn(0);
93217ab2063SBarry Smith }
93317ab2063SBarry Smith 
9345615d1e5SSatish Balay #undef __FUNC__
9355615d1e5SSatish Balay #define __FUNC__ "MatRelax_SeqAIJ"
93644cd7ae7SLois Curfman McInnes int MatRelax_SeqAIJ(Mat A,Vec bb,double omega,MatSORType flag,
93717ab2063SBarry Smith                            double fshift,int its,Vec xx)
93817ab2063SBarry Smith {
939416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
940416022c9SBarry Smith   Scalar     *x, *b, *bs,  d, *xs, sum, *v = a->a,*t,scale,*ts, *xb;
941d5d45c9bSBarry Smith   int        ierr, *idx, *diag,n = a->n, m = a->m, i, shift = a->indexshift;
94217ab2063SBarry Smith 
9433a40ed3dSBarry Smith   PetscFunctionBegin;
944e1311b90SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
945e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
946d00d2cf4SBarry Smith   if (!a->diag) {ierr = MatMarkDiag_SeqAIJ(A);CHKERRQ(ierr);}
947416022c9SBarry Smith   diag = a->diag;
948416022c9SBarry Smith   xs   = x + shift; /* shifted by one for index start of a or a->j*/
94917ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
95017ab2063SBarry Smith    /* apply ( U + D/omega) to the vector */
95117ab2063SBarry Smith     bs = b + shift;
95217ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
953416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
954416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
955488ecbafSBarry Smith 	PLogFlops(2*n-1);
956416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
957416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
95817ab2063SBarry Smith         sum  = b[i]*d/omega;
95917ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
96017ab2063SBarry Smith         x[i] = sum;
96117ab2063SBarry Smith     }
962cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
963cb5b572fSBarry Smith     ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
9643a40ed3dSBarry Smith     PetscFunctionReturn(0);
96517ab2063SBarry Smith   }
96617ab2063SBarry Smith   if (flag == SOR_APPLY_LOWER) {
967a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"SOR_APPLY_LOWER is not done");
9683a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
96917ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
97017ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
97117ab2063SBarry Smith 
97217ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
97317ab2063SBarry Smith 
97417ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
97517ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
97617ab2063SBarry Smith     is the relaxation factor.
97717ab2063SBarry Smith     */
9780452661fSBarry Smith     t = (Scalar *) PetscMalloc( m*sizeof(Scalar) ); CHKPTRQ(t);
97917ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
98017ab2063SBarry Smith 
98117ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
98217ab2063SBarry Smith     for ( i=m-1; i>=0; i-- ) {
983416022c9SBarry Smith       d    = fshift + a->a[diag[i] + shift];
984416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
985488ecbafSBarry Smith       PLogFlops(2*n-1);
986416022c9SBarry Smith       idx  = a->j + diag[i] + (!shift);
987416022c9SBarry Smith       v    = a->a + diag[i] + (!shift);
98817ab2063SBarry Smith       sum  = b[i];
98917ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
99017ab2063SBarry Smith       x[i] = omega*(sum/d);
99117ab2063SBarry Smith     }
99217ab2063SBarry Smith 
99317ab2063SBarry Smith     /*  t = b - (2*E - D)x */
994416022c9SBarry Smith     v = a->a;
99517ab2063SBarry Smith     for ( i=0; i<m; i++ ) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; }
996488ecbafSBarry Smith     PLogFlops(2*m);
997488ecbafSBarry Smith 
99817ab2063SBarry Smith 
99917ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1000416022c9SBarry Smith     ts = t + shift; /* shifted by one for index start of a or a->j*/
1001416022c9SBarry Smith     diag = a->diag;
100217ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
1003416022c9SBarry Smith       d    = fshift + a->a[diag[i]+shift];
1004416022c9SBarry Smith       n    = diag[i] - a->i[i];
1005488ecbafSBarry Smith       PLogFlops(2*n-1);
1006416022c9SBarry Smith       idx  = a->j + a->i[i] + shift;
1007416022c9SBarry Smith       v    = a->a + a->i[i] + shift;
100817ab2063SBarry Smith       sum  = t[i];
100917ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
101017ab2063SBarry Smith       t[i] = omega*(sum/d);
101117ab2063SBarry Smith     }
101217ab2063SBarry Smith 
101317ab2063SBarry Smith     /*  x = x + t */
101417ab2063SBarry Smith     for ( i=0; i<m; i++ ) { x[i] += t[i]; }
10150452661fSBarry Smith     PetscFree(t);
1016cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
1017cb5b572fSBarry Smith     ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
10183a40ed3dSBarry Smith     PetscFunctionReturn(0);
101917ab2063SBarry Smith   }
102017ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
102117ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
102217ab2063SBarry Smith       for ( i=0; i<m; i++ ) {
1023416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1024416022c9SBarry Smith         n    = diag[i] - a->i[i];
1025488ecbafSBarry Smith 	PLogFlops(2*n-1);
1026416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1027416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
102817ab2063SBarry Smith         sum  = b[i];
102917ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
103017ab2063SBarry Smith         x[i] = omega*(sum/d);
103117ab2063SBarry Smith       }
103217ab2063SBarry Smith       xb = x;
10333a40ed3dSBarry Smith     } else xb = b;
103417ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
103517ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
103617ab2063SBarry Smith       for ( i=0; i<m; i++ ) {
1037416022c9SBarry Smith         x[i] *= a->a[diag[i]+shift];
103817ab2063SBarry Smith       }
1039488ecbafSBarry Smith       PLogFlops(m);
104017ab2063SBarry Smith     }
104117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
104217ab2063SBarry Smith       for ( i=m-1; i>=0; i-- ) {
1043416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1044416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1045488ecbafSBarry Smith 	PLogFlops(2*n-1);
1046416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1047416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
104817ab2063SBarry Smith         sum  = xb[i];
104917ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
105017ab2063SBarry Smith         x[i] = omega*(sum/d);
105117ab2063SBarry Smith       }
105217ab2063SBarry Smith     }
105317ab2063SBarry Smith     its--;
105417ab2063SBarry Smith   }
105517ab2063SBarry Smith   while (its--) {
105617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
105717ab2063SBarry Smith       for ( i=0; i<m; i++ ) {
1058416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1059416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1060488ecbafSBarry Smith 	PLogFlops(2*n-1);
1061416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1062416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
106317ab2063SBarry Smith         sum  = b[i];
106417ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
10657e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
106617ab2063SBarry Smith       }
106717ab2063SBarry Smith     }
106817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
106917ab2063SBarry Smith       for ( i=m-1; i>=0; i-- ) {
1070416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1071416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1072488ecbafSBarry Smith 	PLogFlops(2*n-1);
1073416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1074416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
107517ab2063SBarry Smith         sum  = b[i];
107617ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
10777e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
107817ab2063SBarry Smith       }
107917ab2063SBarry Smith     }
108017ab2063SBarry Smith   }
1081cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
1082cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
10833a40ed3dSBarry Smith   PetscFunctionReturn(0);
108417ab2063SBarry Smith }
108517ab2063SBarry Smith 
10865615d1e5SSatish Balay #undef __FUNC__
10875615d1e5SSatish Balay #define __FUNC__ "MatGetInfo_SeqAIJ"
10888f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
108917ab2063SBarry Smith {
1090416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
10914e220ebcSLois Curfman McInnes 
10923a40ed3dSBarry Smith   PetscFunctionBegin;
10934e220ebcSLois Curfman McInnes   info->rows_global    = (double)a->m;
10944e220ebcSLois Curfman McInnes   info->columns_global = (double)a->n;
10954e220ebcSLois Curfman McInnes   info->rows_local     = (double)a->m;
10964e220ebcSLois Curfman McInnes   info->columns_local  = (double)a->n;
10974e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
10984e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
10994e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
11004e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
11014e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
11024e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
11034e220ebcSLois Curfman McInnes   info->memory         = A->mem;
11044e220ebcSLois Curfman McInnes   if (A->factor) {
11054e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
11064e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
11074e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
11084e220ebcSLois Curfman McInnes   } else {
11094e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
11104e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
11114e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
11124e220ebcSLois Curfman McInnes   }
11133a40ed3dSBarry Smith   PetscFunctionReturn(0);
111417ab2063SBarry Smith }
111517ab2063SBarry Smith 
111617ab2063SBarry Smith extern int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,double,Mat*);
111717ab2063SBarry Smith extern int MatLUFactorNumeric_SeqAIJ(Mat,Mat*);
111817ab2063SBarry Smith extern int MatLUFactor_SeqAIJ(Mat,IS,IS,double);
111917ab2063SBarry Smith extern int MatSolve_SeqAIJ(Mat,Vec,Vec);
112017ab2063SBarry Smith extern int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
112117ab2063SBarry Smith extern int MatSolveTrans_SeqAIJ(Mat,Vec,Vec);
112217ab2063SBarry Smith extern int MatSolveTransAdd_SeqAIJ(Mat,Vec,Vec,Vec);
112317ab2063SBarry Smith 
11245615d1e5SSatish Balay #undef __FUNC__
11255615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_SeqAIJ"
11268f6be9afSLois Curfman McInnes int MatZeroRows_SeqAIJ(Mat A,IS is,Scalar *diag)
112717ab2063SBarry Smith {
1128416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1129416022c9SBarry Smith   int         i,ierr,N, *rows,m = a->m - 1,shift = a->indexshift;
113017ab2063SBarry Smith 
11313a40ed3dSBarry Smith   PetscFunctionBegin;
113277c4ece6SBarry Smith   ierr = ISGetSize(is,&N); CHKERRQ(ierr);
113317ab2063SBarry Smith   ierr = ISGetIndices(is,&rows); CHKERRQ(ierr);
113417ab2063SBarry Smith   if (diag) {
113517ab2063SBarry Smith     for ( i=0; i<N; i++ ) {
1136a8c6a408SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range");
1137416022c9SBarry Smith       if (a->ilen[rows[i]] > 0) { /* in case row was completely empty */
1138416022c9SBarry Smith         a->ilen[rows[i]] = 1;
1139416022c9SBarry Smith         a->a[a->i[rows[i]]+shift] = *diag;
1140416022c9SBarry Smith         a->j[a->i[rows[i]]+shift] = rows[i]+shift;
11413a40ed3dSBarry Smith       } else {
1142d64ed03dSBarry Smith         ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
114317ab2063SBarry Smith       }
114417ab2063SBarry Smith     }
11453a40ed3dSBarry Smith   } else {
114617ab2063SBarry Smith     for ( i=0; i<N; i++ ) {
1147a8c6a408SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range");
1148416022c9SBarry Smith       a->ilen[rows[i]] = 0;
114917ab2063SBarry Smith     }
115017ab2063SBarry Smith   }
115117ab2063SBarry Smith   ISRestoreIndices(is,&rows);
115243a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
11533a40ed3dSBarry Smith   PetscFunctionReturn(0);
115417ab2063SBarry Smith }
115517ab2063SBarry Smith 
11565615d1e5SSatish Balay #undef __FUNC__
11575615d1e5SSatish Balay #define __FUNC__ "MatGetSize_SeqAIJ"
11588f6be9afSLois Curfman McInnes int MatGetSize_SeqAIJ(Mat A,int *m,int *n)
115917ab2063SBarry Smith {
1160416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
11613a40ed3dSBarry Smith 
11623a40ed3dSBarry Smith   PetscFunctionBegin;
11630752156aSBarry Smith   if (m) *m = a->m;
11640752156aSBarry Smith   if (n) *n = a->n;
11653a40ed3dSBarry Smith   PetscFunctionReturn(0);
116617ab2063SBarry Smith }
116717ab2063SBarry Smith 
11685615d1e5SSatish Balay #undef __FUNC__
11695615d1e5SSatish Balay #define __FUNC__ "MatGetOwnershipRange_SeqAIJ"
11708f6be9afSLois Curfman McInnes int MatGetOwnershipRange_SeqAIJ(Mat A,int *m,int *n)
117117ab2063SBarry Smith {
1172416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
11733a40ed3dSBarry Smith 
11743a40ed3dSBarry Smith   PetscFunctionBegin;
1175416022c9SBarry Smith   *m = 0; *n = a->m;
11763a40ed3dSBarry Smith   PetscFunctionReturn(0);
117717ab2063SBarry Smith }
1178026e39d0SSatish Balay 
11795615d1e5SSatish Balay #undef __FUNC__
11805615d1e5SSatish Balay #define __FUNC__ "MatGetRow_SeqAIJ"
11814e093b46SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
118217ab2063SBarry Smith {
1183416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1184c456f294SBarry Smith   int        *itmp,i,shift = a->indexshift;
118517ab2063SBarry Smith 
11863a40ed3dSBarry Smith   PetscFunctionBegin;
1187a8c6a408SBarry Smith   if (row < 0 || row >= a->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row out of range");
118817ab2063SBarry Smith 
1189416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1190416022c9SBarry Smith   if (v) *v = a->a + a->i[row] + shift;
119117ab2063SBarry Smith   if (idx) {
1192416022c9SBarry Smith     itmp = a->j + a->i[row] + shift;
11934e093b46SBarry Smith     if (*nz && shift) {
11940452661fSBarry Smith       *idx = (int *) PetscMalloc( (*nz)*sizeof(int) ); CHKPTRQ(*idx);
119517ab2063SBarry Smith       for ( i=0; i<(*nz); i++ ) {(*idx)[i] = itmp[i] + shift;}
11964e093b46SBarry Smith     } else if (*nz) {
11974e093b46SBarry Smith       *idx = itmp;
119817ab2063SBarry Smith     }
119917ab2063SBarry Smith     else *idx = 0;
120017ab2063SBarry Smith   }
12013a40ed3dSBarry Smith   PetscFunctionReturn(0);
120217ab2063SBarry Smith }
120317ab2063SBarry Smith 
12045615d1e5SSatish Balay #undef __FUNC__
12055615d1e5SSatish Balay #define __FUNC__ "MatRestoreRow_SeqAIJ"
12064e093b46SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
120717ab2063SBarry Smith {
12084e093b46SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
12093a40ed3dSBarry Smith 
12103a40ed3dSBarry Smith   PetscFunctionBegin;
12114e093b46SBarry Smith   if (idx) {if (*idx && a->indexshift) PetscFree(*idx);}
12123a40ed3dSBarry Smith   PetscFunctionReturn(0);
121317ab2063SBarry Smith }
121417ab2063SBarry Smith 
12155615d1e5SSatish Balay #undef __FUNC__
12165615d1e5SSatish Balay #define __FUNC__ "MatNorm_SeqAIJ"
12178f6be9afSLois Curfman McInnes int MatNorm_SeqAIJ(Mat A,NormType type,double *norm)
121817ab2063SBarry Smith {
1219416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1220416022c9SBarry Smith   Scalar     *v = a->a;
122117ab2063SBarry Smith   double     sum = 0.0;
1222416022c9SBarry Smith   int        i, j,shift = a->indexshift;
122317ab2063SBarry Smith 
12243a40ed3dSBarry Smith   PetscFunctionBegin;
122517ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1226416022c9SBarry Smith     for (i=0; i<a->nz; i++ ) {
12273a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
1228e20fef11SSatish Balay       sum += PetscReal(PetscConj(*v)*(*v)); v++;
122917ab2063SBarry Smith #else
123017ab2063SBarry Smith       sum += (*v)*(*v); v++;
123117ab2063SBarry Smith #endif
123217ab2063SBarry Smith     }
123317ab2063SBarry Smith     *norm = sqrt(sum);
12343a40ed3dSBarry Smith   } else if (type == NORM_1) {
123517ab2063SBarry Smith     double *tmp;
1236416022c9SBarry Smith     int    *jj = a->j;
123766963ce1SSatish Balay     tmp = (double *) PetscMalloc( (a->n+1)*sizeof(double) ); CHKPTRQ(tmp);
1238cddf8d76SBarry Smith     PetscMemzero(tmp,a->n*sizeof(double));
123917ab2063SBarry Smith     *norm = 0.0;
1240416022c9SBarry Smith     for ( j=0; j<a->nz; j++ ) {
1241a2744918SBarry Smith         tmp[*jj++ + shift] += PetscAbsScalar(*v);  v++;
124217ab2063SBarry Smith     }
1243416022c9SBarry Smith     for ( j=0; j<a->n; j++ ) {
124417ab2063SBarry Smith       if (tmp[j] > *norm) *norm = tmp[j];
124517ab2063SBarry Smith     }
12460452661fSBarry Smith     PetscFree(tmp);
12473a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
124817ab2063SBarry Smith     *norm = 0.0;
1249416022c9SBarry Smith     for ( j=0; j<a->m; j++ ) {
1250416022c9SBarry Smith       v = a->a + a->i[j] + shift;
125117ab2063SBarry Smith       sum = 0.0;
1252416022c9SBarry Smith       for ( i=0; i<a->i[j+1]-a->i[j]; i++ ) {
1253cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
125417ab2063SBarry Smith       }
125517ab2063SBarry Smith       if (sum > *norm) *norm = sum;
125617ab2063SBarry Smith     }
12573a40ed3dSBarry Smith   } else {
1258a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"No support for two norm");
125917ab2063SBarry Smith   }
12603a40ed3dSBarry Smith   PetscFunctionReturn(0);
126117ab2063SBarry Smith }
126217ab2063SBarry Smith 
12635615d1e5SSatish Balay #undef __FUNC__
12645615d1e5SSatish Balay #define __FUNC__ "MatTranspose_SeqAIJ"
12658f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
126617ab2063SBarry Smith {
1267416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1268416022c9SBarry Smith   Mat        C;
1269416022c9SBarry Smith   int        i, ierr, *aj = a->j, *ai = a->i, m = a->m, len, *col;
1270416022c9SBarry Smith   int        shift = a->indexshift;
1271d5d45c9bSBarry Smith   Scalar     *array = a->a;
127217ab2063SBarry Smith 
12733a40ed3dSBarry Smith   PetscFunctionBegin;
1274a8c6a408SBarry Smith   if (B == PETSC_NULL && m != a->n) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Square matrix only for in-place");
12750452661fSBarry Smith   col = (int *) PetscMalloc((1+a->n)*sizeof(int)); CHKPTRQ(col);
1276cddf8d76SBarry Smith   PetscMemzero(col,(1+a->n)*sizeof(int));
127717ab2063SBarry Smith   if (shift) {
127817ab2063SBarry Smith     for ( i=0; i<ai[m]-1; i++ ) aj[i] -= 1;
127917ab2063SBarry Smith   }
128017ab2063SBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) col[aj[i]] += 1;
1281416022c9SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,a->n,m,0,col,&C); CHKERRQ(ierr);
12820452661fSBarry Smith   PetscFree(col);
128317ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
128417ab2063SBarry Smith     len = ai[i+1]-ai[i];
1285416022c9SBarry Smith     ierr = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES); CHKERRQ(ierr);
128617ab2063SBarry Smith     array += len; aj += len;
128717ab2063SBarry Smith   }
128817ab2063SBarry Smith   if (shift) {
128917ab2063SBarry Smith     for ( i=0; i<ai[m]-1; i++ ) aj[i] += 1;
129017ab2063SBarry Smith   }
129117ab2063SBarry Smith 
12926d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
12936d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
129417ab2063SBarry Smith 
12953638b69dSLois Curfman McInnes   if (B != PETSC_NULL) {
1296416022c9SBarry Smith     *B = C;
129717ab2063SBarry Smith   } else {
1298f830108cSBarry Smith     PetscOps *Abops;
12990a6ffc59SBarry Smith     MatOps   Aops;
1300f830108cSBarry Smith 
1301416022c9SBarry Smith     /* This isn't really an in-place transpose */
13020452661fSBarry Smith     PetscFree(a->a);
13030452661fSBarry Smith     if (!a->singlemalloc) {PetscFree(a->i); PetscFree(a->j);}
13040452661fSBarry Smith     if (a->diag) PetscFree(a->diag);
13050452661fSBarry Smith     if (a->ilen) PetscFree(a->ilen);
13060452661fSBarry Smith     if (a->imax) PetscFree(a->imax);
13070452661fSBarry Smith     if (a->solve_work) PetscFree(a->solve_work);
13081073c447SSatish Balay     if (a->inode.size) PetscFree(a->inode.size);
13090452661fSBarry Smith     PetscFree(a);
1310f830108cSBarry Smith 
1311488ecbafSBarry Smith 
1312488ecbafSBarry Smith     ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
1313488ecbafSBarry Smith     ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
1314488ecbafSBarry Smith 
1315f830108cSBarry Smith     /*
1316f830108cSBarry Smith       This is horrible, horrible code. We need to keep the
13178f0f457cSSatish Balay       the bops and ops Structures, copy everything from C
13188f0f457cSSatish Balay       including the function pointers..
1319f830108cSBarry Smith     */
13208f0f457cSSatish Balay     PetscMemcpy(A->ops,C->ops,sizeof(struct _MatOps));
13218f0f457cSSatish Balay     PetscMemcpy(A->bops,C->bops,sizeof(PetscOps));
1322f830108cSBarry Smith     Abops    = A->bops;
1323f830108cSBarry Smith     Aops     = A->ops;
1324f09e8eb9SSatish Balay     PetscMemcpy(A,C,sizeof(struct _p_Mat));
1325f830108cSBarry Smith     A->bops  = Abops;
1326f830108cSBarry Smith     A->ops   = Aops;
132727a8da17SBarry Smith     A->qlist = 0;
1328f830108cSBarry Smith 
13290452661fSBarry Smith     PetscHeaderDestroy(C);
133017ab2063SBarry Smith   }
13313a40ed3dSBarry Smith   PetscFunctionReturn(0);
133217ab2063SBarry Smith }
133317ab2063SBarry Smith 
13345615d1e5SSatish Balay #undef __FUNC__
13355615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_SeqAIJ"
13368f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
133717ab2063SBarry Smith {
1338416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
133917ab2063SBarry Smith   Scalar     *l,*r,x,*v;
1340e1311b90SBarry Smith   int        ierr,i,j,m = a->m, n = a->n, M, nz = a->nz, *jj,shift = a->indexshift;
134117ab2063SBarry Smith 
13423a40ed3dSBarry Smith   PetscFunctionBegin;
134317ab2063SBarry Smith   if (ll) {
13443ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
13453ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1346e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1347a8c6a408SBarry Smith     if (m != a->m) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Left scaling vector wrong length");
1348e1311b90SBarry Smith     ierr = VecGetArray(ll,&l); CHKERRQ(ierr);
1349416022c9SBarry Smith     v = a->a;
135017ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
135117ab2063SBarry Smith       x = l[i];
1352416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
135317ab2063SBarry Smith       for ( j=0; j<M; j++ ) { (*v++) *= x;}
135417ab2063SBarry Smith     }
1355e1311b90SBarry Smith     ierr = VecRestoreArray(ll,&l); CHKERRQ(ierr);
135644cd7ae7SLois Curfman McInnes     PLogFlops(nz);
135717ab2063SBarry Smith   }
135817ab2063SBarry Smith   if (rr) {
1359e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1360a8c6a408SBarry Smith     if (n != a->n) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Right scaling vector wrong length");
1361e1311b90SBarry Smith     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1362416022c9SBarry Smith     v = a->a; jj = a->j;
136317ab2063SBarry Smith     for ( i=0; i<nz; i++ ) {
136417ab2063SBarry Smith       (*v++) *= r[*jj++ + shift];
136517ab2063SBarry Smith     }
1366e1311b90SBarry Smith     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
136744cd7ae7SLois Curfman McInnes     PLogFlops(nz);
136817ab2063SBarry Smith   }
13693a40ed3dSBarry Smith   PetscFunctionReturn(0);
137017ab2063SBarry Smith }
137117ab2063SBarry Smith 
13725615d1e5SSatish Balay #undef __FUNC__
13735615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrix_SeqAIJ"
13746a6a5d1dSBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatGetSubMatrixCall scall,Mat *B)
137517ab2063SBarry Smith {
1376db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ *) A->data,*c;
1377d5db1b72SBarry Smith   int          *smap, i, k, kstart, kend, ierr, oldcols = a->n,*lens;
137899141d43SSatish Balay   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen;
1379a2744918SBarry Smith   register int sum,lensi;
138099141d43SSatish Balay   int          *irow, *icol, nrows, ncols, shift = a->indexshift,*ssmap;
138199141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j, *ai = a->i,ii,*ailen = a->ilen;
138299141d43SSatish Balay   Scalar       *a_new,*mat_a;
1383416022c9SBarry Smith   Mat          C;
1384fee21e36SBarry Smith   PetscTruth   stride;
138517ab2063SBarry Smith 
13863a40ed3dSBarry Smith   PetscFunctionBegin;
1387d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
1388a8c6a408SBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"ISrow is not sorted");
1389d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
1390a8c6a408SBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"IScol is not sorted");
139199141d43SSatish Balay 
139217ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow); CHKERRQ(ierr);
139317ab2063SBarry Smith   ierr = ISGetSize(isrow,&nrows); CHKERRQ(ierr);
139417ab2063SBarry Smith   ierr = ISGetSize(iscol,&ncols); CHKERRQ(ierr);
139517ab2063SBarry Smith 
1396fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step); CHKERRQ(ierr);
1397fee21e36SBarry Smith   ierr = ISStride(iscol,&stride); CHKERRQ(ierr);
1398fee21e36SBarry Smith   if (stride && step == 1) {
139902834360SBarry Smith     /* special case of contiguous rows */
140057faeb66SBarry Smith     lens   = (int *) PetscMalloc((ncols+nrows+1)*sizeof(int)); CHKPTRQ(lens);
140102834360SBarry Smith     starts = lens + ncols;
140202834360SBarry Smith     /* loop over new rows determining lens and starting points */
140302834360SBarry Smith     for (i=0; i<nrows; i++) {
1404a2744918SBarry Smith       kstart  = ai[irow[i]]+shift;
1405a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
140602834360SBarry Smith       for ( k=kstart; k<kend; k++ ) {
1407d8ced48eSBarry Smith         if (aj[k]+shift >= first) {
140802834360SBarry Smith           starts[i] = k;
140902834360SBarry Smith           break;
141002834360SBarry Smith 	}
141102834360SBarry Smith       }
1412a2744918SBarry Smith       sum = 0;
141302834360SBarry Smith       while (k < kend) {
1414d8ced48eSBarry Smith         if (aj[k++]+shift >= first+ncols) break;
1415a2744918SBarry Smith         sum++;
141602834360SBarry Smith       }
1417a2744918SBarry Smith       lens[i] = sum;
141802834360SBarry Smith     }
141902834360SBarry Smith     /* create submatrix */
1420cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
142108480c60SBarry Smith       int n_cols,n_rows;
142208480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols); CHKERRQ(ierr);
1423a8c6a408SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Reused submatrix wrong size");
1424d8ced48eSBarry Smith       ierr = MatZeroEntries(*B); CHKERRQ(ierr);
142508480c60SBarry Smith       C = *B;
14263a40ed3dSBarry Smith     } else {
142702834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
142808480c60SBarry Smith     }
1429db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*) C->data;
1430db02288aSLois Curfman McInnes 
143102834360SBarry Smith     /* loop over rows inserting into submatrix */
1432db02288aSLois Curfman McInnes     a_new    = c->a;
1433db02288aSLois Curfman McInnes     j_new    = c->j;
1434db02288aSLois Curfman McInnes     i_new    = c->i;
1435db02288aSLois Curfman McInnes     i_new[0] = -shift;
143602834360SBarry Smith     for (i=0; i<nrows; i++) {
1437a2744918SBarry Smith       ii    = starts[i];
1438a2744918SBarry Smith       lensi = lens[i];
1439a2744918SBarry Smith       for ( k=0; k<lensi; k++ ) {
1440a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
144102834360SBarry Smith       }
1442a2744918SBarry Smith       PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(Scalar));
1443a2744918SBarry Smith       a_new      += lensi;
1444a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1445a2744918SBarry Smith       c->ilen[i]  = lensi;
144602834360SBarry Smith     }
14470452661fSBarry Smith     PetscFree(lens);
14483a40ed3dSBarry Smith   } else {
144902834360SBarry Smith     ierr = ISGetIndices(iscol,&icol); CHKERRQ(ierr);
14500452661fSBarry Smith     smap  = (int *) PetscMalloc((1+oldcols)*sizeof(int)); CHKPTRQ(smap);
145102834360SBarry Smith     ssmap = smap + shift;
145299141d43SSatish Balay     lens  = (int *) PetscMalloc((1+nrows)*sizeof(int)); CHKPTRQ(lens);
1453cddf8d76SBarry Smith     PetscMemzero(smap,oldcols*sizeof(int));
145417ab2063SBarry Smith     for ( i=0; i<ncols; i++ ) smap[icol[i]] = i+1;
145502834360SBarry Smith     /* determine lens of each row */
145602834360SBarry Smith     for (i=0; i<nrows; i++) {
1457d8ced48eSBarry Smith       kstart  = ai[irow[i]]+shift;
145802834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
145902834360SBarry Smith       lens[i] = 0;
146002834360SBarry Smith       for ( k=kstart; k<kend; k++ ) {
1461d8ced48eSBarry Smith         if (ssmap[aj[k]]) {
146202834360SBarry Smith           lens[i]++;
146302834360SBarry Smith         }
146402834360SBarry Smith       }
146502834360SBarry Smith     }
146617ab2063SBarry Smith     /* Create and fill new matrix */
1467a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
146899141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1469a8c6a408SBarry Smith       if (c->m  != nrows || c->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong size");
147099141d43SSatish Balay       if (PetscMemcmp(c->ilen,lens, c->m *sizeof(int))) {
1471a8c6a408SBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong no of nonzeros");
147299141d43SSatish Balay       }
147399141d43SSatish Balay       PetscMemzero(c->ilen,c->m*sizeof(int));
147408480c60SBarry Smith       C = *B;
14753a40ed3dSBarry Smith     } else {
147602834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
147708480c60SBarry Smith     }
147899141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
147917ab2063SBarry Smith     for (i=0; i<nrows; i++) {
148099141d43SSatish Balay       row    = irow[i];
148199141d43SSatish Balay       kstart = ai[row]+shift;
148299141d43SSatish Balay       kend   = kstart + a->ilen[row];
148399141d43SSatish Balay       mat_i  = c->i[i]+shift;
148499141d43SSatish Balay       mat_j  = c->j + mat_i;
148599141d43SSatish Balay       mat_a  = c->a + mat_i;
148699141d43SSatish Balay       mat_ilen = c->ilen + i;
148717ab2063SBarry Smith       for ( k=kstart; k<kend; k++ ) {
148899141d43SSatish Balay         if ((tcol=ssmap[a->j[k]])) {
148999141d43SSatish Balay           *mat_j++ = tcol - (!shift);
149099141d43SSatish Balay           *mat_a++ = a->a[k];
149199141d43SSatish Balay           (*mat_ilen)++;
149299141d43SSatish Balay 
149317ab2063SBarry Smith         }
149417ab2063SBarry Smith       }
149517ab2063SBarry Smith     }
149602834360SBarry Smith     /* Free work space */
149702834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol); CHKERRQ(ierr);
149899141d43SSatish Balay     PetscFree(smap); PetscFree(lens);
149902834360SBarry Smith   }
15006d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
15016d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
150217ab2063SBarry Smith 
150317ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow); CHKERRQ(ierr);
1504416022c9SBarry Smith   *B = C;
15053a40ed3dSBarry Smith   PetscFunctionReturn(0);
150617ab2063SBarry Smith }
150717ab2063SBarry Smith 
1508a871dcd8SBarry Smith /*
150963b91edcSBarry Smith      note: This can only work for identity for row and col. It would
151063b91edcSBarry Smith    be good to check this and otherwise generate an error.
1511a871dcd8SBarry Smith */
15125615d1e5SSatish Balay #undef __FUNC__
15135615d1e5SSatish Balay #define __FUNC__ "MatILUFactor_SeqAIJ"
15148f6be9afSLois Curfman McInnes int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,double efill,int fill)
1515a871dcd8SBarry Smith {
151663b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) inA->data;
151708480c60SBarry Smith   int        ierr;
151863b91edcSBarry Smith   Mat        outA;
151963b91edcSBarry Smith 
15203a40ed3dSBarry Smith   PetscFunctionBegin;
1521a8c6a408SBarry Smith   if (fill != 0) SETERRQ(PETSC_ERR_SUP,0,"Only fill=0 supported");
1522a871dcd8SBarry Smith 
152363b91edcSBarry Smith   outA          = inA;
152463b91edcSBarry Smith   inA->factor   = FACTOR_LU;
152563b91edcSBarry Smith   a->row        = row;
152663b91edcSBarry Smith   a->col        = col;
152763b91edcSBarry Smith 
1528f0ec6fceSSatish Balay   /* Create the invert permutation so that it can be used in MatLUFactorNumeric() */
1529f0ec6fceSSatish Balay   ierr = ISInvertPermutation(col,&(a->icol)); CHKERRQ(ierr);
15301e4dd532SSatish Balay   PLogObjectParent(inA,a->icol);
1531f0ec6fceSSatish Balay 
153294a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
15330452661fSBarry Smith     a->solve_work = (Scalar *) PetscMalloc( (a->m+1)*sizeof(Scalar));CHKPTRQ(a->solve_work);
153494a9d846SBarry Smith   }
153563b91edcSBarry Smith 
153608480c60SBarry Smith   if (!a->diag) {
153708480c60SBarry Smith     ierr = MatMarkDiag_SeqAIJ(inA); CHKERRQ(ierr);
153863b91edcSBarry Smith   }
153963b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA); CHKERRQ(ierr);
15403a40ed3dSBarry Smith   PetscFunctionReturn(0);
1541a871dcd8SBarry Smith }
1542a871dcd8SBarry Smith 
154374cdf0dfSBarry Smith #include "pinclude/blaslapack.h"
15445615d1e5SSatish Balay #undef __FUNC__
15455615d1e5SSatish Balay #define __FUNC__ "MatScale_SeqAIJ"
15468f6be9afSLois Curfman McInnes int MatScale_SeqAIJ(Scalar *alpha,Mat inA)
1547f0b747eeSBarry Smith {
1548f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) inA->data;
1549f0b747eeSBarry Smith   int        one = 1;
15503a40ed3dSBarry Smith 
15513a40ed3dSBarry Smith   PetscFunctionBegin;
1552f0b747eeSBarry Smith   BLscal_( &a->nz, alpha, a->a, &one );
1553f0b747eeSBarry Smith   PLogFlops(a->nz);
15543a40ed3dSBarry Smith   PetscFunctionReturn(0);
1555f0b747eeSBarry Smith }
1556f0b747eeSBarry Smith 
15575615d1e5SSatish Balay #undef __FUNC__
15585615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrices_SeqAIJ"
15596a6a5d1dSBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n, IS *irow,IS *icol,MatGetSubMatrixCall scall,Mat **B)
1560cddf8d76SBarry Smith {
1561cddf8d76SBarry Smith   int ierr,i;
1562cddf8d76SBarry Smith 
15633a40ed3dSBarry Smith   PetscFunctionBegin;
1564cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
15650452661fSBarry Smith     *B = (Mat *) PetscMalloc( (n+1)*sizeof(Mat) ); CHKPTRQ(*B);
1566cddf8d76SBarry Smith   }
1567cddf8d76SBarry Smith 
1568cddf8d76SBarry Smith   for ( i=0; i<n; i++ ) {
15696a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1570cddf8d76SBarry Smith   }
15713a40ed3dSBarry Smith   PetscFunctionReturn(0);
1572cddf8d76SBarry Smith }
1573cddf8d76SBarry Smith 
15745615d1e5SSatish Balay #undef __FUNC__
15755615d1e5SSatish Balay #define __FUNC__ "MatGetBlockSize_SeqAIJ"
15768f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A, int *bs)
15775a838052SSatish Balay {
1578f830108cSBarry Smith   PetscFunctionBegin;
15795a838052SSatish Balay   *bs = 1;
15803a40ed3dSBarry Smith   PetscFunctionReturn(0);
15815a838052SSatish Balay }
15825a838052SSatish Balay 
15835615d1e5SSatish Balay #undef __FUNC__
15845615d1e5SSatish Balay #define __FUNC__ "MatIncreaseOverlap_SeqAIJ"
15858f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A, int is_max, IS *is, int ov)
15864dcbc457SBarry Smith {
1587e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
158806763907SSatish Balay   int        shift, row, i,j,k,l,m,n, *idx,ierr, *nidx, isz, val;
15898a047759SSatish Balay   int        start, end, *ai, *aj;
1590bbd702dbSSatish Balay   BT         table;
1591bbd702dbSSatish Balay 
15923a40ed3dSBarry Smith   PetscFunctionBegin;
15938a047759SSatish Balay   shift = a->indexshift;
1594e4d965acSSatish Balay   m     = a->m;
1595e4d965acSSatish Balay   ai    = a->i;
15968a047759SSatish Balay   aj    = a->j+shift;
15978a047759SSatish Balay 
1598a8c6a408SBarry Smith   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"illegal overlap value used");
159906763907SSatish Balay 
160006763907SSatish Balay   nidx  = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(nidx);
1601bbd702dbSSatish Balay   ierr  = BTCreate(m,table); CHKERRQ(ierr);
160206763907SSatish Balay 
1603e4d965acSSatish Balay   for ( i=0; i<is_max; i++ ) {
1604b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1605e4d965acSSatish Balay     isz  = 0;
1606bbd702dbSSatish Balay     BTMemzero(m,table);
1607e4d965acSSatish Balay 
1608e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
16094dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);  CHKERRQ(ierr);
161077c4ece6SBarry Smith     ierr = ISGetSize(is[i],&n);  CHKERRQ(ierr);
1611e4d965acSSatish Balay 
1612dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1613e4d965acSSatish Balay     for ( j=0; j<n ; ++j){
1614bbd702dbSSatish Balay       if(!BTLookupSet(table, idx[j])) { nidx[isz++] = idx[j];}
16154dcbc457SBarry Smith     }
161606763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);  CHKERRQ(ierr);
161706763907SSatish Balay     ierr = ISDestroy(is[i]); CHKERRQ(ierr);
1618e4d965acSSatish Balay 
161904a348a9SBarry Smith     k = 0;
162004a348a9SBarry Smith     for ( j=0; j<ov; j++){ /* for each overlap */
162104a348a9SBarry Smith       n = isz;
162206763907SSatish Balay       for ( ; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1623e4d965acSSatish Balay         row   = nidx[k];
1624e4d965acSSatish Balay         start = ai[row];
1625e4d965acSSatish Balay         end   = ai[row+1];
162604a348a9SBarry Smith         for ( l = start; l<end ; l++){
16278a047759SSatish Balay           val = aj[l] + shift;
16282a8f2162SSatish Balay           if (!BTLookupSet(table,val)) {nidx[isz++] = val;}
1629e4d965acSSatish Balay         }
1630e4d965acSSatish Balay       }
1631e4d965acSSatish Balay     }
1632029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF, isz, nidx, (is+i)); CHKERRQ(ierr);
1633e4d965acSSatish Balay   }
1634bbd702dbSSatish Balay   BTDestroy(table);
163506763907SSatish Balay   PetscFree(nidx);
16363a40ed3dSBarry Smith   PetscFunctionReturn(0);
16374dcbc457SBarry Smith }
163817ab2063SBarry Smith 
16390513a670SBarry Smith /* -------------------------------------------------------------- */
16400513a670SBarry Smith #undef __FUNC__
16410513a670SBarry Smith #define __FUNC__ "MatPermute_SeqAIJ"
16420513a670SBarry Smith int MatPermute_SeqAIJ(Mat A, IS rowp, IS colp, Mat *B)
16430513a670SBarry Smith {
16440513a670SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
16450513a670SBarry Smith   Scalar     *vwork;
16460513a670SBarry Smith   int        i, ierr, nz, m = a->m, n = a->n, *cwork;
16470513a670SBarry Smith   int        *row,*col,*cnew,j,*lens;
164856cd22aeSBarry Smith   IS         icolp,irowp;
16490513a670SBarry Smith 
16503a40ed3dSBarry Smith   PetscFunctionBegin;
165156cd22aeSBarry Smith   ierr = ISInvertPermutation(rowp,&irowp); CHKERRQ(ierr);
165256cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row); CHKERRQ(ierr);
165356cd22aeSBarry Smith   ierr = ISInvertPermutation(colp,&icolp); CHKERRQ(ierr);
165456cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col); CHKERRQ(ierr);
16550513a670SBarry Smith 
16560513a670SBarry Smith   /* determine lengths of permuted rows */
16570513a670SBarry Smith   lens = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(lens);
16580513a670SBarry Smith   for (i=0; i<m; i++ ) {
16590513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
16600513a670SBarry Smith   }
16610513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
16620513a670SBarry Smith   PetscFree(lens);
16630513a670SBarry Smith 
16640513a670SBarry Smith   cnew = (int *) PetscMalloc( n*sizeof(int) ); CHKPTRQ(cnew);
16650513a670SBarry Smith   for (i=0; i<m; i++) {
16660513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr);
16670513a670SBarry Smith     for (j=0; j<nz; j++ ) { cnew[j] = col[cwork[j]];}
16680513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES); CHKERRQ(ierr);
16690513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr);
16700513a670SBarry Smith   }
16710513a670SBarry Smith   PetscFree(cnew);
16720513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
16730513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
167456cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row); CHKERRQ(ierr);
167556cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col); CHKERRQ(ierr);
167656cd22aeSBarry Smith   ierr = ISDestroy(irowp); CHKERRQ(ierr);
167756cd22aeSBarry Smith   ierr = ISDestroy(icolp); CHKERRQ(ierr);
16783a40ed3dSBarry Smith   PetscFunctionReturn(0);
16790513a670SBarry Smith }
16800513a670SBarry Smith 
16815615d1e5SSatish Balay #undef __FUNC__
16825615d1e5SSatish Balay #define __FUNC__ "MatPrintHelp_SeqAIJ"
1683682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1684682d7d0cSBarry Smith {
1685682d7d0cSBarry Smith   static int called = 0;
1686682d7d0cSBarry Smith   MPI_Comm   comm = A->comm;
1687682d7d0cSBarry Smith 
16883a40ed3dSBarry Smith   PetscFunctionBegin;
16893a40ed3dSBarry Smith   if (called) {PetscFunctionReturn(0);} else called = 1;
169076be9ce4SBarry Smith   (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");
169176be9ce4SBarry Smith   (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");
169276be9ce4SBarry Smith   (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");
169376be9ce4SBarry Smith   (*PetscHelpPrintf)(comm,"  -mat_aij_no_inode: Do not use inodes\n");
169476be9ce4SBarry Smith   (*PetscHelpPrintf)(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");
1695682d7d0cSBarry Smith #if defined(HAVE_ESSL)
169676be9ce4SBarry Smith   (*PetscHelpPrintf)(comm,"  -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");
1697682d7d0cSBarry Smith #endif
16983a40ed3dSBarry Smith   PetscFunctionReturn(0);
1699682d7d0cSBarry Smith }
17008f6be9afSLois Curfman McInnes extern int MatEqual_SeqAIJ(Mat A,Mat B, PetscTruth* flg);
1701a93ec695SBarry Smith extern int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
1702a93ec695SBarry Smith extern int MatColoringPatch_SeqAIJ(Mat,int,int *,ISColoring *);
1703a93ec695SBarry Smith 
1704cb5b572fSBarry Smith #undef __FUNC__
1705cb5b572fSBarry Smith #define __FUNC__ "MatCopy_SeqAIJ"
1706cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1707cb5b572fSBarry Smith {
1708be6bf707SBarry Smith   int    ierr;
1709cb5b572fSBarry Smith 
1710cb5b572fSBarry Smith   PetscFunctionBegin;
1711be6bf707SBarry Smith   if (str == SAME_NONZERO_PATTERN && B->type == MATSEQAIJ) {
1712be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
1713be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ *) B->data;
1714be6bf707SBarry Smith 
1715be6bf707SBarry Smith     if (a->nonew != 1) SETERRQ(1,1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
1716be6bf707SBarry Smith     if (b->nonew != 1) SETERRQ(1,1,"Must call MatSetOption(B,MAT_NO_NEW_NONZERO_LOCATIONS);first");
1717be6bf707SBarry Smith     if (a->i[a->m]+a->indexshift != b->i[b->m]+a->indexshift) {
1718be6bf707SBarry Smith       SETERRQ(1,1,"Number of nonzeros in two matrices are different");
1719cb5b572fSBarry Smith     }
1720be6bf707SBarry Smith     PetscMemcpy(b->a,a->a,(a->i[a->m]+a->indexshift)*sizeof(Scalar));
1721cb5b572fSBarry Smith   } else {
1722cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1723cb5b572fSBarry Smith   }
1724cb5b572fSBarry Smith   PetscFunctionReturn(0);
1725cb5b572fSBarry Smith }
1726cb5b572fSBarry Smith 
1727682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
17280a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
1729cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
1730cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
1731cb5b572fSBarry Smith        MatMult_SeqAIJ,
1732cb5b572fSBarry Smith        MatMultAdd_SeqAIJ,
1733cb5b572fSBarry Smith        MatMultTrans_SeqAIJ,
1734cb5b572fSBarry Smith        MatMultTransAdd_SeqAIJ,
1735cb5b572fSBarry Smith        MatSolve_SeqAIJ,
1736cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
1737cb5b572fSBarry Smith        MatSolveTrans_SeqAIJ,
1738cb5b572fSBarry Smith        MatSolveTransAdd_SeqAIJ,
1739cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
1740cb5b572fSBarry Smith        0,
174117ab2063SBarry Smith        MatRelax_SeqAIJ,
174217ab2063SBarry Smith        MatTranspose_SeqAIJ,
1743cb5b572fSBarry Smith        MatGetInfo_SeqAIJ,
1744cb5b572fSBarry Smith        MatEqual_SeqAIJ,
1745cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
1746cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
1747cb5b572fSBarry Smith        MatNorm_SeqAIJ,
1748cb5b572fSBarry Smith        0,
1749cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
175017ab2063SBarry Smith        MatCompress_SeqAIJ,
1751cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
1752cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
1753cb5b572fSBarry Smith        MatZeroRows_SeqAIJ,
1754cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
1755cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
1756cb5b572fSBarry Smith        0,
1757cb5b572fSBarry Smith        0,
1758cb5b572fSBarry Smith        MatGetSize_SeqAIJ,
1759cb5b572fSBarry Smith        MatGetSize_SeqAIJ,
1760cb5b572fSBarry Smith        MatGetOwnershipRange_SeqAIJ,
1761cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
1762cb5b572fSBarry Smith        0,
1763cb5b572fSBarry Smith        0,
1764cb5b572fSBarry Smith        0,
1765be6bf707SBarry Smith        MatDuplicate_SeqAIJ,
1766cb5b572fSBarry Smith        0,
1767cb5b572fSBarry Smith        0,
1768cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
1769cb5b572fSBarry Smith        0,
1770cb5b572fSBarry Smith        0,
1771cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
1772cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
1773cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
1774cb5b572fSBarry Smith        MatCopy_SeqAIJ,
1775f0b747eeSBarry Smith        MatPrintHelp_SeqAIJ,
1776cb5b572fSBarry Smith        MatScale_SeqAIJ,
1777cb5b572fSBarry Smith        0,
1778cb5b572fSBarry Smith        0,
17796945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
17806945ee14SBarry Smith        MatGetBlockSize_SeqAIJ,
17813b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
17823b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
17833b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
1784a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
1785a93ec695SBarry Smith        MatFDColoringCreate_SeqAIJ,
17860513a670SBarry Smith        MatColoringPatch_SeqAIJ,
17870513a670SBarry Smith        0,
1788cda55fadSBarry Smith        MatPermute_SeqAIJ,
1789cda55fadSBarry Smith        0,
1790cda55fadSBarry Smith        0,
1791cda55fadSBarry Smith        0,
1792cda55fadSBarry Smith        0,
1793cda55fadSBarry Smith        MatGetMaps_Petsc};
179417ab2063SBarry Smith 
179517ab2063SBarry Smith extern int MatUseSuperLU_SeqAIJ(Mat);
179617ab2063SBarry Smith extern int MatUseEssl_SeqAIJ(Mat);
179717ab2063SBarry Smith extern int MatUseDXML_SeqAIJ(Mat);
179817ab2063SBarry Smith 
17995615d1e5SSatish Balay #undef __FUNC__
1800bef8e0ddSBarry Smith #define __FUNC__ "MatSeqAIJSetColumnIndices_SeqAIJ"
1801bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices)
1802bef8e0ddSBarry Smith {
1803bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
1804bef8e0ddSBarry Smith   int        i,nz,n;
1805bef8e0ddSBarry Smith 
1806bef8e0ddSBarry Smith   PetscFunctionBegin;
1807bef8e0ddSBarry Smith   if (aij->indexshift) SETERRQ(1,1,"No support with 1 based indexing");
1808bef8e0ddSBarry Smith 
1809bef8e0ddSBarry Smith   nz = aij->maxnz;
1810bef8e0ddSBarry Smith   n  = aij->n;
1811bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
1812bef8e0ddSBarry Smith     aij->j[i] = indices[i];
1813bef8e0ddSBarry Smith   }
1814bef8e0ddSBarry Smith   aij->nz = nz;
1815bef8e0ddSBarry Smith   for ( i=0; i<n; i++ ) {
1816bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
1817bef8e0ddSBarry Smith   }
1818bef8e0ddSBarry Smith 
1819bef8e0ddSBarry Smith   PetscFunctionReturn(0);
1820bef8e0ddSBarry Smith }
1821bef8e0ddSBarry Smith 
1822bef8e0ddSBarry Smith #undef __FUNC__
1823bef8e0ddSBarry Smith #define __FUNC__ "MatSeqAIJSetColumnIndices"
1824bef8e0ddSBarry Smith /*@
1825bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
1826bef8e0ddSBarry Smith        in the matrix.
1827bef8e0ddSBarry Smith 
1828bef8e0ddSBarry Smith   Input Parameters:
1829bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
1830bef8e0ddSBarry Smith -  indices - the column indices
1831bef8e0ddSBarry Smith 
1832bef8e0ddSBarry Smith   Notes:
1833bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
1834bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
1835bef8e0ddSBarry Smith   of the MatSetValues() operation.
1836bef8e0ddSBarry Smith 
1837bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
1838bef8e0ddSBarry Smith   MatCreateSeqAIJ().
1839bef8e0ddSBarry Smith 
1840bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
1841bef8e0ddSBarry Smith 
1842bef8e0ddSBarry Smith @*/
1843bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices)
1844bef8e0ddSBarry Smith {
1845bef8e0ddSBarry Smith   int ierr,(*f)(Mat,int *);
1846bef8e0ddSBarry Smith 
1847bef8e0ddSBarry Smith   PetscFunctionBegin;
1848bef8e0ddSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1849bef8e0ddSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void **)&f);
1850bef8e0ddSBarry Smith          CHKERRQ(ierr);
1851bef8e0ddSBarry Smith   if (f) {
1852bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
1853bef8e0ddSBarry Smith   } else {
1854bef8e0ddSBarry Smith     SETERRQ(1,1,"Wrong type of matrix to set column indices");
1855bef8e0ddSBarry Smith   }
1856bef8e0ddSBarry Smith   PetscFunctionReturn(0);
1857bef8e0ddSBarry Smith }
1858bef8e0ddSBarry Smith 
1859be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
1860be6bf707SBarry Smith 
1861be6bf707SBarry Smith #undef __FUNC__
1862be6bf707SBarry Smith #define __FUNC__ "MatStoreValues_SeqAIJ"
1863be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat)
1864be6bf707SBarry Smith {
1865be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
1866be6bf707SBarry Smith   int        nz = aij->i[aij->m]+aij->indexshift;
1867be6bf707SBarry Smith 
1868be6bf707SBarry Smith   PetscFunctionBegin;
1869be6bf707SBarry Smith   if (aij->nonew != 1) {
1870be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
1871be6bf707SBarry Smith   }
1872be6bf707SBarry Smith 
1873be6bf707SBarry Smith   /* allocate space for values if not already there */
1874be6bf707SBarry Smith   if (!aij->saved_values) {
1875be6bf707SBarry Smith     aij->saved_values = (Scalar *) PetscMalloc(nz*sizeof(Scalar));CHKPTRQ(aij->saved_values);
1876be6bf707SBarry Smith   }
1877be6bf707SBarry Smith 
1878be6bf707SBarry Smith   /* copy values over */
1879be6bf707SBarry Smith   PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(Scalar));
1880be6bf707SBarry Smith   PetscFunctionReturn(0);
1881be6bf707SBarry Smith }
1882be6bf707SBarry Smith 
1883be6bf707SBarry Smith #undef __FUNC__
1884be6bf707SBarry Smith #define __FUNC__ "MatStoreValues"
1885be6bf707SBarry Smith /*@
1886be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
1887be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
1888be6bf707SBarry Smith        nonlinear portion.
1889be6bf707SBarry Smith 
1890be6bf707SBarry Smith    Collect on Mat
1891be6bf707SBarry Smith 
1892be6bf707SBarry Smith   Input Parameters:
1893be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
1894be6bf707SBarry Smith 
1895be6bf707SBarry Smith   Common Usage, with SNESSolve():
1896be6bf707SBarry Smith $    Create Jacobian matrix
1897be6bf707SBarry Smith $    Set linear terms into matrix
1898be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
1899be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
1900be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
1901be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
1902be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
1903be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
1904be6bf707SBarry Smith $    In your Jacobian routine
1905be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
1906be6bf707SBarry Smith $      Set nonlinear terms in matrix
1907be6bf707SBarry Smith 
1908be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
1909be6bf707SBarry Smith $    // build linear portion of Jacobian
1910be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
1911be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
1912be6bf707SBarry Smith $    loop over nonlinear iterations
1913be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
1914be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
1915be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
1916be6bf707SBarry Smith $       Solve linear system with Jacobian
1917be6bf707SBarry Smith $    endloop
1918be6bf707SBarry Smith 
1919be6bf707SBarry Smith   Notes:
1920be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
1921be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
1922be6bf707SBarry Smith     calling this routine.
1923be6bf707SBarry Smith 
1924be6bf707SBarry Smith .seealso: MatRetrieveValues()
1925be6bf707SBarry Smith 
1926be6bf707SBarry Smith @*/
1927be6bf707SBarry Smith int MatStoreValues(Mat mat)
1928be6bf707SBarry Smith {
1929be6bf707SBarry Smith   int ierr,(*f)(Mat);
1930be6bf707SBarry Smith 
1931be6bf707SBarry Smith   PetscFunctionBegin;
1932be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1933be6bf707SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1934be6bf707SBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
1935be6bf707SBarry Smith 
1936be6bf707SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void **)&f);
1937be6bf707SBarry Smith          CHKERRQ(ierr);
1938be6bf707SBarry Smith   if (f) {
1939be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
1940be6bf707SBarry Smith   } else {
1941be6bf707SBarry Smith     SETERRQ(1,1,"Wrong type of matrix to store values");
1942be6bf707SBarry Smith   }
1943be6bf707SBarry Smith   PetscFunctionReturn(0);
1944be6bf707SBarry Smith }
1945be6bf707SBarry Smith 
1946be6bf707SBarry Smith #undef __FUNC__
1947be6bf707SBarry Smith #define __FUNC__ "MatRetrieveValues_SeqAIJ"
1948be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat)
1949be6bf707SBarry Smith {
1950be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
1951be6bf707SBarry Smith   int        nz = aij->i[aij->m]+aij->indexshift;
1952be6bf707SBarry Smith 
1953be6bf707SBarry Smith   PetscFunctionBegin;
1954be6bf707SBarry Smith   if (aij->nonew != 1) {
1955be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
1956be6bf707SBarry Smith   }
1957be6bf707SBarry Smith   if (!aij->saved_values) {
1958be6bf707SBarry Smith     SETERRQ(1,1,"Must call MatStoreValues(A);first");
1959be6bf707SBarry Smith   }
1960be6bf707SBarry Smith 
1961be6bf707SBarry Smith   /* copy values over */
1962be6bf707SBarry Smith   PetscMemcpy(aij->a, aij->saved_values,nz*sizeof(Scalar));
1963be6bf707SBarry Smith   PetscFunctionReturn(0);
1964be6bf707SBarry Smith }
1965be6bf707SBarry Smith 
1966be6bf707SBarry Smith #undef __FUNC__
1967be6bf707SBarry Smith #define __FUNC__ "MatRetrieveValues"
1968be6bf707SBarry Smith /*@
1969be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
1970be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
1971be6bf707SBarry Smith        nonlinear portion.
1972be6bf707SBarry Smith 
1973be6bf707SBarry Smith    Collect on Mat
1974be6bf707SBarry Smith 
1975be6bf707SBarry Smith   Input Parameters:
1976be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
1977be6bf707SBarry Smith 
1978be6bf707SBarry Smith .seealso: MatStoreValues()
1979be6bf707SBarry Smith 
1980be6bf707SBarry Smith @*/
1981be6bf707SBarry Smith int MatRetrieveValues(Mat mat)
1982be6bf707SBarry Smith {
1983be6bf707SBarry Smith   int ierr,(*f)(Mat);
1984be6bf707SBarry Smith 
1985be6bf707SBarry Smith   PetscFunctionBegin;
1986be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1987be6bf707SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix");
1988be6bf707SBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix");
1989be6bf707SBarry Smith 
1990be6bf707SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void **)&f);
1991be6bf707SBarry Smith          CHKERRQ(ierr);
1992be6bf707SBarry Smith   if (f) {
1993be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
1994be6bf707SBarry Smith   } else {
1995be6bf707SBarry Smith     SETERRQ(1,1,"Wrong type of matrix to retrieve values");
1996be6bf707SBarry Smith   }
1997be6bf707SBarry Smith   PetscFunctionReturn(0);
1998be6bf707SBarry Smith }
1999be6bf707SBarry Smith 
2000be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
2001cb5b572fSBarry Smith 
2002bef8e0ddSBarry Smith #undef __FUNC__
20035615d1e5SSatish Balay #define __FUNC__ "MatCreateSeqAIJ"
200417ab2063SBarry Smith /*@C
2005682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
20060d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
20076e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
20082bd5e0b2SLois Curfman McInnes    (or the array nzz).  By setting these parameters accurately, performance
20092bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
201017ab2063SBarry Smith 
2011db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2012db81eaa0SLois Curfman McInnes 
201317ab2063SBarry Smith    Input Parameters:
2014db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
201517ab2063SBarry Smith .  m - number of rows
201617ab2063SBarry Smith .  n - number of columns
201717ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2018db81eaa0SLois Curfman McInnes -  nzz - array containing the number of nonzeros in the various rows
20192bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
202017ab2063SBarry Smith 
202117ab2063SBarry Smith    Output Parameter:
2022416022c9SBarry Smith .  A - the matrix
202317ab2063SBarry Smith 
2024b259b22eSLois Curfman McInnes    Notes:
202517ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
202617ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
20270002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
202844cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
202917ab2063SBarry Smith 
203017ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2031a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
20323d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
20336da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
203417ab2063SBarry Smith 
2035682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
20364fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2037682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
20386c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
20396c7ebb05SLois Curfman McInnes 
20406c7ebb05SLois Curfman McInnes    Options Database Keys:
2041db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2042db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2043db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2044db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2045db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
204617ab2063SBarry Smith 
2047bef8e0ddSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices()
204817ab2063SBarry Smith @*/
2049416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz, Mat *A)
205017ab2063SBarry Smith {
2051416022c9SBarry Smith   Mat        B;
2052416022c9SBarry Smith   Mat_SeqAIJ *b;
20536945ee14SBarry Smith   int        i, len, ierr, flg,size;
20546945ee14SBarry Smith 
20553a40ed3dSBarry Smith   PetscFunctionBegin;
20566945ee14SBarry Smith   MPI_Comm_size(comm,&size);
2057a8c6a408SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Comm must be of size 1");
2058d5d45c9bSBarry Smith 
2059416022c9SBarry Smith   *A                  = 0;
2060f830108cSBarry Smith   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATSEQAIJ,comm,MatDestroy,MatView);
2061416022c9SBarry Smith   PLogObjectCreate(B);
20620452661fSBarry Smith   B->data             = (void *) (b = PetscNew(Mat_SeqAIJ)); CHKPTRQ(b);
206344cd7ae7SLois Curfman McInnes   PetscMemzero(b,sizeof(Mat_SeqAIJ));
20640a6ffc59SBarry Smith   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
2065e1311b90SBarry Smith   B->ops->destroy          = MatDestroy_SeqAIJ;
2066e1311b90SBarry Smith   B->ops->view             = MatView_SeqAIJ;
2067416022c9SBarry Smith   B->factor           = 0;
2068416022c9SBarry Smith   B->lupivotthreshold = 1.0;
206990f02eecSBarry Smith   B->mapping          = 0;
2070e1311b90SBarry Smith   ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold,&flg);CHKERRQ(ierr);
20717a743949SBarry Smith   b->ilu_preserve_row_sums = PETSC_FALSE;
2072e1311b90SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums",(int*)&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2073416022c9SBarry Smith   b->row              = 0;
2074416022c9SBarry Smith   b->col              = 0;
207582bf6240SBarry Smith   b->icol             = 0;
2076416022c9SBarry Smith   b->indexshift       = 0;
2077b810aeb4SBarry Smith   b->reallocs         = 0;
207869957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_oneindex", &flg); CHKERRQ(ierr);
207969957df2SSatish Balay   if (flg) b->indexshift = -1;
208017ab2063SBarry Smith 
208144cd7ae7SLois Curfman McInnes   b->m = m; B->m = m; B->M = m;
208244cd7ae7SLois Curfman McInnes   b->n = n; B->n = n; B->N = n;
2083a5ae1ecdSBarry Smith 
20844d197716SBarry Smith   ierr = MapCreateMPI(comm,m,m,&B->rmap);CHKERRQ(ierr);
20854d197716SBarry Smith   ierr = MapCreateMPI(comm,n,n,&B->cmap);CHKERRQ(ierr);
2086a5ae1ecdSBarry Smith 
20870452661fSBarry Smith   b->imax = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(b->imax);
2088b4fd4287SBarry Smith   if (nnz == PETSC_NULL) {
20897b8455f0SLois Curfman McInnes     if (nz == PETSC_DEFAULT) nz = 10;
20907b8455f0SLois Curfman McInnes     else if (nz <= 0)        nz = 1;
2091416022c9SBarry Smith     for ( i=0; i<m; i++ ) b->imax[i] = nz;
209217ab2063SBarry Smith     nz = nz*m;
20933a40ed3dSBarry Smith   } else {
209417ab2063SBarry Smith     nz = 0;
2095416022c9SBarry Smith     for ( i=0; i<m; i++ ) {b->imax[i] = nnz[i]; nz += nnz[i];}
209617ab2063SBarry Smith   }
209717ab2063SBarry Smith 
209817ab2063SBarry Smith   /* allocate the matrix space */
2099416022c9SBarry Smith   len             = nz*(sizeof(int) + sizeof(Scalar)) + (b->m+1)*sizeof(int);
21000452661fSBarry Smith   b->a            = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
2101416022c9SBarry Smith   b->j            = (int *) (b->a + nz);
2102cddf8d76SBarry Smith   PetscMemzero(b->j,nz*sizeof(int));
2103416022c9SBarry Smith   b->i            = b->j + nz;
2104416022c9SBarry Smith   b->singlemalloc = 1;
210517ab2063SBarry Smith 
2106416022c9SBarry Smith   b->i[0] = -b->indexshift;
210717ab2063SBarry Smith   for (i=1; i<m+1; i++) {
2108416022c9SBarry Smith     b->i[i] = b->i[i-1] + b->imax[i-1];
210917ab2063SBarry Smith   }
211017ab2063SBarry Smith 
2111416022c9SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
21120452661fSBarry Smith   b->ilen = (int *) PetscMalloc((m+1)*sizeof(int));
2113f09e8eb9SSatish Balay   PLogObjectMemory(B,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2114416022c9SBarry Smith   for ( i=0; i<b->m; i++ ) { b->ilen[i] = 0;}
211517ab2063SBarry Smith 
2116416022c9SBarry Smith   b->nz               = 0;
2117416022c9SBarry Smith   b->maxnz            = nz;
2118416022c9SBarry Smith   b->sorted           = 0;
2119416022c9SBarry Smith   b->roworiented      = 1;
2120416022c9SBarry Smith   b->nonew            = 0;
2121416022c9SBarry Smith   b->diag             = 0;
2122416022c9SBarry Smith   b->solve_work       = 0;
212371bd300dSLois Curfman McInnes   b->spptr            = 0;
2124754ec7b1SSatish Balay   b->inode.node_count = 0;
2125754ec7b1SSatish Balay   b->inode.size       = 0;
21266c7ebb05SLois Curfman McInnes   b->inode.limit      = 5;
21276c7ebb05SLois Curfman McInnes   b->inode.max_limit  = 5;
2128be6bf707SBarry Smith   b->saved_values     = 0;
21294e220ebcSLois Curfman McInnes   B->info.nz_unneeded = (double)b->maxnz;
213017ab2063SBarry Smith 
2131416022c9SBarry Smith   *A = B;
21324e220ebcSLois Curfman McInnes 
21334b14c69eSBarry Smith   /*  SuperLU is not currently supported through PETSc */
21344b14c69eSBarry Smith #if defined(HAVE_SUPERLU)
213569957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_superlu", &flg); CHKERRQ(ierr);
213669957df2SSatish Balay   if (flg) { ierr = MatUseSuperLU_SeqAIJ(B); CHKERRQ(ierr); }
21374b14c69eSBarry Smith #endif
213869957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_essl", &flg); CHKERRQ(ierr);
213969957df2SSatish Balay   if (flg) { ierr = MatUseEssl_SeqAIJ(B); CHKERRQ(ierr); }
214069957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-mat_aij_dxml", &flg); CHKERRQ(ierr);
214169957df2SSatish Balay   if (flg) {
2142a8c6a408SBarry Smith     if (!b->indexshift) SETERRQ( PETSC_ERR_LIB,0,"need -mat_aij_oneindex with -mat_aij_dxml");
2143416022c9SBarry Smith     ierr = MatUseDXML_SeqAIJ(B); CHKERRQ(ierr);
214417ab2063SBarry Smith   }
214569957df2SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr);
214669957df2SSatish Balay   if (flg) {ierr = MatPrintHelp(B); CHKERRQ(ierr); }
2147bef8e0ddSBarry Smith 
2148bef8e0ddSBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2149bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2150bef8e0ddSBarry Smith                                      (void*)MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2151be6bf707SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",
2152be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2153be6bf707SBarry Smith                                      (void*)MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2154be6bf707SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",
2155be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2156be6bf707SBarry Smith                                      (void*)MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
21573a40ed3dSBarry Smith   PetscFunctionReturn(0);
215817ab2063SBarry Smith }
215917ab2063SBarry Smith 
21605615d1e5SSatish Balay #undef __FUNC__
2161be6bf707SBarry Smith #define __FUNC__ "MatDuplicate_SeqAIJ"
2162be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
216317ab2063SBarry Smith {
2164416022c9SBarry Smith   Mat        C;
2165416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ *) A->data;
2166bef8e0ddSBarry Smith   int        i,len, m = a->m,shift = a->indexshift,ierr;
216717ab2063SBarry Smith 
21683a40ed3dSBarry Smith   PetscFunctionBegin;
21694043dd9cSLois Curfman McInnes   *B = 0;
2170f830108cSBarry Smith   PetscHeaderCreate(C,_p_Mat,struct _MatOps,MAT_COOKIE,MATSEQAIJ,A->comm,MatDestroy,MatView);
2171416022c9SBarry Smith   PLogObjectCreate(C);
21720452661fSBarry Smith   C->data       = (void *) (c = PetscNew(Mat_SeqAIJ)); CHKPTRQ(c);
2173f830108cSBarry Smith   PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));
2174e1311b90SBarry Smith   C->ops->destroy    = MatDestroy_SeqAIJ;
2175e1311b90SBarry Smith   C->ops->view       = MatView_SeqAIJ;
2176416022c9SBarry Smith   C->factor     = A->factor;
2177416022c9SBarry Smith   c->row        = 0;
2178416022c9SBarry Smith   c->col        = 0;
217982bf6240SBarry Smith   c->icol       = 0;
2180416022c9SBarry Smith   c->indexshift = shift;
2181c456f294SBarry Smith   C->assembled  = PETSC_TRUE;
218217ab2063SBarry Smith 
218344cd7ae7SLois Curfman McInnes   c->m = C->m   = a->m;
218444cd7ae7SLois Curfman McInnes   c->n = C->n   = a->n;
218544cd7ae7SLois Curfman McInnes   C->M          = a->m;
218644cd7ae7SLois Curfman McInnes   C->N          = a->n;
218717ab2063SBarry Smith 
21880452661fSBarry Smith   c->imax       = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(c->imax);
21890452661fSBarry Smith   c->ilen       = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(c->ilen);
219017ab2063SBarry Smith   for ( i=0; i<m; i++ ) {
2191416022c9SBarry Smith     c->imax[i] = a->imax[i];
2192416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
219317ab2063SBarry Smith   }
219417ab2063SBarry Smith 
219517ab2063SBarry Smith   /* allocate the matrix space */
2196416022c9SBarry Smith   c->singlemalloc = 1;
2197416022c9SBarry Smith   len     = (m+1)*sizeof(int)+(a->i[m])*(sizeof(Scalar)+sizeof(int));
21980452661fSBarry Smith   c->a  = (Scalar *) PetscMalloc( len ); CHKPTRQ(c->a);
2199416022c9SBarry Smith   c->j  = (int *) (c->a + a->i[m] + shift);
2200416022c9SBarry Smith   c->i  = c->j + a->i[m] + shift;
2201416022c9SBarry Smith   PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));
220217ab2063SBarry Smith   if (m > 0) {
2203416022c9SBarry Smith     PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int));
2204be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2205416022c9SBarry Smith       PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(Scalar));
2206be6bf707SBarry Smith     } else {
2207be6bf707SBarry Smith       PetscMemzero(c->a,(a->i[m]+shift)*sizeof(Scalar));
220817ab2063SBarry Smith     }
220908480c60SBarry Smith   }
221017ab2063SBarry Smith 
2211f09e8eb9SSatish Balay   PLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2212416022c9SBarry Smith   c->sorted      = a->sorted;
2213416022c9SBarry Smith   c->roworiented = a->roworiented;
2214416022c9SBarry Smith   c->nonew       = a->nonew;
22157a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
2216be6bf707SBarry Smith   c->saved_values = 0;
221717ab2063SBarry Smith 
2218416022c9SBarry Smith   if (a->diag) {
22190452661fSBarry Smith     c->diag = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(c->diag);
2220416022c9SBarry Smith     PLogObjectMemory(C,(m+1)*sizeof(int));
222117ab2063SBarry Smith     for ( i=0; i<m; i++ ) {
2222416022c9SBarry Smith       c->diag[i] = a->diag[i];
222317ab2063SBarry Smith     }
22243a40ed3dSBarry Smith   } else c->diag          = 0;
22256c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
22266c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
2227754ec7b1SSatish Balay   if (a->inode.size){
2228daed632aSSatish Balay     c->inode.size       = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(c->inode.size);
2229754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
2230daed632aSSatish Balay     PetscMemcpy( c->inode.size, a->inode.size, (m+1)*sizeof(int));
2231754ec7b1SSatish Balay   } else {
2232754ec7b1SSatish Balay     c->inode.size       = 0;
2233754ec7b1SSatish Balay     c->inode.node_count = 0;
2234754ec7b1SSatish Balay   }
2235416022c9SBarry Smith   c->nz                 = a->nz;
2236416022c9SBarry Smith   c->maxnz              = a->maxnz;
2237416022c9SBarry Smith   c->solve_work         = 0;
223876dd722bSSatish Balay   c->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
2239754ec7b1SSatish Balay 
2240416022c9SBarry Smith   *B = C;
2241bef8e0ddSBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)C,"MatSeqAIJSetColumnIndices_C",
2242bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2243bef8e0ddSBarry Smith                                      (void*)MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
22443a40ed3dSBarry Smith   PetscFunctionReturn(0);
224517ab2063SBarry Smith }
224617ab2063SBarry Smith 
22475615d1e5SSatish Balay #undef __FUNC__
22485615d1e5SSatish Balay #define __FUNC__ "MatLoad_SeqAIJ"
224919bcc07fSBarry Smith int MatLoad_SeqAIJ(Viewer viewer,MatType type,Mat *A)
225017ab2063SBarry Smith {
2251416022c9SBarry Smith   Mat_SeqAIJ   *a;
2252416022c9SBarry Smith   Mat          B;
225317699dbbSLois Curfman McInnes   int          i, nz, ierr, fd, header[4],size,*rowlengths = 0,M,N,shift;
2254bcd2baecSBarry Smith   MPI_Comm     comm;
225517ab2063SBarry Smith 
22563a40ed3dSBarry Smith   PetscFunctionBegin;
225719bcc07fSBarry Smith   PetscObjectGetComm((PetscObject) viewer,&comm);
225817699dbbSLois Curfman McInnes   MPI_Comm_size(comm,&size);
2259a8c6a408SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,0,"view must have one processor");
226090ace30eSBarry Smith   ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr);
22610752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT); CHKERRQ(ierr);
2262a8c6a408SBarry Smith   if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"not matrix object in file");
226317ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
226417ab2063SBarry Smith 
2265d64ed03dSBarry Smith   if (nz < 0) {
2266a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,1,"Matrix stored in special format on disk, cannot load as SeqAIJ");
2267d64ed03dSBarry Smith   }
2268d64ed03dSBarry Smith 
226917ab2063SBarry Smith   /* read in row lengths */
22700452661fSBarry Smith   rowlengths = (int*) PetscMalloc( M*sizeof(int) ); CHKPTRQ(rowlengths);
22710752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT); CHKERRQ(ierr);
227217ab2063SBarry Smith 
227317ab2063SBarry Smith   /* create our matrix */
2274416022c9SBarry Smith   ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A); CHKERRQ(ierr);
2275416022c9SBarry Smith   B = *A;
2276416022c9SBarry Smith   a = (Mat_SeqAIJ *) B->data;
2277416022c9SBarry Smith   shift = a->indexshift;
227817ab2063SBarry Smith 
227917ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
22800752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT); CHKERRQ(ierr);
228117ab2063SBarry Smith   if (shift) {
228217ab2063SBarry Smith     for ( i=0; i<nz; i++ ) {
2283416022c9SBarry Smith       a->j[i] += 1;
228417ab2063SBarry Smith     }
228517ab2063SBarry Smith   }
228617ab2063SBarry Smith 
228717ab2063SBarry Smith   /* read in nonzero values */
22880752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR); CHKERRQ(ierr);
228917ab2063SBarry Smith 
229017ab2063SBarry Smith   /* set matrix "i" values */
2291416022c9SBarry Smith   a->i[0] = -shift;
229217ab2063SBarry Smith   for ( i=1; i<= M; i++ ) {
2293416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2294416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
229517ab2063SBarry Smith   }
22960452661fSBarry Smith   PetscFree(rowlengths);
229717ab2063SBarry Smith 
22986d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
22996d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
23003a40ed3dSBarry Smith   PetscFunctionReturn(0);
230117ab2063SBarry Smith }
230217ab2063SBarry Smith 
23035615d1e5SSatish Balay #undef __FUNC__
23045615d1e5SSatish Balay #define __FUNC__ "MatEqual_SeqAIJ"
23058f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B, PetscTruth* flg)
23067264ac53SSatish Balay {
23077264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data, *b = (Mat_SeqAIJ *)B->data;
23087264ac53SSatish Balay 
23093a40ed3dSBarry Smith   PetscFunctionBegin;
2310a8c6a408SBarry Smith   if (B->type !=MATSEQAIJ)SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type");
23117264ac53SSatish Balay 
23127264ac53SSatish Balay   /* If the  matrix dimensions are not equal, or no of nonzeros or shift */
23137264ac53SSatish Balay   if ((a->m != b->m ) || (a->n !=b->n) ||( a->nz != b->nz)||
2314bcd2baecSBarry Smith       (a->indexshift != b->indexshift)) {
23153a40ed3dSBarry Smith     *flg = PETSC_FALSE; PetscFunctionReturn(0);
2316bcd2baecSBarry Smith   }
23177264ac53SSatish Balay 
23187264ac53SSatish Balay   /* if the a->i are the same */
23198108c231SLois Curfman McInnes   if (PetscMemcmp(a->i,b->i,(a->m+1)*sizeof(int))) {
23203a40ed3dSBarry Smith     *flg = PETSC_FALSE; PetscFunctionReturn(0);
23217264ac53SSatish Balay   }
23227264ac53SSatish Balay 
23237264ac53SSatish Balay   /* if a->j are the same */
2324bcd2baecSBarry Smith   if (PetscMemcmp(a->j, b->j, (a->nz)*sizeof(int))) {
23253a40ed3dSBarry Smith     *flg = PETSC_FALSE; PetscFunctionReturn(0);
2326bcd2baecSBarry Smith   }
2327bcd2baecSBarry Smith 
2328bcd2baecSBarry Smith   /* if a->a are the same */
232919bcc07fSBarry Smith   if (PetscMemcmp(a->a, b->a, (a->nz)*sizeof(Scalar))) {
23303a40ed3dSBarry Smith     *flg = PETSC_FALSE; PetscFunctionReturn(0);
23317264ac53SSatish Balay   }
233277c4ece6SBarry Smith   *flg = PETSC_TRUE;
23333a40ed3dSBarry Smith   PetscFunctionReturn(0);
23347264ac53SSatish Balay 
23357264ac53SSatish Balay }
2336